]> matita.cs.unibo.it Git - fireball-separation.git/blobdiff - ocaml/simple.ml
Finish ignores rigid arguments; auto tries to diverge the arguments of an inert
[fireball-separation.git] / ocaml / simple.ml
index 274b100b8a57e6ff0740ca07ee97b9d17e601110..185c54ef2f025afeea14d38d6bfa74d2423722de 100644 (file)
@@ -1,5 +1,4 @@
 let (++) f g x = f (g x);;\r
-let id x = x;;\r
 let rec fold_nat f x n = if n = 0 then x else f (fold_nat f x (n-1)) n ;;\r
 \r
 let print_hline = Console.print_hline;;\r
@@ -23,8 +22,11 @@ let rec is_stuck = function
 ;;\r
 \r
 let eta_eq' =\r
- let rec aux l1 l2 t1 t2 = match t1, t2 with\r
-  | _, _ when is_stuck t1 || is_stuck t2 -> true\r
+ let rec aux l1 l2 t1 t2 =\r
+  let stuck1, stuck2 = is_stuck t1, is_stuck t2 in\r
+  match t1, t2 with\r
+  | _, _ when not stuck1 && stuck2 -> false\r
+  | _, _ when stuck1 -> true\r
   | L t1, L t2 -> aux l1 l2 (fst t1) (fst t2)\r
   | L t1, t2 -> aux l1 (l2+1) (fst t1) t2\r
   | t1, L t2 -> aux (l1+1) l2 t1 (fst t2)\r
@@ -70,6 +72,7 @@ let string_of_t =
 type problem = {\r
    orig_freshno: int\r
  ; freshno : int\r
+ ; label : string\r
  ; div : t\r
  ; conv : t\r
  ; sigma : (var * t) list (* substitutions *)\r
@@ -85,12 +88,21 @@ let string_of_problem p =
 \r
 exception B;;\r
 exception Done of (var * t) list (* substitution *);;\r
-exception Fail of int * string;;\r
+exception Unseparable of string;;\r
+exception Backtrack of string;;\r
+\r
+let rec try_all label f = function\r
+ | x::xs -> (try f x with Backtrack s -> (if s <> "" then print_endline ("\n<< BACKTRACK: "^s)); try_all label f xs)\r
+ | [] -> raise (Backtrack label)\r
+;;\r
+let try_both label f x g y =\r
+ try_all label (function `L x -> f x | `R y -> g y) [`L x ; `R y]\r
+;;\r
 \r
 let problem_fail p reason =\r
  print_endline "!!!!!!!!!!!!!!! FAIL !!!!!!!!!!!!!!!";\r
  print_endline (string_of_problem p);\r
- raise (Fail (-1, reason))\r
+ failwith reason\r
 ;;\r
 \r
 let freshvar ({freshno} as p) =\r
@@ -169,16 +181,22 @@ and lift n =
   | C -> C\r
  in aux 0\r
 ;;\r
-let subst = subst 0 false;;\r
+let subst' = subst;;\r
+let subst = subst' 0 false;;\r
+\r
+let rec mk_apps t = function\r
+ | u::us -> mk_apps (A(t,u)) us\r
+ | [] -> t\r
+;;\r
 \r
 let subst_in_problem ((v, t) as sub) p =\r
 print_endline ("-- SUBST " ^ string_of_t (V v) ^ " |-> " ^ string_of_t t);\r
  let sigma = sub :: p.sigma in\r
  let div, g = try subst sub p.div with B -> raise (Done sigma) in\r
- assert (g = []);\r
- let conv, f = try subst sub p.conv with B -> raise (Fail(-1, "p.conv diverged")) in\r
- assert (g = []);\r
- {p with div; conv; sigma}\r
+ let divs = div :: g in\r
+ let conv, g = try subst sub p.conv with B -> raise (Backtrack "p.conv diverged") in\r
+ let conv = if g = [] then conv else mk_apps C (conv::g) in\r
divs, {p with div; conv; sigma}\r
 ;;\r
 \r
 let get_subterms_with_head hd_var =\r
@@ -206,28 +224,23 @@ let purify =
 ;;\r
 \r
 let check p sigma =\r
- print_endline "Checking...";\r
+ print_endline "\nChecking...";\r
  let div = purify p.div in\r
  let conv = purify p.conv in\r
  let sigma = List.map (fun (v,t) -> v, purify t) sigma in\r
  let freshno = List.fold_right (max ++ fst) sigma 0 in\r
  let env = Pure.env_of_sigma freshno sigma in\r
- assert (Pure.diverged (Pure.mwhd (env,div,[])));\r
- print_endline " D diverged.";\r
- assert (not (Pure.diverged (Pure.mwhd (env,conv,[]))));\r
- print_endline " C converged.";\r
+ (if not (Pure.diverged (Pure.mwhd (env,div,[])))\r
+  then failwith "D converged in Pure");\r
+ print_endline "- D diverged.";\r
+ (if Pure.diverged (Pure.mwhd (env,conv,[]))\r
+  then failwith "C diverged in Pure");\r
+ print_endline "- C converged.";\r
  ()\r
 ;;\r
 \r
 let sanity p =\r
- print_endline (string_of_problem p); (* non cancellare *)\r
- if not (is_inert p.div) then problem_fail p "p.div converged";\r
- (* Trailing constant args can be removed because do not contribute to eta-diff *)\r
- let rec remove_trailing_constant_args = function\r
- | A(t1, t2) when is_constant t2 -> remove_trailing_constant_args t1\r
- | _ as t -> t in\r
- let p = {p with div=remove_trailing_constant_args p.div} in\r
- p\r
+ print_endline (string_of_problem p) (* non cancellare *); p\r
 ;;\r
 \r
 (* drops the arguments of t after the n-th *)\r
@@ -248,9 +261,11 @@ let inert_cut_at n t =
 let find_eta_difference p t =\r
  let divargs = args_of_inert p.div in\r
  let conargs = args_of_inert t in\r
+ let rec range i j =\r
+  if j = 0 then [] else i :: range (i+1) (j-1) in\r
  let rec aux k divargs conargs =\r
   match divargs,conargs with\r
-     [],_ -> []\r
+     [],conargs -> range k (List.length conargs)\r
    | _::_,[] -> [k]\r
    | t1::divargs,t2::conargs ->\r
       (if not (eta_eq t1 t2) then [k] else []) @ aux (k+1) divargs conargs\r
@@ -260,19 +275,17 @@ let find_eta_difference p t =
 \r
 let compute_max_lambdas_at hd_var j =\r
  let rec aux hd = function\r
- | A(t1,t2) ->\r
+ | A(t1,t2) -> max (max (aux hd t1) (aux hd t2))\r
     (if get_inert t1 = (V hd, j)\r
-      then max ( (*FIXME*)\r
-       if is_inert t2 && let hd', j' = get_inert t2 in hd' = V hd\r
-        then let hd', j' = get_inert t2 in j - j'\r
-        else no_leading_lambdas hd_var j t2)\r
-      else id) (max (aux hd t1) (aux hd t2))\r
+      then no_leading_lambdas hd (j+1) t2\r
+      else 0)\r
  | L(t,_) -> aux (hd+1) t\r
