Search in sources :

Example 1 with TypeVar

use of mb.nabl2.poly.TypeVar 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 2 with TypeVar

use of mb.nabl2.poly.TypeVar 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)

Aggregations

ImmutableTypeVar (mb.nabl2.poly.ImmutableTypeVar)2 TypeVar (mb.nabl2.poly.TypeVar)2 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)2 SolveResult (mb.nabl2.solver.ISolver.SolveResult)2 TypeException (mb.nabl2.solver.TypeException)2 ITerm (mb.nabl2.terms.ITerm)2 ITermVar (mb.nabl2.terms.ITermVar)2 IConstraint (mb.nabl2.constraints.IConstraint)1 Forall (mb.nabl2.poly.Forall)1 ImmutableForall (mb.nabl2.poly.ImmutableForall)1