]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/simple.ml
aa91996027a513fc5f8eaface109bdd8d0bf9600
[fireball-separation.git] / ocaml / simple.ml
1 let (++) f g x = f (g x);;\r
2 let id x = x;;\r
3 let rec fold_nat f x n = if n = 0 then x else f (fold_nat f x (n-1)) n ;;\r
4 \r
5 let print_hline = Console.print_hline;;\r
6 \r
7 open Pure\r
8 \r
9 type var = int;;\r
10 type t =\r
11  | V of var\r
12  | A of t * t\r
13  | L of t\r
14 ;;\r
15 \r
16 let delta = L(A(V 0, V 0));;\r
17 \r
18 let eta_eq' =\r
19  let rec aux l1 l2 t1 t2 = match t1, t2 with\r
20   | L t1, L t2 -> aux l1 l2 t1 t2\r
21   | L t1, t2 -> aux l1 (l2+1) t1 t2\r
22   | t1, L t2 -> aux (l1+1) l2 t1 t2\r
23   | V a, V b -> a + l1 = b + l2\r
24   | A(t1,t2), A(u1,u2) -> aux l1 l2 t1 u1 && aux l1 l2 t2 u2\r
25   | _, _ -> false\r
26  in aux ;;\r
27 let eta_eq = eta_eq' 0 0;;\r
28 \r
29 (* is arg1 eta-subterm of arg2 ? *)\r
30 let eta_subterm u =\r
31  let rec aux lev t = eta_eq' lev 0 u t || match t with\r
32  | L t -> aux (lev+1) t\r
33  | A(t1, t2) -> aux lev t1 || aux lev t2\r
34  | _ -> false\r
35  in aux 0\r
36 ;;\r
37 \r
38 (* does NOT lift the argument *)\r
39 let mk_lams = fold_nat (fun x _ -> L x) ;;\r
40 \r
41 let string_of_t =\r
42   let string_of_bvar =\r
43    let bound_vars = ["x"; "y"; "z"; "w"; "q"] in\r
44    let bvarsno = List.length bound_vars in\r
45    fun nn -> if nn < bvarsno then List.nth bound_vars nn else "x" ^ (string_of_int (nn - bvarsno + 1)) in\r
46   let rec string_of_term_w_pars level = function\r
47     | V v -> if v >= level then "`" ^ string_of_int (v-level) else\r
48        string_of_bvar (level - v-1)\r
49     | A _\r
50     | L _ as t -> "(" ^ string_of_term_no_pars level t ^ ")"\r
51   and string_of_term_no_pars_app level = function\r
52     | A(t1,t2) -> string_of_term_no_pars_app level t1 ^ " " ^ string_of_term_w_pars level t2\r
53     | _ as t -> string_of_term_w_pars level t\r
54   and string_of_term_no_pars level = function\r
55     | L t -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t\r
56     | _ as t -> string_of_term_no_pars_app level t\r
57   in string_of_term_no_pars 0\r
58 ;;\r
59 \r
60 type problem = {\r
61    orig_freshno: int\r
62  ; freshno : int\r
63  ; div : t\r
64  ; conv : t\r
65  ; sigma : (var * t) list (* substitutions *)\r
66  ; phase : [`One | `Two] (* :'( *)\r
67 }\r
68 \r
69 let string_of_problem p =\r
70  let lines = [\r
71   "[DV] " ^ string_of_t p.div;\r
72   "[CV] " ^ string_of_t p.conv;\r
73  ] in\r
74  String.concat "\n" lines\r
75 ;;\r
76 \r
77 exception B;;\r
78 exception Done of (var * t) list (* substitution *);;\r
79 exception Fail of int * string;;\r
80 \r
81 let problem_fail p reason =\r
82  print_endline "!!!!!!!!!!!!!!! FAIL !!!!!!!!!!!!!!!";\r
83  print_endline (string_of_problem p);\r
84  raise (Fail (-1, reason))\r
85 ;;\r
86 \r
87 let freshvar ({freshno} as p) =\r
88  {p with freshno=freshno+1}, freshno+1\r
89 ;;\r
90 \r
91 let rec is_inert =\r
92  function\r
93  | A(t,_) -> is_inert t\r
94  | V _ -> true\r
95  | L _ -> false\r
96 ;;\r
97 \r
98 let is_var = function V _ -> true | _ -> false;;\r
99 let is_lambda = function L _ -> true | _ -> false;;\r
100 \r
101 let rec get_inert = function\r
102  | V n -> (n,0)\r
103  | A(t, _) -> let hd,args = get_inert t in hd,args+1\r
104  | _ -> assert false\r
105 ;;\r
106 \r
107 (* precomputes the number of leading lambdas in a term,\r
108    after replacing _v_ w/ a term starting with n lambdas *)\r
109 let rec no_leading_lambdas v n = function\r
110  | L t -> 1 + no_leading_lambdas (v+1) n t\r
111  | A _ as t -> let v', m = get_inert t in if v = v' then max 0 (n - m) else 0\r
112  | V v' -> if v = v' then n else 0\r
113 ;;\r
114 \r
115 let rec subst level delift sub =\r
116  function\r
117  | V v -> if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v)\r
118  | L t -> L (subst (level + 1) delift sub t)\r
119  | A (t1,t2) ->\r
120   let t1 = subst level delift sub t1 in\r
121   let t2 = subst level delift sub t2 in\r
122   mk_app t1 t2\r
123 and mk_app t1 t2 = if t1 = delta && t2 = delta then raise B\r
124  else match t1 with\r
125  | L t1 -> subst 0 true (0, t2) t1\r
126  | _ -> A (t1, t2)\r
127 and lift n =\r
128  let rec aux lev =\r
129   function\r
130   | V m -> V (if m >= lev then m + n else m)\r
131   | L t -> L (aux (lev+1) t)\r
132   | A (t1, t2) -> A (aux lev t1, aux lev t2)\r
133  in aux 0\r
134 ;;\r
135 let subst = subst 0 false;;\r
136 \r
137 let subst_in_problem ((v, t) as sub) p =\r
138 print_endline ("-- SUBST " ^ string_of_t (V v) ^ " |-> " ^ string_of_t t);\r
139  let sigma = sub::p.sigma in\r
140  let div = try subst sub p.div with B -> raise (Done sigma) in\r
141  let conv = try subst sub p.conv with B -> raise (Fail(-1,"p.conv diverged")) in\r
142  {p with div; conv; sigma}\r
143 ;;\r
144 \r
145 let get_subterm_with_head_and_args hd_var n_args =\r
146  let rec aux lev = function\r
147  | V _ -> None\r
148  | L t -> aux (lev+1) t\r
149  | A(t1,t2) as t ->\r
150    let hd_var', n_args' = get_inert t1 in\r
151    if hd_var' = hd_var + lev && n_args <= 1 + n_args'\r
152     (* the `+1` above is because of t2 *)\r
153     then Some (lift ~-lev t)\r
154     else match aux lev t2 with\r
155     | None -> aux lev t1\r
156     | Some _ as res -> res\r
157  in aux 0\r
158 ;;\r
159 \r
160 let rec purify = function\r
161  | L t -> Pure.L (purify t)\r
162  | A (t1,t2) -> Pure.A (purify t1, purify t2)\r
163  | V n -> Pure.V n\r
164 ;;\r
165 \r
166 let check p sigma =\r
167  print_endline "Checking...";\r
168  let div = purify p.div in\r
169  let conv = purify p.conv in\r
170  let sigma = List.map (fun (v,t) -> v, purify t) sigma in\r
171  let freshno = List.fold_right (max ++ fst) sigma 0 in\r
172  let env = Pure.env_of_sigma freshno sigma in\r
173  assert (Pure.diverged (Pure.mwhd (env,div,[])));\r
174  print_endline " D diverged.";\r
175  assert (not (Pure.diverged (Pure.mwhd (env,conv,[]))));\r
176  print_endline " C converged.";\r
177  ()\r
178 ;;\r
179 \r
180 let sanity p =\r
181  print_endline (string_of_problem p); (* non cancellare *)\r
182  if p.phase = `Two && p.div = delta then raise (Done p.sigma);\r
183  if not (is_inert p.div) then problem_fail p "p.div converged";\r
184  p\r
185 ;;\r
186 \r
187 (* drops the arguments of t after the n-th *)\r
188 (* FIXME! E' usato in modo improprio contando sul fatto\r
189    errato che ritorna un inerte lungo esattamente n *)\r
190 let inert_cut_at n t =\r
191  let rec aux t =\r
192   match t with\r
193   | V _ as t -> 0, t\r
194   | A(t1,_) as t ->\r
195     let k', t' = aux t1 in\r
196      if k' = n then n, t'\r
197       else k'+1, t\r
198   | _ -> assert false\r
199  in snd (aux t)\r
200 ;;\r
201 \r
202 (* return the index of the first argument with a difference\r
203    (the first argument is 0)\r
204    precondition: p.div and t have n+1 arguments\r
205    *)\r
206 let find_eta_difference p t argsno =\r
207  let t = inert_cut_at argsno t in\r
208  let rec aux t u k = match t, u with\r
209  | V _, V _ -> None\r
210  | A(t1,t2), A(u1,u2) ->\r
211     (match aux t1 u1 (k-1) with\r
212     | None ->\r
213       if not (eta_eq t2 u2) then Some (k-1)\r
214       else None\r
215     | Some j -> Some j)\r
216  | _, _ -> assert false\r
217  in match aux p.div t argsno with\r
218  | None -> problem_fail p "no eta difference found (div subterm of conv?)"\r
219  | Some j -> j\r
220 ;;\r
221 \r
222 let compute_max_lambdas_at hd_var j =\r
223  let rec aux hd = function\r
224  | A(t1,t2) ->\r
225     (if get_inert t1 = (hd, j)\r
226       then max ( (*FIXME*)\r
227        if is_inert t2 && let hd', j' = get_inert t2 in hd' = hd\r
228         then let hd', j' = get_inert t2 in j - j'\r
229         else no_leading_lambdas hd_var j t2)\r
230       else id) (max (aux hd t1) (aux hd t2))\r
231  | L t -> aux (hd+1) t\r
232  | V _ -> 0\r
233  in aux hd_var\r
234 ;;\r
235 \r
236 let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);;\r
237 \r
238 (* eat the arguments of the divergent and explode.\r
239  It does NOT perform any check, may fail if done unsafely *)\r
240 let eat p =\r
241 print_cmd "EAT" "";\r
242  let var, k = get_inert p.div in\r
243  let phase = p.phase in\r
244  let p =\r
245   match phase with\r
246   | `One ->\r
247       let n = 1 + max\r
248        (compute_max_lambdas_at var (k-1) p.div)\r
249        (compute_max_lambdas_at var (k-1) p.conv) in\r
250       (* apply fresh vars *)\r
251       let p, t = fold_nat (fun (p, t) _ ->\r
252         let p, v = freshvar p in\r
253         p, A(t, V (v + k))\r
254       ) (p, V 0) n in\r
255       let p = {p with phase=`Two} in\r
256       let t = A(t, delta) in\r
257       let t = fold_nat (fun t m -> A(t, V (k-m))) t (k-1) in\r
258       let subst = var, mk_lams t k in\r
259       let p = subst_in_problem subst p in\r
260       let _, args = get_inert p.div in\r
261       {p with div = inert_cut_at (args-k) p.div}\r
262   | `Two ->\r
263       let subst = var, mk_lams delta k in\r
264       subst_in_problem subst p in\r
265  sanity p\r
266 ;;\r
267 \r
268 (* step on the head of div, on the k-th argument, with n fresh vars *)\r
269 let step k n p =\r
270  let var, _ = get_inert p.div in\r
271 print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")");\r
272  let p, t = (* apply fresh vars *)\r
273   fold_nat (fun (p, t) _ ->\r
274     let p, v = freshvar p in\r
275     p, A(t, V (v + k + 1))\r
276   ) (p, V 0) n in\r
277  let t = (* apply bound variables V_k..V_0 *)\r
278   fold_nat (fun t m -> A(t, V (k-m+1))) t (k+1) in\r
279  let t = mk_lams t (k+1) in (* make leading lambdas *)\r
280  let subst = var, t in\r
281  let p = subst_in_problem subst p in\r
282  sanity p\r
283 ;;\r
284 \r
285 let rec auto p =\r
286  let hd_var, n_args = get_inert p.div in\r
287  match get_subterm_with_head_and_args hd_var n_args p.conv with\r
288  | None ->\r
289    (try\r
290     let phase = p.phase in\r
291      let p = eat p in\r
292      if phase = `Two\r
293       then problem_fail p "Auto.2 did not complete the problem"\r
294       else auto p\r
295     with Done sigma -> sigma)\r
296  | Some t ->\r
297   let j = find_eta_difference p t n_args in\r
298   let k = 1 + max\r
299    (compute_max_lambdas_at hd_var j p.div)\r
300     (compute_max_lambdas_at hd_var j p.conv) in\r
301   let p = step j k p in\r
302   auto p\r
303 ;;\r
304 \r
305 let problem_of (label, div, convs, ps, var_names) =\r
306  print_hline ();\r
307  let rec aux = function\r
308  | `Lam(_, t) -> L (aux t)\r
309  | `I ((v,_), args) -> Listx.fold_left (fun x y -> mk_app x (aux y)) (V v) args\r
310  | `Var(v,_) -> V v\r
311  | `N _ | `Match _ -> assert false in\r
312  assert (List.length ps = 0);\r
313  let convs = List.rev convs in\r
314  let conv = List.fold_left (fun x y -> mk_app x (aux (y :> Num.nf))) (V (List.length var_names)) convs in\r
315  let var_names = "@" :: var_names in\r
316  let div = match div with\r
317  | Some div -> aux (div :> Num.nf)\r
318  | None -> assert false in\r
319  let varno = List.length var_names in\r
320  let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; phase=`One} in\r
321  (* initial sanity check *)\r
322  sanity p\r
323 ;;\r
324 \r
325 let solve p =\r
326  if eta_subterm p.div p.conv\r
327   then print_endline "!!! div is subterm of conv. Problem was not run !!!"\r
328   else check p (auto p)\r
329 ;;\r
330 \r
331 Problems.main (solve ++ problem_of);\r
332 \r
333 (* Example usage of interactive: *)\r
334 \r
335 (* let interactive div conv cmds =\r
336  let p = problem_of div conv in\r
337  try (\r
338  let p = List.fold_left (|>) p cmds in\r
339  let rec f p cmds =\r
340   let nth spl n = int_of_string (List.nth spl n) in\r
341   let read_cmd () =\r
342    let s = read_line () in\r
343    let spl = Str.split (Str.regexp " +") s in\r
344    s, let uno = List.hd spl in\r
345     try if uno = "eat" then eat\r
346     else if uno = "step" then step (nth spl 1) (nth spl 2)\r
347     else failwith "Wrong input."\r
348     with Failure s -> print_endline s; (fun x -> x) in\r
349   let str, cmd = read_cmd () in\r
350   let cmds = (" " ^ str ^ ";")::cmds in\r
351   try\r
352    let p = cmd p in f p cmds\r
353   with\r
354   | Done _ -> print_endline "Done! Commands history: "; List.iter print_endline (List.rev cmds)\r
355  in f p []\r
356  ) with Done _ -> ()\r
357 ;; *)\r
358 \r
359 (* interactive "x y"\r
360  "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat]\r
361 ;; *)\r