use of mb.nabl2.terms.ITermVar in project nabl by metaborg.
the class PolymorphismComponent method solve.
private Optional<SolveResult> solve(CInstantiate inst) {
final ITerm declTerm = unifier().findRecursive(inst.getDeclaration());
if (!declTerm.isGround()) {
return Optional.empty();
}
final Occurrence decl = Occurrence.matcher().match(declTerm, unifier()).orElseThrow(() -> new TypeException("Expected an occurrence as first argument to " + inst));
if (!isInstSafe.test(decl)) {
return Optional.empty();
}
final Optional<ITerm> schemeTerm = getDeclProp.apply(decl, DeclProperties.TYPE_KEY);
if (!schemeTerm.isPresent()) {
return Optional.empty();
}
final Optional<Forall> forall = Forall.matcher().match(schemeTerm.get(), unifier());
final ITerm type;
// linked map to preserve key order
final Map<TypeVar, ITermVar> subst = Maps.newLinkedHashMap();
if (forall.isPresent()) {
final Forall scheme = forall.get();
scheme.getTypeVars().stream().forEach(v -> {
subst.put(v, B.newVar("", fresh(v.getName())));
});
type = subst(scheme.getType(), subst);
} else {
type = schemeTerm.get();
}
final IConstraint constraint = // @formatter:off
ImmutableCExists.of(subst.values(), ImmutableCConj.of(ImmutableCEqual.of(inst.getType(), type, inst.getMessageInfo()), ImmutableCEqual.of(inst.getInstVars(), B.newList(subst.keySet()), inst.getMessageInfo()), MessageInfo.empty()), inst.getMessageInfo());
// @formatter:on
SolveResult result = SolveResult.constraints(constraint);
return Optional.of(result);
}
use of mb.nabl2.terms.ITermVar in project nabl by metaborg.
the class TermMultiset method update.
public boolean update(Collection<ITermVar> vars, IUnifier unifier) {
final Set<ITerm> updatedTerms = vars.stream().flatMap(var -> varTerms.removeAll(var).stream()).collect(Collectors.toSet());
for (ITerm term : updatedTerms) {
final int n = terms.remove(term, terms.count(term));
varTerms.values().remove(term);
add(term, n, unifier);
}
return !updatedTerms.isEmpty();
}
use of mb.nabl2.terms.ITermVar in project nabl by metaborg.
the class TermMultiset method add.
public void add(ITerm term, int n, IUnifier unifier) {
final ITerm rep = unifier.findRecursive(term);
for (ITermVar var : rep.getVars().elementSet()) {
varTerms.put(var, rep);
}
terms.add(rep, n);
}
use of mb.nabl2.terms.ITermVar in project nabl by metaborg.
the class VarMultiset method add.
public boolean add(ITermVar var, int n, IUnifier unifier) {
final Multiset<ITermVar> reps = unifier.findRecursive(var).getVars();
boolean change = false;
for (ITermVar rep : reps) {
vars.add(rep, n);
change |= n > 0;
}
return change;
}
use of mb.nabl2.terms.ITermVar in project nabl by metaborg.
the class VarMultiset method count.
public int count(ITermVar var, IUnifier unifier) {
final Multiset<ITermVar> reps = unifier.findRecursive(var).getVars();
int n = 0;
for (ITermVar rep : reps.elementSet()) {
n += vars.count(rep);
}
return n;
}
Aggregations