forked from drspro/metta-wam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.hvm
36 lines (27 loc) · 1.09 KB
/
main.hvm
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
// Atomic Swapper (HVM builtin)
//(U60.swap 0 a b) = (Both a b)
//(U60.swap n a b) = (Both b a)
// Swaps distant values in parallel; corresponds to a Red Box
(Warp s (Leaf a) (Leaf b)) = (U60.swap (^ (> a b) s) (Leaf a) (Leaf b))
(Warp s (Both a b) (Both c d)) = (Join (Warp s a c) (Warp s b d))
// Rebuilds the warped tree in the original order
(Join (Both a b) (Both c d)) = (Both (Both a c) (Both b d))
// Recursively warps each sub-tree; corresponds to a Blue/Green Box
(Flow s (Leaf a)) = (Leaf a)
(Flow s (Both a b)) = (Down s (Warp s a b))
// Propagates Flow downwards
(Down s (Leaf a)) = (Leaf a)
(Down s (Both a b)) = (Both (Flow s a) (Flow s b))
// Bitonic Sort
(Sort s (Leaf a)) = (Leaf a)
(Sort s (Both a b)) = (Flow s (Both (Sort 0 a) (Sort 1 b)))
// Generates a tree of depth `n`
(Gen 0 x) = (Leaf x)
(Gen n x) = let m = (- n 1); (Both (Gen m (* x 2)) (Gen m (+ (* x 2) 1)))
// Reverses a tree
(Rev (Leaf x)) = (Leaf x)
(Rev (Both a b)) = (Both (Rev b) (Rev a))
// Sums a tree
(Sum (Leaf x)) = x
(Sum (Both a b)) = (+ (Sum a) (Sum b))
(Main n) = (Sum (Sort 0 (Rev (Gen n 0))))