mirror of
https://github.com/HigherOrderCO/Bend.git
synced 2024-09-17 14:47:21 +03:00
Merge pull request #261 from HigherOrderCO/feature/sc-543/add-hvml-test-on-example-programs
[sc-543] Add hvml test on example programs
This commit is contained in:
commit
56b3a0bec8
18
examples/all_tree.hvm
Normal file
18
examples/all_tree.hvm
Normal file
@ -0,0 +1,18 @@
|
||||
data Bool = True | False
|
||||
|
||||
data Tree
|
||||
= (Node lft rgt)
|
||||
| (Leaf val)
|
||||
|
||||
and True True = True
|
||||
and _ _ = False
|
||||
|
||||
all (Node lft rgt) = (and (all lft) (all rgt))
|
||||
all (Leaf val) = val
|
||||
|
||||
main = (all (gen 8))
|
||||
|
||||
gen = λn switch n {
|
||||
0: (Leaf True)
|
||||
_: let tree = (gen n-1); (Node tree tree)
|
||||
}
|
18
examples/alloc_small_tree.hvm
Normal file
18
examples/alloc_small_tree.hvm
Normal file
@ -0,0 +1,18 @@
|
||||
T = λt λf t
|
||||
F = λt λf f
|
||||
And = λpλq (p q F)
|
||||
|
||||
Z = λs λz (z)
|
||||
S = λn λs λz (s n)
|
||||
|
||||
Add = λa λb (a (S b))
|
||||
Mul = λa λb λf (a (b f))
|
||||
Pow = λa λb (a (Mul b) (S Z))
|
||||
|
||||
Node = λa λb λn λl (n a b)
|
||||
Leaf = λn λl l
|
||||
|
||||
Alloc = λn (n (λp (Node (Alloc p) (Alloc p))) Leaf)
|
||||
Destroy = λt (t (λaλb (And (Destroy a) (Destroy b))) T)
|
||||
|
||||
Main = (Destroy (Alloc (Pow (S (S Z)) (S (S (S (S Z)))))))
|
@ -19,8 +19,8 @@
|
||||
|
||||
// You can use 'switch' to pattern match native numbers
|
||||
// The `_` arm binds the `scrutinee`-1 variable to the the value of the number -1
|
||||
(Num.pred) = λn
|
||||
switch n {
|
||||
(Num.pred) = λn
|
||||
switch n {
|
||||
0: 0
|
||||
_: n-1
|
||||
}
|
||||
@ -52,8 +52,9 @@ data Boxed = (Box val)
|
||||
(Box.map (Box val) f) = (Box (f val))
|
||||
|
||||
(Box.unbox) = λbox
|
||||
let (Box val) = box
|
||||
val
|
||||
match box {
|
||||
Box: box.val
|
||||
}
|
||||
|
||||
// Use tuples to store two values together without needing to create a new data type
|
||||
(Tuple.new fst snd) =
|
||||
@ -71,7 +72,7 @@ data Boxed = (Box val)
|
||||
// You can execute a program in HVM with "cargo run -- --run <path to file>"
|
||||
// Other options are "--check" (the default mode) to just see if the file is well formed
|
||||
// and "--compile" to output hvm-core code.
|
||||
(main) =
|
||||
(main) =
|
||||
let tup = (Tuple.new None (Num.pred 5))
|
||||
|
||||
let fst = (Tuple.fst tup)
|
||||
|
11
examples/fib.hvm
Normal file
11
examples/fib.hvm
Normal file
@ -0,0 +1,11 @@
|
||||
add = λa λb (+ a b)
|
||||
|
||||
fib = λx switch x {
|
||||
0: 1
|
||||
_: let p = x-1; switch p {
|
||||
0: 1
|
||||
_: (+ (fib p) (fib p-1))
|
||||
}
|
||||
}
|
||||
|
||||
main = (fib 30)
|
17
examples/gen_tree_kind2.hvm
Normal file
17
examples/gen_tree_kind2.hvm
Normal file
@ -0,0 +1,17 @@
|
||||
_Char = 0
|
||||
_List = λ_T 0
|
||||
_List.cons = λ_head λ_tail λ_P λ_cons λ_nil ((_cons _head) _tail)
|
||||
_List.nil = λ_P λ_cons λ_nil _nil
|
||||
_Nat = 0
|
||||
_Nat.succ = λ_n λ_P λ_succ λ_zero (_succ _n)
|
||||
_Nat.zero = λ_P λ_succ λ_zero _zero
|
||||
_String = (_List _Char)
|
||||
_String.cons = λ_head λ_tail λ_P λ_cons λ_nil ((_cons _head) _tail)
|
||||
_String.nil = λ_P λ_cons λ_nil _nil
|
||||
_Tree = λ_A 0
|
||||
_Tree.gen = λ_n λ_x switch _n = _n { 0: _Tree.leaf _: let _n-1 = _n-1 (((_Tree.node _x) ((_Tree.gen _n-1) (+ (* _x 2) 1))) ((_Tree.gen _n-1) (+ (* _x 2) 2))) }
|
||||
_Tree.leaf = λ_P λ_node λ_leaf _leaf
|
||||
_Tree.node = λ_val λ_lft λ_rgt λ_P λ_node λ_leaf (((_node _val) _lft) _rgt)
|
||||
_main = ((_Tree.gen 8) 2)
|
||||
|
||||
main = _main
|
24
examples/neg_fusion.hvm
Normal file
24
examples/neg_fusion.hvm
Normal file
@ -0,0 +1,24 @@
|
||||
// size = 1 << 8
|
||||
|
||||
Mul = λn λm λs (n (m s))
|
||||
|
||||
// Church nat
|
||||
C2 = λf λx (f (f x))
|
||||
|
||||
// Church powers of two
|
||||
P2 = (Mul C2 C2) // 4
|
||||
P3 = (Mul C2 P2) // 8
|
||||
P4 = (Mul C2 P3) // 16
|
||||
P5 = (Mul C2 P4) // 32
|
||||
P6 = (Mul C2 P5) // 64
|
||||
P7 = (Mul C2 P6) // 128
|
||||
P8 = (Mul C2 P7) // 256
|
||||
|
||||
// Booleans
|
||||
True = λt λf t
|
||||
False = λt λf f
|
||||
Not = λb ((b False) True)
|
||||
Neg = λb λt λf (b f t)
|
||||
|
||||
// Negates 'true' 256 times: 'neg' is faster than 'not' because it fuses
|
||||
Main = (P8 Neg True)
|
@ -418,3 +418,36 @@ fn io() {
|
||||
}),
|
||||
])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn examples() -> Result<(), Diagnostics> {
|
||||
let examples_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples");
|
||||
|
||||
for entry in WalkDir::new(examples_path)
|
||||
.min_depth(1)
|
||||
.into_iter()
|
||||
.filter_map(|e| e.ok())
|
||||
.filter(|e| e.path().extension().map_or(false, |ext| ext == "hvm"))
|
||||
{
|
||||
let path = entry.path();
|
||||
eprintln!("Testing {}", path.display());
|
||||
let code = std::fs::read_to_string(path).map_err(|e| e.to_string())?;
|
||||
|
||||
let book = do_parse_book(&code, path).unwrap();
|
||||
let mut compile_opts = CompileOpts::default_strict();
|
||||
compile_opts.linearize_matches = hvml::OptLevel::Extra;
|
||||
let diagnostics_cfg = DiagnosticsConfig::default_strict();
|
||||
let (res, _) = run_book(book, None, RunOpts::default(), compile_opts, diagnostics_cfg, None)?;
|
||||
|
||||
let mut settings = insta::Settings::clone_current();
|
||||
settings.set_prepend_module_to_snapshot(false);
|
||||
settings.set_omit_expression(true);
|
||||
settings.set_input_file(path);
|
||||
|
||||
settings.bind(|| {
|
||||
assert_snapshot!(format!("examples__{}", path.file_name().unwrap().to_str().unwrap()), res);
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
5
tests/snapshots/examples__all_tree.hvm.snap
Normal file
5
tests/snapshots/examples__all_tree.hvm.snap
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/examples/all_tree.hvm
|
||||
---
|
||||
True
|
5
tests/snapshots/examples__alloc_small_tree.hvm.snap
Normal file
5
tests/snapshots/examples__alloc_small_tree.hvm.snap
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/examples/alloc_small_tree.hvm
|
||||
---
|
||||
λa λ* a
|
5
tests/snapshots/examples__callcc.hvm.snap
Normal file
5
tests/snapshots/examples__callcc.hvm.snap
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: examples/callcc.hvm
|
||||
---
|
||||
52
|
5
tests/snapshots/examples__example.hvm.snap
Normal file
5
tests/snapshots/examples__example.hvm.snap
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: examples/example.hvm
|
||||
---
|
||||
8
|
5
tests/snapshots/examples__fib.hvm.snap
Normal file
5
tests/snapshots/examples__fib.hvm.snap
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/examples/fib.hvm
|
||||
---
|
||||
1346269
|
5
tests/snapshots/examples__fusing_add.hvm.snap
Normal file
5
tests/snapshots/examples__fusing_add.hvm.snap
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: examples/fusing_add.hvm
|
||||
---
|
||||
λa λb λ* (b λc λ* (c a))
|
5
tests/snapshots/examples__fusing_not.hvm.snap
Normal file
5
tests/snapshots/examples__fusing_not.hvm.snap
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: examples/fusing_not.hvm
|
||||
---
|
||||
λa λb λc (a b c)
|
5
tests/snapshots/examples__gen_tree_kind2.hvm.snap
Normal file
5
tests/snapshots/examples__gen_tree_kind2.hvm.snap
Normal file
File diff suppressed because one or more lines are too long
5
tests/snapshots/examples__neg_fusion.hvm.snap
Normal file
5
tests/snapshots/examples__neg_fusion.hvm.snap
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/examples/neg_fusion.hvm
|
||||
---
|
||||
λa λ* a
|
Loading…
Reference in New Issue
Block a user