Compare commits
4 commits
ca790364b6
...
450595ab57
Author | SHA1 | Date | |
---|---|---|---|
Théophile Bastian | 450595ab57 | ||
Théophile Bastian | c84a1ac169 | ||
Théophile Bastian | 29a2ef43a1 | ||
Théophile Bastian | 6f75134474 |
18
src/Defun.ml
18
src/Defun.ml
|
@ -65,8 +65,7 @@ let rec walk_term fs t =
|
|||
| S.LetVal (var, value, next) ->
|
||||
let fs, nNext = walk_term fs next in
|
||||
fs, T.LetVal(var, value, nNext)
|
||||
| S.LetBlo (var, S.Lam(_, vars, body), next) ->
|
||||
(* FIXME is handling of recursive functions correct? *)
|
||||
| S.LetBlo (var, S.Lam(self, vars, body), next) ->
|
||||
let fs, nNext = walk_term fs next in
|
||||
let fs, nBody = walk_term fs body in
|
||||
let arity = List.length vars + 1 in
|
||||
|
@ -77,15 +76,24 @@ let rec walk_term fs t =
|
|||
vars
|
||||
(S.vvars (List.tl args)) in
|
||||
let thisTag = freshTag () in
|
||||
let freeVars = Atom.Set.elements @@
|
||||
let freeVars =
|
||||
Atom.Set.diff
|
||||
(S.fv_term body)
|
||||
(Atom.Set.of_list vars)
|
||||
in
|
||||
let freeVarsBranch, freeVarsCon = (match self with
|
||||
| S.Self self ->
|
||||
let freeVars = Atom.Set.elements @@
|
||||
Atom.Set.diff freeVars
|
||||
(Atom.Set.singleton self) in
|
||||
self :: freeVars, var :: freeVars
|
||||
| S.NoSelf ->
|
||||
let freeVars = Atom.Set.elements freeVars in
|
||||
freeVars, freeVars) in
|
||||
let thisFunc =
|
||||
T.Branch(thisTag, freeVars, nBody) in
|
||||
T.Branch(thisTag, freeVarsBranch, nBody) in
|
||||
let fs = add_func fs arity thisFunc in
|
||||
fs, T.LetBlo (var, T.Con(thisTag, T.vvars freeVars), nNext)
|
||||
fs, T.LetBlo (var, T.Con(thisTag, T.vvars freeVarsCon), nNext)
|
||||
| S.IfZero (value, tIf, tElse) ->
|
||||
let fs, tIf = walk_term fs tIf in
|
||||
let fs, tElse = walk_term fs tElse in
|
||||
|
|
1
src/tests/rec_factorial.exp
Normal file
1
src/tests/rec_factorial.exp
Normal file
|
@ -0,0 +1 @@
|
|||
5040
|
9
src/tests/rec_factorial.lambda
Normal file
9
src/tests/rec_factorial.lambda
Normal file
|
@ -0,0 +1,9 @@
|
|||
let rec fact = fun n ->
|
||||
ifzero n then
|
||||
1
|
||||
else
|
||||
let sub_val = fact (n - 1) in
|
||||
n * sub_val
|
||||
in
|
||||
|
||||
print (fact 7)
|
9
src/tests/rec_fibo.exp
Normal file
9
src/tests/rec_fibo.exp
Normal file
|
@ -0,0 +1,9 @@
|
|||
1
|
||||
1
|
||||
2
|
||||
3
|
||||
5
|
||||
8
|
||||
13
|
||||
21
|
||||
34
|
25
src/tests/rec_fibo.lambda
Normal file
25
src/tests/rec_fibo.lambda
Normal file
|
@ -0,0 +1,25 @@
|
|||
let fibo = fun n ->
|
||||
let rec fibo_inner = fun i -> fun last -> fun last_last ->
|
||||
ifzero (n - i) then
|
||||
last + last_last
|
||||
else
|
||||
fibo_inner (i+1) (last + last_last) last
|
||||
in
|
||||
|
||||
ifzero n then
|
||||
1
|
||||
else ifzero (n - 1) then
|
||||
1
|
||||
else
|
||||
fibo_inner 2 1 1
|
||||
in
|
||||
|
||||
let x = print fibo 0 in
|
||||
let x = print fibo 1 in
|
||||
let x = print fibo 2 in
|
||||
let x = print fibo 3 in
|
||||
let x = print fibo 4 in
|
||||
let x = print fibo 5 in
|
||||
let x = print fibo 6 in
|
||||
let x = print fibo 7 in
|
||||
print fibo 8
|
Loading…
Reference in a new issue