Search in sources :

Example 31 with ITerm

use of mb.nabl2.terms.ITerm in project nabl by metaborg.

the class NameResolutionComponent method solve.

private Optional<SolveResult> solve(CAssoc a) {
    final ITerm declTerm = unifier().findRecursive(a.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 " + a));
    final Label label = a.getLabel();
    final List<Scope> scopes = Lists.newArrayList(scopeGraph.getExportEdges().get(decl, label));
    final SolveResult result;
    switch(scopes.size()) {
        case 0:
            {
                IMessageInfo message = a.getMessageInfo().withDefaultContent(MessageContent.builder().append(decl).append(" has no ").append(label).append(" associated scope.").build());
                result = SolveResult.messages(message);
                break;
            }
        case 1:
            {
                result = SolveResult.constraints(ImmutableCEqual.of(a.getScope(), scopes.get(0), a.getMessageInfo()));
                break;
            }
        default:
            {
                IMessageInfo message = a.getMessageInfo().withDefaultContent(MessageContent.builder().append(decl).append(" has multiple ").append(label).append(" associated scope.").build());
                result = SolveResult.messages(message);
                break;
            }
    }
    return Optional.of(result);
}
Also used : SolveResult(mb.nabl2.solver.ISolver.SolveResult) ImmutableSolveResult(mb.nabl2.solver.ImmutableSolveResult) Scope(mb.nabl2.scopegraph.terms.Scope) Label(mb.nabl2.scopegraph.terms.Label) ITerm(mb.nabl2.terms.ITerm) TypeException(mb.nabl2.solver.TypeException) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo)

Example 32 with ITerm

use of mb.nabl2.terms.ITerm 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 33 with ITerm

use of mb.nabl2.terms.ITerm in project nabl by metaborg.

the class ScopeGraphComponent method solve.

private boolean solve(CGImportEdge c) {
    ITerm scopeRep = unifier().findRecursive(c.getScope());
    if (!scopeRep.isGround()) {
        return false;
    }
    Scope scope = Scope.matcher().match(scopeRep, unifier()).orElseThrow(() -> new TypeException("Expected a scope but got " + scopeRep));
    return findOccurrence(c.getReference()).map(ref -> {
        scopeGraph.addImportEdge(scope, c.getLabel(), ref);
        return true;
    }).orElseGet(() -> {
        scopeGraph.addIncompleteImportEdge(scope, c.getLabel(), c.getReference());
        return true;
    });
}
Also used : TypeException(mb.nabl2.solver.TypeException) ASolver(mb.nabl2.solver.ASolver) ITerm(mb.nabl2.terms.ITerm) CGDirectEdge(mb.nabl2.constraints.scopegraph.CGDirectEdge) IScopeGraphConstraint(mb.nabl2.constraints.scopegraph.IScopeGraphConstraint) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) Scope(mb.nabl2.scopegraph.terms.Scope) CGExportEdge(mb.nabl2.constraints.scopegraph.CGExportEdge) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo) CGRef(mb.nabl2.constraints.scopegraph.CGRef) SolveResult(mb.nabl2.solver.ISolver.SolveResult) SolverCore(mb.nabl2.solver.SolverCore) CGDecl(mb.nabl2.constraints.scopegraph.CGDecl) ISolver(mb.nabl2.solver.ISolver) Label(mb.nabl2.scopegraph.terms.Label) SeedResult(mb.nabl2.solver.ISolver.SeedResult) CGImportEdge(mb.nabl2.constraints.scopegraph.CGImportEdge) Optional(java.util.Optional) Scope(mb.nabl2.scopegraph.terms.Scope) ITerm(mb.nabl2.terms.ITerm) TypeException(mb.nabl2.solver.TypeException)

Example 34 with ITerm

use of mb.nabl2.terms.ITerm in project nabl by metaborg.

the class ScopeGraphComponent method solve.

// ------------------------------------------------------------------------------------------------------//
private boolean solve(CGDecl c) {
    final ITerm scopeTerm = unifier().findRecursive(c.getScope());
    final ITerm declTerm = unifier().findRecursive(c.getDeclaration());
    if (!(scopeTerm.isGround() && declTerm.isGround())) {
        return false;
    }
    Scope scope = Scope.matcher().match(scopeTerm, unifier()).orElseThrow(() -> new TypeException("Expected a scope as first agument to " + c));
    Occurrence decl = Occurrence.matcher().match(declTerm, unifier()).orElseThrow(() -> new TypeException("Expected an occurrence as second argument to " + c));
    scopeGraph.addDecl(scope, decl);
    return true;
}
Also used : Scope(mb.nabl2.scopegraph.terms.Scope) ITerm(mb.nabl2.terms.ITerm) TypeException(mb.nabl2.solver.TypeException) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 35 with ITerm

use of mb.nabl2.terms.ITerm in project nabl by metaborg.

the class ScopeGraphComponent method solve.

private boolean solve(CGExportEdge c) {
    ITerm scopeTerm = unifier().findRecursive(c.getScope());
    ITerm declTerm = unifier().findRecursive(c.getDeclaration());
    if (!(scopeTerm.isGround() && declTerm.isGround())) {
        return false;
    }
    Scope scope = Scope.matcher().match(scopeTerm, unifier()).orElseThrow(() -> new TypeException("Expected a scope as third argument to " + c));
    Occurrence decl = Occurrence.matcher().match(declTerm, unifier()).orElseThrow(() -> new TypeException("Expected an occurrence as first argument to " + c));
    scopeGraph.addExportEdge(decl, c.getLabel(), scope);
    return true;
}
Also used : Scope(mb.nabl2.scopegraph.terms.Scope) ITerm(mb.nabl2.terms.ITerm) TypeException(mb.nabl2.solver.TypeException) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Aggregations

ITerm (mb.nabl2.terms.ITerm)79 Test (org.junit.Test)24 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)19 Scope (mb.nabl2.scopegraph.terms.Scope)18 SolveResult (mb.nabl2.solver.ISolver.SolveResult)16 Optional (java.util.Optional)15 IMessageInfo (mb.nabl2.constraints.messages.IMessageInfo)15 ITermVar (mb.nabl2.terms.ITermVar)15 Label (mb.nabl2.scopegraph.terms.Label)14 Map (java.util.Map)12 Set (java.util.Set)10 MessageContent (mb.nabl2.constraints.messages.MessageContent)10 SolverCore (mb.nabl2.solver.SolverCore)9 IUnifier (mb.nabl2.terms.unification.IUnifier)9 Function1 (org.metaborg.util.functions.Function1)9 Lists (com.google.common.collect.Lists)8 ISolution (mb.nabl2.solver.ISolution)8 HashMultimap (com.google.common.collect.HashMultimap)7 Multimap (com.google.common.collect.Multimap)7 IConstraint (mb.nabl2.constraints.IConstraint)7