]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR2.scl
Merged changes from feature/scl to master.
[simantics/platform.git] / tests / org.simantics.scl.compiler.tests / src / org / simantics / scl / compiler / tests / scl / CHR2.scl
diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR2.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/CHR2.scl
new file mode 100644 (file)
index 0000000..35fb724
--- /dev/null
@@ -0,0 +1,49 @@
+import "Prelude"\r
+\r
+gSum :: Additive a => [a] -> <Proc> a\r
+gSum list = getRef answer\r
+  where\r
+    answer = ref zero\r
+    \r
+    constraint El a\r
+    \r
+    ?x <- list => El ?x\r
+    -El ?x, -El ?y => El (?x + ?y)\r
+    El ?x => answer := ?x\r
+    \r
+main = (gSum [1,6,9], gSum [1.0,6.0,9.0]) \r
+--\r
+(16,16.0)\r
+--\r
+import "StandardLibrary"\r
+\r
+topologicalSort :: [(a,a)] -> <Proc> [a]\r
+topologicalSort dependencies = MList.freeze answer\r
+  where\r
+    answer = MList.create ()\r
+    \r
+    (?x,?y) <- dependencies           =>  Dep ?x ?y, InDegree ?x 0, InDegree ?y 1\r
+    -InDegree ?x ?a, -InDegree ?x ?b  =>  InDegree ?x (?a + ?b)\r
+    InDegree ?x 0                     =>  AdjustInDegrees ?x, MList.add answer ?x\r
+    AdjustInDegrees ?x, Dep ?x ?y     =>  InDegree ?y (-1)\r
+    \r
+main = topologicalSort [(2,4),(3,7),(7,2),(1,3)]\r
+--\r
+[1, 3, 7, 2, 4]\r
+--\r
+import "StandardLibrary"\r
+\r
+topologicalSort :: Show a => [(a,a)] -> <Proc> [a]\r
+topologicalSort dependencies = MList.freeze answer\r
+  where\r
+    answer = MList.create ()\r
+    \r
+    -Candidate ?x, Candidate ?x  =>  True\r
+    (?x,?y) <- dependencies      =>  Dep ?x ?y, Candidate ?x\r
+    -Candidate ?x, Dep _ ?x      =>  True\r
+    Candidate ?x                 =>  MList.add answer ?x \r
+    Candidate ?x, -Dep ?x ?y     =>  Candidate ?y\r
+\r
+main = topologicalSort [(2,4),(3,7),(7,2),(1,3)]\r
+--\r
+[1, 3, 7, 2, 4]\r