X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=ocaml%2Fsimple.ml;h=4e93965e029a606419b3984a2af9e36d4792b5f8;hb=4d85faf22614fae57d4b0c45a5699258a9274272;hp=28603258c04539410f98afb02d2ac301f97c793c;hpb=62d2aea15e05a7366e0bbdc71574ad50cda28ff5;p=fireball-separation.git diff --git a/ocaml/simple.ml b/ocaml/simple.ml index 2860325..4e93965 100644 --- a/ocaml/simple.ml +++ b/ocaml/simple.ml @@ -9,38 +9,16 @@ open Pure type var = int;; type t = | V of var - | A of t * t - | L of t - | B (* bottom *) - | C of int + | A of bool * t * t + | L of (bool * t) ;; -let delta = L(A(V 0, V 0));; - -let eta_eq' = - let rec aux l1 l2 t1 t2 = match t1, t2 with - | 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 ;; -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 - | L t -> aux (lev+1) t - | A(t1, t2) -> aux lev t1 || aux lev t2 - | _ -> false - in aux 0 +let rec measure_of_t = function + | V _ -> 0 + | A(b,t1,t2) -> (if b then 1 else 0) + measure_of_t t1 + measure_of_t t2 + | L(b,t) -> if b then measure_of_t t else 0 ;; -(* does NOT lift the argument *) -let mk_lams = fold_nat (fun x _ -> L x) ;; - let string_of_t = let string_of_bvar = let bound_vars = ["x"; "y"; "z"; "w"; "q"] in @@ -49,38 +27,42 @@ 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 + | A(b,t1,t2) -> string_of_term_no_pars_app level t1 ^ (if b then "," else " ") ^ 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) -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t | _ as t -> string_of_term_no_pars_app level t in string_of_term_no_pars 0 ;; + +let delta = L(true,A(true,V 0, V 0));; + +(* does NOT lift the argument *) +let mk_lams = fold_nat (fun x _ -> L(false,x)) ;; + type problem = { orig_freshno: int ; freshno : int ; 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); + "[measure] " ^ string_of_int (measure_of_t p.div); "[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;; @@ -96,71 +78,87 @@ let freshvar ({freshno} as p) = let rec is_inert = function - | A(t,_) -> is_inert t + | A(_,t,_) -> is_inert t | V _ -> true - | C _ - | L _ | B -> false + | 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 get_inert = function | V n -> (n,0) - | A(t, _) -> let hd,args = get_inert t in hd,args+1 + | A(_,t,_) -> let hd,args = get_inert t in hd,args+1 | _ -> assert false ;; -let rec subst level delift sub = +(* 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 +;; + +(* b' defaults to false *) +let rec subst b' 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 - | 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 + | L(b,t) -> L(b, subst b' (level + 1) delift sub t) + | A(_,t1,(V v as t2)) when b' && v = level + fst sub -> + mk_app b' (subst b' level delift sub t1) (subst b' level delift sub t2) + | A(b,t1,t2) -> + mk_app b (subst b' level delift sub t1) (subst b' level delift sub t2) +and mk_app b' 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) + | L(b,t1) -> subst (b' && not b) 0 true (0, t2) t1 + | _ -> A (b', 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) - | A (t1, t2) -> A (aux lev t1, aux lev t2) - | C _ as t -> t - | B -> B + | L(b,t) -> L(b,aux (lev+1) t) + | A (b,t1, t2) -> A (b,aux lev t1, aux lev t2) 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)); - {p with - div=subst sub p.div; - conv=subst sub p.conv; - stepped=(fst sub)::p.stepped; - sigma=sub::p.sigma} +let subst = subst false 0 false;; +let mk_app = mk_app true;; + +let eta_eq = + let rec aux t1 t2 = match t1, t2 with + | L(_,t1), L(_,t2) -> aux t1 t2 + | L(_,t1), t2 -> aux t1 (A(true,lift 1 t2,V 0)) + | t1, L(_,t2) -> aux (A(true,lift 1 t1,V 0)) t2 + | V a, V b -> a = b + | A(_,t1,t2), A(_,u1,u2) -> aux t1 u1 && aux t2 u2 + | _, _ -> false + in aux ;; + +(* is arg1 eta-subterm of arg2 ? *) +let eta_subterm u = + let rec aux lev t = eta_eq u (lift lev t) || match t with + | L(_, t) -> aux (lev+1) t + | A(_, t1, t2) -> aux lev t1 || aux lev t2 + | _ -> false + in aux 0 +;; + +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 - | L t -> aux (lev+1) t - | A(t1,t2) as t -> + | 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 @@ -169,11 +167,9 @@ let get_subterm_with_head_and_args hd_var n_args = ;; let rec purify = function - | L t -> Pure.L (purify t) - | A (t1,t2) -> Pure.A (purify t1, purify t2) + | 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 = @@ -181,7 +177,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."; @@ -192,19 +188,19 @@ 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 | V _ as t -> 0, t - | A(t1,_) as t -> + | A(_,t1,_) as t -> let k', t' = aux t1 in if k' = n then n, t' else k'+1, t @@ -212,29 +208,37 @@ 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 *) - | 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) + | V _, V _ -> None + | A(_,t1,t2), A(_,u1,u2) -> + (match aux t1 u1 (k-1) with + | None -> + if not (eta_eq t2 u2) then Some (k-1) + else None + | Some j -> Some j) | _, _ -> assert false - in aux p.div t n_args + in match aux p.div t argsno with + | None -> problem_fail p "no eta difference found (div subterm of conv?)" + | Some j -> j ;; let compute_max_lambdas_at hd_var j = let rec aux hd = function - | A(t1,t2) -> + | A(_,t1,t2) -> (if get_inert t1 = (hd, j) then max ( (*FIXME*) if is_inert t2 && let hd', j' = get_inert t2 in hd' = 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 + | L(_,t) -> aux (hd+1) t | V _ -> 0 - | _ -> assert false in aux hd_var ;; @@ -246,23 +250,27 @@ let eat p = print_cmd "EAT" ""; let var, k = get_inert p.div in let phase = p.phase in - let p, t = + let p = 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 + (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, A(false, 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 + let p = {p with phase=`Two} in + let t = A(false, t, delta) in + let t = fold_nat (fun t m -> A(false, 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 ;; @@ -273,32 +281,52 @@ print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")") let p, t = (* apply fresh vars *) fold_nat (fun (p, t) _ -> let p, v = freshvar p in - p, A(t, V (v + k + 1)) + p, A(false, t, V (v + k + 1)) ) (p, V 0) n in - let t = (* apply unused bound variables V_{k-1}..V_1 *) - fold_nat (fun t m -> A(t, V (k-m+1))) t k in + let t = (* apply bound variables V_k..V_0 *) + fold_nat (fun t m -> A(false, t, V (k-m+1))) t (k+1) 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 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 = + 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) + | V _ -> n + in aux 0 in +print_cmd "FINISH" ""; + let div_hd, div_nargs = get_inert p.div 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 + let div_hd, div_nargs = get_inert p.div in + let rec aux m = function + A(_,t1,t2) -> if is_var t2 then + (let delta_var, _ = get_inert t2 in + if delta_var <> div_hd && get_subterm_with_head_and_args delta_var 1 p.conv = None + then m, delta_var + else aux (m-1) t1) else aux (m-1) t1 + | _ -> 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 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 problem_fail (finish p) "Auto.2 did not complete the problem" + with Done sigma -> sigma) + (* (try let phase = p.phase in let p = eat p in @@ -306,24 +334,37 @@ let rec auto p = 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 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 + let m1 = measure_of_t p.div in let p = step j k p in + let m2 = measure_of_t p.div in + (if m2 >= m1 then + (print_string "WARNING! Measure did not decrease (press )"; + ignore(read_line()))); auto p ;; -let problem_of div convs = - let rec conv_join = function - | [] -> "@" - | x::xs -> conv_join xs ^ " ("^ x ^")" in +let problem_of (label, div, convs, ps, var_names) = print_hline (); - let conv = conv_join convs in - let [@warning "-8"] [div; conv], var_names = parse ([div; conv]) in + let rec aux = function + | `Lam(_, t) -> L (true,aux t) + | `I ((v,_), args) -> Listx.fold_left (fun x y -> mk_app x (aux y)) (V v) args + | `Var(v,_) -> 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 var_names = "@" :: var_names in + let div = match div with + | 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 ;; @@ -334,7 +375,7 @@ let solve p = else check p (auto p) ;; -let run x y = solve (problem_of x y);; +Problems.main (solve ++ problem_of); (* Example usage of interactive: *) @@ -365,25 +406,3 @@ let run x y = solve (problem_of x y);; (* interactive "x y" "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat] ;; *) - -run "x x" ["x y"; "y y"; "y x"] ;; -run "x y" ["x (_. x)"; "y z"; "y x"] ;; -run "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)"] ;; - -run "x (y. x y y)" ["x (y. x y x)"] ;; - -run "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 *) -run "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 ">>> EXAMPLES IN simple.ml FINISHED <<<"