]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBind.java
Merge "(refs #7508) Added missing effects in the simplification of EBind"
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / EBind.java
old mode 100755 (executable)
new mode 100644 (file)
index fc6eda2..5664db7
-package org.simantics.scl.compiler.elaboration.expressions;\r
-\r
-import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;\r
-import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;\r
-import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;\r
-import org.simantics.scl.compiler.elaboration.contexts.TypingContext;\r
-import org.simantics.scl.compiler.elaboration.modules.SCLValue;\r
-import org.simantics.scl.compiler.environment.Environment;\r
-import org.simantics.scl.compiler.errors.Locations;\r
-import org.simantics.scl.compiler.internal.codegen.references.IVal;\r
-import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;\r
-import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;\r
-import org.simantics.scl.compiler.types.Type;\r
-import org.simantics.scl.compiler.types.Types;\r
-import org.simantics.scl.compiler.types.exceptions.MatchException;\r
-import org.simantics.scl.compiler.types.exceptions.UnificationException;\r
-import org.simantics.scl.compiler.types.kinds.Kinds;\r
-\r
-import gnu.trove.map.hash.TObjectIntHashMap;\r
-import gnu.trove.set.hash.THashSet;\r
-import gnu.trove.set.hash.TIntHashSet;\r
-\r
-public class EBind extends SimplifiableExpression {\r
-    public Expression pattern;\r
-    public Expression value;\r
-    public Expression in;\r
-    private EVariable monadEvidence;\r
-    SCLValue bindFunction;\r
-    Type monadType;\r
-    Type valueContentType;\r
-    Type inContentType;\r
-    \r
-    public EBind(long loc, Expression pattern, Expression value, Expression in) {\r
-        super(loc);\r
-        this.pattern = pattern;\r
-        this.value = value;\r
-        this.in = in;\r
-    }\r
-\r
-    public EBind(long loc, Expression pattern, Expression value, Expression in,\r
-            SCLValue bindFunction) {\r
-        super(loc);\r
-        this.pattern = pattern;\r
-        this.value = value;\r
-        this.in = in;\r
-    }\r
-\r
-    @Override\r
-    public void collectRefs(final TObjectIntHashMap<Object> allRefs, final TIntHashSet refs) {\r
-        value.collectRefs(allRefs, refs);\r
-        in.collectRefs(allRefs, refs);\r
-    }\r
-    \r
-    @Override\r
-    public void collectVars(TObjectIntHashMap<Variable> allVars,\r
-            TIntHashSet vars) {\r
-        value.collectVars(allVars, vars);\r
-        in.collectVars(allVars, vars);\r
-    }\r
-    \r
-    @Override\r
-    protected void updateType() throws MatchException {\r
-        setType(in.getType());\r
-    }\r
-    \r
-    @Override\r
-    public Expression checkBasicType(TypingContext context, Type requiredType) {\r
-        monadType = Types.metaVar(Kinds.STAR_TO_STAR);\r
-        inContentType = Types.metaVar(Kinds.STAR);\r
-        Type monadContent = Types.apply(monadType, inContentType);\r
-        try {\r
-            Types.unify(requiredType, monadContent);\r
-        } catch (UnificationException e) {\r
-            context.typeError(location, requiredType, monadContent);\r
-            return this;\r
-        }\r
-        \r
-        Variable variable = new Variable("monadEvidence");\r
-        variable.setType(Types.pred(Types.MONAD, monadType));\r
-        monadEvidence = new EVariable(getLocation(), variable);\r
-        monadEvidence.setType(variable.getType());\r
-        context.addConstraintDemand(monadEvidence);\r
-        \r
-        pattern = pattern.checkTypeAsPattern(context, Types.metaVar(Kinds.STAR));\r
-        valueContentType = pattern.getType();\r
-        value = value.checkType(context, Types.apply(monadType, valueContentType));\r
-        in = in.checkType(context, requiredType);\r
-        Type inType = in.getType();\r
-        setType(inType);\r
-        return this;\r
-    }\r
-\r
-    @Override\r
-    public IVal toVal(Environment env, CodeWriter w) {\r
-        throw new InternalCompilerError("EBind should be eliminated.");\r
-    }\r
-\r
-    /**\r
-     * Splits let \r
-     */\r
-    @Override\r
-    public Expression simplify(SimplificationContext context) {    \r
-        value = value.simplify(context);\r
-        in = in.simplify(context);\r
-        pattern = pattern.simplify(context);\r
-        \r
-        long loc = getLocation();\r
-        Expression simplified = new EApply(loc,\r
-                new EConstant(loc, bindFunction, Types.canonical(monadType), Types.canonical(valueContentType), Types.canonical(inContentType)),\r
-                monadEvidence, \r
-                value,\r
-                new ELambda(loc, new Case[] {\r
-                    new Case(new Expression[] { pattern }, in)\r
-                }));\r
-        simplified.setType(getType());\r
-        \r
-        return simplified.simplify(context);\r
-    }\r
-\r
-    @Override\r
-    public void collectFreeVariables(THashSet<Variable> vars) {\r
-        in.collectFreeVariables(vars);\r
-        value.collectFreeVariables(vars);\r
-        pattern.removeFreeVariables(vars);\r
-    }\r
-\r
-    @Override\r
-    public Expression resolve(TranslationContext context) {\r
-        value = value.resolve(context);\r
-        \r
-        context.pushFrame();\r
-        pattern = pattern.resolveAsPattern(context);        \r
-        in = in.resolve(context);\r
-        context.popFrame();\r
-        \r
-        bindFunction = context.getBindFunction();\r
-        \r
-        return this; \r
-    }\r
-    \r
-    @Override\r
-    public Expression decorate(ExpressionDecorator decorator) {\r
-        pattern = pattern.decorate(decorator);\r
-        value = value.decorate(decorator);\r
-        in = in.decorate(decorator);\r
-        return decorator.decorate(this);\r
-    }\r
-\r
-    @Override\r
-    public void collectEffects(THashSet<Type> effects) {\r
-        pattern.collectEffects(effects);\r
-        value.collectEffects(effects);\r
-        in.collectEffects(effects);\r
-    }\r
-    \r
-    @Override\r
-    public void setLocationDeep(long loc) {\r
-        if(location == Locations.NO_LOCATION) {\r
-            location = loc;\r
-            pattern.setLocationDeep(loc);\r
-            value.setLocationDeep(loc);\r
-            in.setLocationDeep(loc);\r
-        }\r
-    }\r
-    \r
-    @Override\r
-    public void accept(ExpressionVisitor visitor) {\r
-        visitor.visit(this);\r
-    }\r
-\r
-    @Override\r
-    public void forVariables(VariableProcedure procedure) {\r
-        pattern.forVariables(procedure);\r
-        value.forVariables(procedure);\r
-        if(monadEvidence != null)\r
-            monadEvidence.forVariables(procedure);\r
-    }\r
-    \r
-    @Override\r
-    public Expression accept(ExpressionTransformer transformer) {\r
-        return transformer.transform(this);\r
-    }\r
-\r
-}\r
+package org.simantics.scl.compiler.elaboration.expressions;
+
+import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
+import org.simantics.scl.compiler.common.names.Names;
+import org.simantics.scl.compiler.compilation.CompilationContext;
+import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
+import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
+import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
+import org.simantics.scl.compiler.elaboration.expressions.block.BlockType;
+import org.simantics.scl.compiler.elaboration.modules.SCLValue;
+import org.simantics.scl.compiler.errors.Locations;
+import org.simantics.scl.compiler.internal.codegen.references.IVal;
+import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
+import org.simantics.scl.compiler.types.Type;
+import org.simantics.scl.compiler.types.Types;
+import org.simantics.scl.compiler.types.exceptions.MatchException;
+import org.simantics.scl.compiler.types.exceptions.UnificationException;
+import org.simantics.scl.compiler.types.kinds.Kinds;
+
+public class EBind extends SimplifiableExpression {
+    BlockType blockType;
+    public Expression pattern;
+    public Expression value;
+    public Expression in;
+    public EVariable monadEvidence;
+    Type monadType;
+    Type effect;
+    Type valueContentType;
+    Type inContentType;
+    
+    public EBind(long loc, BlockType blockType, Expression pattern, Expression value, Expression in) {
+        super(loc);
+        this.blockType = blockType;
+        this.pattern = pattern;
+        this.value = value;
+        this.in = in;
+    }
+
+    public EBind(long loc, BlockType blockType, Expression pattern, Expression value, Expression in,
+            SCLValue bindFunction) {
+        super(loc);
+        this.blockType = blockType;
+        this.pattern = pattern;
+        this.value = value;
+        this.in = in;
+    }
+    
+    @Override
+    protected void updateType() throws MatchException {
+        setType(in.getType());
+    }
+    
+    @Override
+    public Expression checkBasicType(TypingContext context, Type requiredType) {
+        monadType = Types.metaVar(Kinds.STAR_TO_STAR);
+        inContentType = Types.metaVar(Kinds.STAR);
+        Type monadContent = Types.apply(monadType, inContentType);
+        try {
+            Types.unify(requiredType, monadContent);
+        } catch (UnificationException e) {
+            context.typeError(location, requiredType, monadContent);
+            return this;
+        }
+        
+        Variable variable = new Variable("monadEvidence");
+        variable.setType(Types.pred(blockType == BlockType.MonadE ? Types.MONAD_E : Types.MONAD, monadType));
+        monadEvidence = new EVariable(getLocation(), variable);
+        monadEvidence.setType(variable.getType());
+        context.addConstraintDemand(monadEvidence);
+        
+        pattern = pattern.checkTypeAsPattern(context, Types.metaVar(Kinds.STAR));
+        valueContentType = pattern.getType();
+        value = value.checkType(context, Types.apply(monadType, valueContentType));
+        context.pushEffectUpperBound(location, blockType == BlockType.Monad ? Types.NO_EFFECTS : Types.metaVar(Kinds.EFFECT));
+        in = in.checkType(context, requiredType);
+        effect = context.popEffectUpperBound();
+        Type inType = in.getType();
+        setType(inType);
+        return this;
+    }
+
+    @Override
+    public IVal toVal(CompilationContext context, CodeWriter w) {
+        throw new InternalCompilerError("EBind should be eliminated.");
+    }
+
+    /**
+     * Splits let 
+     */
+    @Override
+    public Expression simplify(SimplificationContext context) {    
+        long loc = getLocation();
+        monadType = Types.canonical(monadType);
+        valueContentType = Types.canonical(valueContentType);
+        effect = Types.canonical(effect);
+        inContentType = Types.canonical(inContentType);
+        Type[] types = blockType == BlockType.MonadE 
+                ? new Type[] {monadType, valueContentType, effect, inContentType} 
+                : new Type[] {monadType, valueContentType, inContentType};
+        Expression simplified = new EApply(loc, effect,
+                new EConstant(loc, context.getValue(blockType == BlockType.MonadE ? Names.Prelude_bindE : Names.Prelude_bind), types),
+                monadEvidence, 
+                value,
+                new ELambda(loc, effect, new Case[] {
+                    new Case(new Expression[] { pattern }, in)
+                }));
+        simplified.setType(getType());
+        
+        return simplified.simplify(context);
+    }
+
+    @Override
+    public Expression resolve(TranslationContext context) {
+        value = value.resolve(context);
+        
+        context.pushFrame();
+        pattern = pattern.resolveAsPattern(context);        
+        in = in.resolve(context);
+        context.popFrame();
+        
+        return this; 
+    }
+    
+    @Override
+    public void setLocationDeep(long loc) {
+        if(location == Locations.NO_LOCATION) {
+            location = loc;
+            pattern.setLocationDeep(loc);
+            value.setLocationDeep(loc);
+            in.setLocationDeep(loc);
+        }
+    }
+    
+    @Override
+    public void accept(ExpressionVisitor visitor) {
+        visitor.visit(this);
+    }
+    
+    @Override
+    public Expression accept(ExpressionTransformer transformer) {
+        return transformer.transform(this);
+    }
+
+}