Compare commits
1 commit
93e20cf208
...
243e02af49
Author | SHA1 | Date | |
---|---|---|---|
243e02af49 |
3 changed files with 12 additions and 28 deletions
29
src/CPS.ml
29
src/CPS.ml
|
@ -3,10 +3,7 @@ module S = Lambda
|
||||||
(* The target calculus. *)
|
(* The target calculus. *)
|
||||||
module T = Tail
|
module T = Tail
|
||||||
|
|
||||||
exception NotValue of S.term
|
exception NotValue (** <- Raised when trying to use a non-value term as such *)
|
||||||
(** ^ Raised when trying to use a non-value term as such *)
|
|
||||||
|
|
||||||
exception NotLightCPSable of S.term
|
|
||||||
|
|
||||||
let freshId =
|
let freshId =
|
||||||
(** Generates a fresh variable name string *)
|
(** Generates a fresh variable name string *)
|
||||||
|
@ -31,7 +28,7 @@ let letCont name varName body next =
|
||||||
|
|
||||||
let rec has_calls (t: S.term): bool = match t with
|
let rec has_calls (t: S.term): bool = match t with
|
||||||
| S.Var _ | S.Lit _ | S.BinOp _ -> false
|
| S.Var _ | S.Lit _ | S.BinOp _ -> false
|
||||||
| S.Lam _ -> false
|
| S.Lam _ -> true (* TODO *)
|
||||||
(* A lambda itself may contain calls, but this call is not evaluated at
|
(* A lambda itself may contain calls, but this call is not evaluated at
|
||||||
* declaration time *)
|
* declaration time *)
|
||||||
| S.App _ -> true
|
| S.App _ -> true
|
||||||
|
@ -52,7 +49,7 @@ let rec cps_value (t: S.term) : T.value = match t with
|
||||||
| S.Var v -> T.VVar v
|
| S.Var v -> T.VVar v
|
||||||
| S.Lit v -> T.VLit v
|
| S.Lit v -> T.VLit v
|
||||||
| S.BinOp (l, op, r) -> T.VBinOp (cps_value l, op, cps_value r)
|
| 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 t)
|
| S.Let _ | S.Lam _ | S.App _ | S.Print _ | S.IfZero _ -> raise NotValue
|
||||||
|
|
||||||
let cps_value_as_term (t: S.term) (cont: T.variable): T.term =
|
let cps_value_as_term (t: S.term) (cont: T.variable): T.term =
|
||||||
T.TailCall(T.vvar cont, [cps_value t])
|
T.TailCall(T.vvar cont, [cps_value t])
|
||||||
|
@ -62,10 +59,12 @@ let rec cps_term_inner (t: S.term) (cont: T.variable) (nameHint: string option)
|
||||||
| S.Var _ -> cps_value_as_term t cont
|
| S.Var _ -> cps_value_as_term t cont
|
||||||
| S.Lit _ -> cps_value_as_term t cont
|
| S.Lit _ -> cps_value_as_term t cont
|
||||||
| S.BinOp _ -> cps_value_as_term t cont
|
| S.BinOp _ -> cps_value_as_term t cont
|
||||||
| S.Lam _ as lambda ->
|
| S.Lam (self, var, term) ->
|
||||||
let fName = freshBlockVarHinted nameHint in
|
let fName = freshBlockVarHinted nameHint
|
||||||
light_term fName lambda None @@
|
and innerCont = freshBlockVar () in
|
||||||
T.TailCall(T.vvar cont, T.vvars [fName])
|
T.LetBlo(fName,
|
||||||
|
T.Lam(self, [var; innerCont], cps_term_inner term innerCont None),
|
||||||
|
T.TailCall(T.vvar cont, T.vvars [fName]))
|
||||||
| S.App (f, x) ->
|
| S.App (f, x) ->
|
||||||
let xVal = freshVarHinted nameHint
|
let xVal = freshVarHinted nameHint
|
||||||
and fVal = freshVar () in
|
and fVal = freshVar () in
|
||||||
|
@ -108,15 +107,7 @@ and light_term varName valExpr valHint next =
|
||||||
subLetVar,
|
subLetVar,
|
||||||
cps_value subLetVal,
|
cps_value subLetVal,
|
||||||
light_term varName subLetNext valHint next)
|
light_term varName subLetNext valHint next)
|
||||||
| S.Lam(self, lamVar, lamBody) ->
|
| S.Lam _ | S.App _ | S.Print _ | S.IfZero _ -> assert false
|
||||||
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 +0,0 @@
|
||||||
42
|
|
|
@ -1,6 +0,0 @@
|
||||||
let increase = fun x -> x + 1 in
|
|
||||||
let increase = fun x ->
|
|
||||||
let lop = (increase x) in
|
|
||||||
lop + 1 in
|
|
||||||
|
|
||||||
print (increase 40)
|
|
Loading…
Reference in a new issue