]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/simple.ml
d9bc38917910db68afe1c226d3ae153e85a4e4b4
[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  | B (* bottom *)\r
15  | C of int\r
16 ;;\r
17 \r
18 let delta = L(A(V 0, V 0));;\r
19 \r
20 let eta_eq' =\r
21  let rec aux l1 l2 t1 t2 = match t1, t2 with\r
22   | L t1, L t2 -> aux l1 l2 t1 t2\r
23   | L t1, t2 -> aux l1 (l2+1) t1 t2\r
24   | t1, L t2 -> aux (l1+1) l2 t1 t2\r
25   | V a, V b -> a + l1 = b + l2\r
26   | C a, C b -> a = b\r
27   | A(t1,t2), A(u1,u2) -> aux l1 l2 t1 u1 && aux l1 l2 t2 u2\r
28   | _, _ -> false\r
29  in aux ;;\r
30 let eta_eq = eta_eq' 0 0;;\r
31 \r
32 (* is arg1 eta-subterm of arg2 ? *)\r
33 let eta_subterm u =\r
34  let rec aux lev t = eta_eq' lev 0 u t || match t with\r
35  | L t -> aux (lev+1) t\r
36  | A(t1, t2) -> aux lev t1 || aux lev t2\r
37  | _ -> false\r
38  in aux 0\r
39 ;;\r
40 \r
41 (* does NOT lift t *)\r
42 let mk_lams = fold_nat (fun x _ -> L x) ;;\r
43 \r
44 let string_of_t =\r
45   let string_of_bvar =\r
46    let bound_vars = ["x"; "y"; "z"; "w"; "q"] in\r
47    let bvarsno = List.length bound_vars in\r
48    fun nn -> if nn < bvarsno then List.nth bound_vars nn else "x" ^ (string_of_int (nn - bvarsno + 1)) in\r
49   let rec string_of_term_w_pars level = function\r
50     | V v -> if v >= level then "`" ^ string_of_int (v-level) else\r
51        string_of_bvar (level - v-1)\r
52     | C n -> "c" ^ string_of_int n\r
53     | A _\r
54     | L _ as t -> "(" ^ string_of_term_no_pars level t ^ ")"\r
55     | B -> "BOT"\r
56   and string_of_term_no_pars_app level = function\r
57     | A(t1,t2) -> string_of_term_no_pars_app level t1 ^ " " ^ string_of_term_w_pars level t2\r
58     | _ as t -> string_of_term_w_pars level t\r
59   and string_of_term_no_pars level = function\r
60     | L t -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t\r
61     | _ as t -> string_of_term_no_pars_app level t\r
62   in string_of_term_no_pars 0\r
63 ;;\r
64 \r
65 type problem = {\r
66    orig_freshno: int\r
67  ; freshno : int\r
68  ; div : t\r
69  ; conv : t\r
70  ; sigma : (var * t) list (* substitutions *)\r
71  ; stepped : var list\r
72  ; phase : [`One | `Two] (* :'( *)\r
73 }\r
74 \r
75 let string_of_problem p =\r
76  let lines = [\r
77   "[stepped] " ^ String.concat " " (List.map string_of_int p.stepped);\r
78   "[DV] " ^ string_of_t p.div;\r
79   "[CV] " ^ string_of_t p.conv;\r
80  ] in\r
81  String.concat "\n" lines\r
82 ;;\r
83 \r
84 exception Done of (var * t) list (* substitution *);;\r
85 exception Fail of int * string;;\r
86 \r
87 let problem_fail p reason =\r
88  print_endline "!!!!!!!!!!!!!!! FAIL !!!!!!!!!!!!!!!";\r
89  print_endline (string_of_problem p);\r
90  raise (Fail (-1, reason))\r
91 ;;\r
92 \r
93 let freshvar ({freshno} as p) =\r
94  {p with freshno=freshno+1}, freshno+1\r
95 ;;\r
96 \r
97 let rec is_inert =\r
98  function\r
99  | A(t,_) -> is_inert t\r
100  | V _ -> true\r
101  | C _\r
102  | L _ | B -> false\r
103 ;;\r
104 \r
105 let is_var = function V _ -> true | _ -> false;;\r
106 let is_lambda = function L _ -> true | _ -> false;;\r
107 \r
108 let rec no_leading_lambdas = function\r
109  | L t -> 1 + no_leading_lambdas t\r
110  | _ -> 0\r
111 ;;\r
112 \r
113 let rec get_inert = function\r
114  | V n -> (n,0)\r
115  | A(t, _) -> let hd,args = get_inert t in hd,args+1\r
116  | _ -> assert false\r
117 ;;\r
118 \r
119 let rec subst level delift sub =\r
120  function\r
121  | V v -> if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v)\r
122  | L t -> let t = subst (level + 1) delift sub t in if t = B then B else L t\r
123  | A (t1,t2) ->\r
124   let t1 = subst level delift sub t1 in\r
125   let t2 = subst level delift sub t2 in\r
126   mk_app t1 t2\r
127  | C _ as t -> t\r
128  | B -> B\r
129 and mk_app t1 t2 = if t2 = B || (t1 = delta && t2 = delta) then B\r
130  else match t1 with\r
131  | C _ as t -> t\r
132  | B -> B\r
133  | L t1 -> subst 0 true (0, t2) t1\r
134  | _ -> A (t1, t2)\r
135 and lift n =\r
136  let rec aux lev =\r
137   function\r
138   | V m -> V (if m >= lev then m + n else m)\r
139   | L t -> L (aux (lev+1) t)\r
140   | A (t1, t2) -> A (aux lev t1, aux lev t2)\r
141   | C _ as t -> t\r
142   | B -> B\r
143  in aux 0\r
144 ;;\r
145 let subst = subst 0 false;;\r
146 \r
147 let subst_in_problem (sub: var * t) (p: problem) =\r
148 print_endline ("-- SUBST " ^ string_of_t (V (fst sub)) ^ " |-> " ^ string_of_t (snd sub));\r
149  {p with\r
150   div=subst sub p.div;\r
151   conv=subst sub p.conv;\r
152   stepped=(fst sub)::p.stepped;\r
153   sigma=sub::p.sigma}\r
154 ;;\r
155 \r
156 let get_subterm_with_head_and_args hd_var n_args =\r
157  let rec aux lev = function\r
158  | C _\r
159  | V _ | B -> None\r
160  | L t -> aux (lev+1) t\r
161  | A(t1,t2) as t ->\r
162    let hd_var', n_args' = get_inert t1 in\r
163    if hd_var' = hd_var + lev && n_args <= 1 + n_args'\r
164     then Some (lift ~-lev t)\r
165     else match aux lev t2 with\r
166     | None -> aux lev t1\r
167     | Some _ as res -> res\r
168  in aux 0\r
169 ;;\r
170 \r
171 let rec purify = function\r
172  | L t -> Pure.L (purify t)\r
173  | A (t1,t2) -> Pure.A (purify t1, purify t2)\r
174  | V n -> Pure.V n\r
175  | C _ -> Pure.V max_int (* FIXME *)\r
176  | B -> Pure.B\r
177 ;;\r
178 \r
179 let check p sigma =\r
180  print_endline "Checking...";\r
181  let div = purify p.div in\r
182  let conv = purify p.conv in\r
183  let sigma = List.map (fun (v,t) -> v, purify t) sigma in\r
184  let freshno = List.fold_right (fun (x,_) -> max x) sigma 0 in\r
185  let env = Pure.env_of_sigma freshno sigma in\r
186  assert (Pure.diverged (Pure.mwhd (env,div,[])));\r
187  print_endline " D diverged.";\r
188  assert (not (Pure.diverged (Pure.mwhd (env,conv,[]))));\r
189  print_endline " C converged.";\r
190  ()\r
191 ;;\r
192 \r
193 let sanity p =\r
194  print_endline (string_of_problem p); (* non cancellare *)\r
195  if p.conv = B then problem_fail p "p.conv diverged";\r
196  if p.div = B then raise (Done p.sigma);\r
197  if p.phase = `Two && p.div = delta then raise (Done p.sigma);\r
198  if not (is_inert p.div) then problem_fail p "p.div converged";\r
199  p\r
200 ;;\r
201 \r
202 (* drops the arguments of t after the n-th *)\r
203 let inert_cut_at n t =\r
204  let rec aux t =\r
205   match t with\r
206   | V _ as t -> 0, t\r
207   | A(t1,_) as t ->\r
208     let k', t' = aux t1 in\r
209      if k' = n then n, t'\r
210       else k'+1, t\r
211   | _ -> assert false\r
212  in snd (aux t)\r
213 ;;\r
214 \r
215 let find_eta_difference p t n_args =\r
216  let t = inert_cut_at n_args t in\r
217  let rec aux t u k = match t, u with\r
218  | V _, V _ -> assert false (* div subterm of conv *)\r
219  | A(t1,t2), A(u1,u2) ->\r
220     if not (eta_eq t2 u2) then (print_endline((string_of_t t2) ^ " <> " ^ (string_of_t u2)); k)\r
221     else aux t1 u1 (k-1)\r
222  | _, _ -> assert false\r
223  in aux p.div t n_args\r
224 ;;\r
225 \r
226 let compute_max_lambdas_at hd_var j =\r
227  let rec aux hd = function\r
228  | A(t1,t2) ->\r
229     (if get_inert t1 = (hd, j)\r
230       then max ( (*FIXME*)\r
231        if is_inert t2 && let hd', j' = get_inert t2 in hd' = hd\r
232         then let hd', j' = get_inert t2 in j - j'\r
233         else no_leading_lambdas t2)\r
234       else id) (max (aux hd t1) (aux hd t2))\r
235  | L t -> aux (hd+1) t\r
236  | V _ -> 0\r
237  | _ -> assert false\r
238  in aux hd_var\r
239 ;;\r
240 \r
241 let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);;\r
242 \r
243 (* eat the arguments of the divergent and explode.\r
244  It does NOT perform any check, may fail if done unsafely *)\r
245 let eat p =\r
246 print_cmd "EAT" "";\r
247  let var, k = get_inert p.div in\r
248  let phase = p.phase in\r
249  let p, t =\r
250   match phase with\r
251   | `One ->\r
252       let n = 1 + max\r
253        (compute_max_lambdas_at var k p.div)\r
254        (compute_max_lambdas_at var k p.conv) in\r
255       (* apply fresh vars *)\r
256       let p, t = fold_nat (fun (p, t) _ ->\r
257         let p, v = freshvar p in\r
258         p, A(t, V (v + k))\r
259       ) (p, V 0) n in\r
260       let p = {p with phase=`Two} in p, A(t, delta)\r
261   | `Two -> p, delta in\r
262  let subst = var, mk_lams t k in\r
263  let p = subst_in_problem subst p in\r
264  sanity p;\r
265  let p = if phase = `One then {p with div = (match p.div with A(t,_) -> t | _ -> assert false)} else p in\r
266  sanity p\r
267 ;;\r
268 \r
269 (* step on the head of div, on the k-th argument, with n fresh vars *)\r
270 let step k n p =\r
271  let var, _ = get_inert p.div in\r
272 print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")");\r
273  let p, t = (* apply fresh vars *)\r
274   fold_nat (fun (p, t) _ ->\r
275     let p, v = freshvar p in\r
276     p, A(t, V (v + k + 1))\r
277   ) (p, V 0) n in\r
278  let t = (* apply unused bound variables V_{k-1}..V_1 *)\r
279   fold_nat (fun t m -> A(t, V (k-m+1))) t k in\r
280  let t = mk_lams t (k+1) in (* make leading lambdas *)\r
281  let subst = var, t in\r
282  let p = subst_in_problem subst p in\r
283  sanity p\r
284 ;;\r
285 ;;\r
286 \r
287 let parse strs =\r
288   let rec aux level = function\r
289   | Parser_andrea.Lam t -> L (aux (level + 1) t)\r
290   | Parser_andrea.App (t1, t2) ->\r
291    if level = 0 then mk_app (aux level t1) (aux level t2)\r
292     else A(aux level t1, aux level t2)\r
293   | Parser_andrea.Var v -> V v in\r
294   let (tms, free) = Parser_andrea.parse_many strs in\r
295   (List.map (aux 0) tms, free)\r
296 ;;\r
297 \r
298 let problem_of div conv =\r
299  print_hline ();\r
300  let [@warning "-8"] [div; conv], var_names = parse ([div; conv]) in\r
301  let varno = List.length var_names in\r
302  let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; stepped=[]; phase=`One} in\r
303  (* initial sanity check *)\r
304  sanity p\r
305 ;;\r
306 \r
307 let exec div conv cmds =\r
308  let p = problem_of div conv in\r
309  try\r
310   problem_fail (List.fold_left (|>) p cmds) "Problem not completed"\r
311  with\r
312  | Done _ -> ()\r
313 ;;\r
314 \r
315 let rec auto p =\r
316  let hd_var, n_args = get_inert p.div in\r
317  match get_subterm_with_head_and_args hd_var n_args p.conv with\r
318  | None ->\r
319    (try\r
320     let phase = p.phase in\r
321      let p = eat p in\r
322      if phase = `Two\r
323       then problem_fail p "Auto.2 did not complete the problem"\r
324       else auto p\r
325     with Done sigma -> sigma)\r
326  | Some t ->\r
327   let j = find_eta_difference p t n_args - 1 in\r
328   let k = 1 + max\r
329    (compute_max_lambdas_at hd_var j p.div)\r
330     (compute_max_lambdas_at hd_var j p.conv) in\r
331   let p = step j k p in\r
332   auto p\r
333 ;;\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 let rec conv_join = function\r
360  | [] -> "@"\r
361  | x::xs -> conv_join xs ^ " ("^ x ^")"\r
362 ;;\r
363 \r
364 let auto' a b =\r
365  let p = problem_of a (conv_join b) in\r
366  let sigma = auto p in\r
367  check p sigma\r
368 ;;\r
369 \r
370 (* Example usage of exec, interactive:\r
371 \r
372 exec\r
373  "x x"\r
374  (conv_join["x y"; "y y"; "y x"])\r
375  [ step 0 1; eat ]\r
376 ;;\r
377 \r
378 interactive "x y"\r
379  "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat]\r
380 ;;\r
381 \r
382 *)\r
383 \r
384 auto' "x x" ["x y"; "y y"; "y x"] ;;\r
385 auto' "x y" ["x (_. x)"; "y z"; "y x"] ;;\r
386 auto' "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
387 \r
388 auto' "x (y. x y y)" ["x (y. x y x)"] ;;\r
389 \r
390 auto' "x a a a a" [\r
391  "x b a a a";\r
392  "x a b a a";\r
393  "x a a b a";\r
394  "x a a a b";\r
395 ] ;;\r
396 \r
397 (* Controesempio ad usare un conto dei lambda che non considere le permutazioni *)\r
398 auto' "x a a a a (x (x. x x) @ @ (_._.x. x x) x) b b b" [\r
399  "x a a a a (_. a) b b b";\r
400  "x a a a a (_. _. _. _. x. y. x y)";\r
401 ] ;;\r
402 \r
403 \r
404 print_hline();\r
405 print_endline "ALL DONE. "\r
406 \r
407 let solve div convs =\r
408  let p = problem_of div (conv_join convs) in\r
409  if eta_subterm p.div p.conv\r
410   then print_endline "!!! div is subterm of conv. Problem was not run !!!"\r
411   else check p (auto p)\r
412 ;;\r