]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBind.java
New type class MonadE and corresponding monad syntax with edo keyword
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / EBind.java
index 5c4a729e2e15fb748a7a17e7793c48d4257e196b..c72776b35829ea91b6aa90037b4920bf35c39458 100644 (file)
@@ -1,10 +1,12 @@
 package org.simantics.scl.compiler.elaboration.expressions;
 
 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
 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.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.elaboration.modules.SCLValue;
 import org.simantics.scl.compiler.errors.Locations;
 import org.simantics.scl.compiler.internal.codegen.references.IVal;
@@ -16,25 +18,28 @@ import org.simantics.scl.compiler.types.exceptions.UnificationException;
 import org.simantics.scl.compiler.types.kinds.Kinds;
 
 public class EBind extends SimplifiableExpression {
 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;
     public Expression pattern;
     public Expression value;
     public Expression in;
     public EVariable monadEvidence;
-    SCLValue bindFunction;
     Type monadType;
     Type monadType;
+    Type effect;
     Type valueContentType;
     Type inContentType;
     
     Type valueContentType;
     Type inContentType;
     
-    public EBind(long loc, Expression pattern, Expression value, Expression in) {
+    public EBind(long loc, BlockType blockType, Expression pattern, Expression value, Expression in) {
         super(loc);
         super(loc);
+        this.blockType = blockType;
         this.pattern = pattern;
         this.value = value;
         this.in = in;
     }
 
         this.pattern = pattern;
         this.value = value;
         this.in = in;
     }
 
-    public EBind(long loc, Expression pattern, Expression value, Expression in,
+    public EBind(long loc, BlockType blockType, Expression pattern, Expression value, Expression in,
             SCLValue bindFunction) {
         super(loc);
             SCLValue bindFunction) {
         super(loc);
+        this.blockType = blockType;
         this.pattern = pattern;
         this.value = value;
         this.in = in;
         this.pattern = pattern;
         this.value = value;
         this.in = in;
@@ -58,7 +63,7 @@ public class EBind extends SimplifiableExpression {
         }
         
         Variable variable = new Variable("monadEvidence");
         }
         
         Variable variable = new Variable("monadEvidence");
-        variable.setType(Types.pred(Types.MONAD, monadType));
+        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);
         monadEvidence = new EVariable(getLocation(), variable);
         monadEvidence.setType(variable.getType());
         context.addConstraintDemand(monadEvidence);
@@ -66,7 +71,9 @@ public class EBind extends SimplifiableExpression {
         pattern = pattern.checkTypeAsPattern(context, Types.metaVar(Kinds.STAR));
         valueContentType = pattern.getType();
         value = value.checkType(context, Types.apply(monadType, valueContentType));
         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);
         in = in.checkType(context, requiredType);
+        effect = context.popEffectUpperBound();
         Type inType = in.getType();
         setType(inType);
         return this;
         Type inType = in.getType();
         setType(inType);
         return this;
@@ -87,8 +94,15 @@ public class EBind extends SimplifiableExpression {
         pattern = pattern.simplify(context);
         
         long loc = getLocation();
         pattern = pattern.simplify(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,
         Expression simplified = new EApply(loc,
-                new EConstant(loc, bindFunction, Types.canonical(monadType), Types.canonical(valueContentType), Types.canonical(inContentType)),
+                new EConstant(loc, context.getValue(blockType == BlockType.MonadE ? Names.Prelude_bindE : Names.Prelude_bind), types),
                 monadEvidence, 
                 value,
                 new ELambda(loc, new Case[] {
                 monadEvidence, 
                 value,
                 new ELambda(loc, new Case[] {
@@ -108,8 +122,6 @@ public class EBind extends SimplifiableExpression {
         in = in.resolve(context);
         context.popFrame();
         
         in = in.resolve(context);
         context.popFrame();
         
-        bindFunction = context.getBindFunction();
-        
         return this; 
     }
     
         return this; 
     }