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