Commit d72727c8 authored by Taddeus Kroes's avatar Taddeus Kroes

funclang series5: Added test file for ass12.

parent d2387098
...@@ -13,6 +13,7 @@ type expr = Num of int (* integer constant *) ...@@ -13,6 +13,7 @@ type expr = Num of int (* integer constant *)
| Let of string * expr * expr (* variable binding *) | Let of string * expr * expr (* variable binding *)
| LetRec of string * expr * expr (* recursive variable binding *) | LetRec of string * expr * expr (* recursive variable binding *)
(* Convert a given expression to a string representation *)
let rec expr2string = let rec expr2string =
let monop2string = function let monop2string = function
Neg -> "-" Neg -> "-"
...@@ -50,6 +51,7 @@ let rec expr2string = ...@@ -50,6 +51,7 @@ let rec expr2string =
| LetRec (var, e1, e2) -> "let rec " ^ var ^ " = " ^ expr2string(e1) | LetRec (var, e1, e2) -> "let rec " ^ var ^ " = " ^ expr2string(e1)
^ " in " ^ expr2string(e2) ^ " in " ^ expr2string(e2)
(* Find all free variables within an expression *)
let freevars e = let freevars e =
let rec free_vars bound_vars = function let rec free_vars bound_vars = function
Num _ | Bool _ -> [] Num _ | Bool _ -> []
...@@ -69,9 +71,33 @@ let freevars e = ...@@ -69,9 +71,33 @@ let freevars e =
in in
free_vars [] e free_vars [] e
(*let rec subs e var replacement = let cnt = ref 0
let free = freevars e in let getNewId id = (cnt := !cnt + 1; "_" ^ id ^ "_" ^ string_of_int (! cnt))
match e with
| Num _ | Bool _ -> e (* Substitue all occurences of y by a in exp *)
| Var v when List.mem v bound_vars -> [] let rec subs exp y a =
| Var v -> [v]*) match exp with
| Var x when x = y -> a
| Num _ | Bool _ | Var _ -> exp
| MonopAp (op, e) -> MonopAp (op, subs e y a)
| BinopAp (op, e1, e2) -> BinopAp (op, subs e1 y a, subs e2 y a)
| Fun (arg, _) when arg = y -> exp
| Fun (x, e) ->
let free_a = freevars a in
if List.mem x free_a then
(* Replace x with a new variable w to avoid free variable
* corruption in a *)
(*let all_free_vars = free_a @ (freevars e) in
let rec copy_x =
let new_x = getNewId x in
if List.mem new_x all_free_vars then copy_x else new_x
in*)
let w = getNewId x in
Fun (w, subs (subs e x (Var w)) y a)
else
Fun (x, subs e y a)
| FunAp (e1, e2) -> FunAp (subs e1 y a, subs e2 y a)
| Cond (cond, e1, e2) -> Cond (subs cond y a, subs e1 y a, subs e2 y a)
| Let (x, e1, e2) when x = y -> Let (x, e1, e2)
| Let (x, e1, e2) -> Let (x, e1, e2)
| LetRec (x, e1, e2) -> LetRec (x, e1, e2)
...@@ -20,27 +20,32 @@ let show_freevars e = ...@@ -20,27 +20,32 @@ let show_freevars e =
;; ;;
(* Write the expressions of assignment series 1 in our data types *) (* Write the expressions of assignment series 1 in our data types *)
let u = Var "u" in let l arg body = Fun (arg, body);;
let c = Var "c" in let app func arg = FunAp (func, arg);;
let bcc = l "b" (l "c" c) in
let a = Var "a" in let ass1a =
let aaa = l "a" (app a a) in let c = Var "c" in
let aaa_aaa = app aaa aaa in let bcc = l "b" (l "c" c) in
let v = Var "v" in let a = Var "a" in
let uvw = l "u" (app (l "v" (l "w" v)) (l "a" a)) in let aaa = l "a" (app a a) in
let 1a = (app (app bcc aaa_aaa) uvw);; let aaa_aaa = app aaa aaa in
let v = Var "v" in
let v = Var "v" in let uvw = l "u" (app (l "v" (l "w" v)) (l "a" a)) in
let w = Var "w" in (app (app bcc aaa_aaa) uvw);;
let uvw = l "u" (l "v" (l "w" (app (app u v) w))) in
let x = Var "x" in let ass1b =
let y = Var "y" in let u = Var "u" in
let z = Var "z" in let v = Var "v" in
let xyz = l "x" (l "y" (l "z" (app x (app y z)))) in let w = Var "w" in
let a = Var "a" in let uvw = l "u" (l "v" (l "w" (app (app u v) w))) in
let b = Var "b" in let x = Var "x" in
let c = Var "c" in let y = Var "y" in
let 1b = (app (app (app (app uvw xyz) a) b) c);; let z = Var "z" in
let xyz = l "x" (l "y" (l "z" (app x (app y z)))) in
let a = Var "a" in
let b = Var "b" in
let c = Var "c" in
(app (app (app (app uvw xyz) a) b) c);;
(* [a] *) (* [a] *)
show_freevars (Fun ("b", (BinopAp (Add, Var "a", Num 3))));; show_freevars (Fun ("b", (BinopAp (Add, Var "a", Num 3))));;
...@@ -52,10 +57,10 @@ let l arg body = Fun (arg, body);; ...@@ -52,10 +57,10 @@ let l arg body = Fun (arg, body);;
let app f e = FunAp (f, e);; let app f e = FunAp (f, e);;
(* [] *) (* [] *)
show_freevars 1a;; show_freevars ass1a;;
(* [a, b, c] *) (* [a, b, c] *)
show_freevars 1b;; show_freevars ass1b;;
(* fun u -> fun w -> fun a -> a *) (* fun u -> fun w -> fun a -> a *)
(*print_endline (expr2string (subs 1b));;*) print_endline (expr2string (subs ass1b));;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment