]> matita.cs.unibo.it Git - fireball-separation.git/blobdiff - ocaml/simple.ml
Embarassing bug in eta_eq fixed
[fireball-separation.git] / ocaml / simple.ml
index 28603258c04539410f98afb02d2ac301f97c793c..bfb5ebd71c50d076f070c432aec10451d12cc420 100644 (file)
@@ -11,36 +11,8 @@ type t =
  | V of var\r
  | A of t * t\r
  | L of t\r
- | B (* bottom *)\r
- | C of int\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
-  | L t1, t2 -> aux l1 (l2+1) t1 t2\r
-  | t1, L t2 -> aux (l1+1) l2 t1 t2\r
-  | V a, V b -> a + l1 = b + l2\r
-  | C a, C b -> a = b\r
-  | A(t1,t2), A(u1,u2) -> aux l1 l2 t1 u1 && aux l1 l2 t2 u2\r
-  | _, _ -> false\r
- in aux ;;\r
-let eta_eq = eta_eq' 0 0;;\r
-\r
-(* is arg1 eta-subterm of arg2 ? *)\r
-let eta_subterm u =\r
- let rec aux lev t = eta_eq' lev 0 u t || match t with\r
- | L t -> aux (lev+1) t\r
- | A(t1, t2) -> aux lev t1 || aux lev t2\r
- | _ -> false\r
- in aux 0\r
-;;\r
-\r
-(* does NOT lift the argument *)\r
-let mk_lams = fold_nat (fun x _ -> L x) ;;\r
-\r
 let string_of_t =\r
   let string_of_bvar =\r
    let bound_vars = ["x"; "y"; "z"; "w"; "q"] in\r
@@ -49,10 +21,8 @@ let string_of_t =
   let rec string_of_term_w_pars level = function\r
     | V v -> if v >= level then "`" ^ string_of_int (v-level) else\r
        string_of_bvar (level - v-1)\r
-    | C n -> "c" ^ string_of_int n\r
     | A _\r
     | L _ as t -> "(" ^ string_of_term_no_pars level t ^ ")"\r
-    | B -> "BOT"\r
   and string_of_term_no_pars_app level = function\r
     | 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
@@ -62,25 +32,30 @@ let string_of_t =
   in string_of_term_no_pars 0\r
 ;;\r
 \r
