X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=ocaml%2Fsimple.ml;h=079774e5ac67e5c53feaa53eca9e7e622dd77c76;hb=b0ecd3e4062bb9012ea9623237d0b379bd7646f2;hp=897d6b68a4b736952774d4127e109020f2fb7618;hpb=69f44e4fb3d56f89adcc952514707914886fdd79;p=fireball-separation.git diff --git a/ocaml/simple.ml b/ocaml/simple.ml index 897d6b6..079774e 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+1)) g)) | _ as t -> string_of_term_no_pars_app level t in string_of_term_no_pars 0 ;; @@ -69,6 +70,7 @@ let string_of_t = type problem = { orig_freshno: int ; freshno : int + ; label : string ; div : t ; conv : t ; sigma : (var * t) list (* substitutions *) @@ -84,12 +86,18 @@ let string_of_problem p = exception B;; exception Done of (var * t) list (* substitution *);; -exception Fail of int * string;; +exception Unseparable of string;; +exception Backtrack of string;; + +let rec try_all label f = function + | x::xs -> (try f x with Backtrack _ -> try_all label f xs) + | [] -> raise (Backtrack label) +;; let problem_fail p reason = print_endline "!!!!!!!!!!!!!!! FAIL !!!!!!!!!!!!!!!"; print_endline (string_of_problem p); - raise (Fail (-1, reason)) + failwith reason ;; let freshvar ({freshno} as p) = @@ -111,7 +119,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 +141,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 +149,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 +180,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 (Backtrack "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 = @@ -197,16 +219,18 @@ let check p sigma = let sigma = List.map (fun (v,t) -> v, purify t) sigma 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."; - assert (not (Pure.diverged (Pure.mwhd (env,conv,[])))); - print_endline " C converged."; + (if not (Pure.diverged (Pure.mwhd (env,div,[]))) + then failwith "D converged in Pure"); + print_endline "- D diverged."; + (if Pure.diverged (Pure.mwhd (env,conv,[])) + then failwith "C diverged in Pure"); + print_endline "- C converged."; () ;; let sanity p = print_endline (string_of_problem p); (* non cancellare *) - if not (is_inert p.div) then problem_fail p "p.div converged"; + if not (is_inert p.div) then raise (Backtrack "p.div converged"); (* Trailing constant args can be removed because do not contribute to eta-diff *) let rec remove_trailing_constant_args = function | A(t1, t2) when is_constant t2 -> remove_trailing_constant_args t1 @@ -233,9 +257,11 @@ let inert_cut_at n t = let find_eta_difference p t = let divargs = args_of_inert p.div in let conargs = args_of_inert t in + let rec range i j = + if j = -1 then [] else i :: range (i+1) (j-1) in let rec aux k divargs conargs = match divargs,conargs with - [],_ -> [] + [],conargs -> range k (List.length conargs) | _::_,[] -> [k] | t1::divargs,t2::conargs -> (if not (eta_eq t1 t2) then [k] else []) @ aux (k+1) divargs conargs @@ -252,7 +278,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 +322,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" ""; @@ -338,24 +364,16 @@ let auto p = if List.exists (fun t -> snd (get_inert t) >= n_args) tms then ( (* let tms = List.sort (fun t1 t2 -> - compare (snd (get_inert t1)) (snd (get_inert t2))) tms in *) - List.iter (fun t -> try + try_all "no similar terms" (fun t -> let js = find_eta_difference p t in (* print_endline (String.concat ", " (List.map string_of_int js)); *) - if js = [] then problem_fail p "no eta difference found (div subterm of conv?)"; let js = List.rev js in - List.iter + try_all "no eta difference" (fun j -> - try let k = 1 + max (compute_max_lambdas_at hd_var j p.div) (compute_max_lambdas_at hd_var j p.conv) in - ignore (aux (step j k p)) - with Fail(_, s) -> - print_endline ("Backtracking (eta_diff) because: " ^ s)) js; - raise (Fail(-1, "no eta difference")) - with Fail(_, s) -> - print_endline ("Backtracking (get_subterms) because: " ^ s)) tms; - raise (Fail(-1, "no similar terms")) + aux (step j k p)) js) tms ) else problem_fail (finish p) "Finish did not complete the problem" @@ -368,7 +386,7 @@ let auto p = let problem_of (label, div, convs, ps, var_names) = print_hline (); let rec aux lev = function - | `Lam(_, t) -> L (aux (lev+1) t, []) + | `Lam(_, t, g) -> L (aux (lev+1) t, List.map (aux (lev+1)) g) | `I (v, args) -> Listx.fold_left (fun x y -> fst (mk_app x (aux lev y))) (aux lev (`Var v)) args | `Var(v,_) -> if v >= lev && List.nth var_names (v-lev) = "C" then C else V v | `N _ | `Match _ -> assert false in @@ -380,15 +398,26 @@ let problem_of (label, div, convs, ps, var_names) = | Some div -> aux 0 (div :> Num.nf) | None -> assert false in let varno = List.length var_names in - {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]} + {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; label} ;; let solve p = - if is_constant p.div - then print_endline "!!! div is stuck. Problem was not run !!!" - else if eta_subterm p.div p.conv - then print_endline "!!! div is subterm of conv. Problem was not run !!!" - else let p = sanity p (* initial sanity check *) in check p (auto p) + let c = if String.length p.label > 0 then String.sub (p.label) 0 1 else "" in + let module M = struct exception Okay end in + try + if eta_subterm p.div p.conv + then raise (Unseparable "div is subterm of conv") + else + let p = sanity p (* initial sanity check *) in + check p (auto p); + raise M.Okay + with + | M.Okay -> if c = "?" then + failwith "The problem succeeded, but was supposed to be unseparable" + | e when c = "!" -> + failwith ("The problem was supposed to be separable, but: "^Printexc.to_string e) + | e -> + print_endline ("The problem failed, as expected ("^Printexc.to_string e^")") ;; Problems.main (solve ++ problem_of);