Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

n-ary nodes #85

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bar.hvmc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

@bar = (?<#0 #1 R> R)
@main = R & @bar ~ (#123 R)
8 changes: 4 additions & 4 deletions examples/sort/bitonic/bitonic_sort_ctr.hvm1
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Atomic Swapper (HVM builtin)
//(Data.U60.swap 0 a b) = (Both a b)
//(Data.U60.swap n a b) = (Both b a)
//(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)) = (Data.U60.swap (^ (> a b) s) (Leaf a) (Leaf b))
(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
Expand Down Expand Up @@ -33,4 +33,4 @@
(Sum (Leaf x)) = x
(Sum (Both a b)) = (+ (Sum a) (Sum b))

Main = (Sum (Sort 0 (Rev (Gen 18 0))))
Main = (Sum (Sort 0 (Rev (Gen 19 0))))
40 changes: 40 additions & 0 deletions examples/sort/bitonic/bitonic_sort_ctr.hvm2
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

data Tree = (Leaf a) | (Both a b)

(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))
(Warp s * *) = *

// Rebuilds the warped tree in the original order
(Join (Both a b) (Both c d)) = (Both (Both a c) (Both b d))
(Join * *) = *

// 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 = (Sum (Sort 0 (Rev (Gen 20 0))))
87 changes: 87 additions & 0 deletions examples/sort/bitonic/bitonic_sort_ctr.hvmc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
@Both = (a (b (* ((a (b c)) c))))
@Down = (a ((@Down$C0 (@Down$C1 (a b))) b))
@Down$C0 = (a (* b))
& @Leaf ~ (a b)
@Down$C1 = (a (b ({3 c d} e)))
& @Both ~ (f (g e))
& @Flow ~ (d (b g))
& @Flow ~ (c (a f))
@Flow = (a ((@Flow$C0 (@Flow$C1 (a b))) b))
@Flow$C0 = (a (* b))
& @Leaf ~ (a b)
@Flow$C1 = (a (b ({5 c d} e)))
& @Down ~ (c (f e))
& @Warp ~ (d (a (b f)))
@Gen = (?<@Gen$C0 @Gen$C1 (a b)> (a b))
@Gen$C0 = (a b)
& @Leaf ~ (a b)
@Gen$C1 = (<+ #1 <- #1 {9 a b}>> ({7 <* #2 c> <* #2 <+ #1 d>>} e))
& @Both ~ (f (g e))
& @Gen ~ (b (d g))
& @Gen ~ (a (c f))
@Join = ((@Join$C1 (@Join$C6 (a b))) (a b))
@Join$C0 = (* *)
@Join$C1 = (* @Join$C0)
@Join$C2 = (* *)
@Join$C3 = (* @Join$C2)
@Join$C4 = (* @Join$C3)
@Join$C5 = (a (b (c (d e))))
& @Both ~ (f (g e))
& @Both ~ (d (b g))
& @Both ~ (c (a f))
@Join$C6 = (a (b ((@Join$C4 (@Join$C5 (a (b c)))) c)))
@Leaf = (a ((a b) (* b)))
@Rev = ((@Rev$C0 (@Rev$C1 a)) a)
@Rev$C0 = (a b)
& @Leaf ~ (a b)
@Rev$C1 = (a (b c))
& @Both ~ (d (e c))
& @Rev ~ (a e)
& @Rev ~ (b d)
@Sort = (a ((@Sort$C0 (@Sort$C3 (a b))) b))
@Sort$C0 = (a (* b))
& @Leaf ~ (a b)
@Sort$C1 = a
& @Sort ~ (#0 a)
@Sort$C2 = a
& @Sort ~ (#1 a)
@Sort$C3 = (a (b (c d)))
& @Flow ~ (c (e d))
& @Both ~ (f (g e))
& @Sort$C2 ~ (b g)
& @Sort$C1 ~ (a f)
@Sum = (((a a) (@Sum$C0 b)) b)
@Sum$C0 = (a (b c))
& @Sum ~ (a <+ d c>)
& @Sum ~ (b d)
@U60.swap = (?<@U60.swap$C0 @U60.swap$C2 (a (b c))> (a (b c)))
@U60.swap$C0 = (a (b c))
& @Both ~ (a (b c))
@U60.swap$C1 = (a (b c))
& @Both ~ (b (a c))
@U60.swap$C2 = (* @U60.swap$C1)
@Warp = (a ((@Warp$C5 (@Warp$C11 (a (b c)))) (b c)))
@Warp$C0 = ({11 a b} (c ({13 <> a <^ c d>> e} f)))
& @U60.swap ~ (d (g (h f)))
& @Leaf ~ (b h)
& @Leaf ~ (e g)
@Warp$C1 = (* *)
@Warp$C10 = (a (b ({15 c d} (e (f g)))))
& @Join ~ (h (i g))
& @Warp ~ (d (f (b i)))
& @Warp ~ (c (e (a h)))
@Warp$C11 = (a (b (c ((@Warp$C9 (@Warp$C10 (c (a (b d))))) d))))
@Warp$C2 = (* @Warp$C1)
@Warp$C3 = (* @Warp$C2)
@Warp$C4 = (* @Warp$C3)
@Warp$C5 = (a (b ((@Warp$C0 (@Warp$C4 (b (a c)))) c)))
@Warp$C6 = (* *)
@Warp$C7 = (* @Warp$C6)
@Warp$C8 = (* @Warp$C7)
@Warp$C9 = (* @Warp$C8)
@main = a
& @Sum ~ (b a)
& @Sort ~ (#0 (c b))
& @Rev ~ (d c)
& @Gen ~ (#19 (#0 d))

2 changes: 1 addition & 1 deletion examples/sort/bitonic/bitonic_sort_lam.hvmc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
& @sum ~ (b a)
& @sort ~ (c (#0 b))
& @rev ~ (d c)
& @gen ~ (#10 (#0 d))
& @gen ~ (#16 (#0 d))
@rev = ((@rev$S0 (@rev$S1 a)) a)
@rev$S0 = (a b)
& @Leaf ~ (a b)
Expand Down
48 changes: 24 additions & 24 deletions examples/sort/radix/radix_sort_ctr.hvm1
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,30 @@
// Radix : U60 -> Map
(Radix n) =
let r = Used
let r = (Data.U60.swap (& n 1) r Free)
let r = (Data.U60.swap (& n 2) r Free)
let r = (Data.U60.swap (& n 4) r Free)
let r = (Data.U60.swap (& n 8) r Free)
let r = (Data.U60.swap (& n 16) r Free)
let r = (Data.U60.swap (& n 32) r Free)
let r = (Data.U60.swap (& n 64) r Free)
let r = (Data.U60.swap (& n 128) r Free)
let r = (Data.U60.swap (& n 256) r Free)
let r = (Data.U60.swap (& n 512) r Free)
let r = (Data.U60.swap (& n 1024) r Free)
let r = (Data.U60.swap (& n 2048) r Free)
let r = (Data.U60.swap (& n 4096) r Free)
let r = (Data.U60.swap (& n 8192) r Free)
let r = (Data.U60.swap (& n 16384) r Free)
let r = (Data.U60.swap (& n 32768) r Free)
let r = (Data.U60.swap (& n 65536) r Free)
let r = (Data.U60.swap (& n 131072) r Free)
let r = (Data.U60.swap (& n 262144) r Free)
let r = (Data.U60.swap (& n 524288) r Free)
let r = (Data.U60.swap (& n 1048576) r Free)
let r = (Data.U60.swap (& n 2097152) r Free)
let r = (Data.U60.swap (& n 4194304) r Free)
let r = (Data.U60.swap (& n 8388608) r Free)
let r = (U60.swap (& n 1) r Free)
let r = (U60.swap (& n 2) r Free)
let r = (U60.swap (& n 4) r Free)
let r = (U60.swap (& n 8) r Free)
let r = (U60.swap (& n 16) r Free)
let r = (U60.swap (& n 32) r Free)
let r = (U60.swap (& n 64) r Free)
let r = (U60.swap (& n 128) r Free)
let r = (U60.swap (& n 256) r Free)
let r = (U60.swap (& n 512) r Free)
let r = (U60.swap (& n 1024) r Free)
let r = (U60.swap (& n 2048) r Free)
let r = (U60.swap (& n 4096) r Free)
let r = (U60.swap (& n 8192) r Free)
let r = (U60.swap (& n 16384) r Free)
let r = (U60.swap (& n 32768) r Free)
let r = (U60.swap (& n 65536) r Free)
let r = (U60.swap (& n 131072) r Free)
let r = (U60.swap (& n 262144) r Free)
let r = (U60.swap (& n 524288) r Free)
let r = (U60.swap (& n 1048576) r Free)
let r = (U60.swap (& n 2097152) r Free)
let r = (U60.swap (& n 4194304) r Free)
let r = (U60.swap (& n 8388608) r Free)
r

// Reverse : Arr -> Arr
Expand Down
2 changes: 1 addition & 1 deletion examples/sort/radix/radix_sort_ctr.hvmc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
& @sum ~ (b a)
& @sort ~ (c b)
& @rev ~ (d c)
& @gen ~ (#20 (#0 d))
& @gen ~ (#18 (#0 d))
@merge = (((a a) (@merge$C2 (@merge$C7 (b c)))) (b c))
@merge$C0 = (* *)
@merge$C1 = (* @merge$C0)
Expand Down
32 changes: 32 additions & 0 deletions foo.hvmc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@S = (a {2 {2 a b} {2 * b}})
@Z = {2 * {2 a a}}
@add$C0 = {2 {2 @add$C0 {2 (a a) (b c)}} (b {2 {2 c d} {2 * d}})}
@era = ({2 @era$C0 {2 @Z a}} a)
@era$C0 = {2 {2 @era$C0 {2 @Z a}} a}
@main = a
& @era ~ (b a)
& @pow ~ (c b)
& @S ~ (d c)
& @S ~ (e d)
& @S ~ (f e)
& @S ~ (g f)
& @S ~ (h g)
& @S ~ (i h)
& @S ~ (j i)
& @S ~ (k j)
& @S ~ (l k)
& @S ~ (m l)
& @S ~ (n m)
& @S ~ (o n)
& @S ~ (p o)
& @S ~ (q p)
& @S ~ (r q)
& @S ~ (s r)
& @S ~ (t s)
& @S ~ (u t)
& @S ~ (v u)
& @S ~ (w v)
& @S ~ (x w)
& @S ~ (@Z x)
@pow = ({2 @pow$C0 {2 {2 {2 @Z a} {2 * a}} b}} b)
@pow$C0 = {2 {3 {2 @pow$C0 {2 {2 {2 @Z a} {2 * a}} {2 @add$C0 {2 (b b) (c d)}}}} {2 @pow$C0 {2 {2 {2 @Z e} {2 * e}} c}}} d}
28 changes: 28 additions & 0 deletions foo.hvml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
data Nat = (S pred) | Z

add = λx λy
match x {
Z: y
S: (S (add x.pred y))
}

pow = λx
match x {
Z: (S Z)
S: (add (pow x.pred) (pow x.pred))
}

era = λx
match x {
Z: Z
S: (era x.pred)
}

main = (era (pow
(S (S
(S (S (S (S
(S (S (S (S
(S (S (S (S
(S (S (S (S
(S (S (S (S
Z))))))))))))))))))))))))
39 changes: 39 additions & 0 deletions radix_opt.hvmc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@Node = (a b (:2:3 a b))

@Single = (a (:1:3 a))

@gen = (?<@Single @gen$C1 a> a)

@gen$C1 = ({7 ?<@Single @gen$C1 (a b)> ?<@Single @gen$C1 (c d)>} <<< #1 {9 a <| #1 c>}> (:2:3 b d))

@main = a
& @sum ~ (b a)
& @sort ~ (c b)
& @rev ~ (d c)
& @gen ~ (#18 #0 d)

@merge$C2 = (:0:1 (:1:3) (:1:3) *)

@merge$C6 = (a (b ((:0:1) @merge$C2 @merge$C7 a d) ((:0:1) @merge$C2 @merge$C7 b f) (:2:3 d f)))

@merge$C7 = (a b (:0:1 @Node * @merge$C6 a b))

@radix = ({3 <& #8388608 ?<@Node @swap$C2 (a (:0:3) b)>> <& #4194304 ?<@Node @swap$C2 (c (:0:3) a)>> <& #2097152 ?<@Node @swap$C2 (d (:0:3) c)>> <& #1048576 ?<@Node @swap$C2 (e (:0:3) d)>> <& #524288 ?<@Node @swap$C2 (f (:0:3) e)>> {3 <& #262144 ?<@Node @swap$C2 (g (:0:3) f)>> <& #131072 ?<@Node @swap$C2 (h (:0:3) g)>> <& #65536 ?<@Node @swap$C2 (i (:0:3) h)>> <& #32768 ?<@Node @swap$C2 (j (:0:3) i)>> <& #16384 ?<@Node @swap$C2 (k (:0:3) j)>> <& #8192 ?<@Node @swap$C2 (l (:0:3) k)>> {3 <& #4096 ?<@Node @swap$C2 (m (:0:3) l)>> <& #2048 ?<@Node @swap$C2 (n (:0:3) m)>> <& #1024 ?<@Node @swap$C2 (o (:0:3) n)>> <& #512 ?<@Node @swap$C2 (p (:0:3) o)>> <& #256 ?<@Node @swap$C2 (q (:0:3) p)>> <& #128 ?<@Node @swap$C2 (r (:0:3) q)>> {3 <& #64 ?<@Node @swap$C2 (s (:0:3) r)>> <& #32 ?<@Node @swap$C2 (t (:0:3) s)>> <& #16 ?<@Node @swap$C2 (u (:0:3) t)>> <& #8 ?<@Node @swap$C2 (v (:0:3) u)>> <& #4 ?<@Node @swap$C2 (w (:0:3) v)>> <& #2 ?<@Node @swap$C2 (x (:0:3) w)>> <& #1 ?<@Node @swap$C2 ((:1:3) (:0:3) x)>>}}}} b)

@rev = (:0:1 (:0:3) @Single @rev$C1)

@rev$C1 = (((:0:3) @Single @rev$C1 a) ((:0:3) @Single @rev$C1 b) (:2:3 b a))

@sort = (((:0:3) (@radix @to_map$C1 (:1:4) @Single @to_arr$C2 #0 a)) a)

@sum = (:0:1 #0 (:0:1) @sum$C0)

@sum$C0 = ((#0 (:0:1) @sum$C0 <+ b c>) (#0 (:0:1) @sum$C0 b) c)

@swap$C1 = (a b (:2:3 b a))

@swap$C2 = (* @swap$C1)

@to_arr$C2 = (((:1:4) @Single @to_arr$C2 a b) ((:1:4) @Single @to_arr$C2 c d) {5 <* #2 a> <* #2 <+ #1 c>>} (:2:3 b d))

@to_map$C1 = (((:0:3) (@radix @to_map$C1 (:0:1) @merge$C2 @merge$C7 b c)) ((:0:3) @radix @to_map$C1 b) c)
7 changes: 4 additions & 3 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn compile_def(code: &mut String, host: &Host, name: &str, instr: &[Instruction]
Instruction::LinkConst { trg, port } => {
writeln!(code, "net.link_trg({trg}, Trg::port({}));", compile_port(host, port))
}
Instruction::Ctr { lab, trg, lft, rgt } => {
Instruction::Ctr2 { lab, trg, lft, rgt } => {
writeln!(code, "let ({lft}, {rgt}) = net.do_ctr({lab}, {trg});")
}
Instruction::Op { op, trg, rhs, out } => {
Expand All @@ -75,12 +75,13 @@ fn compile_def(code: &mut String, host: &Host, name: &str, instr: &[Instruction]
Instruction::OpNum { op, trg, rhs, out } => {
writeln!(code, "let {out} = net.do_op_num({op:?}, {trg}, {rhs});")
}
Instruction::Mat { trg, lft, rgt } => {
writeln!(code, "let ({lft}, {rgt}) = net.do_mat({trg});")
Instruction::Mat { trg, zero, succ, out } => {
writeln!(code, "let ({zero}, {succ}, {out}) = net.do_mat({trg});")
}
Instruction::Wires { av, aw, bv, bw } => {
writeln!(code, "let ({av}, {aw}, {bv}, {bw}) = net.do_wires();")
}
_ => todo!(),
}?;
}
writeln!(code, "}}")?;
Expand Down
Loading