]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ELambda.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 / ELambda.java
old mode 100755 (executable)
new mode 100644 (file)
index abf4c89..b01b771
-package org.simantics.scl.compiler.elaboration.expressions;\r
-\r
-import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;\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.errors.Locations;\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
-import org.simantics.scl.compiler.types.util.MultiFunction;\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 ELambda extends SimplifiableExpression {\r
-    public Case[] cases;\r
-    Type effect = Types.NO_EFFECTS;\r
-    \r
-    public ELambda(Case[] cases) {\r
-        this.cases = cases;\r
-    }\r
-    \r
-    public ELambda(Case case_) {\r
-        this(new Case[] {case_});\r
-    }\r
-\r
-    public ELambda(long loc, Case ... cases) {\r
-        super(loc);\r
-        this.cases = cases;\r
-    }\r
-    \r
-    public ELambda(long loc, Expression pat, Expression exp) {\r
-        this(loc, new Case(new Expression[] {pat}, exp));\r
-    }\r
-\r
-       public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {\r
-        for(Case case_ : cases)\r
-            case_.collectRefs(allRefs, refs);\r
-    }\r
-       \r
-       @Override\r
-       public void collectVars(TObjectIntHashMap<Variable> allVars,\r
-               TIntHashSet vars) {\r
-           for(Case case_ : cases)\r
-            case_.collectVars(allVars, vars);\r
-       }\r
-\r
-       public Expression decomposeMatching() {\r
-           Expression[] patterns = cases[0].patterns;\r
-        int arity = patterns.length;\r
-        \r
-        // Simple cases\r
-        if(cases.length == 1 && \r
-                !(cases[0].value instanceof GuardedExpressionGroup)) {\r
-            boolean noMatchingNeeded = true;\r
-            for(int i=0;i<arity;++i)\r
-                if(!(patterns[i] instanceof EVariable)) {\r
-                    noMatchingNeeded = false;\r
-                    break;\r
-                }\r
-            if(noMatchingNeeded) {\r
-                Expression decomposed = cases[0].value.decomposeMatching();\r
-                Type effect = this.effect;\r
-                for(int i=arity-1;i>=0;--i) {\r
-                    Variable var = ((EVariable)patterns[i]).getVariable(); \r
-                    decomposed = new ESimpleLambda(getLocation(), var, effect, decomposed);\r
-                    effect = Types.NO_EFFECTS;\r
-                }\r
-                return decomposed;\r
-            }\r
-        }\r
-        \r
-        // Complex case\r
-        Variable[] vars = new Variable[arity];\r
-        Expression[] scrutinee = new Expression[arity];\r
-        for(int i=0;i<arity;++i) {\r
-            vars[i] = new Variable("temp" + i);\r
-            Type type = patterns[i].getType();\r
-            vars[i].setType(type);\r
-            scrutinee[i] = new EVariable(getLocation(), vars[i]);\r
-            scrutinee[i].setType(type);\r
-        }\r
-        Expression decomposed = new EMatch(getLocation(), scrutinee, cases);\r
-        Type curEffect = this.effect;\r
-        for(int i=arity-1;i>=0;--i) {            \r
-            decomposed = new ESimpleLambda(getLocation(), vars[i], curEffect, decomposed);            \r
-            curEffect = Types.NO_EFFECTS;                 \r
-        }\r
-        return decomposed;\r
-    }\r
-       \r
-       @Override\r
-       protected void updateType() throws MatchException {\r
-           setType(Types.functionE(Types.getTypes(cases[0].patterns), effect, cases[0].value.getType()));\r
-       }\r
-       \r
-       @Override\r
-       public Expression simplify(SimplificationContext context) {\r
-           return decomposeMatching().simplify(context);\r
-       }\r
-\r
-    @Override\r
-    public void collectFreeVariables(THashSet<Variable> vars) {\r
-        for(Case case_ : cases)\r
-            case_.collectFreeVariables(vars);\r
-    }\r
-\r
-    @Override\r
-    public Expression resolve(TranslationContext context) {\r
-        for(Case case_ : cases)\r
-            case_.resolve(context);\r
-        return this;\r
-    }\r
-    \r
-    @Override\r
-    public void setLocationDeep(long loc) {\r
-        if(location == Locations.NO_LOCATION) {\r
-            location = loc;\r
-            for(Case case_ : cases)\r
-                case_.setLocationDeep(loc);\r
-        }\r
-    }\r
-    \r
-    @Override\r
-    public Expression replace(ReplaceContext context) {\r
-        Case[] newCases = new Case[cases.length];\r
-        for(int i=0;i<cases.length;++i)\r
-            newCases[i] = cases[i].replace(context);\r
-        return new ELambda(newCases);\r
-    }\r
-\r
-    public Case[] getCases() {\r
-        return cases;\r
-    }\r
-    \r
-    public Expression checkBasicType(TypingContext context, Type requiredType) {\r
-        int arity = cases[0].patterns.length;\r
-        MultiFunction mfun;\r
-        try {\r
-            mfun = Types.unifyFunction(requiredType, arity);\r
-        } catch (UnificationException e) {\r
-            int requiredArity = Types.getArity(requiredType);\r
-            context.getErrorLog().log(cases[0].getLhs(), "Arity is " + requiredArity + " but "\r
-                    + arity + " patterns have been given.");\r
-            setType(Types.metaVar(Kinds.STAR));\r
-            return this;\r
-        }\r
-        \r
-        effect = mfun.effect;\r
-        context.pushEffectUpperBound(location, mfun.effect);\r
-        for(Case case_ : cases)\r
-            case_.checkType(context, mfun.parameterTypes,  mfun.returnType);\r
-        context.popEffectUpperBound();\r
-        return this;\r
-    }\r
-    \r
-    @Override\r
-    public Expression inferType(TypingContext context) {\r
-        int arity = cases[0].patterns.length;\r
-        effect = Types.metaVar(Kinds.EFFECT);\r
-        context.pushEffectUpperBound(location, effect);\r
-        Type[] parameterTypes = new Type[arity];        \r
-        for(int i=0;i<parameterTypes.length;++i)\r
-            parameterTypes[i] = Types.metaVar(Kinds.STAR);\r
-        Type requiredType = Types.metaVar(Kinds.STAR);\r
-        for(Case case_ : cases)\r
-            case_.checkType(context, parameterTypes, requiredType);\r
-        context.popEffectUpperBound();\r
-        return this;\r
-    }\r
-\r
-    @Override\r
-    public Expression decorate(ExpressionDecorator decorator) {\r
-        if(decorator.decorateSubstructure(this))\r
-            for(Case case_ : cases)\r
-                case_.decorate(decorator);\r
-        return decorator.decorate(this);\r
-    }\r
-    \r
-    @Override\r
-    public boolean isEffectful() {\r
-       return false;\r
-    }\r
-\r
-    @Override\r
-    public void collectEffects(THashSet<Type> effects) {\r
-        for(Case case_ : cases) {\r
-            for(Expression pattern : case_.patterns)\r
-                pattern.collectEffects(effects);\r
-            case_.value.collectEffects(effects);\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
-        for(Case case_ : cases)\r
-            case_.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.elaboration.contexts.ReplaceContext;
+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.errors.Locations;
+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;
+import org.simantics.scl.compiler.types.util.MultiFunction;
+
+public class ELambda extends SimplifiableExpression {
+    public Case[] cases;
+    Type effect = Types.NO_EFFECTS;
+    
+    public ELambda(Case[] cases) {
+        this.cases = cases;
+    }
+    
+    public ELambda(Case case_) {
+        this(new Case[] {case_});
+    }
+
+    public ELambda(long loc, Case ... cases) {
+        super(loc);
+        this.cases = cases;
+    }
+    
+    public ELambda(long loc, Type effect, Case ... cases) {
+        super(loc);
+        this.cases = cases;
+        this.effect = effect;
+    }
+    
+    public ELambda(long loc, Expression pat, Expression exp) {
+        this(loc, new Case(new Expression[] {pat}, exp));
+    }
+
+    public Expression decomposeMatching() {
+        Expression[] patterns = cases[0].patterns;
+        int arity = patterns.length;
+        
+        // Simple cases
+        if(cases.length == 1 && 
+                !(cases[0].value instanceof GuardedExpressionGroup)) {
+            boolean noMatchingNeeded = true;
+            for(int i=0;i<arity;++i)
+                if(!(patterns[i] instanceof EVariable)) {
+                    noMatchingNeeded = false;
+                    break;
+                }
+            if(noMatchingNeeded) {
+                Expression decomposed = cases[0].value.decomposeMatching();
+                Type effect = this.effect;
+                for(int i=arity-1;i>=0;--i) {
+                    Variable var = ((EVariable)patterns[i]).getVariable(); 
+                    decomposed = new ESimpleLambda(getLocation(), var, effect, decomposed);
+                    effect = Types.NO_EFFECTS;
+                }
+                return decomposed;
+            }
+        }
+        
+        // Complex case
+        Variable[] vars = new Variable[arity];
+        Expression[] scrutinee = new Expression[arity];
+        for(int i=0;i<arity;++i) {
+            vars[i] = new Variable("temp" + i);
+            Type type = patterns[i].getType();
+            vars[i].setType(type);
+            scrutinee[i] = new EVariable(getLocation(), vars[i]);
+            scrutinee[i].setType(type);
+        }
+        Expression decomposed = new EMatch(getLocation(), scrutinee, cases);
+        Type curEffect = this.effect;
+        for(int i=arity-1;i>=0;--i) {            
+            decomposed = new ESimpleLambda(getLocation(), vars[i], curEffect, decomposed);            
+            curEffect = Types.NO_EFFECTS;                 
+        }
+        return decomposed;
+    }
+       
+       @Override
+       protected void updateType() throws MatchException {
+           setType(Types.functionE(Types.getTypes(cases[0].patterns), effect, cases[0].value.getType()));
+       }
+       
+       @Override
+       public Expression simplify(SimplificationContext context) {
+           return decomposeMatching().simplify(context);
+       }
+
+    @Override
+    public Expression resolve(TranslationContext context) {
+        for(Case case_ : cases)
+            case_.resolve(context);
+        return this;
+    }
+    
+    @Override
+    public void setLocationDeep(long loc) {
+        if(location == Locations.NO_LOCATION) {
+            location = loc;
+            for(Case case_ : cases)
+                case_.setLocationDeep(loc);
+        }
+    }
+    
+    @Override
+    public Expression replace(ReplaceContext context) {
+        Case[] newCases = new Case[cases.length];
+        for(int i=0;i<cases.length;++i)
+            newCases[i] = cases[i].replace(context);
+        return new ELambda(newCases);
+    }
+
+    public Case[] getCases() {
+        return cases;
+    }
+    
+    public Expression checkBasicType(TypingContext context, Type requiredType) {
+        int arity = cases[0].patterns.length;
+        MultiFunction mfun;
+        try {
+            mfun = Types.unifyFunction(requiredType, arity);
+        } catch (UnificationException e) {
+            int requiredArity = Types.getArity(requiredType);
+            context.getErrorLog().log(cases[0].getLhs(), "Arity is " + requiredArity + " but "
+                    + arity + " patterns have been given.");
+            setType(Types.metaVar(Kinds.STAR));
+            return this;
+        }
+        
+        effect = mfun.effect;
+        context.pushEffectUpperBound(location, mfun.effect);
+        for(Case case_ : cases)
+            case_.checkType(context, mfun.parameterTypes,  mfun.returnType);
+        context.popEffectUpperBound();
+        return this;
+    }
+    
+    @Override
+    public Expression inferType(TypingContext context) {
+        int arity = cases[0].patterns.length;
+        effect = Types.metaVar(Kinds.EFFECT);
+        context.pushEffectUpperBound(location, effect);
+        Type[] parameterTypes = new Type[arity];        
+        for(int i=0;i<parameterTypes.length;++i)
+            parameterTypes[i] = Types.metaVar(Kinds.STAR);
+        Type requiredType = Types.metaVar(Kinds.STAR);
+        for(Case case_ : cases)
+            case_.checkType(context, parameterTypes, requiredType);
+        context.popEffectUpperBound();
+        return this;
+    }
+    
+    @Override
+    public boolean isEffectful() {
+       return false;
+    }
+    
+    @Override
+    public void accept(ExpressionVisitor visitor) {
+        visitor.visit(this);
+    }
+    
+    @Override
+    public Expression accept(ExpressionTransformer transformer) {
+        return transformer.transform(this);
+    }
+    
+    @Override
+    public int getSyntacticFunctionArity() {
+        int result = 0;
+        for(Case case_ : cases)
+            result = Math.max(result, case_.patterns.length + case_.value.getSyntacticFunctionArity());
+        return result;
+    }
+
+}