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);
}
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);
}
Aggregations