X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=ocaml%2Fsimple.ml;h=bac3ddf98a532784a587fa90cd9cbd67ca1f1e0a;hb=2b941d27823cb7d06bbf83353c21779c866f3161;hp=8e4f61946e453827b4024dccca02d6ce8ac2f19b;hpb=5664c5924f59c805c6e658698cc2fa535cab27f6;p=fireball-separation.git diff --git a/ocaml/simple.ml b/ocaml/simple.ml index 8e4f619..bac3ddf 100644 --- a/ocaml/simple.ml +++ b/ocaml/simple.ml @@ -12,18 +12,24 @@ type t = | A of t * t | L of t | B (* bottom *) - | C of int + | C (* constant *) ;; let delta = L(A(V 0, V 0));; +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 ;; @@ -31,11 +37,11 @@ let eta_eq = eta_eq' 0 0;; (* is arg1 eta-subterm of arg2 ? *) let eta_subterm u = - let rec aux lev t = eta_eq' lev 0 u t || match t with + 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 + | _ -> false) in + aux 0 ;; (* does NOT lift the argument *) @@ -49,7 +55,7 @@ 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" @@ -98,24 +104,23 @@ let rec is_inert = function | A(t,_) -> is_inert t | V _ -> true - | C _ + | C | L _ | B -> false ;; -let is_var = function V _ -> true | _ -> false;; -let is_lambda = function L _ -> true | _ -> false;; - 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 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 = v' then max 0 (n - m) else 0 + | V v' -> if v = v' then n else 0 + | B | C -> 0 ;; let rec subst level delift sub = @@ -126,8 +131,7 @@ 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 + | C | B as t -> t and mk_app t1 t2 = if t2 = B || (t1 = delta && t2 = delta) then B else match t1 with | B -> B @@ -139,41 +143,37 @@ 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 | B as t -> t 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)); +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; + stepped=v::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 + | C | V _ | B -> [] + | L t -> aux (lev+1) false 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' - 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 *) + | C -> Pure.V (min_int/2) | B -> Pure.B ;; @@ -182,7 +182,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."; @@ -201,6 +201,8 @@ let sanity 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,28 +215,33 @@ 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 _ -> [] | A(t1,t2), A(u1,u2) -> - if not (eta_eq t2 u2) then ((*print_endline((string_of_t t2) ^ " <> " ^ (string_of_t u2));*) k) + print_endline (string_of_t t2 ^ " vs " ^ string_of_t u2); + if not (eta_eq t2 u2) then (k-1)::aux t1 u1 (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 = 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 hd_var j t2) else id) (max (aux hd t1) (aux hd t2)) | L t -> aux (hd+1) t - | V _ -> 0 + | V _ | C -> 0 | _ -> assert false in aux hd_var ;; @@ -246,6 +253,9 @@ let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);; 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 @@ -273,8 +283,11 @@ print_cmd "EAT" ""; (* 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 _ | B | 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 @@ -288,39 +301,60 @@ 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 - | None -> - (try - let phase = p.phase in +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 n_args 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 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 + else aux p) + in + try + aux p + with Done sigma -> sigma ;; let problem_of (label, div, convs, ps, var_names) = print_hline (); - let rec aux = function - | `Lam(_, t) -> L (aux t) - | `I ((v,_), args) -> Listx.fold_left (fun x y -> mk_app x (aux y)) (V v) args - | `Var(v,_) -> V v + 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 + | `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 (y :> Num.nf))) (V (List.length var_names)) 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 div = match div with - | Some div -> aux (div :> Num.nf) + | 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 @@ -329,7 +363,8 @@ let problem_of (label, div, convs, ps, var_names) = ;; let solve p = - if eta_subterm p.div p.conv + 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) ;;