]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR5.scl
(refs #7250) Refactoring CHR implementation
[simantics/platform.git] / tests / org.simantics.scl.compiler.tests / src / org / simantics / scl / compiler / tests / scl / CHR5.scl
diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR5.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR5.scl
new file mode 100644 (file)
index 0000000..e3c8b8c
--- /dev/null
@@ -0,0 +1,16 @@
+import "StandardLibrary"
+
+topologicalSort :: Show a => [(a,a)] -> <Proc> [a]
+topologicalSort dependencies = MList.freeze answer
+  where
+    answer = MList.create ()
+    
+    (?x,?y) <- dependencies           =>  print "RULE 1, x=\(?x), y=\(?y)", Dep ?x ?y, InDegree ?x 0, InDegree ?y 1
+    -InDegree ?x ?a, -InDegree ?x ?b  =>  print "RULE 2, x=\(?x), a=\(?a), b=\(?b)", InDegree ?x (?a + ?b)
+    InDegree ?x ?a                    =>  print "InDegree \(?x) \(?a)"
+    InDegree ?x 0                     =>  print "RULE 3, x=\(?x)", AdjustInDegrees ?x, MList.add answer ?x
+    AdjustInDegrees ?x, Dep ?x ?y     =>  print "RULE 4, x=\(?x), y=\(?y)", InDegree ?y (-1)
+    
+main = topologicalSort [(2,4),(3,7),(7,2),(1,3)]
+--
+[1, 3, 7, 2, 4]
\ No newline at end of file