1 package org.simantics.scl.compiler.compilation;
3 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.apply;
4 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.applyTypes;
5 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.lambda;
6 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.loc;
7 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.vars;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.Collections;
13 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
14 import org.simantics.scl.compiler.elaboration.expressions.EAmbiguous;
15 import org.simantics.scl.compiler.elaboration.expressions.EPlaceholder;
16 import org.simantics.scl.compiler.elaboration.expressions.ETransformation;
17 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
18 import org.simantics.scl.compiler.elaboration.expressions.Expression;
19 import org.simantics.scl.compiler.elaboration.expressions.Variable;
20 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
21 import org.simantics.scl.compiler.elaboration.query.Query;
22 import org.simantics.scl.compiler.elaboration.relations.ConcreteRelation;
23 import org.simantics.scl.compiler.elaboration.relations.ConcreteRelation.QuerySection;
24 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
25 import org.simantics.scl.compiler.elaboration.rules.MappingRelation;
26 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
27 import org.simantics.scl.compiler.environment.Environment;
28 import org.simantics.scl.compiler.internal.elaboration.constraints.Constraint;
29 import org.simantics.scl.compiler.internal.elaboration.constraints.ConstraintEnvironment;
30 import org.simantics.scl.compiler.internal.elaboration.constraints.ConstraintSolver;
31 import org.simantics.scl.compiler.internal.elaboration.constraints.ExpressionAugmentation;
32 import org.simantics.scl.compiler.internal.elaboration.constraints.ReducedConstraints;
33 import org.simantics.scl.compiler.module.ConcreteModule;
34 import org.simantics.scl.compiler.types.TPred;
35 import org.simantics.scl.compiler.types.TVar;
36 import org.simantics.scl.compiler.types.Type;
37 import org.simantics.scl.compiler.types.Types;
38 import org.simantics.scl.compiler.types.kinds.Kinds;
39 import org.simantics.scl.compiler.types.util.Polarity;
41 import gnu.trove.map.hash.THashMap;
42 import gnu.trove.map.hash.TObjectIntHashMap;
43 import gnu.trove.set.hash.THashSet;
44 import gnu.trove.set.hash.TIntHashSet;
46 public class TypeChecking {
47 final CompilationContext compilationContext;
48 final Environment environment;
49 final ConcreteModule module;
51 ConstraintEnvironment ce;
52 TypeCheckingScheduler scheduler;
54 public TypeChecking(CompilationContext compilationContext, ConcreteModule module) {
55 this.compilationContext = compilationContext;
56 this.environment = compilationContext.environment;
60 private void typeCheckValues() {
61 for(final SCLValue value : module.getValues()) {
62 if(value.getExpression() != null) {
63 if(value.getType() == null)
64 scheduler.addTypeInferableDefinition(new TypeInferableDefinition() {
65 ArrayList<EPlaceholder> recursiveReferences;
66 ArrayList<EVariable> constraintDemand;
67 ArrayList<Variable> freeEvidence;
68 ArrayList<Constraint> unsolvedConstraints;
71 public long getLocation() {
72 return value.getExpression().getLocation();
76 public Collection<Object> getDefinedObjects() {
77 return Collections.<Object>singleton(value);
81 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
82 value.getExpression().collectRefs(allRefs, refs);
86 public void initializeTypeChecking(TypingContext context) {
87 value.setType(Types.metaVar(Kinds.STAR));
88 context.recursiveValues.add(value);
92 public void checkType(TypingContext context) {
93 context.recursiveReferences =
94 this.recursiveReferences = new ArrayList<EPlaceholder>();
96 Expression expression = value.getExpression();
97 context.pushEffectUpperBound(expression.location, Types.PROC);
98 expression = expression.checkType(context, value.getType());
99 context.popEffectUpperBound();
100 for(EAmbiguous overloaded : context.overloadedExpressions)
101 overloaded.assertResolved(compilationContext.errorLog);
102 value.setExpression(expression);
104 ArrayList<EVariable> constraintDemand = context.getConstraintDemand();
105 if(!constraintDemand.isEmpty()) {
106 this.constraintDemand = constraintDemand;
107 context.resetConstraintDemand();
110 expression.getType().addPolarity(Polarity.POSITIVE);
114 public void solveConstraints() {
115 if(constraintDemand != null) {
116 Expression expression = value.getExpression();
118 ReducedConstraints red = ConstraintSolver.solve(
119 ce, new ArrayList<TPred>(0), constraintDemand,
122 expression = ExpressionAugmentation.augmentSolved(
123 red.solvedConstraints,
125 value.setExpression(expression);
126 value.setType(expression.getType());
128 for(Constraint c : red.unsolvedConstraints)
129 if(c.constraint.isGround())
130 compilationContext.errorLog.log(c.getDemandLocation(), "There is no instance for <"+c.constraint+">.");
132 ArrayList<Variable> fe = new ArrayList<Variable>(red.unsolvedConstraints.size());
133 for(Constraint c : red.unsolvedConstraints)
135 unsolvedConstraints = red.unsolvedConstraints;
139 value.setExpression(value.getExpression().decomposeMatching());
140 freeEvidence = new ArrayList<Variable>(0);
145 public void collectFreeTypeVariables(
146 THashSet<TVar> varSet) {
147 Type type = value.getType();
148 type = type.convertMetaVarsToVars();
150 varSet.addAll(Types.freeVars(type));
154 public ArrayList<Variable> getFreeEvidence() {
159 public ArrayList<Constraint> getUnsolvedConstraints() {
160 return unsolvedConstraints;
164 public void injectEvidence(TVar[] vars, TPred[] constraints) {
165 // Create evidence array of every value in the group that has the variables
166 // in the same array as in the shared array
167 THashMap<TPred, Variable> indexedEvidence = new THashMap<TPred, Variable>(freeEvidence.size());
168 for(Variable v : freeEvidence)
169 indexedEvidence.put((TPred)v.getType(), v);
170 freeEvidence.clear();
171 for(TPred c : constraints) {
172 Variable var = indexedEvidence.get(c);
174 // These are variables that are not directly needed in
175 // this definition but in the definitions that are
176 // recursively called
177 var = new Variable("evX");
179 freeEvidence.add(var);
181 freeEvidence.add(var);
184 // Add evidence parameters to the functions
185 value.setExpression(lambda(Types.NO_EFFECTS, freeEvidence, value.getExpression())
187 value.setType(Types.forAll(vars,
188 Types.constrained(constraints, value.getType())));
190 // Add evidence parameters to recursive calls
191 for(EPlaceholder ref : recursiveReferences) {
192 ref.expression = loc(ref.expression.location, apply(
194 applyTypes(ref.expression, vars),
195 vars(freeEvidence)));
200 scheduler.addPostTypeCheckingRunnable(new Runnable() {
203 Type type = value.getType();
205 Expression expression = value.getExpression();
208 ArrayList<TVar> vars = new ArrayList<TVar>();
209 type = Types.removeForAll(type, vars);
210 ArrayList<TPred> givenConstraints = new ArrayList<TPred>();
211 type = Types.removePred(type, givenConstraints);
213 TypingContext context = new TypingContext(compilationContext);
214 context.pushEffectUpperBound(expression.location, Types.PROC);
215 expression = expression.checkType(context, type);
216 context.popEffectUpperBound();
217 for(EAmbiguous overloaded : context.overloadedExpressions)
218 overloaded.assertResolved(compilationContext.errorLog);
219 expression.getType().addPolarity(Polarity.POSITIVE);
220 context.solveSubsumptions(expression.getLocation());
221 ArrayList<EVariable> demands = context.getConstraintDemand();
222 if(!demands.isEmpty() || !givenConstraints.isEmpty()) {
223 ReducedConstraints red =
224 ConstraintSolver.solve(ce, givenConstraints, demands, true);
225 givenConstraints.clear();
226 for(Constraint c : red.unsolvedConstraints) {
227 compilationContext.errorLog.log(c.getDemandLocation(),
228 "Constraint <"+c.constraint+"> is not given and cannot be derived.");
230 if(compilationContext.errorLog.isEmpty()) { // To prevent exceptions
231 expression = ExpressionAugmentation.augmentSolved(
232 red.solvedConstraints,
234 expression = ExpressionAugmentation.augmentUnsolved(
235 red.givenConstraints,
240 if(compilationContext.errorLog.isEmpty()) // To prevent exceptions
241 expression = expression.decomposeMatching();
243 expression = expression.closure(vars.toArray(new TVar[vars.size()]));
244 value.setExpression(expression);
245 } catch(Exception e) {
246 compilationContext.errorLog.log(expression.location, e);
254 private void typeCheckRelations() {
255 for(SCLRelation relation_ : module.getRelations()) {
256 if(!(relation_ instanceof ConcreteRelation))
258 final ConcreteRelation relation = (ConcreteRelation)relation_;
259 scheduler.addTypeInferableDefinition(new TypeInferableDefinition() {
262 public void initializeTypeChecking(TypingContext context) {
263 for(Variable parameter : relation.parameters) {
264 Type type = Types.metaVar(Kinds.STAR);
265 type.addPolarity(Polarity.BIPOLAR);
266 parameter.setType(type);
271 public void solveConstraints() {
275 public void injectEvidence(TVar[] vars, TPred[] constraints) {
276 relation.typeVariables = vars;
280 public ArrayList<Constraint> getUnsolvedConstraints() {
281 return new ArrayList<Constraint>(0); // TODO
285 public long getLocation() {
286 return relation.location;
290 public ArrayList<Variable> getFreeEvidence() {
291 return new ArrayList<Variable>(0); // TODO
295 public Collection<Object> getDefinedObjects() {
296 return Collections.<Object>singleton(relation);
300 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
301 for(QuerySection section : relation.getSections())
302 section.query.collectRefs(allRefs, refs);
306 public void collectFreeTypeVariables(THashSet<TVar> varSet) {
307 for(Variable parameter : relation.parameters) {
308 Type parameterType = parameter.getType().convertMetaVarsToVars();
309 varSet.addAll(Types.freeVars(parameterType));
314 public void checkType(TypingContext context) {
315 for(QuerySection section : relation.getSections()) {
316 section.effect = Types.metaVar(Kinds.EFFECT);
317 context.pushEffectUpperBound(relation.location, section.effect);
318 for(Variable variable : section.existentials)
319 variable.setType(Types.metaVar(Kinds.STAR));
320 section.query.checkType(context);
321 context.popEffectUpperBound();
324 if(relation.enforceSection != null) {
325 relation.writingEffect = Types.metaVar(Kinds.EFFECT);
326 context.pushEffectUpperBound(relation.location, relation.writingEffect);
327 relation.enforceSection.checkType(context);
328 context.popEffectUpperBound();
335 public void typeCheck() {
336 ce = new ConstraintEnvironment(environment);
337 scheduler = new TypeCheckingScheduler(compilationContext);
340 typeCheckRelations();
343 scheduler.schedule();
346 private void typeCheckRules() {
347 scheduler.addTypeInferableDefinition(new TypeInferableDefinition() {
349 public void solveConstraints() {
350 // TODO Auto-generated method stub
354 public void injectEvidence(TVar[] vars, TPred[] constraints) {
355 // TODO Auto-generated method stub
360 public void initializeTypeChecking(TypingContext context) {
361 // TODO Auto-generated method stub
366 public ArrayList<Constraint> getUnsolvedConstraints() {
367 return new ArrayList<Constraint>(0);
369 ArrayList<EVariable> demands = context.getConstraintDemand();
370 if(!demands.isEmpty()) {
371 ReducedConstraints red =
372 ConstraintSolver.solve(ce, new ArrayList<TPred>(), demands, true);
373 for(Constraint c : red.unsolvedConstraints) {
374 errorLog.log(c.getDemandLocation(),
375 "Constraint <"+c.constraint+"> is not given and cannot be derived.");
381 public long getLocation() {
382 // TODO Auto-generated method stub
387 public ArrayList<Variable> getFreeEvidence() {
388 return new ArrayList<Variable>(0);
392 public Collection<Object> getDefinedObjects() {
393 return Collections.singleton(ETransformation.TRANSFORMATION_RULES_TYPECHECKED);
397 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
398 for(TransformationRule rule : module.getRules())
399 for(Query[] queries : rule.sections.values())
400 for(Query query : queries)
401 query.collectRefs(allRefs, refs);
405 public void collectFreeTypeVariables(THashSet<TVar> varSet) {
409 public void checkType(TypingContext context) {
410 for(TransformationRule rule : module.getRules()) {
411 context.pushEffectUpperBound(rule.location, Types.metaVar(Kinds.EFFECT));
412 rule.checkType(context);
413 rule.setEffect(Types.canonical(context.popEffectUpperBound()));
418 if(!module.getMappingRelations().isEmpty())
419 scheduler.addPostTypeCheckingRunnable(new Runnable() {
422 for(MappingRelation mappingRelation : module.getMappingRelations())
423 for(Type parameterType : mappingRelation.parameterTypes)
424 if(!parameterType.isGround()) {
425 compilationContext.errorLog.log(mappingRelation.location, "Parameter types of the mapping relation are not completely determined.");