]> matita.cs.unibo.it Git - fireball-separation.git/blobdiff - ocaml/simple.ml
Step possible beyond the args of p.div
[fireball-separation.git] / ocaml / simple.ml
index 897d6b68a4b736952774d4127e109020f2fb7618..079774e5ac67e5c53feaa53eca9e7e622dd77c76 100644 (file)
@@ -10,11 +10,11 @@ type var = int;;
 type t =\r
  | V of var\r
  | A of t * t\r
- | L of t\r
+ | L of (t * t list (*garbage*))\r
  | C (* constant *)\r
 ;;\r
 \r
-let delta = L(A(V 0, V 0));;\r
+let delta = L(A(V 0, V 0),[]);;\r
 \r
 let rec is_stuck = function\r
  | C -> true\r
@@ -25,9 +25,9 @@ let rec is_stuck = function
 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
-  | L t1, L t2 -> aux l1 l2 t1 t2\r
-  | L t1, t2 -> aux l1 (l2+1) t1 t2\r
-  | t1, L t2 -> aux (l1+1) l2 t1 t2\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
   | V a, V b -> a + l1 = b + l2\r
   | A(t1,t2), A(u1,u2) -> aux l1 l2 t1 u1 && aux l1 l2 t2 u2\r
   | _, _ -> false\r
@@ -37,14 +37,14 @@ let eta_eq = eta_eq' 0 0;;
 (* is arg1 eta-subterm of arg2 ? *)\r
 let eta_subterm u =\r
  let rec aux lev t = if t = C then false else (eta_eq' lev 0 u t || match t with\r
- | L t -> aux (lev+1) t\r
+ | L(t,g) -> List.exists (aux (lev+1)) (t::g)\r
  | A(t1, t2) -> aux lev t1 || aux lev t2\r
  | _ -> false) in\r
  aux 0\r
 ;;\r
 \r
 (* does NOT lift the argument *)\r
-let mk_lams = fold_nat (fun x _ -> L x) ;;\r
+let mk_lams = fold_nat (fun x _ -> L(x,[])) ;;\r
 \r
 let string_of_t =\r
   let string_of_bvar =\r
@@ -61,7 +61,8 @@ let string_of_t =
     | A(t1,t2) -> string_of_term_no_pars_app level t1 ^ " " ^ string_of_term_w_pars level t2\r
     | _ as t -> string_of_term_w_pars level t\r
   and string_of_term_no_pars level = function\r
-    | L t -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t\r
+    | L(t,g) -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t\r
+       ^ (if g = [] then "" else String.concat ", " ("" :: List.map (string_of_term_w_pars (level+1)) g))\r
     | _ as t -> string_of_term_no_pars_app level t\r
   in string_of_term_no_pars 0\r
 ;;\r
@@ -69,6 +70,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
@@ -84,12 +86,18 @@ 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 _ -> try_all label f xs)\r
+ | [] -> raise (Backtrack label)\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
@@ -111,7 +119,7 @@ let rec is_constant =
     C -> true\r
   | V _ -> false\r
   | A(t,_)\r
-  | L t -> is_constant t\r
+  | L(t,_) -> is_constant t\r
 ;;\r
 \r
 let rec get_inert = function\r
@@ -133,7 +141,7 @@ let args_of_inert =
 (* precomputes the number of leading lambdas in a term,\r
    after replacing _v_ w/ a term starting with n lambdas *)\r
 let rec no_leading_lambdas v n = function\r
- | L t -> 1 + no_leading_lambdas (v+1) n t\r
+ | L(t,_) -> 1 + no_leading_lambdas (v+1) n t\r
  | A _ as t -> let v', m = get_inert t in if V v = v' then max 0 (n - m) else 0\r
  | V v' -> if v = v' then n else 0\r
  | C -> 0\r
@@ -141,22 +149,29 @@ let rec no_leading_lambdas v n = function
 \r
 let rec subst level delift sub =\r
  function\r
- | V v -> if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v)\r
- | L t -> let t = subst (level + 1) delift sub t in if t = B then B else L t\r
+ | V v -> (if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v)), []\r
+ | L x -> let t, g = subst_in_lam (level+1) delift sub x in L(t, g), []\r
  | A (t1,t2) ->\r
