X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=ocaml%2Fsimple.ml;h=9de1f2a7c12444f2b651628d7d499e7e8497871b;hb=ad332f3befc4922498db7a9ca6dbb7cc67dbf75b;hp=991779e7ac995f5f980a174b43c292689cac1ce6;hpb=f1873841a3271d332cb8429b46e7bc7e0bca2402;p=fireball-separation.git diff --git a/ocaml/simple.ml b/ocaml/simple.ml index 991779e..9de1f2a 100644 --- a/ocaml/simple.ml +++ b/ocaml/simple.ml @@ -11,8 +11,6 @@ type t = | V of var | A of t * t | L of t - | B (* bottom *) - | C of int ;; let delta = L(A(V 0, V 0));; @@ -23,7 +21,6 @@ let eta_eq' = | L t1, t2 -> aux l1 (l2+1) t1 t2 | t1, L t2 -> aux (l1+1) l2 t1 t2 | V a, V b -> a + l1 = b + l2 - | C a, C b -> a = b | A(t1,t2), A(u1,u2) -> aux l1 l2 t1 u1 && aux l1 l2 t2 u2 | _, _ -> false in aux ;; @@ -49,10 +46,8 @@ let string_of_t = let rec string_of_term_w_pars level = function | V v -> if v >= level then "`" ^ string_of_int (v-level) else string_of_bvar (level - v-1) - | C n -> "c" ^ string_of_int n | A _ | L _ as t -> "(" ^ string_of_term_no_pars level t ^ ")" - | B -> "BOT" 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 | _ as t -> string_of_term_w_pars level t @@ -68,19 +63,18 @@ type problem = { ; div : t ; conv : t ; sigma : (var * t) list (* substitutions *) - ; stepped : var list ; phase : [`One | `Two] (* :'( *) } let string_of_problem p = let lines = [ - "[stepped] " ^ String.concat " " (List.map string_of_int p.stepped); "[DV] " ^ string_of_t p.div; "[CV] " ^ string_of_t p.conv; ] in String.concat "\n" lines ;; +exception B;; exception Done of (var * t) list (* substitution *);; exception Fail of int * string;; @@ -98,8 +92,7 @@ let rec is_inert = function | A(t,_) -> is_inert t | V _ -> true - | C _ - | L _ | B -> false + | L _ -> false ;; let is_var = function V _ -> true | _ -> false;; @@ -111,26 +104,24 @@ let rec get_inert = function | _ -> assert false ;; -let rec no_leading_lambdas hd_var j = function - | L t -> 1 + no_leading_lambdas (hd_var+1) j t - | A _ as t -> let hd_var', n = get_inert t in if hd_var = hd_var' then max 0 (j - n) else 0 - | V n -> if n = hd_var then j else 0 - | B | C _ -> 0 +(* 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 + | 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 = 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 + | 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 - | C _ as t -> t - | B -> B -and mk_app t1 t2 = if t2 = B || (t1 = delta && t2 = delta) then B +and mk_app t1 t2 = if t1 = delta && t2 = delta then raise B else match t1 with - | B -> B | L t1 -> subst 0 true (0, t2) t1 | _ -> A (t1, t2) and lift n = @@ -139,29 +130,26 @@ and lift n = | 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) - | C _ as t -> t - | B -> B in aux 0 ;; let subst = subst 0 false;; -let subst_in_problem sub p = -print_endline ("-- SUBST " ^ string_of_t (V (fst sub)) ^ " |-> " ^ string_of_t (snd sub)); - {p with - div=subst sub p.div; - conv=subst sub p.conv; - stepped=(fst sub)::p.stepped; - sigma=sub::p.sigma} +let subst_in_problem ((v, t) as sub) p = +print_endline ("-- SUBST " ^ string_of_t (V v) ^ " |-> " ^ string_of_t t); + let sigma = sub::p.sigma in + let div = try subst sub p.div with B -> raise (Done sigma) in + let conv = try subst sub p.conv with B -> raise (Fail(-1,"p.conv diverged")) in + {p with div; conv; sigma} ;; let get_subterm_with_head_and_args hd_var n_args = let rec aux lev = function - | C _ - | V _ | B -> None + | V _ -> None | 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 *) then Some (lift ~-lev t) else match aux lev t2 with | None -> aux lev t1 @@ -173,8 +161,6 @@ let rec purify = function | L t -> Pure.L (purify t) | A (t1,t2) -> Pure.A (purify t1, purify t2) | V n -> Pure.V n - | C _ -> Pure.V max_int (* FIXME *) - | B -> Pure.B ;; let check p sigma = @@ -182,7 +168,7 @@ let check p sigma = let div = purify p.div in let conv = purify p.conv in let sigma = List.map (fun (v,t) -> v, purify t) sigma in - let freshno = List.fold_right (fun (x,_) -> max x) sigma 0 in + let freshno = List.fold_right (max ++ fst) sigma 0 in let env = Pure.env_of_sigma freshno sigma in assert (Pure.diverged (Pure.mwhd (env,div,[]))); print_endline " D diverged."; @@ -193,14 +179,14 @@ let check p sigma = let sanity p = print_endline (string_of_problem p); (* non cancellare *) - if p.conv = B then problem_fail p "p.conv diverged"; - if p.div = B then raise (Done p.sigma); if p.phase = `Two && p.div = delta then raise (Done p.sigma); if not (is_inert p.div) then problem_fail p "p.div converged"; p ;; (* drops the arguments of t after the n-th *) +(* FIXME! E' usato in modo improprio contando sul fatto + errato che ritorna un inerte lungo esattamente n *) let inert_cut_at n t = let rec aux t = match t with @@ -213,15 +199,19 @@ let inert_cut_at n t = in snd (aux t) ;; -let find_eta_difference p t n_args = - let t = inert_cut_at n_args t in +(* return the index of the first argument with a difference + (the first argument is 0) + precondition: p.div and t have n+1 arguments + *) +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 _ -> assert false (* div subterm of conv *) + | V _, V _ -> problem_fail p "no eta difference found (div subterm of conv?)" | A(t1,t2), A(u1,u2) -> - if not (eta_eq t2 u2) then ((*print_endline((string_of_t t2) ^ " <> " ^ (string_of_t u2));*) k) + if not (eta_eq t2 u2) then (k-1) else aux t1 u1 (k-1) | _, _ -> assert false - in aux p.div t n_args + in aux p.div t argsno ;; let compute_max_lambdas_at hd_var j = @@ -235,7 +225,6 @@ let compute_max_lambdas_at hd_var j = else id) (max (aux hd t1) (aux hd t2)) | L t -> aux (hd+1) t | V _ -> 0 - | _ -> assert false in aux hd_var ;; @@ -288,9 +277,6 @@ print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")") sanity p ;; -;; - - let rec auto p = let hd_var, n_args = get_inert p.div in match get_subterm_with_head_and_args hd_var n_args p.conv with @@ -303,7 +289,7 @@ let rec auto p = else auto p with Done sigma -> sigma) | Some t -> - let j = find_eta_difference p t n_args - 1 in + let j = find_eta_difference p t n_args in let k = 1 + max (compute_max_lambdas_at hd_var j p.div) (compute_max_lambdas_at hd_var j p.conv) in @@ -326,7 +312,7 @@ let problem_of (label, div, convs, ps, var_names) = | Some div -> aux (div :> Num.nf) | None -> assert false in let varno = List.length var_names in - let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; stepped=[]; phase=`One} in + let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; phase=`One} in (* initial sanity check *) sanity p ;;