]> gerrit.simantics Code Review - simantics/platform.git/blob
3bec2aaa13ecafde91be4380559df456779b6953
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions;
2
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;
17
18 import gnu.trove.map.hash.TObjectIntHashMap;
19 import gnu.trove.set.hash.THashSet;
20 import gnu.trove.set.hash.TIntHashSet;
21
22 public class EBind extends SimplifiableExpression {
23     public Expression pattern;
24     public Expression value;
25     public Expression in;
26     EVariable monadEvidence;
27     SCLValue bindFunction;
28     Type monadType;
29     Type valueContentType;
30     Type inContentType;
31     
32     public EBind(long loc, Expression pattern, Expression value, Expression in) {
33         super(loc);
34         this.pattern = pattern;
35         this.value = value;
36         this.in = in;
37     }
38
39     public EBind(long loc, Expression pattern, Expression value, Expression in,
40             SCLValue bindFunction) {
41         super(loc);
42         this.pattern = pattern;
43         this.value = value;
44         this.in = in;
45     }
46     
47     @Override
48     public void collectVars(TObjectIntHashMap<Variable> allVars,
49             TIntHashSet vars) {
50         value.collectVars(allVars, vars);
51         in.collectVars(allVars, vars);
52     }
53     
54     @Override
55     protected void updateType() throws MatchException {
56         setType(in.getType());
57     }
58     
59     @Override
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);
64         try {
65             Types.unify(requiredType, monadContent);
66         } catch (UnificationException e) {
67             context.typeError(location, requiredType, monadContent);
68             return this;
69         }
70         
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);
76         
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();
82         setType(inType);
83         return this;
84     }
85
86     @Override
87     public IVal toVal(CompilationContext context, CodeWriter w) {
88         throw new InternalCompilerError("EBind should be eliminated.");
89     }
90
91     /**
92      * Splits let 
93      */
94     @Override
95     public Expression simplify(SimplificationContext context) {    
96         value = value.simplify(context);
97         in = in.simplify(context);
98         pattern = pattern.simplify(context);
99         
100         long loc = getLocation();
101         Expression simplified = new EApply(loc,
102                 new EConstant(loc, bindFunction, Types.canonical(monadType), Types.canonical(valueContentType), Types.canonical(inContentType)),
103                 monadEvidence, 
104                 value,
105                 new ELambda(loc, new Case[] {
106                     new Case(new Expression[] { pattern }, in)
107                 }));
108         simplified.setType(getType());
109         
110         return simplified.simplify(context);
111     }
112
113     @Override
114     public void collectFreeVariables(THashSet<Variable> vars) {
115         in.collectFreeVariables(vars);
116         value.collectFreeVariables(vars);
117         pattern.removeFreeVariables(vars);
118     }
119
120     @Override
121     public Expression resolve(TranslationContext context) {
122         value = value.resolve(context);
123         
124         context.pushFrame();
125         pattern = pattern.resolveAsPattern(context);        
126         in = in.resolve(context);
127         context.popFrame();
128         
129         bindFunction = context.getBindFunction();
130         
131         return this; 
132     }
133     
134     @Override
135     public void collectEffects(THashSet<Type> effects) {
136         pattern.collectEffects(effects);
137         value.collectEffects(effects);
138         in.collectEffects(effects);
139     }
140     
141     @Override
142     public void setLocationDeep(long loc) {
143         if(location == Locations.NO_LOCATION) {
144             location = loc;
145             pattern.setLocationDeep(loc);
146             value.setLocationDeep(loc);
147             in.setLocationDeep(loc);
148         }
149     }
150     
151     @Override
152     public void accept(ExpressionVisitor visitor) {
153         visitor.visit(this);
154     }
155     
156     @Override
157     public Expression accept(ExpressionTransformer transformer) {
158         return transformer.transform(this);
159     }
160
161 }