Skip to content

Commit

Permalink
letrec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kmeakin committed Jan 31, 2023
1 parent 74bac1b commit 6713eaf
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/succeed/letrec/factorial.fathom
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// FIXME: using (x - 1) causes a crash in distillation (see https://github.com/yeslogic/fathom/issues/485)

letrec
factorial : U32 -> U32
= fun (x : U32) => match x {
0 => 1,
_ => x * factorial (u32_sub x 1),
},
factorial_iter : U32 -> U32 -> U32
= fun (acc: U32) (x : U32) => match x {
0 => acc,
_ => factorial_iter (acc * x) (u32_sub x 1),
};
(factorial, factorial_iter)
14 changes: 14 additions & 0 deletions tests/succeed/letrec/factorial.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
stdout = '''
letrec
factorial : U32 -> U32 = fun x => match x {
0 => 1,
_ => x * factorial (x - (1 : U32)),
},
factorial_iter : U32 -> U32 -> U32 = fun acc x => match x {
0 => acc,
_ => factorial_iter (acc * x) (x - (1 : U32)),
},
;
(factorial, factorial_iter) : (U32 -> U32, U32 -> U32 -> U32)
'''
stderr = ''
10 changes: 10 additions & 0 deletions tests/succeed/letrec/fibonacci.fathom
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// FIXME: using (x - 1) or (x - 2) causes a crash in distillation (see https://github.com/yeslogic/fathom/issues/485)

letrec
fibonacci : U32 -> U32
= fun (x : U32) => match x {
0 => 0,
1 => 1,
_ => (fibonacci (u32_sub x 1)) + (fibonacci (u32_sub x 2)),
};
fibonacci
11 changes: 11 additions & 0 deletions tests/succeed/letrec/fibonacci.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
stdout = '''
letrec
fibonacci : U32 -> U32 = fun x => match x {
0 => 0,
1 => 1,
_ => fibonacci (x - (1 : U32)) + fibonacci (x - (2 : U32)),
},
;
fibonacci : U32 -> U32
'''
stderr = ''
16 changes: 16 additions & 0 deletions tests/succeed/letrec/parity.fathom
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// FIXME: using (x - 1) causes a crash in distillation (see https://github.com/yeslogic/fathom/issues/485)

letrec
is_even : U32 -> Bool
= fun (x : U32) => match x {
0 => true,
_ => is_odd (u32_sub x 1),
},

is_odd : U32 -> Bool
= fun (x : U32) => match x {
0 => false,
_ => is_even (u32_sub x 1),
},
;
(is_even, is_odd)
14 changes: 14 additions & 0 deletions tests/succeed/letrec/parity.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
stdout = '''
letrec
is_even : U32 -> Bool = fun x => match x {
0 => true,
_ => is_odd (x - (1 : U32)),
},
is_odd : U32 -> Bool = fun x => match x {
0 => false,
_ => is_even (x - (1 : U32)),
},
;
(is_even, is_odd) : (U32 -> Bool, U32 -> Bool)
'''
stderr = ''

0 comments on commit 6713eaf

Please sign in to comment.