X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;ds=sidebyside;f=ocaml%2Fsimple.ml;h=185c54ef2f025afeea14d38d6bfa74d2423722de;hb=47a9f6a73ecea5a2e60932ce324e21f4a90315c2;hp=c318ebd71482eea1a9a4a784d1f27cf2502e5a01;hpb=1dc1445295ffe5e83cbce853387ff0b68d56f85f;p=fireball-separation.git diff --git a/ocaml/simple.ml b/ocaml/simple.ml index c318ebd..185c54e 100644 --- a/ocaml/simple.ml +++ b/ocaml/simple.ml @@ -1,5 +1,4 @@ let (++) f g x = f (g x);; -let id x = x;; let rec fold_nat f x n = if n = 0 then x else f (fold_nat f x (n-1)) n ;; let print_hline = Console.print_hline;; @@ -10,12 +9,11 @@ type var = int;; type t = | V of var | A of t * t - | L of t - | B (* bottom *) + | 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 @@ -24,11 +22,14 @@ 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 + let rec aux l1 l2 t1 t2 = + let stuck1, stuck2 = is_stuck t1, is_stuck t2 in + match t1, t2 with + | _, _ when not stuck1 && stuck2 -> false + | _, _ when stuck1 -> true + | 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 @@ -38,14 +39,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 = @@ -58,12 +59,12 @@ let string_of_t = | C -> "C" | 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 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 ;; @@ -71,41 +72,59 @@ let string_of_t = type problem = { orig_freshno: int ; freshno : int + ; label : string ; 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;; +exception Unseparable of string;; +exception Backtrack of string;; + +let rec try_all label f = function + | x::xs -> (try f x with Backtrack s -> (if s <> "" then print_endline ("\n<< BACKTRACK: "^s)); try_all label f xs) + | [] -> raise (Backtrack label) +;; +let try_both label f x g y = + try_all label (function `L x -> f x | `R y -> g y) [`L x ; `R y] +;; 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) = {p with freshno=freshno+1}, freshno+1 ;; +(* CSC: rename? is an applied C an inert? + is_inert and get_inert work inconsistently *) let rec is_inert = function | A(t,_) -> is_inert t | V _ -> true | C - | L _ | B -> false + | L _ -> false +;; + +let rec is_constant = + function + C -> true + | V _ -> false + | A(t,_) + | L(t,_) -> is_constant t ;; let rec get_inert = function @@ -127,92 +146,104 @@ 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 - | B | C -> 0 + | C -> 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 + | 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 | B as t -> t -and mk_app t1 t2 = if t2 = B || (t1 = delta && t2 = delta) then B + 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 - | B -> B - | 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 | B as t -> t + | C -> C in aux 0 ;; -let subst = subst 0 false;; +let subst' = subst;; +let subst = subst' 0 false;; + +let rec mk_apps t = function + | u::us -> mk_apps (A(t,u)) us + | [] -> t +;; 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; - stepped=v::p.stepped; - sigma=sub::p.sigma} + let sigma = sub :: p.sigma in + let div, g = try subst sub p.div with B -> raise (Done sigma) in + let divs = div :: g in + let conv, g = try subst sub p.conv with B -> raise (Backtrack "p.conv diverged") in + let conv = if g = [] then conv else mk_apps C (conv::g) in + divs, {p with div; conv; sigma} ;; let get_subterms_with_head hd_var = - let rec aux lev inert_done = function - | C | V _ | B -> [] - | L t -> aux (lev+1) false t + 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) - | B -> Pure.B + in aux ;; let check p sigma = - print_endline "Checking..."; + print_endline "\nChecking..."; 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 (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 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 + print_endline (string_of_problem p) (* non cancellare *); 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 @@ -230,9 +261,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 = 0 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 @@ -242,59 +275,35 @@ let find_eta_difference p t = let compute_max_lambdas_at hd_var j = let rec aux hd = function - | A(t1,t2) -> + | A(t1,t2) -> max (max (aux hd t1) (aux hd t2)) (if get_inert t1 = (V hd, j) - then max ( (*FIXME*) - if is_inert t2 && let hd', j' = get_inert t2 in hd' = V 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 - | V _ | C -> 0 - | _ -> assert false + then no_leading_lambdas hd (j+1) t2 + else 0) + | L(t,_) -> aux (hd+1) t + | V _ + | C -> 0 in aux hd_var ;; -let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);; +let print_cmd s1 s2 = print_endline ("\n>> " ^ s1 ^ " " ^ s2);; -(* eat the arguments of the divergent and explode. - It does NOT perform any check, may fail if done unsafely *) -let eat p = -print_cmd "EAT" ""; - let var, k = get_inert p.div in - match var with - | C | L _ | B | A _ -> assert false - | V var -> - let phase = p.phase in - let p = - match phase with - | `One -> - let n = 1 + max - (compute_max_lambdas_at var (k-1) p.div) - (compute_max_lambdas_at var (k-1) p.conv) in - (* apply fresh vars *) - let p, t = fold_nat (fun (p, t) _ -> - let p, v = freshvar p in - p, A(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 subst = var, mk_lams t k in - let p = subst_in_problem subst p in - let _, args = get_inert p.div in - {p with div = inert_cut_at (args-k) p.div} - | `Two -> - let subst = var, mk_lams delta k in - subst_in_problem subst p in - sanity p +(* returns Some i if i is the smallest integer s.t. p holds for the i-th + element of the list in input *) +let smallest_such_that p = + let rec aux i = + function + [] -> None + | hd::_ when p i hd -> Some i + | _::tl -> aux (i+1) tl + in + aux 0 ;; (* step on the head of div, on the k-th argument, with n fresh vars *) let step k n p = let hd, _ = get_inert p.div in match hd with - | C | L _ | B | A _ -> assert false + | C | L _ | A _ -> assert false | V var -> print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (on " ^ string_of_int (k+1) ^ "th)"); let p, t = (* apply fresh vars *) @@ -306,106 +315,125 @@ print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (on " ^ string_of_int (k+1) ^ fold_nat (fun t m -> A(t, V (k-m+1))) t k 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 - sanity p + let divs, p = subst_in_problem subst p in + divs, p +;; + +let finish p arity = + (* one-step version of eat *) + let compute_max_arity = + let rec aux n = function + | A(t1,t2) -> max (aux (n+1) t1) (aux 0 t2) + | L(t,g) -> List.fold_right (max ++ (aux 0)) (t::g) 0 + | _ -> n + in aux 0 in + (* First, a step on the last argument of the divergent. + Because of the sanity check, it will never be a constant term. *) + let div_hd, div_nargs = get_inert p.div in + let div_hd = match div_hd with V n -> n | _ -> raise (Backtrack "Cannot finish on constant tm") in + let j = match + smallest_such_that (fun i t -> i >= arity && not (is_constant t)) (args_of_inert p.div) + with Some j -> j | None -> raise (Backtrack "") in + print_endline "\n>> FINISHING"; + let arity = compute_max_arity p.conv in + let n = 1 + arity + max + (compute_max_lambdas_at div_hd j p.div) + (compute_max_lambdas_at div_hd j p.conv) in + let _, p = step j n p in + (* Now, find first argument of div that is a variable never applied anywhere. + It must exist because of some invariant, since we just did a step, + and because of the arity of the divergent *) + let div_hd, div_nargs = get_inert p.div in + let div_hd = match div_hd with V n -> n | _ -> assert false in + let rec aux m = function + | A(t, V delta_var) -> + if delta_var <> div_hd && get_subterms_with_head delta_var p.conv = [] + then m, delta_var + else aux (m-1) t + | A(t,_) -> aux (m-1) t + | _ -> assert false in + let m, delta_var = aux div_nargs p.div in + let _, p = subst_in_problem (delta_var, delta) p in + ignore (subst_in_problem (div_hd, mk_lams delta (m-1)) p); + assert false ;; let auto p = let rec aux p = - let hd, n_args = get_inert p.div in - match hd with - | C | L _ | B | A _ -> assert false - | V hd_var -> - let tms = get_subterms_with_head hd_var p.conv in - 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 - 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 - (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")) - ) - else - (let phase = p.phase in - let p = eat p in - if phase = `Two - then problem_fail p "Auto.2 did not complete the problem" - else aux p) - in - try - aux p - with Done sigma -> sigma + if eta_subterm p.div p.conv + then raise (Backtrack "div is subterm of conv"); + match p.div with + | L _ as t -> (* case p.div is an abstraction *) + print_endline "\nSOTTO UN LAMBDA"; + let t, g = mk_app t C in + aux ({p with div=mk_apps C (t::g)}) + | V _ | C -> raise (Backtrack "V | C") + | A _ -> ( + if is_constant p.div (* case p.div is rigid inert *) + then (print_endline "\nSOTTO UN C"; try_all "auto.C" + (fun div -> aux (sanity {p with div})) (args_of_inert p.div)) + else (* case p.div is flexible inert *) + let hd, n_args = get_inert p.div in + match hd with + | C | L _ | A _ -> assert false + | V hd_var -> + let tms = get_subterms_with_head hd_var p.conv in + let arity = List.fold_right (max ++ (snd ++ get_inert)) tms 0 in + try_both "???" (finish p) arity + (fun _ -> + let jss = List.concat (List.map (find_eta_difference p) tms) in + let jss = List.sort_uniq compare jss in + let f = try_all "no differences" + (fun j -> + let k = 1 + max + (compute_max_lambdas_at hd_var j p.div) + (compute_max_lambdas_at hd_var j p.conv) in + let divs, p = step j k p in + try_all "p.div" (fun div -> aux (sanity {p with div})) divs + ) in + try_both "step, then diverge arguments" + f jss + (try_all "tried p.div arguments" (fun div -> aux {p with div})) (args_of_inert p.div) + ) () + ) in try + aux p + with Done sigma -> sigma ;; let problem_of (label, div, convs, ps, var_names) = print_hline (); let rec aux lev = function - | `Lam(_, t) -> L (aux (lev+1) t) - | `I (v, args) -> Listx.fold_left (fun x y -> mk_app x (aux lev y)) (aux lev (`Var v)) args + | `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 assert (List.length ps = 0); let convs = List.rev convs in - let conv = List.fold_left (fun x y -> mk_app x (aux 0 (y :> Num.nf))) (V (List.length var_names)) convs in - let var_names = "@" :: var_names in + let conv = List.fold_left (fun x y -> fst (mk_app x (aux 0 (y :> Num.nf)))) C convs in let div = match div with | Some div -> aux 0 (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 - (* initial sanity check *) - sanity p + {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; label} ;; let solve p = - if is_stuck 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 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); - -(* Example usage of interactive: *) - -(* let interactive div conv cmds = - let p = problem_of div conv in - try ( - let p = List.fold_left (|>) p cmds in - let rec f p cmds = - let nth spl n = int_of_string (List.nth spl n) in - let read_cmd () = - let s = read_line () in - let spl = Str.split (Str.regexp " +") s in - s, let uno = List.hd spl in - try if uno = "eat" then eat - else if uno = "step" then step (nth spl 1) (nth spl 2) - else failwith "Wrong input." - with Failure s -> print_endline s; (fun x -> x) in - let str, cmd = read_cmd () in - let cmds = (" " ^ str ^ ";")::cmds in - try - let p = cmd p in f p cmds - with - | Done _ -> print_endline "Done! Commands history: "; List.iter print_endline (List.rev cmds) - in f p [] - ) with Done _ -> () -;; *) - -(* interactive "x y" - "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat] -;; *)