From 10e12242609c3583d188c016f281e5d9b09b2583 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Tue, 6 Feb 2018 14:25:27 +0200 Subject: [PATCH] (refs #7746) Fixed applications with intermediate effects Change-Id: Ida2b7c8e96cbfd45361838e69900a7307e69e343 (cherry picked from commit e515d1fda563f0fa3b8b71f9099696cf49a06d25) --- .../elaboration/expressions/EApply.java | 59 +++++++++-------- .../elaboration/expressions/ELambda.java | 2 +- .../printing/ExpressionToStringVisitor.java | 8 +++ .../simantics/scl/compiler/types/Types.java | 63 +++++++++++++++++++ .../compiler/tests/ModuleRegressionTests.java | 1 + .../compiler/tests/scl/TypeInferenceBug2.scl | 1 + .../compiler/tests/scl/TypeInferenceBug3.scl | 12 ++++ 7 files changed, 120 insertions(+), 26 deletions(-) create mode 100644 tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/TypeInferenceBug3.scl diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EApply.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EApply.java index 5d915a98d..7f50d730b 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EApply.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EApply.java @@ -1,6 +1,7 @@ package org.simantics.scl.compiler.elaboration.expressions; import java.util.ArrayList; +import java.util.Arrays; import org.simantics.scl.compiler.common.names.Name; import org.simantics.scl.compiler.common.names.Names; @@ -28,7 +29,6 @@ import org.simantics.scl.compiler.types.TFun; 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; @@ -220,14 +220,13 @@ public class EApply extends Expression { return new IApply(function.toIExpression(target), parametersI); } - private void inferType(TypingContext context, boolean ignoreResult) { + private Expression inferType(TypingContext context, boolean ignoreResult) { function = function.inferType(context); function = context.instantiate(function); - MultiFunction mfun; - try { - mfun = Types.unifyFunction(function.getType(), parameters.length); - } catch (UnificationException e) { - int arity = Types.getArity(function.getType()); + Type functionType = function.getType(); + + int arity = Types.getMaxArity(functionType); + if(arity < parameters.length) { if(arity == 0) context.getErrorLog().log(location, "Application of non-function."); else @@ -236,40 +235,50 @@ public class EApply extends Expression { setType(Types.metaVar(Kinds.STAR)); for(int i=0;i "); ++indentation; case_.value.accept(this); @@ -346,6 +352,8 @@ public class ExpressionToStringVisitor implements ExpressionVisitor, QueryVisito b.append(' '); show(expression.getParameter()); } + if(SHOW_EFFECTS) + b.append(" {" + expression.effect + "}"); b.append(" -> "); expression.getValue().accept(this); } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/Types.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/Types.java index 5512f5e6e..cae38a128 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/Types.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/Types.java @@ -6,6 +6,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import org.simantics.scl.compiler.common.exceptions.InternalCompilerError; import org.simantics.scl.compiler.errors.Locations; import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException; import org.simantics.scl.compiler.internal.parsing.parser.SCLParserImpl; @@ -643,7 +644,52 @@ public class Types { effect, type); } + + /** + * This function always success, but may return a multi function + * with arity smaller than given parameter + */ + public static MultiFunction unifyFunction2(Type type, int arity) { + type = canonical(type); + Type[] parameterTypes = new Type[arity]; + Type effect = Types.NO_EFFECTS; + int i; + for(i=0;i ()) -> b -> c) contains free variables not mentioned in the type of the value. 5:58-5:60: Expected got . \ No newline at end of file diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/TypeInferenceBug3.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/TypeInferenceBug3.scl new file mode 100644 index 000000000..657183c79 --- /dev/null +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/TypeInferenceBug3.scl @@ -0,0 +1,12 @@ +import "Prelude" + +foo :: Integer -> (Integer -> ()) +foo i = do + print i + \j -> print j + +main = foo 3 4 +-- +3 +4 +() \ No newline at end of file -- 2.47.1