Search in sources :

Example 11 with ITermVar

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);
}
Also used : ImmutableTypeVar(mb.nabl2.poly.ImmutableTypeVar) TypeVar(mb.nabl2.poly.TypeVar) SolveResult(mb.nabl2.solver.ISolver.SolveResult) ITermVar(mb.nabl2.terms.ITermVar) ITerm(mb.nabl2.terms.ITerm) IConstraint(mb.nabl2.constraints.IConstraint) TypeException(mb.nabl2.solver.TypeException) Forall(mb.nabl2.poly.Forall) ImmutableForall(mb.nabl2.poly.ImmutableForall) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 12 with ITermVar

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();
}
Also used : IUnifier(mb.nabl2.terms.unification.IUnifier) HashMultimap(com.google.common.collect.HashMultimap) HashMultiset(com.google.common.collect.HashMultiset) ITerm(mb.nabl2.terms.ITerm) Multiset(com.google.common.collect.Multiset) Collection(java.util.Collection) ITermVar(mb.nabl2.terms.ITermVar) Set(java.util.Set) Multimap(com.google.common.collect.Multimap) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) ITerm(mb.nabl2.terms.ITerm)

Example 13 with ITermVar

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);
}
Also used : ITermVar(mb.nabl2.terms.ITermVar) ITerm(mb.nabl2.terms.ITerm)

Example 14 with ITermVar

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;
}
Also used : ITermVar(mb.nabl2.terms.ITermVar)

Example 15 with ITermVar

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;
}
Also used : ITermVar(mb.nabl2.terms.ITermVar)

Aggregations

ITermVar (mb.nabl2.terms.ITermVar)25 ITerm (mb.nabl2.terms.ITerm)15 Collection (java.util.Collection)3 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)3 SolveResult (mb.nabl2.solver.ISolver.SolveResult)3 HashMultimap (com.google.common.collect.HashMultimap)2 Multimap (com.google.common.collect.Multimap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 NaBL2DebugConfig (mb.nabl2.config.NaBL2DebugConfig)2 IConstraint (mb.nabl2.constraints.IConstraint)2 ImmutableTypeVar (mb.nabl2.poly.ImmutableTypeVar)2 TypeVar (mb.nabl2.poly.TypeVar)2 Scope (mb.nabl2.scopegraph.terms.Scope)2 ISolution (mb.nabl2.solver.ISolution)2 SolverException (mb.nabl2.solver.SolverException)2 TypeException (mb.nabl2.solver.TypeException)2 IUnifier (mb.nabl2.terms.unification.IUnifier)2 HashMultiset (com.google.common.collect.HashMultiset)1 Lists (com.google.common.collect.Lists)1