pieuvre/lam.ml

39 lines
1.0 KiB
OCaml
Raw Normal View History

2024-04-09 11:09:33 +02:00
type id = string
2024-04-15 12:07:49 +02:00
type ty_annot = id * Types.ty
2024-04-09 11:09:33 +02:00
2024-04-16 10:07:09 +02:00
type lam =
Fun of ty_annot * lam
| App of lam * lam
2024-04-09 11:09:33 +02:00
| Var of id
2024-04-16 10:07:09 +02:00
| Exf of lam * Types.ty
2024-04-16 10:23:44 +02:00
2024-04-16 11:40:08 +02:00
2024-04-16 10:23:44 +02:00
(** alpha renaming in a deterministic way *)
2024-04-16 10:30:10 +02:00
let alpha_convert (e : lam) : lam =
2024-04-16 10:23:44 +02:00
let cpt_var = ref 0 in (* id cpt for variables *)
let generator (cpt : int ref) : id = (* id generator *)
2024-04-16 10:40:50 +02:00
let v = "__"^(string_of_int !cpt)
2024-04-16 10:23:44 +02:00
in incr cpt; v
2024-04-16 11:40:08 +02:00
in let get_ren_var (var : id) (g : (id * id) list): id = (* returns var if not found *)
match List.assoc_opt var g with
2024-04-16 10:23:44 +02:00
| Some var' -> var'
2024-04-16 10:40:50 +02:00
| None -> var
2024-04-16 11:40:08 +02:00
in let rec alpha_aux (e : lam) (g : (id * id) list): lam = match e with (* actual renaming *)
Fun ((v, t), e) ->
let v' = generator cpt_var in
Fun ((v', t), alpha_aux e ((v, v')::g))
| App (e1, e2) -> App (alpha_aux e1 g, alpha_aux e2 g)
| Var v -> Var (get_ren_var v g)
| Exf (e, t) -> Exf (alpha_aux e g, t)
in alpha_aux e []
2024-04-16 10:23:44 +02:00
(** alpha equivalence *)
2024-04-16 10:30:10 +02:00
let (=~) (e1 : lam) (e2 : lam) : bool =
alpha_convert e1 = alpha_convert e2
2024-04-16 11:40:08 +02:00
(** beta-reduction *)