LIB = unix.cmxa str.cmxa
UTILS = util.cmx console.cmx listx.cmx pure.cmx num.cmx parser.cmx parser_andrea.cmx
-all: a.out test4.out andrea.out
+all: a.out test4.out simple.out
run: test4.out
bash run
test4.out: $(UTILS) lambda4.cmx test.ml
$(OCAMLC) -o test4.out $(LIB) $^
-andrea.out: $(UTILS) andrea.ml
- $(OCAMLC) -o andrea.out $(LIB) $(UTILS) andrea.ml
+simple.out: $(UTILS) simple.ml
+ $(OCAMLC) -o simple.out $(LIB) $(UTILS) simple.ml
%.cmi: %.mli
$(OCAMLC) -c $<
+++ /dev/null
-let (++) f g x = f (g x);;\r
-let id x = x;;\r
-let rec fold_nat f x n = if n = 0 then x else f (fold_nat f x (n-1)) n ;;\r
-\r
-let print_hline = Console.print_hline;;\r
-\r
-open Pure\r
-\r
-type var = int;;\r
-type t =\r
- | V of var\r
- | A of t * t\r
- | L of t\r
- | B (* bottom *)\r
- | C of int\r
-;;\r
-\r
-let delta = L(A(V 0, V 0));;\r
-\r
-let eta_eq =\r
- let rec aux l1 l2 t1 t2 = match t1, t2 with\r
- | L t1, L t2 -> aux l1 l2 t1 t2\r
- | L t1, t2 -> aux l1 (l2+1) t1 t2\r
- | t1, L t2 -> aux (l1+1) l2 t1 t2\r
- | V a, V b -> a + l1 = b + l2\r
- | C a, C b -> a = b\r
- | A(t1,t2), A(u1,u2) -> aux l1 l2 t1 u1 && aux l1 l2 t2 u2\r
- | _, _ -> false\r
- in aux 0 0\r
-;;\r
-\r
-(* does NOT lift t *)\r
-let mk_lams = fold_nat (fun x _ -> L x) ;;\r
-\r
-let string_of_t =\r
- let string_of_bvar =\r
- let bound_vars = ["x"; "y"; "z"; "w"; "q"] in\r
- let bvarsno = List.length bound_vars in\r
- fun nn -> if nn < bvarsno then List.nth bound_vars nn else "x" ^ (string_of_int (nn - bvarsno + 1)) in\r
- let rec string_of_term_w_pars level = function\r
- | V v -> if v >= level then "`" ^ string_of_int (v-level) else\r
- string_of_bvar (level - v-1)\r
- | C n -> "c" ^ string_of_int n\r
- | A _\r
- | L _ as t -> "(" ^ string_of_term_no_pars level t ^ ")"\r
- | B -> "BOT"\r
- and string_of_term_no_pars_app level = function\r
- | A(t1,t2) -> string_of_term_no_pars_app level t1 ^ " " ^ string_of_term_w_pars level t2\r
- | _ as t -> string_of_term_w_pars level t\r
- and string_of_term_no_pars level = function\r
- | L t -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t\r
- | _ as t -> string_of_term_no_pars_app level t\r
- in string_of_term_no_pars 0\r
-;;\r
-\r
-type problem = {\r
- orig_freshno: int\r
- ; freshno : int\r
- ; div : t\r
- ; conv : t\r
- ; sigma : (var * t) list (* substitutions *)\r
- ; stepped : var list\r
- ; phase : [`One | `Two] (* :'( *)\r
-}\r
-\r
-let string_of_problem p =\r
- let lines = [\r
- "[stepped] " ^ String.concat " " (List.map string_of_int p.stepped);\r
- "[DV] " ^ string_of_t p.div;\r
- "[CV] " ^ string_of_t p.conv;\r
- ] in\r
- String.concat "\n" lines\r
-;;\r
-\r
-exception Done of (var * t) list (* substitution *);;\r
-exception Fail of int * string;;\r
-\r
-let problem_fail p reason =\r
- print_endline "!!!!!!!!!!!!!!! FAIL !!!!!!!!!!!!!!!";\r
- print_endline (string_of_problem p);\r
- raise (Fail (-1, reason))\r
-;;\r
-\r
-let freshvar ({freshno} as p) =\r
- {p with freshno=freshno+1}, freshno+1\r
-;;\r
-\r
-let rec is_inert =\r
- function\r
- | A(t,_) -> is_inert t\r
- | V _ -> true\r
- | C _\r
- | L _ | B -> false\r
-;;\r
-\r
-let is_var = function V _ -> true | _ -> false;;\r
-let is_lambda = function L _ -> true | _ -> false;;\r
-\r
-let rec no_leading_lambdas = function\r
- | L t -> 1 + no_leading_lambdas t\r
- | _ -> 0\r
-;;\r
-\r
-let rec get_inert = function\r
- | V n -> (n,0)\r
- | A(t, _) -> let hd,args = get_inert t in hd,args+1\r
- | _ -> assert false\r
-;;\r
-\r
-let rec subst level delift sub =\r
- function\r
- | V v -> if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v)\r
- | L t -> let t = subst (level + 1) delift sub t in if t = B then B else L t\r
- | A (t1,t2) ->\r
- let t1 = subst level delift sub t1 in\r
- let t2 = subst level delift sub t2 in\r
- mk_app t1 t2\r
- | C _ as t -> t\r
- | B -> B\r
-and mk_app t1 t2 = if t2 = B || (t1 = delta && t2 = delta) then B\r
- else match t1 with\r
- | C _ as t -> t\r
- | B -> B\r
- | L t1 -> subst 0 true (0, t2) t1\r
- | _ -> A (t1, t2)\r
-and lift n =\r
- let rec aux lev =\r
- function\r
- | V m -> V (if m >= lev then m + n else m)\r
- | L t -> L (aux (lev+1) t)\r
- | A (t1, t2) -> A (aux lev t1, aux lev t2)\r
- | C _ as t -> t\r
- | B -> B\r
- in aux 0\r
-;;\r
-let subst = subst 0 false;;\r
-\r
-let subst_in_problem (sub: var * t) (p: problem) =\r
-print_endline ("-- SUBST " ^ string_of_t (V (fst sub)) ^ " |-> " ^ string_of_t (snd sub));\r
- {p with\r
- div=subst sub p.div;\r
- conv=subst sub p.conv;\r
- stepped=(fst sub)::p.stepped;\r
- sigma=sub::p.sigma}\r
-;;\r
-\r
-let get_subterm_with_head_and_args hd_var n_args =\r
- let rec aux lev = function\r
- | C _\r
- | V _ | B -> None\r
- | L t -> aux (lev+1) t\r
- | A(t1,t2) as t ->\r
- let hd_var', n_args' = get_inert t1 in\r
- if hd_var' = hd_var + lev && n_args <= 1 + n_args'\r
- then Some (lift ~-lev t)\r
- else match aux lev t2 with\r
- | None -> aux lev t1\r
- | Some _ as res -> res\r
- in aux 0\r
-;;\r
-\r
-let rec purify = function\r
- | L t -> Pure.L (purify t)\r
- | A (t1,t2) -> Pure.A (purify t1, purify t2)\r
- | V n -> Pure.V n\r
- | C _ -> Pure.V max_int (* FIXME *)\r
- | B -> Pure.B\r
-;;\r
-\r
-let check p sigma =\r
- print_endline "Checking...";\r
- let div = purify p.div in\r
- let conv = purify p.conv in\r
- let sigma = List.map (fun (v,t) -> v, purify t) sigma in\r
- let freshno = List.fold_right (fun (x,_) -> max x) sigma 0 in\r
- let env = Pure.env_of_sigma freshno sigma in\r
- assert (Pure.diverged (Pure.mwhd (env,div,[])));\r
- assert (not (Pure.diverged (Pure.mwhd (env,conv,[]))));\r
- ()\r
-;;\r
-\r
-let sanity p =\r
- print_endline (string_of_problem p); (* non cancellare *)\r
- if p.conv = B then problem_fail p "p.conv diverged";\r
- if p.div = B then raise (Done p.sigma);\r
- if p.phase = `Two && p.div = delta then raise (Done p.sigma);\r
- if not (is_inert p.div) then problem_fail p "p.div converged"\r
-;;\r
-\r
-(* drops the arguments of t after the n-th *)\r
-let inert_cut_at n t =\r
- let rec aux t =\r
- match t with\r
- | V _ as t -> 0, t\r
- | A(t1,_) as t ->\r
- let k', t' = aux t1 in\r
- if k' = n then n, t'\r
- else k'+1, t\r
- | _ -> assert false\r
- in snd (aux t)\r
-;;\r
-\r
-let find_eta_difference p t n_args =\r
- let t = inert_cut_at n_args t in\r
- let rec aux t u k = match t, u with\r
- | V _, V _ -> assert false (* div subterm of conv *)\r
- | A(t1,t2), A(u1,u2) ->\r
- if not (eta_eq t2 u2) then (print_endline((string_of_t t2) ^ " <> " ^ (string_of_t u2)); k)\r
- else aux t1 u1 (k-1)\r
- | _, _ -> assert false\r
- in aux p.div t n_args\r
-;;\r
-\r
-let compute_max_lambdas_at hd_var j =\r
- let rec aux hd = function\r
- | A(t1,t2) ->\r
- (if get_inert t1 = (hd, j)\r
- then max ( (*FIXME*)\r
- if is_inert t2 && let hd', j' = get_inert t2 in hd' = hd\r
- then let hd', j' = get_inert t2 in j - j'\r
- else no_leading_lambdas t2)\r
- else id) (max (aux hd t1) (aux hd t2))\r
- | L t -> aux (hd+1) t\r
- | V _ -> 0\r
- | _ -> assert false\r
- in aux hd_var\r
-;;\r
-\r
-let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);;\r
-\r
-(* eat the arguments of the divergent and explode.\r
- It does NOT perform any check, may fail if done unsafely *)\r
-let eat p =\r
-print_cmd "EAT" "";\r
- let var, k = get_inert p.div in\r
- let phase = p.phase in\r
- let p, t =\r
- match phase with\r
- | `One ->\r
- let n = 1 + max\r
- (compute_max_lambdas_at var k p.div)\r
- (compute_max_lambdas_at var k p.conv) in\r
- (* apply fresh vars *)\r
- let p, t = fold_nat (fun (p, t) _ ->\r
- let p, v = freshvar p in\r
- p, A(t, V (v + k))\r
- ) (p, V 0) n in\r
- let p = {p with phase=`Two} in p, A(t, delta)\r
- | `Two -> p, delta in\r
- let subst = var, mk_lams t k in\r
- let p = subst_in_problem subst p in\r
- let p = if phase = `One then {p with div = (match p.div with A(t,_) -> t | _ -> assert false)} else p in\r
- sanity p; p\r
-;;\r
-\r
-(* step on the head of div, on the k-th argument, with n fresh vars *)\r
-let step k n p =\r
- let var, _ = get_inert p.div in\r
-print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")");\r
- let p, t = (* apply fresh vars *)\r
- fold_nat (fun (p, t) _ ->\r
- let p, v = freshvar p in\r
- p, A(t, V (v + k + 1))\r
- ) (p, V 0) n in\r
- let t = (* apply unused bound variables V_{k-1}..V_1 *)\r
- fold_nat (fun t m -> A(t, V (k-m+1))) t k in\r
- let t = mk_lams t (k+1) in (* make leading lambdas *)\r
- let subst = var, t in\r
- let p = subst_in_problem subst p in\r
- sanity p; p\r
-;;\r
-\r
-let parse strs =\r
- let rec aux level = function\r
- | Parser_andrea.Lam t -> L (aux (level + 1) t)\r
- | Parser_andrea.App (t1, t2) ->\r
- if level = 0 then mk_app (aux level t1) (aux level t2)\r
- else A(aux level t1, aux level t2)\r
- | Parser_andrea.Var v -> V v in\r
- let (tms, free) = Parser_andrea.parse_many strs in\r
- (List.map (aux 0) tms, free)\r
-;;\r
-\r
-let problem_of div conv =\r
- print_hline ();\r
- let [@warning "-8"] [div; conv], var_names = parse ([div; conv]) in\r
- let varno = List.length var_names in\r
- let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; stepped=[]; phase=`One} in\r
- (* initial sanity check *)\r
- sanity p; p\r
-;;\r
-\r
-let exec div conv cmds =\r
- let p = problem_of div conv in\r
- try\r
- problem_fail (List.fold_left (|>) p cmds) "Problem not completed"\r
- with\r
- | Done _ -> ()\r
-;;\r
-\r
-let rec auto p =\r
- let hd_var, n_args = get_inert p.div in\r
- match get_subterm_with_head_and_args hd_var n_args p.conv with\r
- | None ->\r
- (try\r
- let phase = p.phase in\r
- let p = eat p in\r
- if phase = `Two\r
- then problem_fail p "Auto.2 did not complete the problem"\r
- else auto p\r
- with Done sigma -> sigma)\r
- | Some t ->\r
- let j = find_eta_difference p t n_args - 1 in\r
- let k = 1 + max\r
- (compute_max_lambdas_at hd_var j p.div)\r
- (compute_max_lambdas_at hd_var j p.conv) in\r
- let p = step j k p in\r
- auto p\r
-;;\r
-\r
-let interactive div conv cmds =\r
- let p = problem_of div conv in\r
- try (\r
- let p = List.fold_left (|>) p cmds in\r
- let rec f p cmds =\r
- let nth spl n = int_of_string (List.nth spl n) in\r
- let read_cmd () =\r
- let s = read_line () in\r
- let spl = Str.split (Str.regexp " +") s in\r
- s, let uno = List.hd spl in\r
- try if uno = "eat" then eat\r
- else if uno = "step" then step (nth spl 1) (nth spl 2)\r
- else failwith "Wrong input."\r
- with Failure s -> print_endline s; (fun x -> x) in\r
- let str, cmd = read_cmd () in\r
- let cmds = (" " ^ str ^ ";")::cmds in\r
- try\r
- let p = cmd p in f p cmds\r
- with\r
- | Done _ -> print_endline "Done! Commands history: "; List.iter print_endline (List.rev cmds)\r
- in f p []\r
- ) with Done _ -> ()\r
-;;\r
-\r
-let rec conv_join = function\r
- | [] -> "@"\r
- | x::xs -> conv_join xs ^ " ("^ x ^")"\r
-;;\r
-\r
-let auto' a b =\r
- let p = problem_of a (conv_join b) in\r
- let sigma = auto p in\r
- check p sigma\r
-;;\r
-\r
-(* Example usage of exec, interactive:\r
-\r
-exec\r
- "x x"\r
- (conv_join["x y"; "y y"; "y x"])\r
- [ step 0 1; eat ]\r
-;;\r
-\r
-interactive "x y"\r
- "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat]\r
-;;\r
-\r
-*)\r
-\r
-auto' "x x" ["x y"; "y y"; "y x"] ;;\r
-auto' "x y" ["x (_. x)"; "y z"; "y x"] ;;\r
-auto' "a (x. x b) (x. x c)" ["a (x. b b) @"; "a @ c"; "a (x. x x) a"; "a (a a a) (a c c)"] ;;\r
-\r
-auto' "x (y. x y y)" ["x (y. x y x)"] ;;\r
-\r
-auto' "x a a a a" [\r
- "x b a a a";\r
- "x a b a a";\r
- "x a a b a";\r
- "x a a a b";\r
-] ;;\r
-\r
-(* Controesempio ad usare un conto dei lambda che non considere le permutazioni *)\r
-auto' "x a a a a (x (x. x x) @ @ (_._.x. x x) x) b b b" [\r
- "x a a a a (_. a) b b b";\r
- "x a a a a (_. _. _. _. x. y. x y)";\r
-] ;;\r
-\r
-\r
-print_hline();\r
-print_endline "ALL DONE. "\r
--- /dev/null
+let (++) f g x = f (g x);;\r
+let id x = x;;\r
+let rec fold_nat f x n = if n = 0 then x else f (fold_nat f x (n-1)) n ;;\r
+\r
+let print_hline = Console.print_hline;;\r
+\r
+open Pure\r
+\r
+type var = int;;\r
+type t =\r
+ | V of var\r
+ | A of t * t\r
+ | L of t\r
+ | B (* bottom *)\r
+ | C of int\r
+;;\r
+\r
+let delta = L(A(V 0, V 0));;\r
+\r
+let eta_eq =\r
+ let rec aux l1 l2 t1 t2 = match t1, t2 with\r
+ | L t1, L t2 -> aux l1 l2 t1 t2\r
+ | L t1, t2 -> aux l1 (l2+1) t1 t2\r
+ | t1, L t2 -> aux (l1+1) l2 t1 t2\r
+ | V a, V b -> a + l1 = b + l2\r
+ | C a, C b -> a = b\r
+ | A(t1,t2), A(u1,u2) -> aux l1 l2 t1 u1 && aux l1 l2 t2 u2\r
+ | _, _ -> false\r
+ in aux 0 0\r
+;;\r
+\r
+(* does NOT lift t *)\r
+let mk_lams = fold_nat (fun x _ -> L x) ;;\r
+\r
+let string_of_t =\r
+ let string_of_bvar =\r
+ let bound_vars = ["x"; "y"; "z"; "w"; "q"] in\r
+ let bvarsno = List.length bound_vars in\r
+ fun nn -> if nn < bvarsno then List.nth bound_vars nn else "x" ^ (string_of_int (nn - bvarsno + 1)) in\r
+ let rec string_of_term_w_pars level = function\r
+ | V v -> if v >= level then "`" ^ string_of_int (v-level) else\r
+ string_of_bvar (level - v-1)\r
+ | C n -> "c" ^ string_of_int n\r
+ | A _\r
+ | L _ as t -> "(" ^ string_of_term_no_pars level t ^ ")"\r
+ | B -> "BOT"\r
+ and string_of_term_no_pars_app level = function\r
+ | A(t1,t2) -> string_of_term_no_pars_app level t1 ^ " " ^ string_of_term_w_pars level t2\r
+ | _ as t -> string_of_term_w_pars level t\r
+ and string_of_term_no_pars level = function\r
+ | L t -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t\r
+ | _ as t -> string_of_term_no_pars_app level t\r
+ in string_of_term_no_pars 0\r
+;;\r
+\r
+type problem = {\r
+ orig_freshno: int\r
+ ; freshno : int\r
+ ; div : t\r
+ ; conv : t\r
+ ; sigma : (var * t) list (* substitutions *)\r
+ ; stepped : var list\r
+ ; phase : [`One | `Two] (* :'( *)\r
+}\r
+\r
+let string_of_problem p =\r
+ let lines = [\r
+ "[stepped] " ^ String.concat " " (List.map string_of_int p.stepped);\r
+ "[DV] " ^ string_of_t p.div;\r
+ "[CV] " ^ string_of_t p.conv;\r
+ ] in\r
+ String.concat "\n" lines\r
+;;\r
+\r
+exception Done of (var * t) list (* substitution *);;\r
+exception Fail of int * string;;\r
+\r
+let problem_fail p reason =\r
+ print_endline "!!!!!!!!!!!!!!! FAIL !!!!!!!!!!!!!!!";\r
+ print_endline (string_of_problem p);\r
+ raise (Fail (-1, reason))\r
+;;\r
+\r
+let freshvar ({freshno} as p) =\r
+ {p with freshno=freshno+1}, freshno+1\r
+;;\r
+\r
+let rec is_inert =\r
+ function\r
+ | A(t,_) -> is_inert t\r
+ | V _ -> true\r
+ | C _\r
+ | L _ | B -> false\r
+;;\r
+\r
+let is_var = function V _ -> true | _ -> false;;\r
+let is_lambda = function L _ -> true | _ -> false;;\r
+\r
+let rec no_leading_lambdas = function\r
+ | L t -> 1 + no_leading_lambdas t\r
+ | _ -> 0\r
+;;\r
+\r
+let rec get_inert = function\r
+ | V n -> (n,0)\r
+ | A(t, _) -> let hd,args = get_inert t in hd,args+1\r
+ | _ -> assert false\r
+;;\r
+\r
+let rec subst level delift sub =\r
+ function\r
+ | V v -> if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v)\r
+ | L t -> let t = subst (level + 1) delift sub t in if t = B then B else L t\r
+ | A (t1,t2) ->\r
+ let t1 = subst level delift sub t1 in\r
+ let t2 = subst level delift sub t2 in\r
+ mk_app t1 t2\r
+ | C _ as t -> t\r
+ | B -> B\r
+and mk_app t1 t2 = if t2 = B || (t1 = delta && t2 = delta) then B\r
+ else match t1 with\r
+ | C _ as t -> t\r
+ | B -> B\r
+ | L t1 -> subst 0 true (0, t2) t1\r
+ | _ -> A (t1, t2)\r
+and lift n =\r
+ let rec aux lev =\r
+ function\r
+ | V m -> V (if m >= lev then m + n else m)\r
+ | L t -> L (aux (lev+1) t)\r
+ | A (t1, t2) -> A (aux lev t1, aux lev t2)\r
+ | C _ as t -> t\r
+ | B -> B\r
+ in aux 0\r
+;;\r
+let subst = subst 0 false;;\r
+\r
+let subst_in_problem (sub: var * t) (p: problem) =\r
+print_endline ("-- SUBST " ^ string_of_t (V (fst sub)) ^ " |-> " ^ string_of_t (snd sub));\r
+ {p with\r
+ div=subst sub p.div;\r
+ conv=subst sub p.conv;\r
+ stepped=(fst sub)::p.stepped;\r
+ sigma=sub::p.sigma}\r
+;;\r
+\r
+let get_subterm_with_head_and_args hd_var n_args =\r
+ let rec aux lev = function\r
+ | C _\r
+ | V _ | B -> None\r
+ | L t -> aux (lev+1) t\r
+ | A(t1,t2) as t ->\r
+ let hd_var', n_args' = get_inert t1 in\r
+ if hd_var' = hd_var + lev && n_args <= 1 + n_args'\r
+ then Some (lift ~-lev t)\r
+ else match aux lev t2 with\r
+ | None -> aux lev t1\r
+ | Some _ as res -> res\r
+ in aux 0\r
+;;\r
+\r
+let rec purify = function\r
+ | L t -> Pure.L (purify t)\r
+ | A (t1,t2) -> Pure.A (purify t1, purify t2)\r
+ | V n -> Pure.V n\r
+ | C _ -> Pure.V max_int (* FIXME *)\r
+ | B -> Pure.B\r
+;;\r
+\r
+let check p sigma =\r
+ print_endline "Checking...";\r
+ let div = purify p.div in\r
+ let conv = purify p.conv in\r
+ let sigma = List.map (fun (v,t) -> v, purify t) sigma in\r
+ let freshno = List.fold_right (fun (x,_) -> max x) sigma 0 in\r
+ let env = Pure.env_of_sigma freshno sigma in\r
+ assert (Pure.diverged (Pure.mwhd (env,div,[])));\r
+ assert (not (Pure.diverged (Pure.mwhd (env,conv,[]))));\r
+ ()\r
+;;\r
+\r
+let sanity p =\r
+ print_endline (string_of_problem p); (* non cancellare *)\r
+ if p.conv = B then problem_fail p "p.conv diverged";\r
+ if p.div = B then raise (Done p.sigma);\r
+ if p.phase = `Two && p.div = delta then raise (Done p.sigma);\r
+ if not (is_inert p.div) then problem_fail p "p.div converged"\r
+;;\r
+\r
+(* drops the arguments of t after the n-th *)\r
+let inert_cut_at n t =\r
+ let rec aux t =\r
+ match t with\r
+ | V _ as t -> 0, t\r
+ | A(t1,_) as t ->\r
+ let k', t' = aux t1 in\r
+ if k' = n then n, t'\r
+ else k'+1, t\r
+ | _ -> assert false\r
+ in snd (aux t)\r
+;;\r
+\r
+let find_eta_difference p t n_args =\r
+ let t = inert_cut_at n_args t in\r
+ let rec aux t u k = match t, u with\r
+ | V _, V _ -> assert false (* div subterm of conv *)\r
+ | A(t1,t2), A(u1,u2) ->\r
+ if not (eta_eq t2 u2) then (print_endline((string_of_t t2) ^ " <> " ^ (string_of_t u2)); k)\r
+ else aux t1 u1 (k-1)\r
+ | _, _ -> assert false\r
+ in aux p.div t n_args\r
+;;\r
+\r
+let compute_max_lambdas_at hd_var j =\r
+ let rec aux hd = function\r
+ | A(t1,t2) ->\r
+ (if get_inert t1 = (hd, j)\r
+ then max ( (*FIXME*)\r
+ if is_inert t2 && let hd', j' = get_inert t2 in hd' = hd\r
+ then let hd', j' = get_inert t2 in j - j'\r
+ else no_leading_lambdas t2)\r
+ else id) (max (aux hd t1) (aux hd t2))\r
+ | L t -> aux (hd+1) t\r
+ | V _ -> 0\r
+ | _ -> assert false\r
+ in aux hd_var\r
+;;\r
+\r
+let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);;\r
+\r
+(* eat the arguments of the divergent and explode.\r
+ It does NOT perform any check, may fail if done unsafely *)\r
+let eat p =\r
+print_cmd "EAT" "";\r
+ let var, k = get_inert p.div in\r
+ let phase = p.phase in\r
+ let p, t =\r
+ match phase with\r
+ | `One ->\r
+ let n = 1 + max\r
+ (compute_max_lambdas_at var k p.div)\r
+ (compute_max_lambdas_at var k p.conv) in\r
+ (* apply fresh vars *)\r
+ let p, t = fold_nat (fun (p, t) _ ->\r
+ let p, v = freshvar p in\r
+ p, A(t, V (v + k))\r
+ ) (p, V 0) n in\r
+ let p = {p with phase=`Two} in p, A(t, delta)\r
+ | `Two -> p, delta in\r
+ let subst = var, mk_lams t k in\r
+ let p = subst_in_problem subst p in\r
+ let p = if phase = `One then {p with div = (match p.div with A(t,_) -> t | _ -> assert false)} else p in\r
+ sanity p; p\r
+;;\r
+\r
+(* step on the head of div, on the k-th argument, with n fresh vars *)\r
+let step k n p =\r
+ let var, _ = get_inert p.div in\r
+print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")");\r
+ let p, t = (* apply fresh vars *)\r
+ fold_nat (fun (p, t) _ ->\r
+ let p, v = freshvar p in\r
+ p, A(t, V (v + k + 1))\r
+ ) (p, V 0) n in\r
+ let t = (* apply unused bound variables V_{k-1}..V_1 *)\r
+ fold_nat (fun t m -> A(t, V (k-m+1))) t k in\r
+ let t = mk_lams t (k+1) in (* make leading lambdas *)\r
+ let subst = var, t in\r
+ let p = subst_in_problem subst p in\r
+ sanity p; p\r
+;;\r
+\r
+let parse strs =\r
+ let rec aux level = function\r
+ | Parser_andrea.Lam t -> L (aux (level + 1) t)\r
+ | Parser_andrea.App (t1, t2) ->\r
+ if level = 0 then mk_app (aux level t1) (aux level t2)\r
+ else A(aux level t1, aux level t2)\r
+ | Parser_andrea.Var v -> V v in\r
+ let (tms, free) = Parser_andrea.parse_many strs in\r
+ (List.map (aux 0) tms, free)\r
+;;\r
+\r
+let problem_of div conv =\r
+ print_hline ();\r
+ let [@warning "-8"] [div; conv], var_names = parse ([div; conv]) in\r
+ let varno = List.length var_names in\r
+ let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; stepped=[]; phase=`One} in\r
+ (* initial sanity check *)\r
+ sanity p; p\r
+;;\r
+\r
+let exec div conv cmds =\r
+ let p = problem_of div conv in\r
+ try\r
+ problem_fail (List.fold_left (|>) p cmds) "Problem not completed"\r
+ with\r
+ | Done _ -> ()\r
+;;\r
+\r
+let rec auto p =\r
+ let hd_var, n_args = get_inert p.div in\r
+ match get_subterm_with_head_and_args hd_var n_args p.conv with\r
+ | None ->\r
+ (try\r
+ let phase = p.phase in\r
+ let p = eat p in\r
+ if phase = `Two\r
+ then problem_fail p "Auto.2 did not complete the problem"\r
+ else auto p\r
+ with Done sigma -> sigma)\r
+ | Some t ->\r
+ let j = find_eta_difference p t n_args - 1 in\r
+ let k = 1 + max\r
+ (compute_max_lambdas_at hd_var j p.div)\r
+ (compute_max_lambdas_at hd_var j p.conv) in\r
+ let p = step j k p in\r
+ auto p\r
+;;\r
+\r
+let interactive div conv cmds =\r
+ let p = problem_of div conv in\r
+ try (\r
+ let p = List.fold_left (|>) p cmds in\r
+ let rec f p cmds =\r
+ let nth spl n = int_of_string (List.nth spl n) in\r
+ let read_cmd () =\r
+ let s = read_line () in\r
+ let spl = Str.split (Str.regexp " +") s in\r
+ s, let uno = List.hd spl in\r
+ try if uno = "eat" then eat\r
+ else if uno = "step" then step (nth spl 1) (nth spl 2)\r
+ else failwith "Wrong input."\r
+ with Failure s -> print_endline s; (fun x -> x) in\r
+ let str, cmd = read_cmd () in\r
+ let cmds = (" " ^ str ^ ";")::cmds in\r
+ try\r
+ let p = cmd p in f p cmds\r
+ with\r
+ | Done _ -> print_endline "Done! Commands history: "; List.iter print_endline (List.rev cmds)\r
+ in f p []\r
+ ) with Done _ -> ()\r
+;;\r
+\r
+let rec conv_join = function\r
+ | [] -> "@"\r
+ | x::xs -> conv_join xs ^ " ("^ x ^")"\r
+;;\r
+\r
+let auto' a b =\r
+ let p = problem_of a (conv_join b) in\r
+ let sigma = auto p in\r
+ check p sigma\r
+;;\r
+\r
+(* Example usage of exec, interactive:\r
+\r
+exec\r
+ "x x"\r
+ (conv_join["x y"; "y y"; "y x"])\r
+ [ step 0 1; eat ]\r
+;;\r
+\r
+interactive "x y"\r
+ "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat]\r
+;;\r
+\r
+*)\r
+\r
+auto' "x x" ["x y"; "y y"; "y x"] ;;\r
+auto' "x y" ["x (_. x)"; "y z"; "y x"] ;;\r
+auto' "a (x. x b) (x. x c)" ["a (x. b b) @"; "a @ c"; "a (x. x x) a"; "a (a a a) (a c c)"] ;;\r
+\r
+auto' "x (y. x y y)" ["x (y. x y x)"] ;;\r
+\r
+auto' "x a a a a" [\r
+ "x b a a a";\r
+ "x a b a a";\r
+ "x a a b a";\r
+ "x a a a b";\r
+] ;;\r
+\r
+(* Controesempio ad usare un conto dei lambda che non considere le permutazioni *)\r
+auto' "x a a a a (x (x. x x) @ @ (_._.x. x x) x) b b b" [\r
+ "x a a a a (_. a) b b b";\r
+ "x a a a a (_. _. _. _. x. y. x y)";\r
+] ;;\r
+\r
+\r
+print_hline();\r
+print_endline "ALL DONE. "\r