- | V _ | C -> 0\r
+ | V _\r
+ | C -> 0\r
  in aux hd_var\r
 ;;\r
 \r
-let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);;\r
+let print_cmd s1 s2 = print_endline ("\n>> " ^ s1 ^ " " ^ s2);;\r
 \r
 (* returns Some i if i is the smallest integer s.t. p holds for the i-th\r
    element of the list in input *)\r
@@ -280,7 +293,7 @@ let smallest_such_that p =
  let rec aux i =\r
   function\r
      [] -> None\r
-   | hd::_ when (print_endline (string_of_t hd) ; p hd) -> Some i\r
+   | hd::_ when p i hd -> Some i\r
    | _::tl -> aux (i+1) tl\r
  in\r
   aux 0\r
@@ -302,11 +315,11 @@ 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\r
  let t = mk_lams t (k+1) in (* make leading lambdas *)\r
  let subst = var, t in\r
- let p = subst_in_problem subst p in\r
sanity p\r
+ let divs, p = subst_in_problem subst p in\r
divs, p\r
 ;;\r
 \r
-let finish p =\r
+let finish p arity =\r
  (* one-step version of eat *)\r
  let compute_max_arity =\r
    let rec aux n = function\r
@@ -314,17 +327,19 @@ let finish p =
    | L(t,g) -> List.fold_right (max ++ (aux 0)) (t::g) 0\r
    | _ -> n\r
  in aux 0 in\r
-print_cmd "FINISH" "";\r
  (* First, a step on the last argument of the divergent.\r
     Because of the sanity check, it will never be a constant term. *)\r
  let div_hd, div_nargs = get_inert p.div in\r
