From: acondolu Date: Thu, 7 Jun 2018 08:06:23 +0000 (+0200) Subject: Added flag to lam and app, their propagation during subst, and a measure X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=commitdiff_plain;h=26ff6df470edda1ce022f1ba88e3769fe6323a4a;p=fireball-separation.git Added flag to lam and app, their propagation during subst, and a measure (may fail after eat steps) --- diff --git a/ocaml/simple.ml b/ocaml/simple.ml index bfb5ebd..27d0119 100644 --- a/ocaml/simple.ml +++ b/ocaml/simple.ml @@ -9,8 +9,14 @@ open Pure type var = int;; type t = | V of var - | A of t * t - | L of t + | A of bool * t * t + | L of (bool * t) +;; + +let rec measure_of_t = function + | V _ -> 0 + | A(b,t1,t2) -> (if b then 1 else 0) + measure_of_t t1 + measure_of_t t2 + | L(b,t) -> if b then measure_of_t t else 0 ;; let string_of_t = @@ -24,19 +30,19 @@ let string_of_t = | A _ | L _ as t -> "(" ^ string_of_term_no_pars level t ^ ")" and string_of_term_no_pars_app level = function - | A(t1,t2) -> string_of_term_no_pars_app level t1 ^ " " ^ string_of_term_w_pars level t2 + | A(b,t1,t2) -> string_of_term_no_pars_app level t1 ^ (if b then "," else " ") ^ 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) -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t | _ as t -> string_of_term_no_pars_app level t in string_of_term_no_pars 0 ;; -let delta = L(A(V 0, V 0));; +let delta = L(true,A(true,V 0, V 0));; (* does NOT lift the argument *) -let mk_lams = fold_nat (fun x _ -> L x) ;; +let mk_lams = fold_nat (fun x _ -> L(false,x)) ;; type problem = { orig_freshno: int @@ -49,6 +55,7 @@ type problem = { let string_of_problem p = let lines = [ + "[measure] " ^ string_of_int (measure_of_t p.div); "[DV] " ^ string_of_t p.div; "[CV] " ^ string_of_t p.conv; ] in @@ -71,7 +78,7 @@ let freshvar ({freshno} as p) = let rec is_inert = function - | A(t,_) -> is_inert t + | A(_,t,_) -> is_inert t | V _ -> true | L _ -> false ;; @@ -81,55 +88,57 @@ let is_lambda = function L _ -> true | _ -> false;; let rec get_inert = function | V n -> (n,0) - | A(t, _) -> let hd,args = get_inert t in hd,args+1 + | A(_,t,_) -> let hd,args = get_inert t in hd,args+1 | _ -> assert false ;; (* 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' then max 0 (n - m) else 0 | V v' -> if v = v' then n else 0 ;; -let rec subst level delift sub = +(* b' defaults to false *) +let rec subst b' 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 -> L (subst (level + 1) delift sub t) - | A (t1,t2) -> - let t1 = subst level delift sub t1 in - let t2 = subst level delift sub t2 in - mk_app t1 t2 -and mk_app t1 t2 = if t1 = delta && t2 = delta then raise B + | L(b,t) -> L(b, subst b' (level + 1) delift sub t) + | A(_,t1,(V v as t2)) when b' && v = level + fst sub -> + mk_app b' (subst b' level delift sub t1) (subst b' level delift sub t2) + | A(b,t1,t2) -> + mk_app b (subst b' level delift sub t1) (subst b' level delift sub t2) +and mk_app b' 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(b,t1) -> subst (b' && not b) 0 true (0, t2) t1 + | _ -> A (b', 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) - | A (t1, t2) -> A (aux lev t1, aux lev t2) + | L(b,t) -> L(b,aux (lev+1) t) + | A (b,t1, t2) -> A (b,aux lev t1, aux lev t2) in aux 0 ;; -let subst = subst 0 false;; +let subst = subst false 0 false;; +let mk_app = mk_app true;; let eta_eq = let rec aux t1 t2 = match t1, t2 with - | L t1, L t2 -> aux t1 t2 - | L t1, t2 -> aux t1 (A(lift 1 t2,V 0)) - | t1, L t2 -> aux (A(lift 1 t1,V 0)) t2 + | L(_,t1), L(_,t2) -> aux t1 t2 + | L(_,t1), t2 -> aux t1 (A(true,lift 1 t2,V 0)) + | t1, L(_,t2) -> aux (A(true,lift 1 t1,V 0)) t2 | V a, V b -> a = b - | A(t1,t2), A(u1,u2) -> aux t1 u1 && aux t2 u2 + | A(_,t1,t2), A(_,u1,u2) -> aux t1 u1 && aux t2 u2 | _, _ -> false in aux ;; (* is arg1 eta-subterm of arg2 ? *) let eta_subterm u = let rec aux lev t = eta_eq u (lift lev t) || match t with - | L t -> aux (lev+1) t - | A(t1, t2) -> aux lev t1 || aux lev t2 + | L(_, t) -> aux (lev+1) t + | A(_, t1, t2) -> aux lev t1 || aux lev t2 | _ -> false in aux 0 ;; @@ -145,8 +154,8 @@ print_endline ("-- SUBST " ^ string_of_t (V v) ^ " |-> " ^ string_of_t t); let get_subterm_with_head_and_args hd_var n_args = let rec aux lev = function | V _ -> None - | L t -> aux (lev+1) t - | A(t1,t2) as t -> + | L(_,t) -> aux (lev+1) t + | A(_,t1,t2) as t -> let hd_var', n_args' = get_inert t1 in if hd_var' = hd_var + lev && n_args <= 1 + n_args' (* the `+1` above is because of t2 *) @@ -158,8 +167,8 @@ let get_subterm_with_head_and_args hd_var n_args = ;; let rec purify = function - | L t -> Pure.L (purify t) - | A (t1,t2) -> Pure.A (purify t1, purify t2) + | L(_,t) -> Pure.L (purify t) + | A(_,t1,t2) -> Pure.A (purify t1, purify t2) | V n -> Pure.V n ;; @@ -191,7 +200,7 @@ let inert_cut_at n t = let rec aux t = match t with | V _ as t -> 0, t - | A(t1,_) as t -> + | A(_,t1,_) as t -> let k', t' = aux t1 in if k' = n then n, t' else k'+1, t @@ -207,7 +216,7 @@ let find_eta_difference p t argsno = let t = inert_cut_at argsno t in let rec aux t u k = match t, u with | V _, V _ -> None - | A(t1,t2), A(u1,u2) -> + | A(_,t1,t2), A(_,u1,u2) -> (match aux t1 u1 (k-1) with | None -> if not (eta_eq t2 u2) then Some (k-1) @@ -221,14 +230,14 @@ let find_eta_difference p t argsno = let compute_max_lambdas_at hd_var j = let rec aux hd = function - | A(t1,t2) -> + | A(_,t1,t2) -> (if get_inert t1 = (hd, j) then max ( (*FIXME*) if is_inert t2 && let hd', j' = get_inert t2 in hd' = hd 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 _ -> 0 in aux hd_var ;; @@ -250,11 +259,11 @@ print_cmd "EAT" ""; (* apply fresh vars *) let p, t = fold_nat (fun (p, t) _ -> let p, v = freshvar p in - p, A(t, V (v + k)) + p, A(false, t, V (v + k)) ) (p, V 0) n in let p = {p with phase=`Two} in - let t = A(t, delta) in - let t = fold_nat (fun t m -> A(t, V (k-m))) t (k-1) in + let t = A(false, t, delta) in + let t = fold_nat (fun t m -> A(false, t, V (k-m))) t (k-1) in let subst = var, mk_lams t k in let p = subst_in_problem subst p in let _, args = get_inert p.div in @@ -272,10 +281,10 @@ print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")") let p, t = (* apply fresh vars *) fold_nat (fun (p, t) _ -> let p, v = freshvar p in - p, A(t, V (v + k + 1)) + p, A(false, t, V (v + k + 1)) ) (p, V 0) n in let t = (* apply bound variables V_k..V_0 *) - fold_nat (fun t m -> A(t, V (k-m+1))) t (k+1) in + fold_nat (fun t m -> A(false, t, V (k-m+1))) t (k+1) in let t = mk_lams t (k+1) in (* make leading lambdas *) let subst = var, t in let p = subst_in_problem subst p in @@ -298,14 +307,19 @@ let rec auto p = let k = 1 + max (compute_max_lambdas_at hd_var j p.div) (compute_max_lambdas_at hd_var j p.conv) in + let m1 = measure_of_t p.div in let p = step j k p in + let m2 = measure_of_t p.div in + (if m2 >= m1 then + (print_string "WARNING! Measure did not decrease (press )"; + ignore(read_line()))); auto p ;; let problem_of (label, div, convs, ps, var_names) = print_hline (); let rec aux = function - | `Lam(_, t) -> L (aux t) + | `Lam(_, t) -> L (true,aux t) | `I ((v,_), args) -> Listx.fold_left (fun x y -> mk_app x (aux y)) (V v) args | `Var(v,_) -> V v | `N _ | `Match _ -> assert false in