1 package org.simantics.scl.compiler.elaboration.relations;
3 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.apply;
4 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.if_;
5 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.lambda;
6 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.let;
7 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.letRec;
8 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.tuple;
9 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.var;
11 import org.simantics.scl.compiler.common.names.Name;
12 import org.simantics.scl.compiler.elaboration.expressions.EApply;
13 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
14 import org.simantics.scl.compiler.elaboration.expressions.Expression;
15 import org.simantics.scl.compiler.elaboration.expressions.Variable;
16 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationContext;
17 import org.simantics.scl.compiler.types.TVar;
18 import org.simantics.scl.compiler.types.Type;
19 import org.simantics.scl.compiler.types.Types;
21 public class TransitiveClosureRelation extends AbstractRelation implements CompositeRelation {
23 SCLRelation baseRelation;
25 public TransitiveClosureRelation(SCLRelation baseRelation) {
26 this.baseRelation = baseRelation;
30 public TVar[] getTypeVariables() {
31 return baseRelation.getTypeVariables();
35 public Type[] getParameterTypes() {
36 return baseRelation.getParameterTypes();
40 public double getSelectivity(int boundVariabes) {
41 return baseRelation.getSelectivity(boundVariabes)*5.0;
45 public int getRequiredVariablesMask() {
46 return baseRelation.getRequiredVariablesMask();
50 public void generate(long location,
51 QueryCompilationContext context,
52 Type[] typeParameters, Variable[] parameters, int boundVariables) {
53 Variable bound, solved;
54 if(boundVariables == (1<<(parameters.length-1))-1) {
55 bound = parameters[0];
56 solved = parameters[parameters.length-1];
58 else if(boundVariables == (1<<(parameters.length))-2) {
59 bound = parameters[parameters.length-1];
60 solved = parameters[0];
62 else //if(boundVariables == 3 || boundVariables == 0)
63 throw new UnsupportedOperationException("boundVariables = " + boundVariables);
65 Type type = baseRelation.getParameterTypes()[0];
66 if(typeParameters.length > 0)
67 type = type.replace(getTypeVariables(), typeParameters);
69 Expression continuation = context.getContinuation();
70 Variable set = new Variable("set", Types.apply(Types.con("MSet", "T"), type));
71 Variable f = new Variable("f", Types.functionE(type, Types.PROC, continuation.getType()));
72 Variable innerSolved = new Variable("tcTemp", solved.getType());
74 QueryCompilationContext newContext = context.createSubcontext(new EApply(
75 new EVariable(f), new EVariable(innerSolved)
77 Variable[] innerParameters = new Variable[parameters.length];
78 if(boundVariables == (1<<(parameters.length-1))-1) {
79 innerParameters[0] = solved;
80 innerParameters[parameters.length-1] = innerSolved;
83 innerParameters[0] = innerSolved;
84 innerParameters[parameters.length-1] = solved;
86 for(int i=1;i<parameters.length-1;++i)
87 innerParameters[i] = parameters[i];
88 baseRelation.generate(location,
91 innerParameters, boundVariables);
93 continuation = context.disjunction(continuation, newContext.getContinuation());
94 continuation = if_(apply(context, Types.PROC, Name.create("MSet", "add"), type,
95 var(set), var(solved)),
98 continuation = lambda(Types.PROC, solved, continuation);
99 continuation = letRec(f, continuation, apply(var(f), var(bound)));
100 continuation = let(set,
101 apply(context, Types.PROC, Name.create("MSet", "create"), type, tuple()),
103 context.setContinuation(continuation);
105 // TODO Auto-generated method stub
106 // base :: (a -> <Proc> ()) -> a -> <Proc> ()
108 // cont :: a -> <Proc> ()
109 /* s = MSet.create ()
110 * f cur = if MSet.add s cur
115 * f cur = MSet.add s cur && (cont cur || base f cur)
116 * f cur = if MSet.add s cur
117 * then match cont cur with
118 * result @ (Just _) -> result
123 /* let s = MSet.new ()
124 f = \r -> if MSet.add s r
134 public SCLRelation[] getSubrelations() {
135 return new SCLRelation[] { baseRelation };