type var = int;;\r
type t =\r
| V of var\r
- | A of t * t\r
- | L of t\r
+ | A of bool * t * t\r
+ | L of (bool * t)\r
+;;\r
+\r
+let rec measure_of_t = function\r
+ | V _ -> 0\r
+ | A(b,t1,t2) -> (if b then 1 else 0) + measure_of_t t1 + measure_of_t t2\r
+ | L(b,t) -> if b then measure_of_t t else 0\r
;;\r
\r
let string_of_t =\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(t1,t2) -> string_of_term_no_pars_app level t1 ^ " " ^ string_of_term_w_pars level t2\r
+ | A(b,t1,t2) -> string_of_term_no_pars_app level t1 ^ (if b then "," else " ") ^ 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(A(V 0, V 0));;\r
+let delta = L(true,A(true,V 0, V 0));;\r
\r
(* does NOT lift the argument *)\r
-let mk_lams = fold_nat (fun x _ -> L x) ;;\r
+let mk_lams = fold_nat (fun x _ -> L(false,x)) ;;\r
\r
type problem = {\r
orig_freshno: int\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
\r
let rec is_inert =\r
function\r
- | A(t,_) -> is_inert t\r
+ | A(_,t,_) -> is_inert t\r
| V _ -> true\r
| L _ -> false\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
+ | A(_,t,_) -> let hd,args = get_inert t in hd,args+1\r
| _ -> assert false\r
;;\r
\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
-let rec subst level delift sub =\r
+(* b' defaults to false *)\r
+let rec subst b' 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 -> L (subst (level + 1) delift sub 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
-and mk_app t1 t2 = if t1 = delta && t2 = delta then raise B\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 b' (subst b' level delift sub t1) (subst b' level delift sub t2)\r
+ | A(b,t1,t2) ->\r
+ mk_app b (subst b' level delift sub t1) (subst b' level delift sub t2)\r
+and mk_app b' t1 t2 = if t1 = delta && t2 = delta then raise B\r
else match t1 with\r
- | L t1 -> subst 0 true (0, t2) t1\r
- | _ -> A (t1, t2)\r
+ | L(b,t1) -> subst (b' && not b) 0 true (0, t2) t1\r
+ | _ -> A (b', 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
+ | L(b,t) -> L(b,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 0 false;;\r
+let subst = subst false 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(lift 1 t2,V 0))\r
- | t1, L t2 -> aux (A(lift 1 t1,V 0)) t2\r
+ | L(_,t1), L(_,t2) -> aux t1 t2\r
+ | L(_,t1), t2 -> aux t1 (A(true,lift 1 t2,V 0))\r
+ | t1, L(_,t2) -> aux (A(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
+ | A(_,t1,t2), A(_,u1,u2) -> aux t1 u1 && aux t2 u2\r
| _, _ -> false\r
in aux ;;\r
\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
- | A(t1, t2) -> aux lev t1 || aux lev t2\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
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
+ | 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
;;\r
\r
let rec purify = function\r
- | L t -> Pure.L (purify t)\r
- | A (t1,t2) -> Pure.A (purify t1, purify t2)\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
\r
let rec aux t =\r
match t with\r
| V _ as t -> 0, t\r
- | A(t1,_) as 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
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(t1,t2), A(u1,u2) ->\r
+ | A(_,t1,t2), A(_,u1,u2) ->\r
(match aux t1 u1 (k-1) with\r
| None ->\r
if not (eta_eq t2 u2) then Some (k-1)\r
\r
let compute_max_lambdas_at hd_var j =\r
let rec aux hd = function\r
- | A(t1,t2) ->\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
+ | L(_,t) -> aux (hd+1) t\r
| V _ -> 0\r
in aux hd_var\r
;;\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, A(false, t, V (v + k))\r
) (p, V 0) n in\r
let p = {p with phase=`Two} in\r
- let t = A(t, delta) in\r
- let t = fold_nat (fun t m -> A(t, V (k-m))) t (k-1) in\r
+ let t = A(false, t, delta) in\r
+ let t = fold_nat (fun t m -> A(false, t, V (k-m))) t (k-1) in\r
let subst = var, mk_lams t k in\r
let p = subst_in_problem subst p in\r
let _, args = get_inert p.div in\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, A(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(t, V (k-m+1))) t (k+1) in\r
+ fold_nat (fun t m -> A(false, 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 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 (press <Enter>)";\r
+ ignore(read_line())));\r
auto p\r
;;\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
+ | `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
| `Var(v,_) -> V v\r
| `N _ | `Match _ -> assert false in\r