X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fexpressions%2FEBind.java;h=af31cf9c200c5286f8c0737991807c5dfa0344d3;hb=1cb8ccadc93ab1a1ac5981ea6634b3ef5dd27ec4;hp=5c4a729e2e15fb748a7a17e7793c48d4257e196b;hpb=6f11a60dee43d620d500c0cf5af34a1d91c80a8b;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBind.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBind.java index 5c4a729e2..af31cf9c2 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBind.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EBind.java @@ -1,14 +1,17 @@ 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.internal.header.ModuleHeader; import org.simantics.scl.compiler.types.Type; import org.simantics.scl.compiler.types.Types; import org.simantics.scl.compiler.types.exceptions.MatchException; @@ -16,25 +19,28 @@ 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; - SCLValue bindFunction; Type monadType; + Type effect; 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); + this.blockType = blockType; 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); + this.blockType = blockType; this.pattern = pattern; this.value = value; this.in = in; @@ -47,7 +53,11 @@ public class EBind extends SimplifiableExpression { @Override public Expression checkBasicType(TypingContext context, Type requiredType) { + ModuleHeader header = context.getCompilationContext().header; + boolean edo = header != null && header.edo; + monadType = Types.metaVar(Kinds.STAR_TO_STAR); + inContentType = Types.metaVar(Kinds.STAR); Type monadContent = Types.apply(monadType, inContentType); try { @@ -58,7 +68,7 @@ public class EBind extends SimplifiableExpression { } 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); @@ -66,7 +76,15 @@ public class EBind extends SimplifiableExpression { pattern = pattern.checkTypeAsPattern(context, Types.metaVar(Kinds.STAR)); valueContentType = pattern.getType(); value = value.checkType(context, Types.apply(monadType, valueContentType)); + if(edo) + context.pushEffectUpperBound(location, blockType == BlockType.Monad ? Types.NO_EFFECTS : Types.metaVar(Kinds.EFFECT)); in = in.checkType(context, requiredType); + if(edo) { + effect = context.popEffectUpperBound(); + context.declareEffect(location, effect); + } + else + effect = Types.NO_EFFECTS; Type inType = in.getType(); setType(inType); return this; @@ -82,16 +100,19 @@ public class EBind extends SimplifiableExpression { */ @Override public Expression simplify(SimplificationContext context) { - value = value.simplify(context); - in = in.simplify(context); - pattern = pattern.simplify(context); - long loc = getLocation(); - Expression simplified = new EApply(loc, - new EConstant(loc, bindFunction, Types.canonical(monadType), Types.canonical(valueContentType), Types.canonical(inContentType)), + 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, new Case[] { + new ELambda(loc, effect, new Case[] { new Case(new Expression[] { pattern }, in) })); simplified.setType(getType()); @@ -108,8 +129,6 @@ public class EBind extends SimplifiableExpression { in = in.resolve(context); context.popFrame(); - bindFunction = context.getBindFunction(); - return this; }