1 package org.simantics.scl.compiler.elaboration.expressions;
3 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
4 import org.simantics.scl.compiler.compilation.CompilationContext;
5 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
7 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
8 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
9 import org.simantics.scl.compiler.errors.Locations;
10 import org.simantics.scl.compiler.internal.codegen.references.IVal;
11 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
12 import org.simantics.scl.compiler.types.Type;
13 import org.simantics.scl.compiler.types.Types;
14 import org.simantics.scl.compiler.types.exceptions.MatchException;
15 import org.simantics.scl.compiler.types.exceptions.UnificationException;
16 import org.simantics.scl.compiler.types.kinds.Kinds;
18 import gnu.trove.map.hash.TObjectIntHashMap;
19 import gnu.trove.set.hash.THashSet;
20 import gnu.trove.set.hash.TIntHashSet;
22 public class EBind extends SimplifiableExpression {
23 public Expression pattern;
24 public Expression value;
26 EVariable monadEvidence;
27 SCLValue bindFunction;
29 Type valueContentType;
32 public EBind(long loc, Expression pattern, Expression value, Expression in) {
34 this.pattern = pattern;
39 public EBind(long loc, Expression pattern, Expression value, Expression in,
40 SCLValue bindFunction) {
42 this.pattern = pattern;
48 public void collectVars(TObjectIntHashMap<Variable> allVars,
50 value.collectVars(allVars, vars);
51 in.collectVars(allVars, vars);
55 protected void updateType() throws MatchException {
56 setType(in.getType());
60 public Expression checkBasicType(TypingContext context, Type requiredType) {
61 monadType = Types.metaVar(Kinds.STAR_TO_STAR);
62 inContentType = Types.metaVar(Kinds.STAR);
63 Type monadContent = Types.apply(monadType, inContentType);
65 Types.unify(requiredType, monadContent);
66 } catch (UnificationException e) {
67 context.typeError(location, requiredType, monadContent);
71 Variable variable = new Variable("monadEvidence");
72 variable.setType(Types.pred(Types.MONAD, monadType));
73 monadEvidence = new EVariable(getLocation(), variable);
74 monadEvidence.setType(variable.getType());
75 context.addConstraintDemand(monadEvidence);
77 pattern = pattern.checkTypeAsPattern(context, Types.metaVar(Kinds.STAR));
78 valueContentType = pattern.getType();
79 value = value.checkType(context, Types.apply(monadType, valueContentType));
80 in = in.checkType(context, requiredType);
81 Type inType = in.getType();
87 public IVal toVal(CompilationContext context, CodeWriter w) {
88 throw new InternalCompilerError("EBind should be eliminated.");
95 public Expression simplify(SimplificationContext context) {
96 value = value.simplify(context);
97 in = in.simplify(context);
98 pattern = pattern.simplify(context);
100 long loc = getLocation();
101 Expression simplified = new EApply(loc,
102 new EConstant(loc, bindFunction, Types.canonical(monadType), Types.canonical(valueContentType), Types.canonical(inContentType)),
105 new ELambda(loc, new Case[] {
106 new Case(new Expression[] { pattern }, in)
108 simplified.setType(getType());
110 return simplified.simplify(context);
114 public void collectFreeVariables(THashSet<Variable> vars) {
115 in.collectFreeVariables(vars);
116 value.collectFreeVariables(vars);
117 pattern.removeFreeVariables(vars);
121 public Expression resolve(TranslationContext context) {
122 value = value.resolve(context);
125 pattern = pattern.resolveAsPattern(context);
126 in = in.resolve(context);
129 bindFunction = context.getBindFunction();
135 public void setLocationDeep(long loc) {
136 if(location == Locations.NO_LOCATION) {
138 pattern.setLocationDeep(loc);
139 value.setLocationDeep(loc);
140 in.setLocationDeep(loc);
145 public void accept(ExpressionVisitor visitor) {
150 public Expression accept(ExpressionTransformer transformer) {
151 return transformer.transform(this);