From 9fd3a997f550b855d334259f8a67b1dc4f376cce Mon Sep 17 00:00:00 2001 From: FranchuFranchu Date: Thu, 29 Feb 2024 10:11:00 -0300 Subject: [PATCH] Recompile tests. Move `main::create_host` to `util::create_host`. --- examples/machine_u32/num_match.hvm2 | 2 +- examples/sort/bitonic/bitonic_sort_lam.hvm2 | 4 +- examples/sort/merge/merge_sort.hvm2 | 4 +- examples/sort/radix/radix_sort_lam.hvm2 | 4 +- src/main.rs | 15 +- src/util.rs | 21 ++- tests/programs/list_put_got.hvmc | 154 ++++++++++---------- tests/programs/stress_tests/all_tree.hvm2 | 2 +- tests/programs/stress_tests/apelacion.hvm2 | 4 +- tests/programs/stress_tests/fib_rec.hvm2 | 4 +- tests/programs/stress_tests/sum_rec.hvm2 | 2 +- tests/programs/stress_tests/sum_tail.hvm2 | 2 +- tests/programs/stress_tests/sum_tree.hvm2 | 2 +- tests/programs/stress_tests/tuple_rots.hvm2 | 2 +- tests/snapshots/tests__run@log.hvmc.snap | 12 ++ tests/tests.rs | 26 ++-- 16 files changed, 134 insertions(+), 126 deletions(-) create mode 100644 tests/snapshots/tests__run@log.hvmc.snap diff --git a/examples/machine_u32/num_match.hvm2 b/examples/machine_u32/num_match.hvm2 index dc2d897c..706cddf1 100644 --- a/examples/machine_u32/num_match.hvm2 +++ b/examples/machine_u32/num_match.hvm2 @@ -1,6 +1,6 @@ pred = λx match x { 0: 0 - +: x-1 + 1+: x-1 } main = (pred 10) diff --git a/examples/sort/bitonic/bitonic_sort_lam.hvm2 b/examples/sort/bitonic/bitonic_sort_lam.hvm2 index 6059d13d..3e60e4c8 100644 --- a/examples/sort/bitonic/bitonic_sort_lam.hvm2 +++ b/examples/sort/bitonic/bitonic_sort_lam.hvm2 @@ -3,7 +3,7 @@ Node = @x0 @x1 @Leaf @Node (Node x0 x1) swap = λn match n { 0: λx0 λx1 (Node x0 x1) - +: λx0 λx1 (Node x1 x0) + 1+: λx0 λx1 (Node x1 x0) } warp = λa @@ -42,7 +42,7 @@ sort = λa gen = λn match n { 0: λx (Leaf x) - +: λx (Node (gen n-1 (* x 2)) (gen n-1 (+ (* x 2) 1))) + 1+: λx (Node (gen n-1 (* x 2)) (gen n-1 (+ (* x 2) 1))) } rev = λa diff --git a/examples/sort/merge/merge_sort.hvm2 b/examples/sort/merge/merge_sort.hvm2 index b7f05b99..a5a8643a 100644 --- a/examples/sort/merge/merge_sort.hvm2 +++ b/examples/sort/merge/merge_sort.hvm2 @@ -11,7 +11,7 @@ merge = λxs let ys_nil = λx λxs (List.cons x xs) let ys_cons = λy λys λx λxs let t = λt (t (List.cons x) λx(x) (List.cons y)) - let t = let k = (< x y); (t (match k { 0: λaλbλcλt(t c a b); +: λaλbλcλt(t a b c) })) + let t = let k = (< x y); (t (match k { 0: λaλbλcλt(t c a b); 1+: λaλbλcλt(t a b c) })) (t λa λb λc (a (merge (b xs) (c ys)))) (ys ys_nil ys_cons x xs) (xs xs_nil xs_cons) @@ -23,7 +23,7 @@ sum = λxs range = λn match n { 0: λx (Leaf x) - +: λx (Node (range n-1 (+ (* x 2) 1)) (range n-1 (* x 2))) + 1+: λx (Node (range n-1 (+ (* x 2) 1)) (range n-1 (* x 2))) } main = (sum (sort (range 2 0))) diff --git a/examples/sort/radix/radix_sort_lam.hvm2 b/examples/sort/radix/radix_sort_lam.hvm2 index 418dc3b8..56047a2a 100644 --- a/examples/sort/radix/radix_sort_lam.hvm2 +++ b/examples/sort/radix/radix_sort_lam.hvm2 @@ -11,7 +11,7 @@ Node = λx0 λx1 λfree λused λnode (node x0 x1) // gen : u32 -> Arr gen = λn match n { 0: λx (Single x) - +: λx + 1+: λx let x0 = (<< x 1) let x1 = (| x0 1) (Concat (gen n-1 x0) (gen n-1 x1)) @@ -102,7 +102,7 @@ radix = λn // swap : u32 -> Map -> Map -> Map swap = λn match n { 0: λx0 λx1 (Node x0 x1) - +: λx0 λx1 (Node x1 x0) + 1+: λx0 λx1 (Node x1 x0) } // main : u32 diff --git a/src/main.rs b/src/main.rs index f3c2f19c..8b6f19cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use hvmc::{ ast::{Book, Net, Tree}, host::Host, run::{DynNet, Mode, Strict, Trg}, + util::create_host, *, }; @@ -287,20 +288,6 @@ fn load_book(files: &[String], compile_opts: &CompileArgs) -> Book { book } -fn create_host(book: &Book) -> Arc> { - let host = Arc::new(Mutex::new(host::Host::default())); - host.lock().unwrap().insert_def( - "HVM.log", - host::DefRef::Owned(Box::new(stdlib::LogDef::new({ - let host = Arc::downgrade(&host); - move |wire| { - println!("{}", host.upgrade().unwrap().lock().unwrap().readback_tree(&wire)); - } - }))), - ); - host.lock().unwrap().insert_book(&book); - host -} fn reduce_exprs(host: &Host, exprs: &[Net], opts: &RuntimeOpts) { let heap = run::Net::::init_heap_bytes(opts.memory as usize); diff --git a/src/util.rs b/src/util.rs index b9d0bdff..85f2c12b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,8 +1,7 @@ use crate::{ - ast::{Net, Tree}, - run::Rewrites, + ast::{Book, Net, Tree}, host::{DefRef, Host}, run::Rewrites }; -use std::time::Duration; +use std::{sync::{Arc, Mutex}, time::Duration}; /// Creates a variable uniquely identified by `id`. pub(crate) fn create_var(mut id: usize) -> String { @@ -249,3 +248,19 @@ fn pretty_num(n: u64) -> String { .skip(1) .collect() } + +/// Create a `Host` from a `Book`, including `hvm-core`'s built-in definitions +pub fn create_host(book: &Book) -> Arc> { + let host = Arc::new(Mutex::new(Host::default())); + host.lock().unwrap().insert_def( + "HVM.log", + DefRef::Owned(Box::new(crate::stdlib::LogDef::new({ + let host = Arc::downgrade(&host); + move |wire| { + println!("{}", host.upgrade().unwrap().lock().unwrap().readback_tree(&wire)); + } + }))), + ); + host.lock().unwrap().insert_book(&book); + host +} \ No newline at end of file diff --git a/tests/programs/list_put_got.hvmc b/tests/programs/list_put_got.hvmc index 8c35278b..6566bb11 100644 --- a/tests/programs/list_put_got.hvmc +++ b/tests/programs/list_put_got.hvmc @@ -7,10 +7,9 @@ @GotC = (a (b ((@GotS (@GotZ (a (b c)))) c))) @GotN = (* [@None @Nil]) @GotS = (a (b (c d))) -& @GotS$match$1 ~ (e (b d)) -& @Got ~ (c (a e)) -@GotS$match$1 = ([a b] (c [a d])) -& @Cons ~ (c (b d)) +& (e [f g]) ~ (b d) +& @Cons ~ (e (h g)) +& @Got ~ (c (a [f h])) @GotZ = ({3 a b} (c [d e])) & @Cons ~ (b (c e)) & @Some ~ (a d) @@ -19,19 +18,16 @@ @Put = ((@PutN (@PutC a)) a) @PutC = (a (b ((@PutCS (@PutCZ (a (b c)))) c))) @PutCS = (a (b (c (d e)))) -& @PutCS$match$1 ~ (f (b e)) -& @Put ~ (c (a (d f))) -@PutCS$match$1 = ([a b] (c [a d])) -& @Cons ~ (c (b d)) +& (f [g h]) ~ (b e) +& @Cons ~ (f (i h)) +& @Put ~ (c (a (d [g i]))) @PutCZ = (a (b (c [d e]))) & @Cons ~ (c (b e)) & @Some ~ (a d) @PutN = ((@PutNS (@PutNZ a)) a) -@PutNS = (a (b c)) -& @PutNS$match$1 ~ (d c) -& @PutN ~ (a (b d)) -@PutNS$match$1 = ([a b] [a c]) -& @Cons ~ (@None (b c)) +@PutNS = (a (b [c d])) +& @Cons ~ (@None (e d)) +& @PutN ~ (a (b [c e])) @PutNZ = (a [@None b]) & @Cons ~ (a (@Nil b)) @S0 = (* (a a)) @@ -596,71 +592,69 @@ @S9$S7 = ((@S9$S6 a) (* a)) @S9$S8 = ((@S9$S7 a) (* a)) @Some = (a (* ((a b) b))) -@main = ((a b) c) -& @main$match$1 ~ (b c) -& @Cons ~ (d (e a)) -& @Cons ~ (f (g e)) -& @Cons ~ (h (i g)) -& @Cons ~ (j (k i)) -& @Cons ~ (l (m k)) -& @Cons ~ (n (o m)) -& @Cons ~ (p (q o)) -& @Cons ~ (r (s q)) -& @Cons ~ (t (u s)) -& @Cons ~ (v (w u)) -& @Cons ~ (x (y w)) -& @Cons ~ (z (ab y)) -& @Cons ~ (bb (cb ab)) -& @Cons ~ (db (eb cb)) -& @Cons ~ (fb (gb eb)) -& @Cons ~ (hb (ib gb)) -& @Cons ~ (jb (kb ib)) -& @Cons ~ (lb (mb kb)) -& @Cons ~ (nb (ob mb)) -& @Cons ~ (pb (qb ob)) -& @Cons ~ (rb (sb qb)) -& @Cons ~ (tb (ub sb)) -& @Cons ~ (vb (wb ub)) -& @Cons ~ (xb (yb wb)) -& @Cons ~ (zb (ac yb)) -& @Cons ~ (bc (cc ac)) -& @Cons ~ (dc (ec cc)) -& @Cons ~ (fc (gc ec)) -& @Cons ~ (hc (ic gc)) -& @Cons ~ (jc (kc ic)) -& @Cons ~ (lc (mc kc)) -& @Cons ~ (nc (@Nil mc)) -& @Some ~ (@S31 nc) -& @Some ~ (@S30 lc) -& @Some ~ (@S29 jc) -& @Some ~ (@S28 hc) -& @Some ~ (@S27 fc) -& @Some ~ (@S26 dc) -& @Some ~ (@S25 bc) -& @Some ~ (@S24 zb) -& @Some ~ (@S23 xb) -& @Some ~ (@S22 vb) -& @Some ~ (@S21 tb) -& @Some ~ (@S20 rb) -& @Some ~ (@S19 pb) -& @Some ~ (@S18 nb) -& @Some ~ (@S17 lb) -& @Some ~ (@S16 jb) -& @Some ~ (@S15 hb) -& @Some ~ (@S14 fb) -& @Some ~ (@S13 db) -& @Some ~ (@S12 bb) -& @Some ~ (@S11 z) -& @Some ~ (@S10 x) -& @Some ~ (@S9 v) -& @Some ~ (@S8 t) -& @Some ~ (@S7 r) -& @Some ~ (@S6 p) -& @Some ~ (@S5 n) -& @Some ~ (@S4 l) -& @Some ~ (@S3 j) -& @Some ~ (@S2 h) -& @Some ~ (@S1 f) -& @Some ~ (@S0 d) -@main$match$1 = ([a *] a) +@main = ((a [b *]) b) +& @Cons ~ (c (d a)) +& @Cons ~ (e (f d)) +& @Cons ~ (g (h f)) +& @Cons ~ (i (j h)) +& @Cons ~ (k (l j)) +& @Cons ~ (m (n l)) +& @Cons ~ (o (p n)) +& @Cons ~ (q (r p)) +& @Cons ~ (s (t r)) +& @Cons ~ (u (v t)) +& @Cons ~ (w (x v)) +& @Cons ~ (y (z x)) +& @Cons ~ (ab (bb z)) +& @Cons ~ (cb (db bb)) +& @Cons ~ (eb (fb db)) +& @Cons ~ (gb (hb fb)) +& @Cons ~ (ib (jb hb)) +& @Cons ~ (kb (lb jb)) +& @Cons ~ (mb (nb lb)) +& @Cons ~ (ob (pb nb)) +& @Cons ~ (qb (rb pb)) +& @Cons ~ (sb (tb rb)) +& @Cons ~ (ub (vb tb)) +& @Cons ~ (wb (xb vb)) +& @Cons ~ (yb (zb xb)) +& @Cons ~ (ac (bc zb)) +& @Cons ~ (cc (dc bc)) +& @Cons ~ (ec (fc dc)) +& @Cons ~ (gc (hc fc)) +& @Cons ~ (ic (jc hc)) +& @Cons ~ (kc (lc jc)) +& @Cons ~ (mc (@Nil lc)) +& @Some ~ (@S31 mc) +& @Some ~ (@S30 kc) +& @Some ~ (@S29 ic) +& @Some ~ (@S28 gc) +& @Some ~ (@S27 ec) +& @Some ~ (@S26 cc) +& @Some ~ (@S25 ac) +& @Some ~ (@S24 yb) +& @Some ~ (@S23 wb) +& @Some ~ (@S22 ub) +& @Some ~ (@S21 sb) +& @Some ~ (@S20 qb) +& @Some ~ (@S19 ob) +& @Some ~ (@S18 mb) +& @Some ~ (@S17 kb) +& @Some ~ (@S16 ib) +& @Some ~ (@S15 gb) +& @Some ~ (@S14 eb) +& @Some ~ (@S13 cb) +& @Some ~ (@S12 ab) +& @Some ~ (@S11 y) +& @Some ~ (@S10 w) +& @Some ~ (@S9 u) +& @Some ~ (@S8 s) +& @Some ~ (@S7 q) +& @Some ~ (@S6 o) +& @Some ~ (@S5 m) +& @Some ~ (@S4 k) +& @Some ~ (@S3 i) +& @Some ~ (@S2 g) +& @Some ~ (@S1 e) +& @Some ~ (@S0 c) diff --git a/tests/programs/stress_tests/all_tree.hvm2 b/tests/programs/stress_tests/all_tree.hvm2 index defa4293..73dccc24 100644 --- a/tests/programs/stress_tests/all_tree.hvm2 +++ b/tests/programs/stress_tests/all_tree.hvm2 @@ -8,7 +8,7 @@ and = λa (a λb(b) λb(False)) gen = λn match n { 0: (Leaf True) - +: (Node (gen n-1) (gen n-1)) + 1+: (Node (gen n-1) (gen n-1)) } all = λt diff --git a/tests/programs/stress_tests/apelacion.hvm2 b/tests/programs/stress_tests/apelacion.hvm2 index b985c9fe..8d5eb44c 100644 --- a/tests/programs/stress_tests/apelacion.hvm2 +++ b/tests/programs/stress_tests/apelacion.hvm2 @@ -1,11 +1,11 @@ sum = λa match a { 0: λs s - +: λs (sum a-1 (+ a-1 s)) + 1+: λs (sum a-1 (+ a-1 s)) } rec = λa match a { 0: (sum 1000000 0) - +: (+ (rec a-1) (rec a-1)) + 1+: (+ (rec a-1) (rec a-1)) } main = (rec 6) diff --git a/tests/programs/stress_tests/fib_rec.hvm2 b/tests/programs/stress_tests/fib_rec.hvm2 index 23325c31..5b5da50a 100644 --- a/tests/programs/stress_tests/fib_rec.hvm2 +++ b/tests/programs/stress_tests/fib_rec.hvm2 @@ -2,9 +2,9 @@ add = λa λb (+ a b) fib = λx match x { 0: 1 - +: let p = x-1; match p { + 1+: let p = x-1; match p { 0: 1 - +: (+ (fib p) (fib p-1)) + 1+: (+ (fib p) (fib p-1)) } } diff --git a/tests/programs/stress_tests/sum_rec.hvm2 b/tests/programs/stress_tests/sum_rec.hvm2 index 9dea25f8..d094f909 100644 --- a/tests/programs/stress_tests/sum_rec.hvm2 +++ b/tests/programs/stress_tests/sum_rec.hvm2 @@ -2,7 +2,7 @@ add = λa λb (+ a b) sum = λn match n { 0: 1 - +: (add (sum n-1) (sum n-1)) + 1+: (add (sum n-1) (sum n-1)) } main = (sum 26) diff --git a/tests/programs/stress_tests/sum_tail.hvm2 b/tests/programs/stress_tests/sum_tail.hvm2 index 04c09f55..11cc502a 100644 --- a/tests/programs/stress_tests/sum_tail.hvm2 +++ b/tests/programs/stress_tests/sum_tail.hvm2 @@ -1,6 +1,6 @@ sum = λa match a { 0: λs s - +: λs (sum a-1 (+ a-1 s)) + 1+: λs (sum a-1 (+ a-1 s)) } main = (sum 10000000 0) diff --git a/tests/programs/stress_tests/sum_tree.hvm2 b/tests/programs/stress_tests/sum_tree.hvm2 index 790acb37..5b1fb5c4 100644 --- a/tests/programs/stress_tests/sum_tree.hvm2 +++ b/tests/programs/stress_tests/sum_tree.hvm2 @@ -5,7 +5,7 @@ add = λa λb (+ a b) gen = λn match n { 0: (Leaf 1) - +: (Node (gen n-1) (gen n-1)) + 1+: (Node (gen n-1) (gen n-1)) } sum = λt diff --git a/tests/programs/stress_tests/tuple_rots.hvm2 b/tests/programs/stress_tests/tuple_rots.hvm2 index c90fe8f5..d181428f 100644 --- a/tests/programs/stress_tests/tuple_rots.hvm2 +++ b/tests/programs/stress_tests/tuple_rots.hvm2 @@ -4,7 +4,7 @@ rot = λx (x λa λb λc λd λe λf λg λh (MkTup8 b c d e f g h a)) app = λn match n { 0: λf λx x - +: λf λx (app n-1 f (f x)) + 1+: λf λx (app n-1 f (f x)) } main = (app 2000000 rot (MkTup8 1 2 3 4 5 6 7 8)) diff --git a/tests/snapshots/tests__run@log.hvmc.snap b/tests/snapshots/tests__run@log.hvmc.snap new file mode 100644 index 00000000..df3f86c8 --- /dev/null +++ b/tests/snapshots/tests__run@log.hvmc.snap @@ -0,0 +1,12 @@ +--- +source: tests/tests.rs +expression: output +input_file: tests/programs/log.hvmc +--- +#2 +RWTS : 7 +- ANNI : 2 +- COMM : 0 +- ERAS : 1 +- DREF : 4 +- OPER : 0 diff --git a/tests/tests.rs b/tests/tests.rs index 28608de2..3d30f1d6 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,9 +1,5 @@ use std::{ - fs, - io::{self, Write}, - path::{Path, PathBuf}, - str::FromStr, - time::Instant, + fs, io::{self, Write}, path::{Path, PathBuf}, str::FromStr, sync::{Arc, Mutex}, time::Instant }; use hvmc::{ @@ -59,28 +55,32 @@ fn test_bool_and() { assert_debug_snapshot!(rwts.total(), @"9"); } -fn test_run(name: &str, host: Host) { +fn test_run(name: &str, host: Arc>) { print!("{name}..."); io::stdout().flush().unwrap(); - let Some(entrypoint) = host.defs.get("main") else { - println!(" skipping"); - return; - }; let heap = run::Net::::init_heap(1 << 29); let mut net = run::Net::::new(&heap); - net.boot(entrypoint); + // The host is locked inside this block. + { + let lock = host.lock().unwrap(); + let Some(entrypoint) = lock.defs.get("main") else { + println!(" skipping"); + return; + }; + net.boot(entrypoint); + } let start = Instant::now(); net.parallel_normal(); println!(" {:.3?}", start.elapsed()); - let output = format!("{}\n{}", host.readback(&net), show_rewrites(&net.rwts)); + let output = format!("{}\n{}", host.lock().unwrap().readback(&net), show_rewrites(&net.rwts)); assert_snapshot!(output); } fn test_path(path: &Path) { let code = fs::read_to_string(&path).unwrap(); let book = ast::Book::from_str(&code).unwrap(); - let host = Host::new(&book); + let host = hvmc::util::create_host(&book); let path = path.strip_prefix(env!("CARGO_MANIFEST_DIR")).unwrap();