X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=ocaml%2Fsimple.ml;h=8e4f61946e453827b4024dccca02d6ce8ac2f19b;hb=428691717031468f6583a899fe8edbe58163428a;hp=d9bc38917910db68afe1c226d3ae153e85a4e4b4;hpb=276d4f75cbe40801f2d9faa82ac82d1c82204e55;p=fireball-separation.git diff --git a/ocaml/simple.ml b/ocaml/simple.ml index d9bc389..8e4f619 100644 --- a/ocaml/simple.ml +++ b/ocaml/simple.ml @@ -38,7 +38,7 @@ let eta_subterm u = 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 = @@ -105,17 +105,19 @@ let rec is_inert = 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 | _ -> 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 +;; + 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) @@ -128,7 +130,6 @@ let rec subst level delift sub = | B -> B and mk_app t1 t2 = if t2 = B || (t1 = delta && t2 = delta) then B else match t1 with - | C _ as t -> t | B -> B | L t1 -> subst 0 true (0, t2) t1 | _ -> A (t1, t2) @@ -144,7 +145,7 @@ and lift n = ;; let subst = subst 0 false;; -let subst_in_problem (sub: var * t) (p: problem) = +let subst_in_problem sub p = print_endline ("-- SUBST " ^ string_of_t (V (fst sub)) ^ " |-> " ^ string_of_t (snd sub)); {p with div=subst sub p.div; @@ -217,7 +218,7 @@ let find_eta_difference p t n_args = 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) + 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 @@ -230,7 +231,7 @@ let compute_max_lambdas_at hd_var 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 | V _ -> 0 @@ -246,23 +247,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, 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(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 ;; @@ -282,35 +287,6 @@ print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")") 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 problem_of div conv = - print_hline (); - let [@warning "-8"] [div; conv], var_names = parse ([div; conv]) 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 -;; - -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 rec auto p = let hd_var, n_args = get_inert p.div in @@ -332,7 +308,37 @@ let rec auto p = auto p ;; -let interactive div conv cmds = +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 + | `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 + (* initial sanity check *) + sanity p +;; + +let solve p = + if eta_subterm p.div p.conv + then print_endline "!!! div is subterm of conv. Problem was not run !!!" + else check p (auto p) +;; + +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 @@ -354,59 +360,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 div convs = - let p = problem_of div (conv_join convs) in - if eta_subterm p.div p.conv - then print_endline "!!! div is subterm of conv. Problem was not run !!!" - else check p (auto p) -;; +;; *)