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;
+import org.simantics.scl.compiler.compilation.CompilationContext;
import org.simantics.scl.compiler.constants.NoRepConstant;
import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
import org.simantics.scl.compiler.elaboration.java.ListConstructor;
import org.simantics.scl.compiler.elaboration.macros.MacroRule;
import org.simantics.scl.compiler.elaboration.modules.SCLValue;
-import org.simantics.scl.compiler.environment.Environment;
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.elaboration.utils.ExpressionDecorator;
import org.simantics.scl.compiler.internal.interpreted.IApply;
import org.simantics.scl.compiler.internal.interpreted.IExpression;
import org.simantics.scl.compiler.internal.interpreted.IListLiteral;
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;
-import gnu.trove.map.hash.TObjectIntHashMap;
-import gnu.trove.set.hash.THashSet;
-import gnu.trove.set.hash.TIntHashSet;
-
public class EApply extends Expression {
public Expression function;
public Expression[] parameters;
- Type effect = Types.NO_EFFECTS;
+ public Type effect = Types.NO_EFFECTS;
public EApply(Expression function, Expression ... parameters) {
this.function = function;
public Expression[] getParameters() {
return parameters;
}
-
- public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
- function.collectRefs(allRefs, refs);
- for(Expression parameter : parameters)
- parameter.collectRefs(allRefs, refs);
- }
-
- public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
- function.collectVars(allVars, vars);
- for(Expression parameter : parameters)
- parameter.collectVars(allVars, vars);
- }
-
- @Override
- protected void updateType() throws MatchException {
+ @Override
+ protected void updateType() throws MatchException {
MultiFunction mfun = Types.matchFunction(function.getType(), parameters.length);
/*for(int i=0;i<parameters.length;++i)
if(!Types.equals(parameters[i].getType(), mfun.parameterTypes[i]))
}
@Override
- public IVal toVal(Environment env, CodeWriter w) {
- IVal functionVal = function.toVal(env, w);
+ public IVal toVal(CompilationContext context, CodeWriter w) {
+ IVal functionVal = function.toVal(context, w);
IVal[] parameterVals = new IVal[parameters.length];
for(int i=0;i<parameters.length;++i)
- parameterVals[i] = parameters[i].toVal(env, w);
+ parameterVals[i] = parameters[i].toVal(context, w);
Type type = getType();
effect = Types.simplifyFinalEffect(effect);
return w.applyWithEffect(location, effect, type, functionVal, parameterVals);
}
-
- @Override
- public void collectFreeVariables(THashSet<Variable> vars) {
- function.collectFreeVariables(vars);
- for(Expression parameter : parameters)
- parameter.collectFreeVariables(vars);
- }
private void combineApplications() {
if(function instanceof EApply) {
for(Expression parameter : this.parameters)
parameters.add(parameter);
}
-
- @Override
- public void removeFreeVariables(THashSet<Variable> vars) {
- function.removeFreeVariables(vars);
- for(Expression parameter : parameters)
- parameter.removeFreeVariables(vars);
- }
@Override
public Expression replace(ReplaceContext context) {
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
setType(Types.metaVar(Kinds.STAR));
for(int i=0;i<parameters.length;++i)
parameters[i] = parameters[i].inferType(context);
- return;
- }
- if((ignoreResult && Skeletons.canonicalSkeleton(mfun.returnType) instanceof TFun &&
- Types.canonical(mfun.effect) == Types.NO_EFFECTS) ||
- (context.isInPattern() && Skeletons.canonicalSkeleton(mfun.returnType) instanceof TFun)) {
- context.getErrorLog().log(location, "The function is applied with too few parameters.");
+ return this;
}
- // Check parameter types
- for(int i=0;i<parameters.length;++i)
- parameters[i] = parameters[i].checkType(context, mfun.parameterTypes[i]);
+ EApply current = this;
+ while(true) {
+ MultiFunction mfun = Types.unifyFunction2(functionType, current.parameters.length);
+ int marity = mfun.parameterTypes.length;
+ for(int i=0;i<marity;++i)
+ current.parameters[i] = current.parameters[i].checkType(context, mfun.parameterTypes[i]);
+ current.effect = mfun.effect;
+ context.declareEffect(location, mfun.effect);
+ current.setType(mfun.returnType);
- effect = mfun.effect;
-
- context.declareEffect(location, mfun.effect);
- setType(mfun.returnType);
+ if(marity < current.parameters.length) {
+ if (marity == 0) {
+ // Cannot eat away any more parameters
+ context.getErrorLog().log(location, "Application of non-function");
+ return current;
+ }
+
+ Expression[] missingParameters = Arrays.copyOfRange(current.parameters, marity, current.parameters.length);
+ functionType = mfun.returnType;
+ current.parameters = Arrays.copyOf(current.parameters, marity);
+ current = new EApply(current, missingParameters);
+ }
+ else {
+ if((ignoreResult && mfun.effect == Types.NO_EFFECTS && Skeletons.canonicalSkeleton(mfun.returnType) instanceof TFun) ||
+ (context.isInPattern() && Skeletons.canonicalSkeleton(mfun.returnType) instanceof TFun)) {
+ context.getErrorLog().log(location, "The function is applied with too few parameters.");
+ }
+ return current;
+ }
+ }
}
@Override
public Expression inferType(TypingContext context) {
- inferType(context, false);
- return this;
+ if(parameters.length == 2 && function instanceof EConstant && ((EConstant)function).value.getName() == Names.Prelude_dollar)
+ return new EApply(location, parameters[0], parameters[1]).inferType(context);
+ return inferType(context, false);
}
@Override
public Expression checkIgnoredType(TypingContext context) {
- inferType(context, true);
+ if(parameters.length == 2 && function instanceof EConstant && ((EConstant)function).value.getName() == Names.Prelude_dollar)
+ return new EApply(location, parameters[0], parameters[1]).checkIgnoredType(context);
+ Expression expression = inferType(context, true);
if(Types.canonical(getType()) != Types.UNIT)
- return new ESimpleLet(location, null, this, new ELiteral(NoRepConstant.PUNIT));
- return this;
- }
-
- @Override
- public Expression decorate(ExpressionDecorator decorator) {
- if(decorator.decorateSubstructure(this)) {
- function = function.decorate(decorator);
- for(int i=0;i<parameters.length;++i)
- parameters[i] = parameters[i].decorate(decorator);
- }
- return decorator.decorate(this);
+ expression = new ESimpleLet(location, null, expression, new ELiteral(NoRepConstant.PUNIT));
+ return expression;
}
public Type getLocalEffect() {
return function.isConstructorApplication();
}
- @Override
- public void collectEffects(THashSet<Type> effects) {
- effects.add(effect);
- function.collectEffects(effects);
- for(Expression parameter : parameters)
- parameter.collectEffects(effects);
- }
-
@Override
public void accept(ExpressionVisitor visitor) {
visitor.visit(this);
return false;
}
}
-
- @Override
- public void forVariables(VariableProcedure procedure) {
- function.forVariables(procedure);
- for(Expression parameter : parameters)
- parameter.forVariables(procedure);
- }
@Override
public boolean isPattern(int arity) {