1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
// Merge sort
msort(ret) >< [] => ret~[];
msort(ret) >< x:xs => (*L)msort_tail(ret, x)~xs;
msort_tail(ret, int n) >< [] => ret~[n];
msort_tail(ret, int n) >< x:xs =>
split(left,right) ~ n:(*R)(x:xs),
msort(a)~left, msort(b)~right, (*L)merge(ret,b)~a;
merge(ret, snd) >< [] => ret~snd;
merge(ret, snd) >< x:xs => (*L)mergeCC(ret, x, xs)~snd;
mergeCC(ret, int y, ys) >< [] => ret~(*R)(y:ys);
mergeCC(ret, int y, ys) >< (int x):xs
| x <= y => ret~(x:cnt), (*L)mergeCC(cnt, y, ys) ~ xs
| _ => ret~(y:cnt), (*L)mergeCC(cnt, x, xs) ~ ys;
split(right,left) >< [] => right~[], left~[];
split(right,left) >< x:xs =>
right~(*R)(x:cntl), left~cntr, (*L)split(cntr,cntl)~xs;
// creates a random list
make_RandList(ret) >< (int n)
| n>0 => ret~(rd:cnt), (*L)make_RandList(cnt)~(n-1)
where rd=rand(10000)
| _ => ret~[];
// validation checks
valid(ret) >< [] => ret~True;
valid(ret) >< x:xs => (*L)valid_Cons(ret,x)~xs;
valid_Cons(ret, int x) >< [] => ret~True;
valid_Cons(ret, int x) >< (int y):ys
| x<=y => (*L)valid_Cons(ret,y)~ys
| _ => ret~False, Eraser~ys;
// The `Eraser' is a built-in agent defined for any agents as follows:
//Eraser >< Alpha(a1, ..., a5) => Eraser~a1, ..., Eraser~a5;
// Main
const LIST_ELEM=800000;
make_RandList(rndlist)~(LIST_ELEM),
msort(sorted)~rndlist,
valid(ret)~sorted;
ret;
|