]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 import "Prelude"\r
2 \r
3 gSum :: Additive a => [a] -> <Proc> a\r
4 gSum list = getRef answer\r
5   where\r
6     answer = ref zero\r
7     \r
8     constraint El a\r
9     \r
10     ?x <- list => El ?x\r
11     -El ?x, -El ?y => El (?x + ?y)\r
12     El ?x => answer := ?x\r
13     \r
14 main = (gSum [1,6,9], gSum [1.0,6.0,9.0]) \r
15 --\r
16 (16,16.0)\r
17 --\r
18 import "StandardLibrary"\r
19 \r
20 topologicalSort :: [(a,a)] -> <Proc> [a]\r
21 topologicalSort dependencies = MList.freeze answer\r
22   where\r
23     answer = MList.create ()\r
24     \r
25     (?x,?y) <- dependencies           =>  Dep ?x ?y, InDegree ?x 0, InDegree ?y 1\r
26     -InDegree ?x ?a, -InDegree ?x ?b  =>  InDegree ?x (?a + ?b)\r
27     InDegree ?x 0                     =>  AdjustInDegrees ?x, MList.add answer ?x\r
28     AdjustInDegrees ?x, Dep ?x ?y     =>  InDegree ?y (-1)\r
29     \r
30 main = topologicalSort [(2,4),(3,7),(7,2),(1,3)]\r
31 --\r
32 [1, 3, 7, 2, 4]\r
33 --\r
34 import "StandardLibrary"\r
35 \r
36 topologicalSort :: Show a => [(a,a)] -> <Proc> [a]\r
37 topologicalSort dependencies = MList.freeze answer\r
38   where\r
39     answer = MList.create ()\r
40     \r
41     -Candidate ?x, Candidate ?x  =>  True\r
42     (?x,?y) <- dependencies      =>  Dep ?x ?y, Candidate ?x\r
43     -Candidate ?x, Dep _ ?x      =>  True\r
44     Candidate ?x                 =>  MList.add answer ?x \r
45     Candidate ?x, -Dep ?x ?y     =>  Candidate ?y\r
46 \r
47 main = topologicalSort [(2,4),(3,7),(7,2),(1,3)]\r
48 --\r
49 [1, 3, 7, 2, 4]\r