]> matita.cs.unibo.it Git - fireball-separation.git/blobdiff - ocaml/andrea.ml
Restored good check for Done
[fireball-separation.git] / ocaml / andrea.ml
index 8535988b7c48fc490342951fd68691f460897811..41b7372b471e12e87d7514a286b917c730fcf478 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
@@ -107,7 +112,7 @@ let rec subst level delift sub =
   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
+and mk_app t1 t2 = if t1 = delta && t2 = delta then B else match t1 with\r
  | B | _ when t2 = B -> B\r
  | L t1 -> subst 0 true (0, t2) t1\r
  | t1 -> A (t1, t2)\r
@@ -145,22 +150,95 @@ 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
+ 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
@@ -196,7 +274,7 @@ let problem_of div conv =
  print_hline ();\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
+ 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
@@ -209,50 +287,17 @@ let exec div conv cmds =
  | Done _ -> ()\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 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
@@ -291,7 +336,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