]> matita.cs.unibo.it Git - fireball-separation.git/blobdiff - ocaml/andrea.ml
Made bombs more explosive (strongly)
[fireball-separation.git] / ocaml / andrea.ml
index bd2ad0919520b15bebb73d849d81413afc02488c..07cf670c134c75c5787c332302ca1b8c265fa186 100644 (file)
@@ -4,6 +4,8 @@ let rec fold_nat f x n = if n = 0 then x else f (fold_nat f x (n-1)) n ;;
 \r
 let print_hline = Console.print_hline;;\r
 \r
+open Pure\r
+\r
 type var = int;;\r
 type t =\r
  | V of var\r
@@ -12,6 +14,8 @@ type t =
  | B (* bottom *)\r
 ;;\r
 \r
+let delta = L(A(V 0, V 0));;\r
+\r
 let eta_eq =\r
  let rec aux l1 l2 t1 t2 = match t1, t2 with\r
   | L t1, L t2 -> aux l1 l2 t1 t2\r
@@ -53,6 +57,7 @@ type problem = {
  ; conv : t\r
  ; sigma : (var * t) list (* substitutions *)\r
  ; stepped : var list\r
+ ; phase : [`One | `Two] (* :'( *)\r
 }\r
 \r
 let string_of_problem p =\r
@@ -87,6 +92,11 @@ let rec is_inert =
 let is_var = function V _ -> true | _ -> false;;\r
 let is_lambda = function L _ -> true | _ -> false;;\r
 \r
+let rec no_leading_lambdas = function\r
+ | L t -> 1 + no_leading_lambdas t\r
+ | _ -> 0\r
+;;\r
+\r
 let rec get_inert = function\r
  | V n -> (n,0)\r
  | A(t, _) -> let hd,args = get_inert t in hd,args+1\r
@@ -96,16 +106,17 @@ let rec get_inert = function
 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 -> L (subst (level + 1) delift sub t)\r
+ | L t -> let t = subst (level + 1) delift sub t in if t = B then B else L t\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
  | B -> B\r
-and mk_app t1 t2 = match t1 with\r
- | B | _ when t2 = B -> B\r
+and mk_app t1 t2 = if t2 = B || (t1 = delta && t2 = delta) then B\r
+ else match t1 with\r
+ | B -> B\r
  | L t1 -> subst 0 true (0, t2) t1\r
- | t1 -> A (t1, t2)\r
+ | _ -> A (t1, t2)\r
 and lift n =\r
  let rec aux lev =\r
   function\r
@@ -140,22 +151,96 @@ let get_subterm_with_head_and_args hd_var n_args =
  in aux 0\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
+ | B -> Pure.B\r
+;;\r
+\r
+let check p sigma =\r
+ print_endline "Checking...";\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 (fun (x,_) -> max x) sigma 0 in\r
+ let env = Pure.env_of_sigma freshno sigma in\r
+ assert (Pure.diverged (Pure.mwhd (env,div,[])));\r
+ assert (not (Pure.diverged (Pure.mwhd (env,conv,[]))));\r
+ ()\r
+;;\r
+\r
 let sanity p =\r
  print_endline (string_of_problem p); (* non cancellare *)\r
  if p.conv = B then problem_fail p "p.conv diverged";\r
  if p.div = B then raise (Done p.sigma);\r
+ if p.phase = `Two && p.div = delta then raise (Done p.sigma);\r
  if not (is_inert p.div) then problem_fail p "p.div converged"\r
 ;;\r
 \r
+(* drops the arguments of t after the n-th *)\r
+let inert_cut_at n t =\r
+ let rec aux t =\r
+  match t with\r
+  | V _ as t -> 0, t\r
+  | A(t1,_) as t ->\r
+    let k', t' = aux t1 in\r
+     if k' = n then n, t'\r
+      else k'+1, t\r
+  | _ -> assert false\r
+ in snd (aux t)\r
+;;\r
+\r
+let find_eta_difference p t n_args =\r
+ let t = inert_cut_at n_args t in\r
+ let rec aux t u k = match t, u with\r
+ | V _, V _ -> assert false (* div subterm of conv *)\r
+ | A(t1,t2), A(u1,u2) ->\r
+    if not (eta_eq t2 u2) then (print_endline((string_of_t t2) ^ " <> " ^ (string_of_t u2)); k)\r
+    else aux t1 u1 (k-1)\r
+ | _, _ -> assert false\r
+ in aux p.div t n_args\r
+;;\r
+\r
+let compute_max_lambdas_at hd_var j =\r
+ let rec aux hd = function\r
+ | A(t1,t2) ->\r
+    (if get_inert t1 = (hd, j)\r
+      then max ( (*FIXME*)\r
+       if is_inert t2 && let hd', j' = get_inert t2 in hd' = hd\r
+        then let hd', j' = get_inert t2 in j - j'\r
+        else no_leading_lambdas t2)\r
+      else id) (max (aux hd t1) (aux hd t2))\r
+ | L t -> aux (hd+1) t\r
+ | V _ -> 0\r
+ | _ -> assert false\r
+ in aux hd_var\r
+;;\r
+\r
 let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);;\r
 \r
 (* eat the arguments of the divergent and explode.\r
  It does NOT perform any check, may fail if done unsafely *)\r
 let eat p =\r
 print_cmd "EAT" "";\r
- let var, n = get_inert p.div in\r
- let subst = var, mk_lams B n in\r
+ let var, k = get_inert p.div in\r
+ let phase = p.phase in\r
+ let p, t =\r
+  match phase with\r
+  | `One ->\r
+      let n = 1 + max\r
+       (compute_max_lambdas_at var k p.div)\r
+       (compute_max_lambdas_at var k p.conv) in\r
+      (* apply fresh vars *)\r
+      let p, t = fold_nat (fun (p, t) _ ->\r
+        let p, v = freshvar p in\r
+        p, A(t, V (v + k))\r
+      ) (p, V 0) n in\r
+      let p = {p with phase=`Two} in p, A(t, delta)\r
+  | `Two -> p, delta in\r
+ let subst = var, mk_lams t k in\r
  let p = subst_in_problem subst p in\r
+ let p = if phase = `One then {p with div = (match p.div with A(t,_) -> t | _ -> assert false)} else p in\r
  sanity p; p\r
 ;;\r
 \r
@@ -182,22 +267,16 @@ let parse strs =
   | Parser_andrea.App (t1, t2) ->\r
    if level = 0 then mk_app (aux level t1) (aux level t2)\r
     else A(aux level t1, aux level t2)\r
-  | Parser_andrea.Var v -> V v\r
-  in let (tms, free) = Parser_andrea.parse_many strs\r
-  in (List.map (aux 0) tms, free)\r
+  | Parser_andrea.Var v -> V v in\r
+  let (tms, free) = Parser_andrea.parse_many strs in\r
+  (List.map (aux 0) tms, free)\r
 ;;\r
 \r
 let problem_of div conv =\r
  print_hline ();\r
- let all_tms, var_names = parse ([div; conv]) in\r
- let div, conv = List.hd all_tms, List.hd (List.tl all_tms) in\r
+ let [@warning "-8"] [div; conv], var_names = parse ([div; conv]) in\r
  let varno = List.length var_names in\r
- let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; stepped=[]} in\r
- (* activate bombs *)\r
- let p = try\r
-  let subst = Util.index_of "BOMB" var_names, L B in\r
-   subst_in_problem subst p\r
-  with Not_found -> p in\r
+ let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; stepped=[]; phase=`One} in\r
  (* initial sanity check *)\r
  sanity p; p\r
 ;;\r
@@ -210,54 +289,17 @@ let exec div conv cmds =
  | Done _ -> ()\r
 ;;\r
 \r
-let inert_cut_at n t =\r
- let rec aux t =\r
-  match t with\r
-  | V _ as t -> 0, t\r
-  | A(t1,_) as t ->\r
-    let k', t' = aux t1 in\r
-     if k' = n then n, t'\r
-      else k'+1, t\r
-  | _ -> assert false\r
- in snd (aux t)\r
-;;\r
-\r
-let find_eta_difference p t n_args =\r
- let t = inert_cut_at n_args t in\r
- let rec aux t u k = match t, u with\r
- | V _, V _ -> assert false (* div subterm of conv *)\r
- | A(t1,t2), A(u1,u2) ->\r
-    if not (eta_eq t2 u2) then (print_endline((string_of_t t2) ^ " <> " ^ (string_of_t u2)); k)\r
-    else aux t1 u1 (k-1)\r
- | _, _ -> assert false\r
- in aux p.div t n_args\r
-;;\r
-\r
-let rec no_leading_lambdas = function\r
- | L t -> 1 + no_leading_lambdas t\r
- | _ -> 0\r
-;;\r
-\r
-let compute_max_lambdas_at hd_var j =\r
- let rec aux hd = function\r
- | A(t1,t2) ->\r
-    (if get_inert t1 = (hd, j)\r
-      then max ( (*FIXME*)\r
-       if is_inert t2 && let hd', j' = get_inert t2 in hd' = hd\r
-        then let hd', j' = get_inert t2 in j - j'\r
-        else no_leading_lambdas t2)\r
-      else id) (max (aux hd t1) (aux hd t2))\r
- | L t -> aux (hd+1) t\r
- | V _ -> 0\r
- | _ -> assert false\r
- in aux hd_var\r
-;;\r
-\r
 let rec auto p =\r
  let hd_var, n_args = get_inert p.div in\r
  match get_subterm_with_head_and_args hd_var n_args p.conv with\r
  | None ->\r
-    (try problem_fail (eat p) "Auto did not complete the problem"  with Done _ -> ())\r
+   (try\r
+    let phase = p.phase in\r
+     let p = eat p in\r
+     if phase = `Two\r
+      then problem_fail p "Auto.2 did not complete the problem"\r
+      else auto p\r
+    with Done sigma -> sigma)\r
  | Some t ->\r
   let j = find_eta_difference p t n_args - 1 in\r
   let k = 1 + max\r
@@ -296,7 +338,11 @@ let rec conv_join = function
  | x::xs -> conv_join xs ^ " ("^ x ^")"\r
 ;;\r
 \r
-let auto' a b = auto (problem_of a (conv_join b));;\r
+let auto' a b =\r
+ let p = problem_of a (conv_join b) in\r
+ let sigma = auto p in\r
+ check p sigma\r
+;;\r
 \r
 (* Example usage of exec, interactive:\r
 \r