+\r
+let delta = L(A(V 0, V 0));;\r
+\r
+(* does NOT lift the argument *)\r
+let mk_lams = fold_nat (fun x _ -> L x) ;;\r
+\r
 type problem = {\r
    orig_freshno: int\r
  ; freshno : int\r
  ; div : t\r
  ; 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
  let lines = [\r
-  "[stepped] " ^ String.concat " " (List.map string_of_int p.stepped);\r
   "[DV] " ^ string_of_t p.div;\r
   "[CV] " ^ string_of_t p.conv;\r
  ] in\r
  String.concat "\n" lines\r
 ;;\r
 \r
+exception B;;\r
 exception Done of (var * t) list (* substitution *);;\r
 exception Fail of int * string;;\r
 \r
@@ -98,38 +73,36 @@ let rec is_inert =
  function\r
  | A(t,_) -> is_inert t\r
  | V _ -> true\r
- | C _\r
- | L _ | B -> false\r
+ | L _ -> false\r
 ;;\r
 \r
 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
  | _ -> assert false\r
 ;;\r
 \r
+(* 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
+ | A _ as t -> let v', m = get_inert t in if v = v' then max 0 (n - m) else 0\r
+ | V v' -> if v = v' then n else 0\r
+;;\r
+\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
+ | L t -> L (subst (level + 1) delift sub 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
- | C _ as t -> t\r
- | B -> B\r
-and mk_app t1 t2 = if t2 = B || (t1 = delta && t2 = delta) then B\r
+and mk_app t1 t2 = if t1 = delta && t2 = delta then raise B\r
  else match t1 with\r
- | C _ as t -> t\r
- | B -> B\r
  | L t1 -> subst 0 true (0, t2) t1\r
  | _ -> A (t1, t2)\r
 and lift n =\r
@@ -138,29 +111,45 @@ and lift n =
   | V m -> V (if m >= lev then m + n else m)\r
   | L t -> L (aux (lev+1) t)\r
   | A (t1, t2) -> A (aux lev t1, aux lev t2)\r
-  | C _ as t -> t\r
-  | B -> B\r
  in aux 0\r
 ;;\r
 let subst = subst 0 false;;\r
 \r
-let subst_in_problem (sub: var * t) (p: problem) =\r
-print_endline ("-- SUBST " ^ string_of_t (V (fst sub)) ^ " |-> " ^ string_of_t (snd sub));\r
- {p with\r
-  div=subst sub p.div;\r
-  conv=subst sub p.conv;\r
-  stepped=(fst sub)::p.stepped;\r
-  sigma=sub::p.sigma}\r
+let eta_eq =\r
+ let rec aux t1 t2 = match t1, t2 with\r
+  | L t1, L t2 -> aux t1 t2\r
+  | L t1, t2 -> aux t1 (A(lift 1 t2,V 0))\r
+  | t1, L t2 -> aux (A(lift 1 t1,V 0)) t2\r
+  | V a, V b -> a = b\r
+  | A(t1,t2), A(u1,u2) -> aux t1 u1 && aux t2 u2\r
+  | _, _ -> false\r
+ in aux ;;\r
+\r
+(* is arg1 eta-subterm of arg2 ? *)\r
+let eta_subterm u =\r
+ let rec aux lev t = eta_eq u (lift lev t) || match t with\r
+ | L t -> aux (lev+1) t\r
+ | A(t1, t2) -> aux lev t1 || aux lev t2\r
+ | _ -> false\r
+ in aux 0\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 = try subst sub p.div with B -> raise (Done sigma) in\r
+ let conv = try subst sub p.conv with B -> raise (Fail(-1,"p.conv diverged")) in\r
+ {p with div; conv; sigma}\r
 ;;\r
 \r
 let get_subterm_with_head_and_args hd_var n_args =\r
  let rec aux lev = function\r
- | C _\r
- | V _ | B -> None\r
+ | V _ -> None\r
  | L t -> aux (lev+1) t\r
  | A(t1,t2) as t ->\r
    let hd_var', n_args' = get_inert t1 in\r
    if hd_var' = hd_var + lev && n_args <= 1 + n_args'\r
+    (* the `+1` above is because of t2 *)\r
     then Some (lift ~-lev t)\r
     else match aux lev t2 with\r
     | None -> aux lev t1\r
@@ -172,8 +161,6 @@ let rec purify = function
  | L t -> Pure.L (purify t)\r
  | A (t1,t2) -> Pure.A (purify t1, purify t2)\r
  | V n -> Pure.V n\r
- | C _ -> Pure.V max_int (* FIXME *)\r
- | B -> Pure.B\r
 ;;\r
 \r
 let check p sigma =\r
@@ -181,7 +168,7 @@ let check p sigma =
  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 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
@@ -192,14 +179,14 @@ let check p sigma =
 \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
  p\r
 ;;\r
 \r
 (* drops the arguments of t after the n-th *)\r
+(* FIXME! E' usato in modo improprio contando sul fatto\r
+   errato che ritorna un inerte lungo esattamente n *)\r
 let inert_cut_at n t =\r
  let rec aux t =\r
   match t with\r
@@ -212,15 +199,24 @@ let inert_cut_at n t =
  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
+(* return the index of the first argument with a difference\r
+   (the first argument is 0)\r
+   precondition: p.div and t have n+1 arguments\r
+   *)\r
+let find_eta_difference p t argsno =\r
+ let t = inert_cut_at argsno t in\r
  let rec aux t u k = match t, u with\r
- | V _, V _ -> assert false (* div subterm of conv *)\r
+ | V _, V _ -> None\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
+    (match aux t1 u1 (k-1) with\r
+    | None ->\r
+      if not (eta_eq t2 u2) then Some (k-1)\r
+      else None\r
+    | Some j -> Some j)\r
  | _, _ -> assert false\r
- in aux p.div t n_args\r
+ in match aux p.div t argsno with\r
+ | None -> problem_fail p "no eta difference found (div subterm of conv?)"\r
+ | Some j -> j\r
 ;;\r
 \r
 let compute_max_lambdas_at hd_var j =\r
@@ -230,11 +226,10 @@ let compute_max_lambdas_at hd_var j =
       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 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
  | V _ -> 0\r
- | _ -> assert false\r
  in aux hd_var\r
 ;;\r
 \r
@@ -246,23 +241,27 @@ let eat p =
 print_cmd "EAT" "";\r
  let var, k = get_inert p.div in\r
  let phase = p.phase in\r
- let p, t =\r
+ let p =\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
+       (compute_max_lambdas_at var (k-1) p.div)\r
+       (compute_max_lambdas_at var (k-1) 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
- sanity p;\r
- let p = if phase = `One then {p with div = (match p.div with A(t,_) -> t | _ -> assert false)} else p in\r
+      let p = {p with phase=`Two} in\r
+      let t = A(t, delta) in\r
+      let t = fold_nat (fun t m -> A(t, V (k-m))) t (k-1) in\r
+      let subst = var, mk_lams t k in\r
+      let p = subst_in_problem subst p in\r
+      let _, args = get_inert p.div in\r
+      {p with div = inert_cut_at (args-k) p.div}\r
+  | `Two ->\r
+      let subst = var, mk_lams delta k in\r
+      subst_in_problem subst p in\r
  sanity p\r
 ;;\r
 \r
@@ -275,25 +274,13 @@ print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")")
     let p, v = freshvar p in\r
     p, A(t, V (v + k + 1))\r
   ) (p, V 0) n in\r
- let t = (* apply unused bound variables V_{k-1}..V_1 *)\r
-  fold_nat (fun t m -> A(t, V (k-m+1))) t k in\r
+ let t = (* apply bound variables V_k..V_0 *)\r
+  fold_nat (fun t m -> A(t, V (k-m+1))) t (k+1) 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
 ;;\r
-;;\r
-\r
-let parse strs =\r
-  let rec aux level = function\r
-  | Parser_andrea.Lam t -> L (aux (level + 1) t)\r
-  | 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 in\r
-  let (tms, free) = Parser_andrea.parse_many strs in\r
-  (List.map (aux 0) tms, free)\r
-;;\r
 \r
 let rec auto p =\r
  let hd_var, n_args = get_inert p.div in\r
@@ -307,7 +294,7 @@ let rec auto p =
       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 j = find_eta_difference p t n_args in\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
@@ -315,15 +302,22 @@ let rec auto p =
   auto p\r
 ;;\r
 \r
-let problem_of div convs =\r
- let rec conv_join = function\r
-  | [] -> "@"\r
-  | x::xs -> conv_join xs ^ " ("^ x ^")" in\r
+let problem_of (label, div, convs, ps, var_names) =\r
  print_hline ();\r
- let conv = conv_join convs in\r
- let [@warning "-8"] [div; conv], var_names = parse ([div; conv]) in\r
+ let rec aux = function\r
+ | `Lam(_, t) -> L (aux t)\r
+ | `I ((v,_), args) -> Listx.fold_left (fun x y -> mk_app x (aux y)) (V v) args\r
+ | `Var(v,_) -> 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 -> mk_app x (aux (y :> Num.nf))) (V (List.length var_names)) convs in\r
+ let var_names = "@" :: var_names in\r
+ let div = match div with\r
+ | Some div -> aux (div :> Num.nf)\r
+ | None -> assert false in\r
  let varno = List.length var_names in\r
- let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; stepped=[]; phase=`One} in\r
+ let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; phase=`One} in\r
  (* initial sanity check *)\r
  sanity p\r
 ;;\r
@@ -334,7 +328,7 @@ let solve p =
   else check p (auto p)\r
 ;;\r
 \r
-let run x y = solve (problem_of x y);;\r
+Problems.main (solve ++ problem_of);\r
 \r
 (* Example usage of interactive: *)\r
 \r
@@ -365,25 +359,3 @@ let run x y = solve (problem_of x y);;
 (* interactive "x y"\r
  "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat]\r
 ;; *)\r
-\r
-run "x x" ["x y"; "y y"; "y x"] ;;\r
-run "x y" ["x (_. x)"; "y z"; "y x"] ;;\r
-run "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)"] ;;\r
-\r
-run "x (y. x y y)" ["x (y. x y x)"] ;;\r
-\r
-run "x a a a a" [\r
- "x b a a a";\r
- "x a b a a";\r
- "x a a b a";\r
- "x a a a b";\r
-] ;;\r
-\r
-(* Controesempio ad usare un conto dei lambda che non considere le permutazioni *)\r
-run "x a a a a (x (x. x x) @ @ (_._.x. x x) x) b b b" [\r
- "x a a a a (_. a) b b b";\r
- "x a a a a (_. _. _. _. x. y. x y)";\r
-] ;;\r
-\r
-print_hline();\r
-print_endline ">>> EXAMPLES IN simple.ml FINISHED <<<"\r