type var = int;;\r
type t =\r
| V of var\r
- | A of bool * t * t\r
+ | A of (bool ref) * 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
+let measure_of_t =\r
+ let rec aux acc = function\r
+ | V _ -> acc, 0\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
+ in snd ++ (aux [])\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(b,t1,t2) -> string_of_term_no_pars_app level t1 ^ (if b then "," else " ") ^ 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
;;\r
\r
\r
-let delta = L(true,A(true,V 0, V 0));;\r
+let delta = L(true,A(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
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
+ | 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(b,t1) -> subst (b' && not b) 0 true (0, t2) t1\r
+ | L(b,t1) -> subst (ref (!b' && not b)) 0 true (0, t2) t1\r
| _ -> A (b', t1, t2)\r
and lift n =\r
let rec aux lev =\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 = mk_app true;;\r
+let subst = subst (ref false) 0 false;;\r
+let mk_app = mk_app (ref 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(true,lift 1 t2,V 0))\r
- | t1, L(_,t2) -> aux (A(true,lift 1 t1,V 0)) 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
| V a, V b -> a = b\r
| A(_,t1,t2), A(_,u1,u2) -> aux t1 u1 && aux t2 u2\r
| _, _ -> false\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 =\r
- match phase with\r
- | `One ->\r
- let n = 1 + max\r
- (compute_max_lambdas_at var (k-1) p.div)\r
- (compute_max_lambdas_at var (k-1) 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(false, t, V (v + k))\r
- ) (p, V 0) n in\r
- let p = {p with phase=`Two} 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
- {p with div = inert_cut_at (args-k) p.div}\r
- | `Two ->\r
- let subst = var, mk_lams delta k in\r
- subst_in_problem subst p in\r
- sanity 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
let p, t = (* apply fresh vars *)\r
fold_nat (fun (p, t) _ ->\r
let p, v = freshvar p in\r
- p, A(false, t, V (v + k + 1))\r
+ p, A(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(false, t, V (k-m+1))) t (k+1) in\r
+ fold_nat (fun t m -> A(ref 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