mirror of
https://github.com/tweag/nickel.git
synced 2024-10-04 23:27:15 +03:00
Recursive let tests
This commit is contained in:
parent
686d87643f
commit
6151e20586
@ -1,9 +1,9 @@
|
||||
{
|
||||
run = {
|
||||
f = fun n =>
|
||||
if n == 0 || n == 1 then
|
||||
1
|
||||
else
|
||||
f (n - 1) + f (n - 2)
|
||||
}.f
|
||||
run =
|
||||
let rec f = fun n =>
|
||||
if n == 0 || n == 1 then
|
||||
1
|
||||
else
|
||||
f (n - 1) + f (n - 2)
|
||||
in f
|
||||
}
|
||||
|
@ -163,3 +163,19 @@ fn nested_records() {
|
||||
HashMap::from([("a", vec!["c"]), ("b", vec!["a", "c"]), ("c", vec!["b"]),])
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recursive_let() {
|
||||
assert!(check_stat_vars(
|
||||
"{
|
||||
a = let rec b = b + a + h in b,
|
||||
b = let rec a = a + b in f (a + 1) z,
|
||||
c = let rec foo = b in let rec bar = c in if a.r then [b] else {foo = c}
|
||||
}",
|
||||
HashMap::from([
|
||||
("a", vec!["a"]),
|
||||
("b", vec!["b"]),
|
||||
("c", vec!["a", "b", "c"])
|
||||
])
|
||||
));
|
||||
}
|
||||
|
@ -117,3 +117,8 @@ fn importing() {
|
||||
fn overriding() {
|
||||
check_file("overriding.ncl");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recursive_let() {
|
||||
check_file("recursive_let.ncl");
|
||||
}
|
||||
|
7
tests/pass/recursive_let.ncl
Normal file
7
tests/pass/recursive_let.ncl
Normal file
@ -0,0 +1,7 @@
|
||||
let Assert = fun l x => x || %blame% l in
|
||||
|
||||
[
|
||||
let rec f = fun n => if n == 0 then n else f (n - 1) in f 10 == 0,
|
||||
let rec fib = fun n => if n == 0 || n == 1 then 1 else fib (n - 1) + fib (n - 2) in fib 5 == 8,
|
||||
]
|
||||
|> array.foldl (fun x y => (x | Assert) && y) true
|
@ -74,6 +74,15 @@ let typecheck = [
|
||||
fun x => switch {`blo => `bla, `ble => `bli, _ => `bla} x in
|
||||
f `bli,
|
||||
|
||||
# recursive let bindings
|
||||
let rec f : forall a. a -> Num -> a = fun x n =>
|
||||
if n == 0 then x else if f "0" n == "1" then f x (n - 1) else f x (f 1 n) in
|
||||
(f "0" 2 : Str),
|
||||
let rec f : Num -> Num = fun x => if x == 0 then x else f (x - 1) in
|
||||
(f 10 : Num),
|
||||
let rec repeat : forall a. Num -> a -> Array a = fun n x =>
|
||||
if n <= 0 then [] else repeat (n - 1) x @ [x] in (repeat 3 "foo" : Array Str),
|
||||
|
||||
# static records
|
||||
({bla = 1} : {bla : Num}),
|
||||
({blo = true, bla = 1} : {bla : Num, blo : Bool}),
|
||||
|
@ -230,3 +230,10 @@ fn piecewise_signature() {
|
||||
Err(TypecheckError::TypeMismatch(..))
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn recursive_let() {
|
||||
assert_matches!(
|
||||
type_check_expr("let rec f : Num -> Num = fun x => f \"hoi\" in null"),
|
||||
Err(TypecheckError::TypeMismatch(..))
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user