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 collectRefs(TObjectIntHashMap<Object> allRefs,
53 left.collectRefs(allRefs, refs);
54 transformer.collectRefs(allRefs, refs);
56 by.collectRefs(allRefs, refs);
60 public void collectVars(TObjectIntHashMap<Variable> allVars,
62 left.collectVars(allVars, vars);
63 transformer.collectVars(allVars, vars);
65 by.collectVars(allVars, vars);
69 public void collectFreeVariables(THashSet<Variable> vars) {
70 left.collectFreeVariables(vars);
71 transformer.collectFreeVariables(vars);
73 by.collectFreeVariables(vars);
77 public CompiledQualifier compile(SimplificationContext context) {
78 CompiledQualifier q = left.compile(context);
81 Types.unify(cType, q.pattern.getType());
82 } catch (UnificationException e) {
83 context.getErrorLog().log(location, "Transformation function must be generic on list elements.");
87 q.value = context.apply(transformer, q.value);
89 q.value = context.apply(transformer, context.lambda(q.pattern.copy(), by), q.value);
94 public void resolve(TranslationContext context) {
95 transformer = transformer.resolve(context);
96 left.resolve(context);
98 by = by.resolve(context);
102 public void collectEffects(THashSet<Type> effects) {
103 left.collectEffects(effects);
104 transformer.collectEffects(effects);
106 by.collectEffects(effects);
110 public void setLocationDeep(long loc) {
111 if(location == Locations.NO_LOCATION) {
113 left.setLocationDeep(loc);
114 transformer.setLocationDeep(loc);
116 by.setLocationDeep(loc);
121 public void accept(ListQualifierVisitor visitor) {
126 public ListQualifier accept(ListQualifierTransformer transformer) {
127 return transformer.transform(this);