\r
open Pure\r
\r
-type var_flag = [\r
- `Inherit | `Some of bool ref\r
- (* bool:\r
- true if original application and may determine a distinction\r
- *)\r
- | `Duplicate\r
-] ;;\r
+type var_flag = bool ;;\r
\r
type var = int;;\r
type t =\r
| L of t\r
;;\r
\r
-let index_of x =\r
- let rec aux n =\r
- function\r
- [] -> None\r
- | x'::_ when x == x' -> Some n\r
- | _::xs -> aux (n+1) xs\r
- in aux 1\r
-;;\r
-\r
-let sep_of_app =\r
- let apps = ref [] in\r
- function\r
- r when not !r -> " "\r
- | r ->\r
- let i =\r
- match index_of r !apps with\r
- Some i -> i\r
- | None ->\r
- apps := !apps @ [r];\r
- List.length !apps\r
- in " " ^ string_of_int i ^ ":"\r
-;;\r
-let string_of_var_flag = function\r
- | `Some b -> sep_of_app b\r
- | `Inherit -> " ?"\r
- | `Duplicate -> " !"\r
- ;;\r
-\r
-\r
let string_of_t =\r
+ let sep_of_app b = if b then " +" else " " in\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
+ fun nn -> if nn < bvarsno then List.nth bound_vars nn else "v" ^ (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
+ | V v -> if v >= level then string_of_int (v-level) else\r
string_of_bvar (level - v-1)\r
| A _\r
| L _ as t -> "(" ^ string_of_term_no_pars level t ^ ")"\r
and string_of_term_no_pars_app level = function\r
- | A(b,t1,t2) -> string_of_term_no_pars_app level t1 ^ string_of_var_flag b ^ string_of_term_w_pars level t2\r
+ | A(b,t1,t2) -> string_of_term_no_pars_app level t1 ^ sep_of_app b ^ 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
in string_of_term_no_pars 0\r
;;\r
\r
-\r
-let delta = L(A(`Some (ref true),V 0, V 0));;\r
-\r
(* does NOT lift the argument *)\r
let mk_lams = fold_nat (fun x _ -> L x) ;;\r
\r
let measure_of_t =\r
- let rec aux acc = function\r
- | V _ -> acc, 0\r
+ let rec aux = function\r
+ | V _ -> 0\r
| A(b,t1,t2) ->\r
- let acc, m1 = aux acc t1 in\r
- let acc, m2 = aux acc t2 in\r
- (match b with\r
- | `Some b when !b && not (List.memq b acc) -> b::acc, 1 + m1 + m2\r
- | _ -> acc, m1 + m2)\r
- | L t -> aux acc t\r
- in snd ++ (aux [])\r
+ (if b then 1 else 0) + aux t1 + aux t2\r
+ | L t -> aux t\r
+ in aux\r
;;\r
\r
type problem = {\r
orig_freshno: int\r
; freshno : int\r
- ; div : t\r
- ; conv : t\r
+ ; tms : t list\r
; sigma : (var * t) list (* substitutions *)\r
- ; phase : [`One | `Two] (* :'( *)\r
}\r
\r
let string_of_problem p =\r
- let lines = [\r
- "[measure] " ^ string_of_int (measure_of_t p.div);\r
- "[DV] " ^ string_of_t p.div;\r
- "[CV] " ^ string_of_t p.conv;\r
- ] in\r
+ let measure = List.fold_left (+) 0 (List.map measure_of_t p.tms) in\r
+ let lines = ("[measure] " ^ string_of_int measure) ::\r
+ List.map (fun x -> "[TM] " ^ string_of_t x) p.tms in\r
String.concat "\n" lines\r
;;\r
\r
-exception B;;\r
exception Done of (var * t) list (* substitution *);;\r
exception Fail of int * string;;\r
\r
\r
let rec erase = function\r
| L t -> L (erase t)\r
- | A(_,t1,t2) -> A(`Some(ref false), erase t1, erase t2)\r
+ | A(_,t1,t2) -> A(false, erase t1, erase t2)\r
| V _ as t -> t\r
;;\r
\r
-let rec subst top level delift ((flag, var, tm) as sub) =\r
+let explode =\r
+ let rec aux args = function\r
+ | L _ -> assert false\r
+ | V _ as x -> x, args\r
+ | A(b,t1,t2) -> aux ((b,t2)::args) t1\r
+ in aux []\r
+;;\r
+\r
+let rec implode hd args =\r
+ match args with\r
+ | [] -> hd\r
+ | (f,a)::args -> implode (A(f,hd,a)) args\r
+;;\r
+\r
+let get_head =\r
+ let rec aux lev = function\r
+ | L t -> aux (lev+1) t\r
+ | A(_,t,_) -> aux lev t\r
+ | V v -> v - lev\r
+ in aux 0\r
+;;\r
+\r
+let rec subst level delift ((var, tm) as sub) =\r
function\r
| V v -> if v = level + var then lift level tm else V (if delift && v > level then v-1 else v)\r
- | L t -> L (subst top (level + 1) delift sub t)\r
+ | L t -> L (subst (level + 1) delift sub t)\r
| A(b,t1,t2) ->\r
- let special = b = `Duplicate && top && t2 = V (level + var) in\r
- let t1' = subst (if special then false else top) level delift sub t1 in\r
- let t2' = subst false level delift sub t2 in\r
- match b with\r
- | `Duplicate when special ->\r
- assert (match t1' with L _ -> false | _ -> true) ;\r
- (match flag with\r
- | `Some b when !b -> b := false\r
- | `Some b -> ()\r
- (*print_string "WARNING! Stepping on a useless argument!";\r
- ignore(read_line())*)\r
- | `Inherit | `Duplicate -> assert false);\r
- A(flag, t1', erase t2')\r
- | `Inherit | `Duplicate ->\r
- let b' = if t2 = V (level + var)\r
- then (assert (flag <> `Inherit); flag)\r
- else b in\r
- assert (match t1' with L _ -> false | _ -> true) ;\r
- A(b', t1', t2')\r
- | `Some b' -> mk_app top b' t1' t2'\r
-and mk_app top flag t1 t2 = if t1 = delta && t2 = delta then raise B\r
- else match t1 with\r
- | L t1 -> subst top 0 true (`Some flag, 0, t2) t1\r
- | _ -> A (`Some flag, t1, t2)\r
+ let t1' = subst level delift sub t1 in\r
+ let t2' = subst level delift sub t2 in\r
+ mk_app b t1' t2'\r
+and mk_app flag t1 t2 = match t1 with\r
+ | L t1 -> subst 0 true (0, t2) t1\r
+ | _ -> A (flag, t1, t2)\r
and lift n =\r
let rec aux lev =\r
function\r
| A (b,t1, t2) -> A (b,aux lev t1, aux lev t2)\r
in aux 0\r
;;\r
-let subst top = subst top 0 false;;\r
-let mk_app = mk_app true;;\r
+let subst = subst 0 false;;\r
+(* let mk_app = mk_app true;; *)\r
+let rec mk_apps t = function\r
+ | [] -> t\r
+ | (f,x)::xs -> mk_apps (mk_app f t x) xs\r
+;;\r
\r
let eta_eq =\r
let rec aux t1 t2 = match t1, t2 with\r
| L t1, L t2 -> aux t1 t2\r
- | L t1, t2 -> aux t1 (A(`Some (ref true),lift 1 t2,V 0))\r
- | t1, L t2 -> aux (A(`Some (ref true),lift 1 t1,V 0)) t2\r
+ | L t1, t2 -> aux t1 (A(false,lift 1 t2,V 0))\r
+ | t1, L t2 -> aux (A(false,lift 1 t1,V 0)) t2\r
| V a, V b -> a = b\r
| A(_,t1,t2), A(_,u1,u2) -> aux t1 u1 && aux t2 u2\r
| _, _ -> false\r
let subst_in_problem ?(top=true) ((v, t) as sub) p =\r
print_endline ("-- SUBST " ^ string_of_t (V v) ^ " |-> " ^ string_of_t t);\r
let sigma = sub::p.sigma in\r
- let sub = (`Inherit, v, t) in\r
- let div = try subst top sub p.div with B -> raise (Done sigma) in\r
- let conv = try subst false sub p.conv with B -> raise (Fail(-1,"p.conv diverged")) in\r
- {p with div; conv; sigma}\r
-;;\r
-\r
-let get_subterm_with_head_and_args hd_var n_args =\r
- let rec aux lev = function\r
- | V _ -> 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
- (* the `+1` above is because of t2 *)\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
+ let sub = (v, t) in\r
+ let tms = List.map (subst sub) p.tms in\r
+ {p with tms; sigma}\r
;;\r
\r
let rec purify = function\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
+ assert false (* FIXME *)\r
+ (* print_endline "Checking...";\r
+ let tms = List.map purify p.tms in\r
let sigma = List.map (fun (v,t) -> v, purify t) sigma in\r
let freshno = List.fold_right (max ++ fst) sigma 0 in\r
let env = Pure.env_of_sigma freshno sigma in\r
print_endline " D diverged.";\r
assert (not (Pure.diverged (Pure.mwhd (env,conv,[]))));\r
print_endline " C converged.";\r
- ()\r
+ () *)\r
;;\r
\r
let sanity p =\r
print_endline (string_of_problem p); (* non cancellare *)\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
+ let rec all_different = function\r
+ | [] -> true\r
+ | x::xs -> List.for_all ((<>) x) xs && all_different xs in\r
+ if List.for_all is_var p.tms && all_different p.tms\r
+ then raise (Done p.sigma);\r
+ if List.exists (not ++ is_inert) p.tms\r
+ then problem_fail p "used a non-effective path";\r
p\r
;;\r
\r
-(* drops the arguments of t after the n-th *)\r
-(* FIXME! E' usato in modo improprio contando sul fatto\r
- errato che ritorna un inerte lungo esattamente n *)\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
-(* return the index of the first argument with a difference\r
- (the first argument is 0)\r
- precondition: p.div and t have n+1 arguments\r
- *)\r
-let find_eta_difference p t argsno =\r
- let t = inert_cut_at argsno t in\r
- let rec aux t u k = match t, u with\r
- | V _, V _ -> None\r
- | A(b1,t1,t2), A(b2,u1,u2) ->\r
- (match aux t1 u1 (k-1) with\r
- | None ->\r
- if not (eta_eq t2 u2) then begin\r
- let is_relevant = function `Some b -> !b | _ -> false in\r
- if not (is_relevant b1 || is_relevant b2) then begin\r
- print_string "WARNING! Stepping on a useless argument!";\r
-print_string (string_of_t t ^ " <==> " ^ string_of_t u);\r
- ignore(read_line())\r
- end ;\r
- Some (k-1)\r
- end\r
- else None\r
- | Some j -> Some j)\r
- | _, _ -> assert false\r
- in match aux p.div t argsno with\r
- | None -> problem_fail p "no eta difference found (div subterm of conv?)"\r
- | Some j -> j\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 hd_var j t2)\r
- else id) (max (aux hd t1) (aux hd t2))\r
- | L t -> aux (hd+1) t\r
- | V _ -> 0\r
- in aux hd_var\r
-;;\r
-\r
let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);;\r
\r
-(* step on the head of div, on the k-th argument, with n fresh vars *)\r
-let step ?(isfinish=false) 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(`Some (ref false), t, V (v + k + 1))\r
- ) (p, V 0) n in\r
- let t = (* apply bound variables V_k..V_0 *)\r
- fold_nat (fun t m -> A((if m = k+1 then `Duplicate else `Inherit), t, V (k-m+1))) t (k+1) 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 ~top:(not isfinish) subst p in\r
- sanity p\r
+let step var j n p =\r
+ let atsnd f (a,b) = (a, f b) in\r
+ let p, alphas = (* make fresh vars *)\r
+ fold_nat (fun (p, vs) _ ->\r
+ let p, v = freshvar p in\r
+ p, v::vs\r
+ ) (p, []) n in let alphas = List.rev alphas in\r
+ let rec aux lev (inside:bool) = function\r
+ | L t -> L (aux (lev+1) inside t)\r
+ | _ as x ->\r
+ let hd, args = explode x in\r
+ if hd = V (var+lev) then\r
+ (let nargs = List.length args in\r
+ let k = max 0 (j + 1 - nargs) in\r
+ let args = List.mapi\r
+ (fun i (f, t) -> f, lift k (aux lev (if i=j then true else inside) t)) args in\r
+ let bound = fold_nat (fun x n -> (false,V(n-1)) :: x) [] k in\r
+ let args = args @ bound in\r
+ let _, head = List.nth args j in\r
+ let args = List.mapi\r
+ (fun i (f, t) -> (if i=j && not inside then false else f), if i=j && not inside then erase t else t) args in\r
+ let head = (if inside then erase else id) head in\r
+ print_endline ("HEAD: " ^ string_of_t head);\r
+ let alphas = List.map (fun v -> false, V(lev+k+v)) alphas in\r
+ let t = mk_apps head (alphas @ args) in\r
+ let t = mk_lams t k in\r
+ t\r
+ ) else\r
+ (let args = List.map (atsnd (aux lev inside)) args in\r
+ implode hd args) in\r
+ let sigma = (var, aux 0 false (V var)) :: p.sigma in\r
+ {p with tms=List.map (aux 0 false) p.tms; sigma}\r
;;\r
\r
-let finish p =\r
- let compute_max_arity =\r
- let rec aux n = function\r
- | A(_,t1,t2) -> max (aux (n+1) t1) (aux 0 t2)\r
- | L t -> max n (aux 0 t)\r
- | V _ -> n\r
- in aux 0 in\r
-print_cmd "FINISH" "";\r
- let div_hd, div_nargs = get_inert p.div in\r
- let j = div_nargs - 1 in\r
- let arity = compute_max_arity p.conv in\r
- let n = 1 + arity + max\r
- (compute_max_lambdas_at div_hd j p.div)\r
- (compute_max_lambdas_at div_hd j p.conv) in\r
- let p = step ~isfinish:true j n p in\r
- let div_hd, div_nargs = get_inert p.div in\r
- let rec aux m = function\r
- A(_,t1,t2) -> if is_var t2 then\r
- (let delta_var, _ = get_inert t2 in\r
- if delta_var <> div_hd && get_subterm_with_head_and_args delta_var 1 p.conv = None\r
- then m, delta_var\r
- else aux (m-1) t1) else aux (m-1) t1\r
- | _ -> assert false in\r
- let m, delta_var = aux div_nargs p.div in\r
- let p = subst_in_problem (delta_var, delta) p in\r
- let p = subst_in_problem (div_hd, mk_lams delta (m-1)) p in\r
- sanity p\r
-;;\r
+let finish p = assert false ;;\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 problem_fail (finish p) "Auto.2 did not complete the problem"\r
- with Done sigma -> sigma)\r
- (*\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
- *)\r
- | Some t ->\r
- let j = find_eta_difference p t n_args 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 m1 = measure_of_t p.div in\r
- let p = step j k p in\r
- let m2 = measure_of_t p.div in\r
- (if m2 >= m1 then\r
- (print_string ("WARNING! Measure did not decrease : " ^ string_of_int m2 ^ " >= " ^ string_of_int m1 ^ " (press <Enter>)");\r
- ignore(read_line())));\r
- auto p\r
-;;\r
+let rec auto p = assert false ;;\r
\r
let problem_of (label, div, convs, ps, var_names) =\r
print_hline ();\r
let rec aux = function\r
| `Lam(_,t) -> L (aux t)\r
- | `I ((v,_), args) -> Listx.fold_left (fun x y -> mk_app (ref true) x (aux y)) (V v) args\r
+ | `I ((v,_), args) -> Listx.fold_left (fun x y -> mk_app true x (aux y)) (V v) args\r
| `Var(v,_) -> V v\r
| `N _ | `Match _ -> assert false in\r
- assert (List.length ps = 0);\r
let convs = (List.rev convs :> Num.nf list) in\r
- let conv = aux\r
- (if List.length convs = 1\r
- then List.hd convs\r
- else `I((List.length var_names, min_int), Listx.from_list convs)) in\r
- let var_names = "@" :: var_names in\r
- let div = match div with\r
- | Some div -> aux (div :> Num.nf)\r
- | None -> assert false in\r
+ let tms = List.map aux (convs @ (ps :> Num.nf list)) in\r
+ let tms = match div with\r
+ | Some div -> aux (div :> Num.nf) :: tms\r
+ | None -> tms in\r
let varno = List.length var_names in\r
- let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; phase=`One} in\r
+ let p = {orig_freshno=varno; freshno=1+varno; tms; sigma=[]} in\r
(* initial sanity check *)\r
sanity p\r
;;\r
\r
+let rec interactive p =\r
+ print_string "[varno index alphano] ";\r
+ let s = read_line () in\r
+ let spl = Str.split (Str.regexp " +") s in\r
+ let nth n = int_of_string (List.nth spl n) in\r
+ let p = step (nth 0) (nth 1) (nth 2) p in\r
+ interactive (sanity p)\r
+;;\r
+\r
let solve p =\r
- if eta_subterm p.div p.conv\r
- then print_endline "!!! div is subterm of conv. Problem was not run !!!"\r
- else check p (auto p)\r
+ let rec aux = function\r
+ | [] -> false\r
+ | x::xs -> List.exists (eta_subterm x) xs || aux xs in\r
+ if aux p.tms\r
+ then print_endline "!!! Problem stopped: subterm problem !!!"\r
+ else check p (interactive p)\r
;;\r
\r
Problems.main (solve ++ problem_of);\r
-\r
-(* Example usage of interactive: *)\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
-(* interactive "x y"\r
- "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat]\r
-;; *)\r