Browse Source

adding alpha conversion

master
julia 2 years ago
parent
commit
d93a2169d9
  1. 21
      lambda/lambda.ml
  2. 6
      lambda/main.ml
  3. BIN
      lambda/tab1.png
  4. BIN
      lambda/tab2.png

21
lambda/lambda.ml

@ -29,6 +29,25 @@ let rec beta = function
| App (Lambda (x, t), u) -> subst t [(x, u)] | App (Lambda (x, t), u) -> subst t [(x, u)]
| _ -> failwith "This term is not a redex" | _ -> failwith "This term is not a redex"
(* return i where i is the smallest suffix needed so that "x ^ i" isn't in l *)
let rec var_counter i x l =
if List.mem (x ^ (string_of_int i)) l then var_counter (i+1) x l
else i
(* Alpha Conversion : renames lambda variables duplicates by adding a number to them*)
let alpha_conv t =
let rec aux l = function
| Var x when List.mem x l ->
let i = string_of_int ( (var_counter 0 x l) - 1) in
if i = "-1" then Var x
else Var (x^i)
| Var x -> Var x
| Lambda(x, t) when List.mem x l ->
let i = string_of_int (var_counter 0 x l) in
Lambda (x^i, aux ((x^i)::l) t)
| Lambda(x, t) -> Lambda (x, aux (x::l) t)
| App (t, u) -> App (aux l t, aux l u)
in aux [] t
(* --------------------------------------- (* ---------------------------------------
Exemples Exemples
--------------------------------------- *) --------------------------------------- *)
@ -145,3 +164,5 @@ let rec cbn = function
let rec cbn_eval t = let rec cbn_eval t =
try let t' = cbv t in try let t' = cbv t in
cbn_eval t' with Irreductible -> t cbn_eval t' with Irreductible -> t

6
lambda/main.ml

@ -49,6 +49,12 @@ let _ =
let t = termc Lexer.read lexbuf in let t = termc Lexer.read lexbuf in
print_endline (string_of_term t) print_endline (string_of_term t)
with _ -> print_endline "Syntax error. Please try again.") with _ -> print_endline "Syntax error. Please try again.")
| "alpha"::string_list ->
let lexbuf = Lexing.from_string (String.concat " " string_list) in
(try
let t = termc Lexer.read lexbuf in
print_endline (string_of_term (alpha_conv t))
with _ -> print_endline "Syntax error. Please try again.")
| ["cbn"; "-f"; filename] -> | ["cbn"; "-f"; filename] ->
begin try begin try
let lexbuf = Lexing.from_channel (open_in filename) in let lexbuf = Lexing.from_channel (open_in filename) in

BIN
lambda/tab1.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 249 KiB

BIN
lambda/tab2.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 KiB

Loading…
Cancel
Save