- let div_hd = match div_hd with V n -> n | _ -> assert false in\r
- let j = div_nargs - 1 in\r
+ let div_hd = match div_hd with V n -> n | _ -> raise (Backtrack "Cannot finish on constant tm") in\r
+ let j = match\r
+  smallest_such_that (fun i t -> i >= arity && not (is_constant t)) (args_of_inert p.div)\r
+  with Some j -> j | None -> raise (Backtrack "") in\r
+ print_endline "\n>> FINISHING";\r
  let arity = compute_max_arity p.conv in\r
  let n = 1 + arity + max\r
   (compute_max_lambdas_at div_hd j p.div)\r
   (compute_max_lambdas_at div_hd j p.conv) in\r
- let p = step j n p in\r
+ let _, p = step j n p in\r
  (* Now, find first argument of div that is a variable never applied anywhere.\r
  It must exist because of some invariant, since we just did a step,\r
  and because of the arity of the divergent *)\r
@@ -338,102 +353,87 @@ print_cmd "FINISH" "";
  | A(t,_) -> aux (m-1) t\r
  | _ -> assert false in\r
  let m, delta_var = aux div_nargs p.div in\r
- let p = subst_in_problem (delta_var, delta) p in\r
- let p = subst_in_problem (div_hd, mk_lams delta (m-1)) p in\r
- sanity p\r
+ let _, p = subst_in_problem (delta_var, delta) p in\r
+ ignore (subst_in_problem (div_hd, mk_lams delta (m-1)) p);\r
+ assert false\r
 ;;\r
 \r
 let auto p =\r
  let rec aux p =\r
- let hd, n_args = get_inert p.div in\r
- match hd with\r
- | C | L _ | A _  -> assert false\r
- | V hd_var ->\r
- let tms = get_subterms_with_head hd_var p.conv in\r
- if List.exists (fun t -> snd (get_inert t) >= n_args) tms\r
-  then (\r
-    (* let tms = List.sort (fun t1 t2 -> - compare (snd (get_inert t1)) (snd (get_inert t2))) tms in *)\r
-    List.iter (fun t -> try\r
-      let js = find_eta_difference p t in\r
-      (* print_endline (String.concat ", " (List.map string_of_int js)); *)\r
-      if js = [] then problem_fail p "no eta difference found (div subterm of conv?)";\r
-      let js = List.rev js in\r
-       List.iter\r
-        (fun j ->\r
-         try\r
-          let k = 1 + max\r
-           (compute_max_lambdas_at hd_var j p.div)\r
-            (compute_max_lambdas_at hd_var j p.conv) in\r
-          ignore (aux (step j k p))\r
-         with Fail(_, s) ->\r
-          print_endline ("Backtracking (eta_diff) because: " ^ s)) js;\r
-       raise (Fail(-1, "no eta difference"))\r
-      with Fail(_, s) ->\r
-       print_endline ("Backtracking (get_subterms) because: " ^ s)) tms;\r
-     raise (Fail(-1, "no similar terms"))\r
-   )\r
-  else\r
-    problem_fail (finish p) "Finish did not complete the problem"\r
-  in\r
-  try\r
-   aux p\r
-  with Done sigma -> sigma\r
+ if eta_subterm p.div p.conv\r
+ then raise (Backtrack "div is subterm of conv");\r
+ match p.div with\r
+ | L _ as t -> (* case p.div is an abstraction *)\r
+    print_endline "\nSOTTO UN LAMBDA";\r
+    let t, g = mk_app t C in\r
+    aux ({p with div=mk_apps C (t::g)})\r
+ | V _ | C -> raise (Backtrack "V | C")\r
+ | A _ -> (\r
+   if is_constant p.div (* case p.div is rigid inert *)\r
+   then (print_endline "\nSOTTO UN C"; try_all "auto.C"\r
+    (fun div -> aux (sanity {p with div})) (args_of_inert p.div))\r
+   else (* case p.div is flexible inert *)\r
+    let hd, n_args = get_inert p.div in\r
+   match hd with\r
+   | C | L _ | A _  -> assert false\r
+   | V hd_var ->\r
+   let tms = get_subterms_with_head hd_var p.conv in\r
+   let arity = List.fold_right (max ++ (snd ++ get_inert)) tms 0 in\r
+   try_both "???" (finish p) arity\r
+    (fun _ ->\r
+      let jss = List.concat (List.map (find_eta_difference p) tms) in\r
+      let jss = List.sort_uniq compare jss in\r
+      let f = try_all "no differences"\r
+       (fun j ->\r
+         let k = 1 + max\r
+          (compute_max_lambdas_at hd_var j p.div)\r
+           (compute_max_lambdas_at hd_var j p.conv) in\r
+         let divs, p = step j k p in\r
+         try_all "p.div" (fun div -> aux (sanity {p with div})) divs\r
+         ) in\r
+       try_both "step, then diverge arguments"\r
+        f jss\r
+        (try_all "tried p.div arguments" (fun div -> aux {p with div})) (args_of_inert p.div)\r
+      ) ()\r
+ ) in try\r
+  aux p\r
+ with Done sigma -> sigma\r
 ;;\r
 \r
 let problem_of (label, div, convs, ps, var_names) =\r
  print_hline ();\r
  let rec aux lev = function\r
