Compare commits

..

5 commits

Author SHA1 Message Date
93e20cf208 Light CPS: cleaner error when lightening non-value 2018-02-16 16:06:36 +01:00
f5783f2b2b Light CPS: implement lambdas 2018-02-16 15:48:56 +01:00
613adf0621 Lighter CPS: first tentative version
Try to lighten the CPS transformation, using fewer continuations when
there is no need for one
2018-02-16 15:48:56 +01:00
b0cd872baf Make NotValue more explicit 2018-02-16 15:43:01 +01:00
3ca59d6975 Add test for redefinition of variables 2018-02-16 15:42:47 +01:00
3 changed files with 28 additions and 12 deletions

View file

@ -3,7 +3,10 @@ module S = Lambda
(* The target calculus. *)
module T = Tail
exception NotValue (** <- Raised when trying to use a non-value term as such *)
exception NotValue of S.term
(** ^ Raised when trying to use a non-value term as such *)
exception NotLightCPSable of S.term
let freshId =
(** Generates a fresh variable name string *)
@ -28,7 +31,7 @@ let letCont name varName body next =
let rec has_calls (t: S.term): bool = match t with
| S.Var _ | S.Lit _ | S.BinOp _ -> false
| S.Lam _ -> true (* TODO *)
| S.Lam _ -> false
(* A lambda itself may contain calls, but this call is not evaluated at
* declaration time *)
| S.App _ -> true
@ -49,7 +52,7 @@ let rec cps_value (t: S.term) : T.value = match t with
| S.Var v -> T.VVar v
| S.Lit v -> T.VLit v
| S.BinOp (l, op, r) -> T.VBinOp (cps_value l, op, cps_value r)
| S.Let _ | S.Lam _ | S.App _ | S.Print _ | S.IfZero _ -> raise NotValue
| S.Let _ | S.Lam _ | S.App _ | S.Print _ | S.IfZero _ -> raise (NotValue t)
let cps_value_as_term (t: S.term) (cont: T.variable): T.term =
T.TailCall(T.vvar cont, [cps_value t])
@ -59,12 +62,10 @@ let rec cps_term_inner (t: S.term) (cont: T.variable) (nameHint: string option)
| S.Var _ -> cps_value_as_term t cont
| S.Lit _ -> cps_value_as_term t cont
| S.BinOp _ -> cps_value_as_term t cont
| S.Lam (self, var, term) ->
let fName = freshBlockVarHinted nameHint
and innerCont = freshBlockVar () in
T.LetBlo(fName,
T.Lam(self, [var; innerCont], cps_term_inner term innerCont None),
T.TailCall(T.vvar cont, T.vvars [fName]))
| S.Lam _ as lambda ->
let fName = freshBlockVarHinted nameHint in
light_term fName lambda None @@
T.TailCall(T.vvar cont, T.vvars [fName])
| S.App (f, x) ->
let xVal = freshVarHinted nameHint
and fVal = freshVar () in
@ -107,7 +108,15 @@ and light_term varName valExpr valHint next =
subLetVar,
cps_value subLetVal,
light_term varName subLetNext valHint next)
| S.Lam _ | S.App _ | S.Print _ | S.IfZero _ -> assert false
| S.Lam(self, lamVar, lamBody) ->
let lamCont = freshBlockVar () in
T.LetBlo (
varName, T.Lam(
self, [lamVar; lamCont],
cps_term_inner lamBody lamCont None),
next)
| S.App _ | S.Print _ | S.IfZero _ ->
raise (NotLightCPSable valExpr)
)

1
src/tests/redef.exp Normal file
View file

@ -0,0 +1 @@
42

6
src/tests/redef.lambda Normal file
View file

@ -0,0 +1,6 @@
let increase = fun x -> x + 1 in
let increase = fun x ->
let lop = (increase x) in
lop + 1 in
print (increase 40)