2000-02-24 19:33:54 -08:00
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* Use maps for substitutions and sets for free variables *)
|
|
|
|
|
|
|
|
module Subst = Map.Make(struct type t = string let compare = compare end)
|
|
|
|
module Names = Set.Make(struct type t = string let compare = compare end)
|
|
|
|
|
|
|
|
|
|
|
|
(* Variables are common to lambda and expr *)
|
|
|
|
|
2000-06-13 19:33:45 -07:00
|
|
|
type var = [`Var of string]
|
2000-02-24 19:33:54 -08:00
|
|
|
|
2000-06-13 19:33:45 -07:00
|
|
|
let subst_var ~subst : var -> _ =
|
2000-02-24 19:33:54 -08:00
|
|
|
function `Var s as x ->
|
2000-06-13 19:33:45 -07:00
|
|
|
try Subst.find s subst
|
2000-02-24 19:33:54 -08:00
|
|
|
with Not_found -> x
|
|
|
|
|
|
|
|
let free_var : var -> _ = function `Var s -> Names.singleton s
|
|
|
|
|
|
|
|
|
|
|
|
(* The lambda language: free variables, substitutions, and evaluation *)
|
|
|
|
|
2000-06-13 19:33:45 -07:00
|
|
|
type 'a lambda = [`Var of string | `Abs of string * 'a | `App of 'a * 'a]
|
2000-02-24 19:33:54 -08:00
|
|
|
|
2000-06-13 19:33:45 -07:00
|
|
|
let free_lambda ~free_rec : _ lambda -> _ = function
|
2000-02-24 19:33:54 -08:00
|
|
|
#var as x -> free_var x
|
2000-06-13 19:33:45 -07:00
|
|
|
| `Abs (s, t) -> Names.remove s (free_rec t)
|
2000-02-24 19:33:54 -08:00
|
|
|
| `App (t1, t2) -> Names.union (free_rec t1) (free_rec t2)
|
|
|
|
|
2000-06-13 19:33:45 -07:00
|
|
|
let map_lambda ~map_rec : _ lambda -> _ = function
|
2000-02-24 19:33:54 -08:00
|
|
|
#var as x -> x
|
|
|
|
| `Abs (s, t) as l ->
|
|
|
|
let t' = map_rec t in
|
|
|
|
if t == t' then l else `Abs(s, t')
|
|
|
|
| `App (t1, t2) as l ->
|
|
|
|
let t'1 = map_rec t1 and t'2 = map_rec t2 in
|
|
|
|
if t'1 == t1 && t'2 == t2 then l else `App (t'1, t'2)
|
|
|
|
|
|
|
|
let next_id =
|
|
|
|
let current = ref 3 in
|
|
|
|
fun () -> incr current; !current
|
|
|
|
|
2000-06-13 19:33:45 -07:00
|
|
|
let subst_lambda ~subst_rec ~free ~subst : _ lambda -> _ = function
|
|
|
|
#var as x -> subst_var ~subst x
|
2000-02-24 19:33:54 -08:00
|
|
|
| `Abs(s, t) as l ->
|
|
|
|
let used = free t in
|
|
|
|
let used_expr =
|
2000-06-13 19:33:45 -07:00
|
|
|
Subst.fold subst ~init:[]
|
|
|
|
~f:(fun ~key ~data acc ->
|
|
|
|
if Names.mem s used then data::acc else acc) in
|
|
|
|
if List.exists used_expr ~f:(fun t -> Names.mem s (free t)) then
|
2000-02-24 19:33:54 -08:00
|
|
|
let name = s ^ string_of_int (next_id ()) in
|
2000-06-13 19:33:45 -07:00
|
|
|
`Abs(name,
|
|
|
|
subst_rec ~subst:(Subst.add ~key:s ~data:(`Var name) subst) t)
|
2000-02-24 19:33:54 -08:00
|
|
|
else
|
2000-06-13 19:33:45 -07:00
|
|
|
map_lambda ~map_rec:(subst_rec ~subst:(Subst.remove s subst)) l
|
2000-02-24 19:33:54 -08:00
|
|
|
| `App _ as l ->
|
2000-06-13 19:33:45 -07:00
|
|
|
map_lambda ~map_rec:(subst_rec ~subst) l
|
2000-02-24 19:33:54 -08:00
|
|
|
|
2000-06-13 19:33:45 -07:00
|
|
|
let eval_lambda ~eval_rec ~subst l =
|
|
|
|
match map_lambda ~map_rec:eval_rec l with
|
2000-02-24 19:33:54 -08:00
|
|
|
`App(`Abs(s,t1), t2) ->
|
2000-06-13 19:33:45 -07:00
|
|
|
eval_rec (subst ~subst:(Subst.add ~key:s ~data:t2 Subst.empty) t1)
|
2000-02-24 19:33:54 -08:00
|
|
|
| t -> t
|
|
|
|
|
|
|
|
(* Specialized versions to use on lambda *)
|
|
|
|
|
2000-06-13 19:33:45 -07:00
|
|
|
let rec free1 x = free_lambda ~free_rec:free1 x
|
|
|
|
let rec subst1 ~subst = subst_lambda ~subst_rec:subst1 ~free:free1 ~subst
|
|
|
|
let rec eval1 x = eval_lambda ~eval_rec:eval1 ~subst:subst1 x
|
2000-02-24 19:33:54 -08:00
|
|
|
|
|
|
|
|
|
|
|
(* The expr language of arithmetic expressions *)
|
|
|
|
|
|
|
|
type 'a expr =
|
2000-06-13 19:33:45 -07:00
|
|
|
[`Var of string | `Num of int | `Add of 'a * 'a
|
|
|
|
| `Neg of 'a | `Mult of 'a * 'a]
|
2000-02-24 19:33:54 -08:00
|
|
|
|
2000-06-13 19:33:45 -07:00
|
|
|
let free_expr ~free_rec : _ expr -> _ = function
|
2000-02-24 19:33:54 -08:00
|
|
|
#var as x -> free_var x
|
|
|
|
| `Num _ -> Names.empty
|
|
|
|
| `Add(x, y) -> Names.union (free_rec x) (free_rec y)
|
|
|
|
| `Neg x -> free_rec x
|
|
|
|
| `Mult(x, y) -> Names.union (free_rec x) (free_rec y)
|
|
|
|
|
|
|
|
(* Here map_expr helps a lot *)
|
2000-06-13 19:33:45 -07:00
|
|
|
let map_expr ~map_rec : _ expr -> _ = function
|
2000-02-24 19:33:54 -08:00
|
|
|
#var as x -> x
|
|
|
|
| `Num _ as x -> x
|
|
|
|
| `Add(x, y) as e ->
|
|
|
|
let x' = map_rec x and y' = map_rec y in
|
|
|
|
if x == x' && y == y' then e
|
|
|
|
else `Add(x', y')
|
|
|
|
| `Neg x as e ->
|
|
|
|
let x' = map_rec x in
|
|
|
|
if x == x' then e else `Neg x'
|
|
|
|
| `Mult(x, y) as e ->
|
|
|
|
let x' = map_rec x and y' = map_rec y in
|
|
|
|
if x == x' && y == y' then e
|
|
|
|
else `Mult(x', y')
|
|
|
|
|
2000-06-13 19:33:45 -07:00
|
|
|
let subst_expr ~subst_rec ~subst : _ expr -> _ = function
|
|
|
|
#var as x -> subst_var ~subst x
|
|
|
|
| #expr as e -> map_expr ~map_rec:(subst_rec ~subst) e
|
2000-02-24 19:33:54 -08:00
|
|
|
|
2000-06-13 19:33:45 -07:00
|
|
|
let eval_expr ~eval_rec e =
|
|
|
|
match map_expr ~map_rec:eval_rec e with
|
2000-02-24 19:33:54 -08:00
|
|
|
`Add(`Num m, `Num n) -> `Num (m+n)
|
|
|
|
| `Neg(`Num n) -> `Num (-n)
|
|
|
|
| `Mult(`Num m, `Num n) -> `Num (m*n)
|
|
|
|
| #expr as e -> e
|
|
|
|
|
|
|
|
(* Specialized versions *)
|
|
|
|
|
2000-06-13 19:33:45 -07:00
|
|
|
let rec free2 x = free_expr ~free_rec:free2 x
|
|
|
|
let rec subst2 ~subst = subst_expr ~subst_rec:subst2 ~subst
|
|
|
|
let rec eval2 x = eval_expr ~eval_rec:eval2 x
|
2000-02-24 19:33:54 -08:00
|
|
|
|
|
|
|
|
|
|
|
(* The lexpr language, reunion of lambda and expr *)
|
|
|
|
|
|
|
|
type lexpr =
|
2000-06-13 19:33:45 -07:00
|
|
|
[ `Var of string | `Abs of string * lexpr | `App of lexpr * lexpr
|
|
|
|
| `Num of int | `Add of lexpr * lexpr | `Neg of lexpr
|
|
|
|
| `Mult of lexpr * lexpr ]
|
2000-02-24 19:33:54 -08:00
|
|
|
|
|
|
|
let rec free : lexpr -> _ = function
|
2000-06-13 19:33:45 -07:00
|
|
|
#lambda as x -> free_lambda ~free_rec:free x
|
|
|
|
| #expr as x -> free_expr ~free_rec:free x
|
2000-02-24 19:33:54 -08:00
|
|
|
|
2000-06-13 19:33:45 -07:00
|
|
|
let rec subst ~subst:s : lexpr -> _ = function
|
|
|
|
#lambda as x -> subst_lambda ~subst_rec:subst ~subst:s ~free x
|
|
|
|
| #expr as x -> subst_expr ~subst_rec:subst ~subst:s x
|
2000-02-24 19:33:54 -08:00
|
|
|
|
|
|
|
let rec eval : lexpr -> _ = function
|
2000-06-13 19:33:45 -07:00
|
|
|
#lambda as x -> eval_lambda ~eval_rec:eval ~subst x
|
|
|
|
| #expr as x -> eval_expr ~eval_rec:eval x
|
2000-02-24 19:33:54 -08:00
|
|
|
|
|
|
|
(* A few examples:
|
|
|
|
eval1 (`App(`Abs("x",`Var"x"), `Var"y"));;
|
|
|
|
eval2 (`Add(`Mult(`Num 3,`Neg(`Num 2)), `Var"x"));;
|
|
|
|
eval (`Add(`App(`Abs("x",`Mult(`Var"x",`Var"x")),`Num 2), `Num 5));;
|
|
|
|
*)
|