1 package org.simantics.scl.compiler.elaboration.expressions.list;
3 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
4 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
5 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
6 import org.simantics.scl.compiler.elaboration.expressions.Expression;
7 import org.simantics.scl.compiler.elaboration.expressions.Variable;
8 import org.simantics.scl.compiler.errors.Locations;
9 import org.simantics.scl.compiler.types.TMetaVar;
10 import org.simantics.scl.compiler.types.Type;
11 import org.simantics.scl.compiler.types.Types;
12 import org.simantics.scl.compiler.types.exceptions.UnificationException;
13 import org.simantics.scl.compiler.types.kinds.Kinds;
15 import gnu.trove.map.hash.TObjectIntHashMap;
16 import gnu.trove.set.hash.THashSet;
17 import gnu.trove.set.hash.TIntHashSet;
19 public class ListThen extends ListQualifier {
20 public ListQualifier left;
21 public Expression transformer;
22 public Expression by; // optional
25 public ListThen(Expression transformer, Expression by) {
26 this.transformer = transformer;
30 public void setLeft(ListQualifier inner) {
35 public void checkType(TypingContext context) {
36 left.checkType(context);
38 cType = Types.metaVar(Kinds.STAR);
39 Type transformerType = Types.function(Types.list(cType), Types.list(cType));
41 by = by.checkType(context, Types.metaVar(Kinds.STAR));
42 transformerType = Types.function(Types.function(cType, by.getType()), transformerType);
44 transformer = transformer.checkType(context, transformerType);
45 if(!(Types.canonical(cType) instanceof TMetaVar)) {
46 context.getErrorLog().log(location, "Transformation function must be generic on list elements.");
51 public void collectVars(TObjectIntHashMap<Variable> allVars,
53 left.collectVars(allVars, vars);
54 transformer.collectVars(allVars, vars);
56 by.collectVars(allVars, vars);
60 public void collectFreeVariables(THashSet<Variable> vars) {
61 left.collectFreeVariables(vars);
62 transformer.collectFreeVariables(vars);
64 by.collectFreeVariables(vars);
68 public CompiledQualifier compile(SimplificationContext context) {
69 CompiledQualifier q = left.compile(context);
72 Types.unify(cType, q.pattern.getType());
73 } catch (UnificationException e) {
74 context.getErrorLog().log(location, "Transformation function must be generic on list elements.");
78 q.value = context.apply(transformer, q.value);
80 q.value = context.apply(transformer, context.lambda(q.pattern.copy(), by), q.value);
85 public void resolve(TranslationContext context) {
86 transformer = transformer.resolve(context);
87 left.resolve(context);
89 by = by.resolve(context);
93 public void setLocationDeep(long loc) {
94 if(location == Locations.NO_LOCATION) {
96 left.setLocationDeep(loc);
97 transformer.setLocationDeep(loc);
99 by.setLocationDeep(loc);
104 public void accept(ListQualifierVisitor visitor) {
109 public ListQualifier accept(ListQualifierTransformer transformer) {
110 return transformer.transform(this);