From: acondolu Date: Sat, 9 Jun 2018 08:27:14 +0000 (+0200) Subject: Added garbage to L X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=commitdiff_plain;h=b532f22eeae82f364dbc533926ea657df765d1e8;p=fireball-separation.git Added garbage to L Note: eta_eq ignores garbage at the moment --- diff --git a/ocaml/simple.ml b/ocaml/simple.ml index 897d6b6..a028b1f 100644 --- a/ocaml/simple.ml +++ b/ocaml/simple.ml @@ -10,11 +10,11 @@ type var = int;; type t = | V of var | A of t * t - | L of t + | L of (t * t list (*garbage*)) | C (* constant *) ;; -let delta = L(A(V 0, V 0));; +let delta = L(A(V 0, V 0),[]);; let rec is_stuck = function | C -> true @@ -25,9 +25,9 @@ let rec is_stuck = function let eta_eq' = let rec aux l1 l2 t1 t2 = match t1, t2 with | _, _ when is_stuck t1 || is_stuck t2 -> true - | L t1, L t2 -> aux l1 l2 t1 t2 - | L t1, t2 -> aux l1 (l2+1) t1 t2 - | t1, L t2 -> aux (l1+1) l2 t1 t2 + | L t1, L t2 -> aux l1 l2 (fst t1) (fst t2) + | L t1, t2 -> aux l1 (l2+1) (fst t1) t2 + | t1, L t2 -> aux (l1+1) l2 t1 (fst t2) | V a, V b -> a + l1 = b + l2 | A(t1,t2), A(u1,u2) -> aux l1 l2 t1 u1 && aux l1 l2 t2 u2 | _, _ -> false @@ -37,14 +37,14 @@ let eta_eq = eta_eq' 0 0;; (* is arg1 eta-subterm of arg2 ? *) let eta_subterm u = let rec aux lev t = if t = C then false else (eta_eq' lev 0 u t || match t with - | L t -> aux (lev+1) t + | L(t,g) -> List.exists (aux (lev+1)) (t::g) | A(t1, t2) -> aux lev t1 || aux lev t2 | _ -> false) in aux 0 ;; (* does NOT lift the argument *) -let mk_lams = fold_nat (fun x _ -> L x) ;; +let mk_lams = fold_nat (fun x _ -> L(x,[])) ;; let string_of_t = let string_of_bvar = @@ -61,7 +61,8 @@ let string_of_t = | A(t1,t2) -> string_of_term_no_pars_app level t1 ^ " " ^ string_of_term_w_pars level t2 | _ as t -> string_of_term_w_pars level t and string_of_term_no_pars level = function - | L t -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t + | L(t,g) -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t + ^ (if g = [] then "" else String.concat ", " ("" :: List.map (string_of_term_w_pars level) g)) | _ as t -> string_of_term_no_pars_app level t in string_of_term_no_pars 0 ;; @@ -111,7 +112,7 @@ let rec is_constant = C -> true | V _ -> false | A(t,_) - | L t -> is_constant t + | L(t,_) -> is_constant t ;; let rec get_inert = function @@ -133,7 +134,7 @@ let args_of_inert = (* precomputes the number of leading lambdas in a term, after replacing _v_ w/ a term starting with n lambdas *) let rec no_leading_lambdas v n = function - | L t -> 1 + no_leading_lambdas (v+1) n t + | L(t,_) -> 1 + no_leading_lambdas (v+1) n t | A _ as t -> let v', m = get_inert t in if V v = v' then max 0 (n - m) else 0 | V v' -> if v = v' then n else 0 | C -> 0 @@ -141,22 +142,29 @@ let rec no_leading_lambdas v n = function let rec subst level delift sub = function - | V v -> if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v) - | L t -> let t = subst (level + 1) delift sub t in if t = B then B else L t + | V v -> (if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v)), [] + | L x -> let t, g = subst_in_lam (level+1) delift sub x in L(t, g), [] | A (t1,t2) -> - let t1 = subst level delift sub t1 in - let t2 = subst level delift sub t2 in - mk_app t1 t2 - | C -> C + let t1, g1 = subst level delift sub t1 in + let t2, g2 = subst level delift sub t2 in + let t3, g3 = mk_app t1 t2 in + t3, g1 @ g2 @ g3 + | C -> C, [] +and subst_in_lam level delift sub (t, g) = + let t', g' = subst level delift sub t in + let g'' = List.fold_left + (fun xs t -> + let x,y = subst level delift sub t in + (x :: y @ xs)) g' g in t', g'' and mk_app t1 t2 = if t1 = delta && t2 = delta then raise B else match t1 with - | L t1 -> subst 0 true (0, t2) t1 - | _ -> A (t1, t2) + | L x -> subst_in_lam 0 true (0, t2) x + | _ -> A (t1, t2), [] and lift n = let rec aux lev = function | V m -> V (if m >= lev then m + n else m) - | L t -> L (aux (lev+1) t) + | L(t,g) -> L (aux (lev+1) t, List.map (aux (lev+1)) g) | A (t1, t2) -> A (aux lev t1, aux lev t2) | C -> C in aux 0 @@ -165,29 +173,36 @@ let subst = subst 0 false;; let subst_in_problem ((v, t) as sub) p = print_endline ("-- SUBST " ^ string_of_t (V v) ^ " |-> " ^ string_of_t t); - {p with - div=subst sub p.div; - conv=subst sub p.conv; - sigma=sub::p.sigma} + let sigma = sub :: p.sigma in + let div, g = try subst sub p.div with B -> raise (Done sigma) in + assert (g = []); + let conv, f = try subst sub p.conv with B -> raise (Fail(-1, "p.conv diverged")) in + assert (g = []); + {p with div; conv; sigma} ;; let get_subterms_with_head hd_var = - let rec aux lev inert_done = function - | L t -> aux (lev+1) false t - | C | V _ -> [] + let rec aux lev inert_done g = function + | L(t,g') -> List.fold_left (aux (lev+1) false) g (t::g') + | C | V _ -> g | A(t1,t2) as t -> let hd_var', n_args' = get_inert t1 in if not inert_done && hd_var' = V (hd_var + lev) - then lift ~-lev t :: aux lev true t1 @ aux lev false t2 - else aux lev true t1 @ aux lev false t2 - in aux 0 false + then lift ~-lev t :: aux lev false (aux lev true g t1) t2 + else aux lev false (aux lev true g t1) t2 + in aux 0 false [] ;; -let rec purify = function - | L t -> Pure.L (purify t) - | A (t1,t2) -> Pure.A (purify t1, purify t2) - | V n -> Pure.V n +let purify = + let rec aux = function + | L(t,g) -> + let t = aux (lift (List.length g) t) in + let t = List.fold_left (fun t g -> Pure.A(Pure.L t, aux g)) t g in + Pure.L t + | A (t1,t2) -> Pure.A (aux t1, aux t2) + | V n -> Pure.V (n) | C -> Pure.V (min_int/2) + in aux ;; let check p sigma = @@ -252,7 +267,7 @@ let compute_max_lambdas_at hd_var j = then let hd', j' = get_inert t2 in j - j' else no_leading_lambdas hd_var j t2) else id) (max (aux hd t1) (aux hd t2)) - | L t -> aux (hd+1) t + | L(t,_) -> aux (hd+1) t | V _ | C -> 0 in aux hd_var ;; @@ -296,7 +311,7 @@ let finish p = let compute_max_arity = let rec aux n = function | A(t1,t2) -> max (aux (n+1) t1) (aux 0 t2) - | L t -> max n (aux 0 t) + | L(t,g) -> List.fold_right (max ++ (aux 0)) (t::g) 0 | _ -> n in aux 0 in print_cmd "FINISH" "";