Compare commits

...

4 commits

Author SHA1 Message Date
Théophile Bastian 450595ab57 Add complicated recursion and nesting -- fail 2018-02-16 01:40:23 +01:00
Théophile Bastian c84a1ac169 Fix: recursive functions correctly defunctionalized 2018-02-16 01:29:01 +01:00
Théophile Bastian 29a2ef43a1 Fix: self is no longer considered free var
For recursive functions, the self-reference was considered a free
variable wrt. defunctionalization
2018-02-16 01:13:06 +01:00
Théophile Bastian 6f75134474 Add test rec_factorial (computes 7!) -- fails 2018-02-16 01:09:21 +01:00
5 changed files with 57 additions and 5 deletions

View file

@ -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

View file

@ -0,0 +1 @@
5040

View 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
View file

@ -0,0 +1,9 @@
1
1
2
3
5
8
13
21
34

25
src/tests/rec_fibo.lambda Normal file
View 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