type t =\r
| V of var\r
| A of t * t\r
- | L of t\r
+ | L of (t * t list (*garbage*))\r
| C (* constant *)\r
;;\r
\r
-let delta = L(A(V 0, V 0));;\r
+let delta = L(A(V 0, V 0),[]);;\r
\r
let rec is_stuck = function\r
| C -> true\r
let eta_eq' =\r
let rec aux l1 l2 t1 t2 = match t1, t2 with\r
| _, _ when is_stuck t1 || is_stuck t2 -> true\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
+ | L t1, L t2 -> aux l1 l2 (fst t1) (fst t2)\r
+ | L t1, t2 -> aux l1 (l2+1) (fst t1) t2\r
+ | t1, L t2 -> aux (l1+1) l2 t1 (fst t2)\r
| V a, V b -> a + l1 = b + l2\r
| A(t1,t2), A(u1,u2) -> aux l1 l2 t1 u1 && aux l1 l2 t2 u2\r
| _, _ -> false\r
(* is arg1 eta-subterm of arg2 ? *)\r
let eta_subterm u =\r
let rec aux lev t = if t = C then false else (eta_eq' lev 0 u t || match t with\r
- | L t -> aux (lev+1) t\r
+ | L(t,g) -> List.exists (aux (lev+1)) (t::g)\r
| A(t1, t2) -> aux lev t1 || aux lev t2\r
| _ -> false) in\r
aux 0\r
;;\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(x,[])) ;;\r
\r
let string_of_t =\r
let string_of_bvar =\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
+ | L(t,g) -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t\r
+ ^ (if g = [] then "" else String.concat ", " ("" :: List.map (string_of_term_w_pars level) g))\r
| _ as t -> string_of_term_no_pars_app level t\r
in string_of_term_no_pars 0\r
;;\r
C -> true\r
| V _ -> false\r
| A(t,_)\r
- | L t -> is_constant t\r
+ | L(t,_) -> is_constant t\r
;;\r
\r
let rec get_inert = function\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 = v' then max 0 (n - m) else 0\r
| V v' -> if v = v' then n else 0\r
| C -> 0\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
+ | 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 x -> let t, g = subst_in_lam (level+1) delift sub x in L(t, g), []\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 -> C\r
+ let t1, g1 = subst level delift sub t1 in\r
+ let t2, g2 = subst level delift sub t2 in\r
+ let t3, g3 = mk_app t1 t2 in\r
+ t3, g1 @ g2 @ g3\r
+ | C -> C, []\r
+and subst_in_lam level delift sub (t, g) =\r
+ let t', g' = subst level delift sub t in\r
+ let g'' = List.fold_left\r
+ (fun xs t ->\r
+ let x,y = subst level delift sub t in\r
+ (x :: y @ xs)) g' g in t', g''\r
and mk_app 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 x -> subst_in_lam 0 true (0, t2) x\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
+ | L(t,g) -> L (aux (lev+1) t, List.map (aux (lev+1)) g)\r
| A (t1, t2) -> A (aux lev t1, aux lev t2)\r
| C -> C\r
in aux 0\r
\r
let subst_in_problem ((v, t) as sub) p =\r
print_endline ("-- SUBST " ^ string_of_t (V v) ^ " |-> " ^ string_of_t t);\r
- {p with\r
- div=subst sub p.div;\r
- conv=subst sub p.conv;\r
- sigma=sub::p.sigma}\r
+ let sigma = sub :: p.sigma in\r
+ let div, g = try subst sub p.div with B -> raise (Done sigma) in\r
+ assert (g = []);\r
+ let conv, f = try subst sub p.conv with B -> raise (Fail(-1, "p.conv diverged")) in\r
+ assert (g = []);\r
+ {p with div; conv; sigma}\r
;;\r
\r
let get_subterms_with_head hd_var =\r
- let rec aux lev inert_done = function\r
- | L t -> aux (lev+1) false t\r
- | C | V _ -> []\r
+ let rec aux lev inert_done g = function\r
+ | L(t,g') -> List.fold_left (aux (lev+1) false) g (t::g')\r
+ | C | V _ -> g\r
| A(t1,t2) as t ->\r
let hd_var', n_args' = get_inert t1 in\r
if not inert_done && hd_var' = V (hd_var + lev)\r
- then lift ~-lev t :: aux lev true t1 @ aux lev false t2\r
- else aux lev true t1 @ aux lev false t2\r
- in aux 0 false\r
+ then lift ~-lev t :: aux lev false (aux lev true g t1) t2\r
+ else aux lev false (aux lev true g t1) t2\r
+ in aux 0 false []\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
+let purify =\r
+ let rec aux = function\r
+ | L(t,g) ->\r
+ let t = aux (lift (List.length g) t) in\r
+ let t = List.fold_left (fun t g -> Pure.A(Pure.L t, aux g)) t g in\r
+ Pure.L t\r
+ | A (t1,t2) -> Pure.A (aux t1, aux t2)\r
+ | V n -> Pure.V (n)\r
| C -> Pure.V (min_int/2)\r
+ in aux\r
;;\r
\r
let check p sigma =\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 _ | C -> 0\r
in aux hd_var\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,g) -> List.fold_right (max ++ (aux 0)) (t::g) 0\r
| _ -> n\r
in aux 0 in\r
print_cmd "FINISH" "";\r