Search in sources :

Example 1 with SolveResult

use of mb.nabl2.solver.ISolver.SolveResult in project nabl by metaborg.

the class EqualityComponent method solve.

private Optional<SolveResult> solve(CInequal constraint) {
    ITerm left = constraint.getLeft();
    ITerm right = constraint.getRight();
    if (unifier().areEqual(left, right)) {
        MessageContent content = MessageContent.builder().append(constraint.getLeft().toString()).append(" and ").append(constraint.getRight().toString()).append(" must be inequal, but are not.").build();
        IMessageInfo message = constraint.getMessageInfo().withDefaultContent(content);
        return Optional.of(SolveResult.messages(message));
    } else {
        return unifier().areUnequal(left, right) ? Optional.of(SolveResult.empty()) : Optional.empty();
    }
}
Also used : MessageContent(mb.nabl2.constraints.messages.MessageContent) ITerm(mb.nabl2.terms.ITerm) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo)

Example 2 with SolveResult

use of mb.nabl2.solver.ISolver.SolveResult in project nabl by metaborg.

the class SetComponent method solve.

private Optional<SolveResult> solve(CDistinct constraint) {
    ITerm setTerm = unifier().findRecursive(constraint.getSet());
    if (!setTerm.isGround()) {
        return Optional.empty();
    }
    Optional<Set<IElement<ITerm>>> maybeSet = evaluator.match(setTerm, unifier());
    if (!(maybeSet.isPresent())) {
        return Optional.empty();
    }
    Multimap<Object, IElement<ITerm>> proj = SetEvaluator.project(maybeSet.get(), constraint.getProjection());
    List<IElement<ITerm>> duplicates = Lists.newArrayList();
    for (Object key : proj.keySet()) {
        Collection<IElement<ITerm>> values = proj.get(key);
        if (values.size() > 1) {
            duplicates.addAll(values);
        }
    }
    if (duplicates.isEmpty()) {
        return Optional.of(SolveResult.empty());
    } else {
        MessageContent content = MessageContent.builder().append(B.newAppl(NAME_OP)).append(" has duplicates in ").append(setTerm).build();
        Iterable<IMessageInfo> messages = makeMessages(constraint.getMessageInfo().withDefaultContent(content), duplicates);
        return Optional.of(SolveResult.messages(messages));
    }
}
Also used : MessageContent(mb.nabl2.constraints.messages.MessageContent) IElement(mb.nabl2.sets.IElement) CEvalSet(mb.nabl2.constraints.sets.CEvalSet) Set(java.util.Set) ITerm(mb.nabl2.terms.ITerm) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo)

Example 3 with SolveResult

use of mb.nabl2.solver.ISolver.SolveResult in project nabl by metaborg.

the class NameResolutionComponent method solve.

// ------------------------------------------------------------------------------------------------------//
private Optional<SolveResult> solve(CResolve r) {
    final ITerm refTerm = unifier().findRecursive(r.getReference());
    if (!refTerm.isGround()) {
        return Optional.empty();
    }
    final Occurrence ref = Occurrence.matcher().match(refTerm, unifier()).orElseThrow(() -> new TypeException("Expected an occurrence as first argument to " + r));
    final Optional<java.util.Set<IResolutionPath<Scope, Label, Occurrence>>> maybePathsAndDeps = nameResolution.resolve(ref);
    if (!maybePathsAndDeps.isPresent()) {
        return Optional.empty();
    }
    final java.util.Set<IResolutionPath<Scope, Label, Occurrence>> paths = maybePathsAndDeps.get();
    final List<Occurrence> declarations = Paths.resolutionPathsToDecls(paths);
    final Multimap<String, String> deps = HashMultimap.create();
    deps.putAll(ref.getIndex().getResource(), declarations.stream().map(d -> d.getIndex().getResource()).collect(Collectors.toSet()));
    final SolveResult result;
    switch(declarations.size()) {
        case 0:
            {
                IMessageInfo message = r.getMessageInfo().withDefaultContent(MessageContent.builder().append(ref).append(" does not resolve.").build());
                result = SolveResult.messages(message);
                break;
            }
        case 1:
            {
                final Occurrence decl = declarations.get(0);
                result = SolveResult.constraints(ImmutableCEqual.of(r.getDeclaration(), decl, r.getMessageInfo()));
                break;
            }
        default:
            {
                IMessageInfo message = r.getMessageInfo().withDefaultContent(MessageContent.builder().append("Resolution of ").append(ref).append(" is ambiguous.").build());
                result = SolveResult.messages(message);
                break;
            }
    }
    return Optional.of(ImmutableSolveResult.copyOf(result).withDependencies(deps));
}
Also used : IResolutionPath(mb.nabl2.scopegraph.path.IResolutionPath) Label(mb.nabl2.scopegraph.terms.Label) TypeException(mb.nabl2.solver.TypeException) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo) SolveResult(mb.nabl2.solver.ISolver.SolveResult) ImmutableSolveResult(mb.nabl2.solver.ImmutableSolveResult) Scope(mb.nabl2.scopegraph.terms.Scope) ITerm(mb.nabl2.terms.ITerm) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 4 with SolveResult

use of mb.nabl2.solver.ISolver.SolveResult 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 5 with SolveResult

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

Aggregations

ITerm (mb.nabl2.terms.ITerm)16 SolveResult (mb.nabl2.solver.ISolver.SolveResult)14 IMessageInfo (mb.nabl2.constraints.messages.IMessageInfo)11 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)9 Optional (java.util.Optional)8 MessageContent (mb.nabl2.constraints.messages.MessageContent)8 SolverCore (mb.nabl2.solver.SolverCore)8 IConstraint (mb.nabl2.constraints.IConstraint)7 Label (mb.nabl2.scopegraph.terms.Label)7 Scope (mb.nabl2.scopegraph.terms.Scope)7 Map (java.util.Map)5 IVariantRelation (mb.nabl2.relations.variants.IVariantRelation)5 VariantRelations (mb.nabl2.relations.variants.VariantRelations)5 IEsopScopeGraph (mb.nabl2.scopegraph.esop.IEsopScopeGraph)5 IUnifier (mb.nabl2.terms.unification.IUnifier)5 Function1 (org.metaborg.util.functions.Function1)5 Predicate1 (org.metaborg.util.functions.Predicate1)5 Iterables2 (org.metaborg.util.iterators.Iterables2)5 Set (java.util.Set)4 NaBL2DebugConfig (mb.nabl2.config.NaBL2DebugConfig)4