X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;ds=sidebyside;f=ocaml%2Fsimple.ml;h=897d6b68a4b736952774d4127e109020f2fb7618;hb=69f44e4fb3d56f89adcc952514707914886fdd79;hp=534100d4f0787961fb688461dd34f52d82314bed;hpb=1e41340acff1c4e4439b44ce5437542c905c352f;p=fireball-separation.git diff --git a/ocaml/simple.ml b/ocaml/simple.ml index 534100d..897d6b6 100644 --- a/ocaml/simple.ml +++ b/ocaml/simple.ml @@ -11,25 +11,39 @@ type t = | V of var | A of t * t | L of t - | B (* bottom *) - | C of int + | C (* constant *) ;; let delta = L(A(V 0, V 0));; -let eta_eq = +let rec is_stuck = function + | C -> true + | A(t,_) -> is_stuck t + | _ -> false +;; + +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 | 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 0 0 + in aux ;; +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 + | A(t1, t2) -> aux lev t1 || aux lev t2 + | _ -> false) in + aux 0 ;; -(* does NOT lift t *) +(* does NOT lift the argument *) let mk_lams = fold_nat (fun x _ -> L x) ;; let string_of_t = @@ -40,10 +54,9 @@ 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 + | 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 @@ -59,19 +72,17 @@ 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;; @@ -85,28 +96,49 @@ 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 + | C + | L _ -> false ;; -let is_var = function V _ -> true | _ -> false;; -let is_lambda = function L _ -> true | _ -> false;; - -let rec no_leading_lambdas = function - | L t -> 1 + no_leading_lambdas t - | _ -> 0 +let rec is_constant = + function + C -> true + | V _ -> false + | A(t,_) + | L t -> is_constant t ;; let rec get_inert = function - | V n -> (n,0) + | V _ | C as t -> (t,0) | A(t, _) -> let hd,args = get_inert t in hd,args+1 | _ -> assert false ;; +let args_of_inert = + let rec aux acc = + function + | V _ | C -> acc + | A(t, a) -> aux (a::acc) t + | _ -> assert false + in + aux [] +;; + +(* 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 = v' then max 0 (n - m) else 0 + | V v' -> if v = v' then n else 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) @@ -115,12 +147,9 @@ let rec subst level delift sub = 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 + | C -> C +and mk_app t1 t2 = if t1 = delta && t2 = delta then raise B else match t1 with - | C _ as t -> t - | B -> B | L t1 -> subst 0 true (0, t2) t1 | _ -> A (t1, t2) and lift n = @@ -129,42 +158,36 @@ 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 + | C -> C in aux 0 ;; let subst = subst 0 false;; -let subst_in_problem (sub: var * t) (p: problem) = -print_endline ("-- SUBST " ^ string_of_t (V (fst sub)) ^ " |-> " ^ string_of_t (snd sub)); +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=(fst sub)::p.stepped; sigma=sub::p.sigma} ;; -let get_subterm_with_head_and_args hd_var n_args = - let rec aux lev = function - | C _ - | V _ | B -> None - | L t -> aux (lev+1) t +let get_subterms_with_head hd_var = + let rec aux lev inert_done = function + | L t -> aux (lev+1) false t + | C | V _ -> [] | 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' - then Some (lift ~-lev t) - else match aux lev t2 with - | None -> aux lev t1 - | Some _ as res -> res - in aux 0 + 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 ;; 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 + | C -> Pure.V (min_int/2) ;; let check p sigma = @@ -172,7 +195,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."; @@ -183,10 +206,13 @@ 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" + if not (is_inert p.div) then problem_fail p "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 + | _ as t -> t in + let p = {p with div=remove_trailing_constant_args p.div} in + p ;; (* drops the arguments of t after the n-th *) @@ -202,64 +228,56 @@ 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 - let rec aux t u k = match t, u with - | V _, V _ -> assert false (* 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) - else aux t1 u1 (k-1) - | _, _ -> assert false - in aux p.div t n_args +(* return the index of the first argument with a difference + (the first argument is 0) *) +let find_eta_difference p t = + let divargs = args_of_inert p.div in + let conargs = args_of_inert t in + let rec aux k divargs conargs = + match divargs,conargs with + [],_ -> [] + | _::_,[] -> [k] + | t1::divargs,t2::conargs -> + (if not (eta_eq t1 t2) then [k] else []) @ aux (k+1) divargs conargs + in + aux 0 divargs conargs ;; let compute_max_lambdas_at hd_var j = let rec aux hd = function | A(t1,t2) -> - (if get_inert t1 = (hd, j) + (if get_inert t1 = (V hd, j) then max ( (*FIXME*) - if is_inert t2 && let hd', j' = get_inert t2 in hd' = hd + 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 t2) + else no_leading_lambdas hd_var j t2) else id) (max (aux hd t1) (aux hd t2)) | L t -> aux (hd+1) t - | V _ -> 0 - | _ -> assert false + | V _ | C -> 0 in aux hd_var ;; let print_cmd s1 s2 = print_endline (">> " ^ 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 - let phase = p.phase in - let p, t = - match phase with - | `One -> - let n = 1 + max - (compute_max_lambdas_at var k p.div) - (compute_max_lambdas_at var k 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 p, A(t, delta) - | `Two -> p, delta in - let subst = var, mk_lams t k in - let p = subst_in_problem subst p in - sanity p; - let p = if phase = `One then {p with div = (match p.div with A(t,_) -> t | _ -> assert false)} else p in - sanity p; 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 (print_endline (string_of_t hd) ; p 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 var, _ = get_inert p.div in -print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")"); + let hd, _ = get_inert p.div in + match hd with + | 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 *) fold_nat (fun (p, t) _ -> let p, v = freshvar p in @@ -270,58 +288,114 @@ print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")") 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; p + sanity p ;; -let parse strs = - let rec aux level = function - | Parser_andrea.Lam t -> L (aux (level + 1) t) - | Parser_andrea.App (t1, t2) -> - if level = 0 then mk_app (aux level t1) (aux level t2) - else A(aux level t1, aux level t2) - | Parser_andrea.Var v -> V v in - let (tms, free) = Parser_andrea.parse_many strs in - (List.map (aux 0) tms, free) +let finish p = + (* 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 -> max n (aux 0 t) + | _ -> n + in aux 0 in +print_cmd "FINISH" ""; + (* 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 | _ -> assert false in + let j = div_nargs - 1 in + 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 + let p = subst_in_problem (div_hd, mk_lams delta (m-1)) p in + sanity p ;; -let problem_of div conv = +let auto p = + let rec aux p = + 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 + 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 + problem_fail (finish p) "Finish did not complete the problem" + in + try + aux p + with Done sigma -> sigma +;; + +let problem_of (label, div, convs, ps, var_names) = print_hline (); - let [@warning "-8"] [div; conv], var_names = parse ([div; conv]) in + let rec aux lev = function + | `Lam(_, t) -> L (aux (lev+1) t, []) + | `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 -> fst (mk_app x (aux 0 (y :> Num.nf)))) (V (List.length var_names)) convs in + let var_names = "@" :: var_names 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; p + {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]} ;; -let exec div conv cmds = - let p = problem_of div conv in - try - problem_fail (List.fold_left (|>) p cmds) "Problem not completed" - with - | Done _ -> () +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 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 - | None -> - (try - 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 auto p - with Done sigma -> sigma) - | Some t -> - let j = find_eta_difference p t n_args - 1 in - let k = 1 + max - (compute_max_lambdas_at hd_var j p.div) - (compute_max_lambdas_at hd_var j p.conv) in - let p = step j k p in - auto p -;; +Problems.main (solve ++ problem_of); + +(* Example usage of interactive: *) -let interactive div conv cmds = +(* let interactive div conv cmds = let p = problem_of div conv in try ( let p = List.fold_left (|>) p cmds in @@ -343,54 +417,8 @@ let interactive div conv cmds = | Done _ -> print_endline "Done! Commands history: "; List.iter print_endline (List.rev cmds) in f p [] ) with Done _ -> () -;; - -let rec conv_join = function - | [] -> "@" - | x::xs -> conv_join xs ^ " ("^ x ^")" -;; - -let auto' a b = - let p = problem_of a (conv_join b) in - let sigma = auto p in - check p sigma -;; - -(* Example usage of exec, interactive: - -exec - "x x" - (conv_join["x y"; "y y"; "y x"]) - [ step 0 1; eat ] -;; +;; *) -interactive "x y" +(* interactive "x y" "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat] -;; - -*) - -auto' "x x" ["x y"; "y y"; "y x"] ;; -auto' "x y" ["x (_. x)"; "y z"; "y x"] ;; -auto' "a (x. x b) (x. x c)" ["a (x. b b) @"; "a @ c"; "a (x. x x) a"; "a (a a a) (a c c)"] ;; - -auto' "x (y. x y y)" ["x (y. x y x)"] ;; - -auto' "x a a a a" [ - "x b a a a"; - "x a b a a"; - "x a a b a"; - "x a a a b"; -] ;; - -(* Controesempio ad usare un conto dei lambda che non considere le permutazioni *) -auto' "x a a a a (x (x. x x) @ @ (_._.x. x x) x) b b b" [ - "x a a a a (_. a) b b b"; - "x a a a a (_. _. _. _. x. y. x y)"; -] ;; - - -print_hline(); -print_endline "ALL DONE. " - -let solve = auto';; +;; *)