]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ELambda.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / ELambda.java
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ELambda.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ELambda.java
new file mode 100755 (executable)
index 0000000..1d46b80
--- /dev/null
@@ -0,0 +1,215 @@
+package org.simantics.scl.compiler.elaboration.expressions;\r
+\r
+import gnu.trove.map.hash.TObjectIntHashMap;\r
+import gnu.trove.set.hash.THashSet;\r
+import gnu.trove.set.hash.TIntHashSet;\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
+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