]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 import "StandardLibrary"
2
3 topologicalSort :: Show a => [(a,a)] -> <Proc> [a]
4 topologicalSort dependencies = MList.freeze answer
5   where
6     answer = MList.create ()
7     
8     (?x,?y) <- dependencies           =>  print "RULE 1, x=\(?x), y=\(?y)", Dep ?x ?y, InDegree ?x 0, InDegree ?y 1
9     -InDegree ?x ?a, -InDegree ?x ?b  =>  print "RULE 2, x=\(?x), a=\(?a), b=\(?b)", InDegree ?x (?a + ?b)
10     InDegree ?x ?a                    =>  print "InDegree \(?x) \(?a)"
11     InDegree ?x 0                     =>  print "RULE 3, x=\(?x)", AdjustInDegrees ?x, MList.add answer ?x
12     AdjustInDegrees ?x, Dep ?x ?y     =>  print "RULE 4, x=\(?x), y=\(?y)", InDegree ?y (-1)
13     
14 main = topologicalSort [(2,4),(3,7),(7,2),(1,3)]
15 --
16 [1, 3, 7, 2, 4]