on rend l'alpha conversion lisible en gardant les noms de variables

This commit is contained in:
Marwan 2024-04-25 04:33:42 +02:00
parent a87e2293b7
commit 544a8bf093
4 changed files with 12 additions and 8 deletions

14
lam.ml
View File

@ -8,12 +8,14 @@ type lam =
| Var of id | Var of id
| Exf of lam * Types.ty | Exf of lam * Types.ty
(** alpha renaming in a deterministic way *) (** alpha renaming in a deterministic way
let alpha_convert (e : lam) : lam = if readable is set to true, original variable's names are used to rename *)
let alpha_convert ?(readable=false) (e : lam) : lam =
let cpt_var = ref 0 in (* id cpt for variables *) let cpt_var = ref 0 in (* id cpt for variables *)
let generator (cpt : int ref) : id = (* id generator *) let generator (v : id) (cpt : int ref) : id = (* id generator *)
let v = "__"^(string_of_int !cpt) let start = if readable then v else "" in
in incr cpt; v let v = start ^ "__"^(string_of_int !cpt) in
incr cpt; v
in let get_ren_var (var : id) (g : (id * id) list): id = (* returns var if not found *) in let get_ren_var (var : id) (g : (id * id) list): id = (* returns var if not found *)
match List.assoc_opt var g with match List.assoc_opt var g with
@ -22,7 +24,7 @@ let alpha_convert (e : lam) : lam =
in let rec alpha_aux (e : lam) (g : (id * id) list): lam = match e with (* actual renaming *) in let rec alpha_aux (e : lam) (g : (id * id) list): lam = match e with (* actual renaming *)
Fun ((v, t), e) -> Fun ((v, t), e) ->
let v' = generator cpt_var in let v' = generator v cpt_var in
Fun ((v', t), alpha_aux e ((v, v')::g)) Fun ((v', t), alpha_aux e ((v, v')::g))
| App (e1, e2) -> App (alpha_aux e1 g, alpha_aux e2 g) | App (e1, e2) -> App (alpha_aux e1 g, alpha_aux e2 g)
| Var v -> Var (get_ren_var v g) | Var v -> Var (get_ren_var v g)

View File

@ -16,7 +16,7 @@ let beta_reduce e =
| None -> () | None -> ()
in print_expr e; in print_expr e;
print_newline (); print_newline ();
let e = Lam.alpha_convert e in let e = Lam.alpha_convert ~readable:true e in
print_expr e; print_expr e;
print_newline (); print_newline ();
aux (Lam.betastep e) aux (Lam.betastep e)

View File

@ -0,0 +1 @@
fun (x : A) => fun (x : A) => x & fun (y : A) => fun (x : A) => x

View File

@ -0,0 +1 @@
fun (x : A) => fun (x : A) => x & fun (x : A) => fun (y : A) => x