forked from HigherOrderCO/HVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.hvm
47 lines (40 loc) · 1.02 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
37
38
39
40
41
42
43
44
45
46
47
// The Scott-Encoded Bits type
(End) = λe λo λi e
(B0 p) = λe λo λi (o p)
(B1 p) = λe λo λi (i p)
// Applies `f` `xs` times to `x`
(App xs f x) =
let e = λf λx x
let o = λpλfλx(App p λk(f (f k)) x)
let i = λpλfλx(App p λk(f (f k)) (f x))
(xs e o i f x)
// Increments a Bits by 1
(Inc xs) = λex λox λix
let e = ex
let o = ix
let i = λp (ox (Inc p))
(xs e o i)
// Adds two Bits
(Add xs ys) = (App xs λx(Inc x) ys)
// Multiplies two Bits
(Mul xs ys) =
let e = End
let o = λp (B0 (Mul p ys))
let i = λp (Add ys (B0 (Mul p ys)))
(xs e o i)
// Converts a Bits to an U32
(ToU32 ys) =
let e = 0
let o = λp (+ 0 (* 2 (ToU32 p)))
let i = λp (+ 1 (* 2 (ToU32 p)))
(ys e o i)
// Converts an U32 to a Bits
(FromU32 0 i) = End
(FromU32 s i) = (FromU32Put (- s 1) (% i 2) (/ i 2))
(FromU32Put s 0 i) = (B0 (FromU32 s i))
(FromU32Put s 1 i) = (B1 (FromU32 s i))
// Squares (n * 100k)
(Main n) =
let a = (FromU32 32 (* 100000 n))
let b = (FromU32 32 (* 100000 n))
(ToU32 (Mul a b))