Search in sources :

Example 6 with TypeException

use of mb.nabl2.solver.TypeException 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)

Example 7 with TypeException

use of mb.nabl2.solver.TypeException in project nabl by metaborg.

the class ScopeGraphComponent method solve.

private boolean solve(CGDirectEdge c) {
    ITerm sourceScopeRep = unifier().findRecursive(c.getSourceScope());
    if (!sourceScopeRep.isGround()) {
        return false;
    }
    Scope sourceScope = Scope.matcher().match(sourceScopeRep, unifier()).orElseThrow(() -> new TypeException("Expected a scope but got " + sourceScopeRep));
    return findScope(c.getTargetScope()).map(targetScope -> {
        scopeGraph.addDirectEdge(sourceScope, c.getLabel(), targetScope);
        return true;
    }).orElseGet(() -> {
        scopeGraph.addIncompleteDirectEdge(sourceScope, c.getLabel(), c.getTargetScope());
        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 8 with TypeException

use of mb.nabl2.solver.TypeException in project nabl by metaborg.

the class NameResolutionComponent method solve.

private Optional<SolveResult> solve(CDeclProperty c) {
    final ITerm declTerm = unifier().findRecursive(c.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 " + c));
    final SolveResult result = putProperty(decl, c.getKey(), c.getValue(), c.getMessageInfo()).map(cc -> SolveResult.constraints(cc)).orElseGet(() -> SolveResult.empty());
    return Optional.of(result);
}
Also used : INameResolutionConstraint(mb.nabl2.constraints.nameresolution.INameResolutionConstraint) CDeclProperty(mb.nabl2.constraints.nameresolution.CDeclProperty) TypeException(mb.nabl2.solver.TypeException) ITerm(mb.nabl2.terms.ITerm) ImmutableCEqual(mb.nabl2.constraints.equality.ImmutableCEqual) IConstraint(mb.nabl2.constraints.IConstraint) Multimap(com.google.common.collect.Multimap) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) IProperties(mb.nabl2.util.collections.IProperties) IEsopNameResolution(mb.nabl2.scopegraph.esop.IEsopNameResolution) SolveResult(mb.nabl2.solver.ISolver.SolveResult) SolverCore(mb.nabl2.solver.SolverCore) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) VarMultimap(mb.nabl2.terms.collection.VarMultimap) Value(org.immutables.value.Value) Label(mb.nabl2.scopegraph.terms.Label) ASolver(mb.nabl2.solver.ASolver) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) CResolve(mb.nabl2.constraints.nameresolution.CResolve) Scope(mb.nabl2.scopegraph.terms.Scope) IResolutionPath(mb.nabl2.scopegraph.path.IResolutionPath) Collectors(java.util.stream.Collectors) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo) Sets(com.google.common.collect.Sets) MessageContent(mb.nabl2.constraints.messages.MessageContent) ImmutableSolveResult(mb.nabl2.solver.ImmutableSolveResult) List(java.util.List) Paths(mb.nabl2.scopegraph.terms.path.Paths) Serial(org.immutables.serial.Serial) SeedResult(mb.nabl2.solver.ISolver.SeedResult) Optional(java.util.Optional) CAssoc(mb.nabl2.constraints.nameresolution.CAssoc) SolveResult(mb.nabl2.solver.ISolver.SolveResult) ImmutableSolveResult(mb.nabl2.solver.ImmutableSolveResult) ITerm(mb.nabl2.terms.ITerm) TypeException(mb.nabl2.solver.TypeException) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 9 with TypeException

use of mb.nabl2.solver.TypeException in project nabl by metaborg.

the class PolymorphismComponent method solve.

// ------------------------------------------------------------------------------------------------------//
private Optional<SolveResult> solve(CGeneralize gen) {
    final ITerm declTerm = unifier().findRecursive(gen.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 " + gen));
    final ITerm type = gen.getType();
    if (!isGenSafe.test(type)) {
        return Optional.empty();
    }
    // linked map to preserve key order
    final Map<ITermVar, TypeVar> subst = Maps.newLinkedHashMap();
    final ITerm scheme;
    {
        int c = 0;
        for (ITermVar var : type.getVars()) {
            subst.put(var, ImmutableTypeVar.of("T" + (++c)));
        }
        scheme = subst.isEmpty() ? type : ImmutableForall.of(subst.values(), subst(type, subst));
    }
    SolveResult result = SolveResult.constraints(// @formatter:off
    ImmutableCDeclProperty.of(decl, DeclProperties.TYPE_KEY, scheme, 0, gen.getMessageInfo()), ImmutableCEqual.of(gen.getGenVars(), B.newList(subst.keySet()), gen.getMessageInfo()));
    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) TypeException(mb.nabl2.solver.TypeException) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 10 with TypeException

use of mb.nabl2.solver.TypeException in project nabl by metaborg.

the class ScopeGraphComponent method solve.

private boolean solve(CGRef c) {
    final ITerm scopeTerm = unifier().findRecursive((c.getScope()));
    final ITerm refTerm = unifier().findRecursive((c.getReference()));
    if (!(scopeTerm.isGround() && refTerm.isGround())) {
        return false;
    }
    Occurrence ref = Occurrence.matcher().match(refTerm, unifier()).orElseThrow(() -> new TypeException("Expected an occurrence as first argument to " + c));
    Scope scope = Scope.matcher().match(scopeTerm, unifier()).orElseThrow(() -> new TypeException("Expected a scope as second argument to " + c));
    scopeGraph.addRef(ref, 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

Occurrence (mb.nabl2.scopegraph.terms.Occurrence)10 TypeException (mb.nabl2.solver.TypeException)10 ITerm (mb.nabl2.terms.ITerm)10 Scope (mb.nabl2.scopegraph.terms.Scope)8 SolveResult (mb.nabl2.solver.ISolver.SolveResult)7 IMessageInfo (mb.nabl2.constraints.messages.IMessageInfo)5 Label (mb.nabl2.scopegraph.terms.Label)5 Optional (java.util.Optional)3 IEsopScopeGraph (mb.nabl2.scopegraph.esop.IEsopScopeGraph)3 ASolver (mb.nabl2.solver.ASolver)3 SeedResult (mb.nabl2.solver.ISolver.SeedResult)3 ImmutableSolveResult (mb.nabl2.solver.ImmutableSolveResult)3 SolverCore (mb.nabl2.solver.SolverCore)3 IConstraint (mb.nabl2.constraints.IConstraint)2 CGDecl (mb.nabl2.constraints.scopegraph.CGDecl)2 CGDirectEdge (mb.nabl2.constraints.scopegraph.CGDirectEdge)2 CGExportEdge (mb.nabl2.constraints.scopegraph.CGExportEdge)2 CGImportEdge (mb.nabl2.constraints.scopegraph.CGImportEdge)2 CGRef (mb.nabl2.constraints.scopegraph.CGRef)2 IScopeGraphConstraint (mb.nabl2.constraints.scopegraph.IScopeGraphConstraint)2