-  let t1 = subst level delift sub t1 in\r
-  let t2 = subst level delift sub t2 in\r
-  mk_app t1 t2\r
- | C -> C\r
+  let t1, g1 = subst level delift sub t1 in\r
+  let t2, g2 = subst level delift sub t2 in\r
+  let t3, g3 = mk_app t1 t2 in\r
+  t3, g1 @ g2 @ g3\r
+ | C -> C, []\r
+and subst_in_lam level delift sub (t, g) =\r
+  let t', g' = subst level delift sub t in\r
+  let g'' = List.fold_left\r
+   (fun xs t ->\r
+     let x,y = subst level delift sub t in\r
+     (x :: y @ xs)) g' g in t', g''\r
 and mk_app t1 t2 = if t1 = delta && t2 = delta then raise B\r
  else match t1 with\r
- | L t1 -> subst 0 true (0, t2) t1\r
- | _ -> A (t1, t2)\r
+ | L x -> subst_in_lam 0 true (0, t2) x\r
+ | _ -> A (t1, t2), []\r
 and lift n =\r
  let rec aux lev =\r
   function\r
   | V m -> V (if m >= lev then m + n else m)\r
-  | L t -> L (aux (lev+1) t)\r
+  | L(t,g) -> L (aux (lev+1) t, List.map (aux (lev+1)) g)\r
   | A (t1, t2) -> A (aux lev t1, aux lev t2)\r
   | C -> C\r
  in aux 0\r
@@ -165,29 +180,36 @@ let subst = subst 0 false;;
 \r
 let subst_in_problem ((v, t) as sub) p =\r
 print_endline ("-- SUBST " ^ string_of_t (V v) ^ " |-> " ^ string_of_t t);\r
- {p with\r
-  div=subst sub p.div;\r
-  conv=subst sub p.conv;\r
-  sigma=sub::p.sigma}\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 (Backtrack "p.conv diverged") in\r
+ assert (g = []);\r
+ {p with div; conv; sigma}\r
 ;;\r
 \r
 let get_subterms_with_head hd_var =\r
- let rec aux lev inert_done = function\r
- | L t -> aux (lev+1) false t\r
- | C | V _ -> []\r
+ let rec aux lev inert_done = function\r
+ | L(t,g') -> List.fold_left (aux (lev+1) false) g (t::g')\r
+ | C | V _ -> g\r
  | A(t1,t2) as t ->\r
    let hd_var', n_args' = get_inert t1 in\r
    if not inert_done && hd_var' = V (hd_var + lev)\r
-    then lift ~-lev t :: aux lev true t1 @ aux lev false t2\r
-    else aux lev true t1 @ aux lev false t2\r
- in aux 0 false\r
+    then lift ~-lev t :: aux lev false (aux lev true g t1) t2\r
+    else                 aux lev false (aux lev true g t1) t2\r
+ in aux 0 false []\r
 ;;\r
 \r
-let rec purify = function\r
- | L t -> Pure.L (purify t)\r
- | A (t1,t2) -> Pure.A (purify t1, purify t2)\r
- | V n -> Pure.V n\r
+let purify =\r
+ let rec aux = function\r
+ | L(t,g) ->\r
+    let t = aux (lift (List.length g) t) in\r
+    let t = List.fold_left (fun t g -> Pure.A(Pure.L t, aux g)) t g in\r
+    Pure.L t\r
+ | A (t1,t2) -> Pure.A (aux t1, aux t2)\r
+ | V n -> Pure.V (n)\r
  | C -> Pure.V (min_int/2)\r
+ in aux\r
 ;;\r
 \r
 let check p sigma =\r
@@ -197,16 +219,18 @@ let check p sigma =
  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
+ if not (is_inert p.div) then raise (Backtrack "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
@@ -233,9 +257,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 = -1 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
@@ -252,7 +278,7 @@ let compute_max_lambdas_at hd_var j =
         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
- | L t -> aux (hd+1) t\r
+ | L(t,_) -> aux (hd+1) t\r
  | V _ | C -> 0\r
  in aux hd_var\r
 ;;\r
@@ -296,7 +322,7 @@ let finish p =
  let compute_max_arity =\r
    let rec aux n = function\r
    | A(t1,t2) -> max (aux (n+1) t1) (aux 0 t2)\r
-   | L t -> max n (aux 0 t)\r
+   | L(t,g) -> List.fold_right (max ++ (aux 0)) (t::g) 0\r
    | _ -> n\r
  in aux 0 in\r
 print_cmd "FINISH" "";\r
@@ -338,24 +364,16 @@ let auto p =
  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
+    try_all "no similar terms" (fun t ->\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
+       try_all "no eta difference"\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
+          aux (step j k p)) js) tms\r
    )\r
   else\r
     problem_fail (finish p) "Finish did not complete the problem"\r
@@ -368,7 +386,7 @@ let auto p =
 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
@@ -380,15 +398,26 @@ let problem_of (label, div, convs, ps, var_names) =
  | 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