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.TIntHashSet;
21 public class EBind extends SimplifiableExpression {
22 public Expression pattern;
23 public Expression value;
25 public EVariable monadEvidence;
26 SCLValue bindFunction;
28 Type valueContentType;
31 public EBind(long loc, Expression pattern, Expression value, Expression in) {
33 this.pattern = pattern;
38 public EBind(long loc, Expression pattern, Expression value, Expression in,
39 SCLValue bindFunction) {
41 this.pattern = pattern;
47 public void collectVars(TObjectIntHashMap<Variable> allVars,
49 value.collectVars(allVars, vars);
50 in.collectVars(allVars, vars);
54 protected void updateType() throws MatchException {
55 setType(in.getType());
59 public Expression checkBasicType(TypingContext context, Type requiredType) {
60 monadType = Types.metaVar(Kinds.STAR_TO_STAR);
61 inContentType = Types.metaVar(Kinds.STAR);
62 Type monadContent = Types.apply(monadType, inContentType);
64 Types.unify(requiredType, monadContent);
65 } catch (UnificationException e) {
66 context.typeError(location, requiredType, monadContent);
70 Variable variable = new Variable("monadEvidence");
71 variable.setType(Types.pred(Types.MONAD, monadType));
72 monadEvidence = new EVariable(getLocation(), variable);
73 monadEvidence.setType(variable.getType());
74 context.addConstraintDemand(monadEvidence);
76 pattern = pattern.checkTypeAsPattern(context, Types.metaVar(Kinds.STAR));
77 valueContentType = pattern.getType();
78 value = value.checkType(context, Types.apply(monadType, valueContentType));
79 in = in.checkType(context, requiredType);
80 Type inType = in.getType();
86 public IVal toVal(CompilationContext context, CodeWriter w) {
87 throw new InternalCompilerError("EBind should be eliminated.");
94 public Expression simplify(SimplificationContext context) {
95 value = value.simplify(context);
96 in = in.simplify(context);
97 pattern = pattern.simplify(context);
99 long loc = getLocation();
100 Expression simplified = new EApply(loc,
101 new EConstant(loc, bindFunction, Types.canonical(monadType), Types.canonical(valueContentType), Types.canonical(inContentType)),
104 new ELambda(loc, new Case[] {
105 new Case(new Expression[] { pattern }, in)
107 simplified.setType(getType());
109 return simplified.simplify(context);
113 public Expression resolve(TranslationContext context) {
114 value = value.resolve(context);
117 pattern = pattern.resolveAsPattern(context);
118 in = in.resolve(context);
121 bindFunction = context.getBindFunction();
127 public void setLocationDeep(long loc) {
128 if(location == Locations.NO_LOCATION) {
130 pattern.setLocationDeep(loc);
131 value.setLocationDeep(loc);
132 in.setLocationDeep(loc);
137 public void accept(ExpressionVisitor visitor) {
142 public Expression accept(ExpressionTransformer transformer) {
143 return transformer.transform(this);