- | `Lam(_, t) -> L (aux (lev+1) t, [])\r
+ | `Lam(_, t, g) -> L (aux (lev+1) t, List.map (aux (lev+1)) g)\r
  | `I (v, args) -> Listx.fold_left (fun x y -> fst (mk_app x (aux lev y))) (aux lev (`Var v)) args\r
  | `Var(v,_) -> if v >= lev && List.nth var_names (v-lev) = "C" then C else V v\r
  | `N _ | `Match _ -> assert false in\r
  assert (List.length ps = 0);\r
  let convs = List.rev convs in\r
- let conv = List.fold_left (fun x y -> fst (mk_app x (aux 0 (y :> Num.nf)))) (V (List.length var_names)) convs in\r
- let var_names = "@" :: var_names in\r
+ let conv = List.fold_left (fun x y -> fst (mk_app x (aux 0 (y :> Num.nf)))) C convs in\r
  let div = match div with\r
  | Some div -> aux 0 (div :> Num.nf)\r
  | None -> assert false in\r
  let varno = List.length var_names in\r
- {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]}\r
+ {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; label}\r
 ;;\r
 \r
 let solve p =\r
- if is_constant p.div\r
-  then print_endline "!!! div is stuck. Problem was not run !!!"\r
- else if eta_subterm p.div p.conv\r
-  then print_endline "!!! div is subterm of conv. Problem was not run !!!"\r
-  else let p = sanity p (* initial sanity check *) in check p (auto p)\r
+ let c = if String.length p.label > 0 then String.sub (p.label) 0 1 else "" in\r
+ let module M = struct exception Okay end in\r
+ try\r
+  if eta_subterm p.div p.conv\r
+  then raise (Unseparable "div is subterm of conv")\r
+  else\r
+   let p = sanity p (* initial sanity check *) in\r
+   check p (auto p);\r
+   raise M.Okay\r
+ with\r
+  | M.Okay -> if c = "?" then\r
+    failwith "The problem succeeded, but was supposed to be unseparable"\r
+  | e when c = "!" ->\r
+    failwith ("The problem was supposed to be separable, but: "^Printexc.to_string e)\r
+  | e ->\r
+    print_endline ("The problem failed, as expected ("^Printexc.to_string e^")")\r
 ;;\r
 \r
 Problems.main (solve ++ problem_of);\r
-\r
-(* Example usage of interactive: *)\r
-\r
-(* let interactive div conv cmds =\r
- let p = problem_of div conv in\r
- try (\r
- let p = List.fold_left (|>) p cmds in\r
- let rec f p cmds =\r
-  let nth spl n = int_of_string (List.nth spl n) in\r
-  let read_cmd () =\r
-   let s = read_line () in\r
-   let spl = Str.split (Str.regexp " +") s in\r
-   s, let uno = List.hd spl in\r
-    try if uno = "eat" then eat\r
-    else if uno = "step" then step (nth spl 1) (nth spl 2)\r
-    else failwith "Wrong input."\r
-    with Failure s -> print_endline s; (fun x -> x) in\r
-  let str, cmd = read_cmd () in\r
-  let cmds = (" " ^ str ^ ";")::cmds in\r
-  try\r
-   let p = cmd p in f p cmds\r
-  with\r
-  | Done _ -> print_endline "Done! Commands history: "; List.iter print_endline (List.rev cmds)\r
- in f p []\r
- ) with Done _ -> ()\r
-;; *)\r
-\r
-(* interactive "x y"\r
- "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat]\r
-;; *)\r