\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
+\r
type var = int;;\r
type t =\r
| V of var\r
- | A of (bool ref) * t * t\r
- | L of (bool * t)\r
+ | A of var_flag * t * t\r
+ | L of t\r
;;\r
\r
let measure_of_t =\r
| A(b,t1,t2) ->\r
let acc, m1 = aux acc t1 in\r
let acc, m2 = aux acc t2 in\r
- if not (List.memq b acc) && !b then b::acc, 1 + m1 + m2 else acc, m1 + m2\r
- | L(b,t) -> if b then aux acc t else acc, 0\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
;;\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 string_of_bvar =\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 ^ sep_of_app b ^ string_of_term_w_pars level t2\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
| _ 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
+ | 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
\r
-let delta = L(true,A(ref true,V 0, V 0));;\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(false,x)) ;;\r
+let mk_lams = fold_nat (fun x _ -> L x) ;;\r
\r
type problem = {\r
orig_freshno: int\r
(* precomputes the number of leading lambdas in a term,\r
after replacing _v_ w/ a term starting with n lambdas *)\r
let rec no_leading_lambdas v n = function\r
- | L(_,t) -> 1 + no_leading_lambdas (v+1) n t\r
+ | L t -> 1 + no_leading_lambdas (v+1) n t\r
| A _ as t -> let v', m = get_inert t in if v = v' then max 0 (n - m) else 0\r
| V v' -> if v = v' then n else 0\r
;;\r
\r
-(* b' is true iff we are substituting the argument of a step\r
- and the application of the redex was true. Therefore we need to\r
- set the new app to true. *)\r
-let rec subst b' level delift sub =\r
+let rec erase = function\r
+ | L t -> L (erase t)\r
+ | A(_,t1,t2) -> A(`Some(ref 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
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(b,t) -> L(b, subst b' (level + 1) delift sub t)\r
- | A(_,t1,(V v as t2)) when b' && v = level + fst sub ->\r
- mk_app (ref true) (subst b' level delift sub t1) (subst b' level delift sub t2)\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
| A(b,t1,t2) ->\r
- mk_app b (subst b' level delift sub t1) (subst b' level delift sub t2)\r
-(* b is\r
- - a fresh ref true if we want to create a real application from scratch\r
- - a shared ref true if we substituting in the head of a real application *)\r
-and mk_app b' t1 t2 = if t1 = delta && t2 = delta then raise B\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(b,t1) ->\r
- let last_lam = match t1 with L _ -> false | _ -> true in\r
- if not b && last_lam then b' := false ;\r
- subst (!b' && not b && not last_lam) 0 true (0, t2) t1\r
- | _ -> A (b', t1, t2)\r
+ | L t1 -> subst top 0 true (`Some flag, 0, t2) t1\r
+ | _ -> A (`Some flag, 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(b,t) -> L(b,aux (lev+1) t)\r
+ | L t -> L(aux (lev+1) t)\r
| A (b,t1, t2) -> A (b,aux lev t1, aux lev t2)\r
in aux 0\r
;;\r
-let subst = subst false 0 false;;\r
-let mk_app t1 = mk_app (ref true) t1;;\r
+let subst top = subst top 0 false;;\r
+let mk_app = mk_app true;;\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(ref true,lift 1 t2,V 0))\r
- | t1, L(_,t2) -> aux (A(ref true,lift 1 t1,V 0)) t2\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
| V a, V b -> a = b\r
| A(_,t1,t2), A(_,u1,u2) -> aux t1 u1 && aux t2 u2\r
| _, _ -> false\r
(* is arg1 eta-subterm of arg2 ? *)\r
let eta_subterm u =\r
let rec aux lev t = eta_eq u (lift lev t) || match t with\r
- | L(_, t) -> aux (lev+1) t\r
+ | L t -> aux (lev+1) t\r
| A(_, t1, t2) -> aux lev t1 || aux lev t2\r
| _ -> false\r
in aux 0\r
;;\r
\r
-let subst_in_problem ((v, t) as sub) p =\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 div = try subst sub p.div with B -> raise (Done sigma) in\r
- let conv = try subst sub p.conv with B -> raise (Fail(-1,"p.conv diverged")) 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
+ | 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
;;\r
\r
let rec purify = function\r
- | L(_,t) -> Pure.L (purify t)\r
+ | L t -> Pure.L (purify t)\r
| A(_,t1,t2) -> Pure.A (purify t1, purify t2)\r
| V n -> Pure.V n\r
;;\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
+ | L t -> aux (hd+1) t\r
| V _ -> 0\r
in aux hd_var\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 k n p =\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(ref false, t, V (v + k + 1))\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(ref false, t, V (k-m+1))) t (k+1) in\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 subst p in\r
+ let p = subst_in_problem ~top:(not isfinish) subst p in\r
sanity p\r
;;\r
\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
+ | L t -> max n (aux 0 t)\r
| V _ -> n\r
in aux 0 in\r
print_cmd "FINISH" "";\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 j n p 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 problem_of (label, div, convs, ps, var_names) =\r
print_hline ();\r
let rec aux = function\r
- | `Lam(_, t) -> L (true,aux t)\r
- | `I ((v,_), args) -> Listx.fold_left (fun x y -> mk_app x (aux y)) (V v) args\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
| `Var(v,_) -> V v\r
| `N _ | `Match _ -> assert false in\r
assert (List.length ps = 0);\r