]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/simple.ml
e5e829a5accccbf0e37ce2fbefe62b4a6c9f0ed5
[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 the argument *)\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 get_inert = function\r
109  | V n -> (n,0)\r
110  | A(t, _) -> let hd,args = get_inert t in hd,args+1\r
111  | _ -> assert false\r
112 ;;\r
113 \r
114 (* precomputes the number of leading lambdas in a term,\r
115    after replacing _v_ w/ a term starting with n lambdas *)\r
116 let rec no_leading_lambdas v n = function\r
117  | L t -> 1 + no_leading_lambdas (v+1) n t\r
118  | A _ as t -> let v', m = get_inert t in if v = v' then max 0 (n - m) else 0\r
119  | V v' -> if v = v' then n else 0\r
120  | B | C _ -> 0\r
121 ;;\r
122 \r
123 let rec subst level delift sub =\r
124  function\r
125  | V v -> if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v)\r
126  | L t -> let t = subst (level + 1) delift sub t in if t = B then B else L t\r
127  | A (t1,t2) ->\r
128   let t1 = subst level delift sub t1 in\r
129   let t2 = subst level delift sub t2 in\r
130   mk_app t1 t2\r
131  | C _ as t -> t\r
132  | B -> B\r
133 and mk_app t1 t2 = if t2 = B || (t1 = delta && t2 = delta) then B\r
134  else match t1 with\r
135  | B -> B\r
136  | L t1 -> subst 0 true (0, t2) t1\r
137  | _ -> A (t1, t2)\r
138 and lift n =\r
139  let rec aux lev =\r
140   function\r
141   | V m -> V (if m >= lev then m + n else m)\r
142   | L t -> L (aux (lev+1) t)\r
143   | A (t1, t2) -> A (aux lev t1, aux lev t2)\r
144   | C _ as t -> t\r
145   | B -> B\r
146  in aux 0\r
147 ;;\r
148 let subst = subst 0 false;;\r
149 \r
150 let subst_in_problem ((v, t) as sub) p =\r
151 print_endline ("-- SUBST " ^ string_of_t (V v) ^ " |-> " ^ string_of_t t);\r
152  {p with\r
153   div=subst sub p.div;\r
154   conv=subst sub p.conv;\r
155   stepped=v::p.stepped;\r
156   sigma=sub::p.sigma}\r
157 ;;\r
158 \r
159 let get_subterm_with_head_and_args hd_var n_args =\r
160  let rec aux lev = function\r
161  | C _\r
162  | V _ | B -> None\r
163  | L t -> aux (lev+1) t\r
164  | A(t1,t2) as t ->\r
165    let hd_var', n_args' = get_inert t1 in\r
166    if hd_var' = hd_var + lev && n_args <= 1 + n_args'\r
167     (* the `+1` above is because of t2 *)\r
168     then Some (lift ~-lev t)\r
169     else match aux lev t2 with\r
170     | None -> aux lev t1\r
171     | Some _ as res -> res\r
172  in aux 0\r
173 ;;\r
174 \r
175 let rec purify = function\r
176  | L t -> Pure.L (purify t)\r
177  | A (t1,t2) -> Pure.A (purify t1, purify t2)\r
178  | V n -> Pure.V n\r
179  | C _ -> Pure.V max_int (* FIXME *)\r
180  | B -> Pure.B\r
181 ;;\r
182 \r
183 let check p sigma =\r
184  print_endline "Checking...";\r
185  let div = purify p.div in\r
186  let conv = purify p.conv in\r
187  let sigma = List.map (fun (v,t) -> v, purify t) sigma in\r
188  let freshno = List.fold_right (max ++ fst) sigma 0 in\r
189  let env = Pure.env_of_sigma freshno sigma in\r
190  assert (Pure.diverged (Pure.mwhd (env,div,[])));\r
191  print_endline " D diverged.";\r
192  assert (not (Pure.diverged (Pure.mwhd (env,conv,[]))));\r
193  print_endline " C converged.";\r
194  ()\r
195 ;;\r
196 \r
197 let sanity p =\r
198  print_endline (string_of_problem p); (* non cancellare *)\r
199  if p.conv = B then problem_fail p "p.conv diverged";\r
200  if p.div = B then raise (Done p.sigma);\r
201  if p.phase = `Two && p.div = delta then raise (Done p.sigma);\r
202  if not (is_inert p.div) then problem_fail p "p.div converged";\r
203  p\r
204 ;;\r
205 \r
206 (* drops the arguments of t after the n-th *)\r
207 (* FIXME! E' usato in modo improprio contando sul fatto\r
208    errato che ritorna un inerte lungo esattamente n *)\r
209 let inert_cut_at n t =\r
210  let rec aux t =\r
211   match t with\r
212   | V _ as t -> 0, t\r
213   | A(t1,_) as t ->\r
214     let k', t' = aux t1 in\r
215      if k' = n then n, t'\r
216       else k'+1, t\r
217   | _ -> assert false\r
218  in snd (aux t)\r
219 ;;\r
220 \r
221 (* return the index of the first argument with a difference\r
222    (the first argument is 0)\r
223    precondition: p.div and t have n+1 arguments\r
224    *)\r
225 let find_eta_difference p t argsno =\r
226  let t = inert_cut_at argsno t in\r
227  let rec aux t u k = match t, u with\r
228  | V _, V _ -> problem_fail p "no eta difference found (div subterm of conv?)"\r
229  | A(t1,t2), A(u1,u2) ->\r
230     if not (eta_eq t2 u2) then (k-1)\r
231     else aux t1 u1 (k-1)\r
232  | _, _ -> assert false\r
233  in aux p.div t argsno\r
234 ;;\r
235 \r
236 let compute_max_lambdas_at hd_var j =\r
237  let rec aux hd = function\r
238  | A(t1,t2) ->\r
239     (if get_inert t1 = (hd, j)\r
240       then max ( (*FIXME*)\r
241        if is_inert t2 && let hd', j' = get_inert t2 in hd' = hd\r
242         then let hd', j' = get_inert t2 in j - j'\r
243         else no_leading_lambdas hd_var j t2)\r
244       else id) (max (aux hd t1) (aux hd t2))\r
245  | L t -> aux (hd+1) t\r
246  | V _ -> 0\r
247  | _ -> assert false\r
248  in aux hd_var\r
249 ;;\r
250 \r
251 let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);;\r
252 \r
253 (* eat the arguments of the divergent and explode.\r
254  It does NOT perform any check, may fail if done unsafely *)\r
255 let eat p =\r
256 print_cmd "EAT" "";\r
257  let var, k = get_inert p.div in\r
258  let phase = p.phase in\r
259  let p =\r
260   match phase with\r
261   | `One ->\r
262       let n = 1 + max\r
263        (compute_max_lambdas_at var (k-1) p.div)\r
264        (compute_max_lambdas_at var (k-1) p.conv) in\r
265       (* apply fresh vars *)\r
266       let p, t = fold_nat (fun (p, t) _ ->\r
267         let p, v = freshvar p in\r
268         p, A(t, V (v + k))\r
269       ) (p, V 0) n in\r
270       let p = {p with phase=`Two} in\r
271       let t = A(t, delta) in\r
272       let t = fold_nat (fun t m -> A(t, V (k-m))) t (k-1) in\r
273       let subst = var, mk_lams t k in\r
274       let p = subst_in_problem subst p in\r
275       let _, args = get_inert p.div in\r
276       {p with div = inert_cut_at (args-k) p.div}\r
277   | `Two ->\r
278       let subst = var, mk_lams delta k in\r
279       subst_in_problem subst p in\r
280  sanity p\r
281 ;;\r
282 \r
283 (* step on the head of div, on the k-th argument, with n fresh vars *)\r
284 let step k n p =\r
285  let var, _ = get_inert p.div in\r
286 print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")");\r
287  let p, t = (* apply fresh vars *)\r
288   fold_nat (fun (p, t) _ ->\r
289     let p, v = freshvar p in\r
290     p, A(t, V (v + k + 1))\r
291   ) (p, V 0) n in\r
292  let t = (* apply unused bound variables V_{k-1}..V_1 *)\r
293   fold_nat (fun t m -> A(t, V (k-m+1))) t k in\r
294  let t = mk_lams t (k+1) in (* make leading lambdas *)\r
295  let subst = var, t in\r
296  let p = subst_in_problem subst p in\r
297  sanity p\r
298 ;;\r
299 \r
300 let rec auto p =\r
301  let hd_var, n_args = get_inert p.div in\r
302  match get_subterm_with_head_and_args hd_var n_args p.conv with\r
303  | None ->\r
304    (try\r
305     let phase = p.phase in\r
306      let p = eat p in\r
307      if phase = `Two\r
308       then problem_fail p "Auto.2 did not complete the problem"\r
309       else auto p\r
310     with Done sigma -> sigma)\r
311  | Some t ->\r
312   let j = find_eta_difference p t n_args in\r
313   let k = 1 + max\r
314    (compute_max_lambdas_at hd_var j p.div)\r
315     (compute_max_lambdas_at hd_var j p.conv) in\r
316   let p = step j k p in\r
317   auto p\r
318 ;;\r
319 \r
320 let problem_of (label, div, convs, ps, var_names) =\r
321  print_hline ();\r
322  let rec aux = function\r
323  | `Lam(_, t) -> L (aux t)\r
324  | `I ((v,_), args) -> Listx.fold_left (fun x y -> mk_app x (aux y)) (V v) args\r
325  | `Var(v,_) -> V v\r
326  | `N _ | `Match _ -> assert false in\r
327  assert (List.length ps = 0);\r
328  let convs = List.rev convs in\r
329  let conv = List.fold_left (fun x y -> mk_app x (aux (y :> Num.nf))) (V (List.length var_names)) convs in\r
330  let var_names = "@" :: var_names in\r
331  let div = match div with\r
332  | Some div -> aux (div :> Num.nf)\r
333  | None -> assert false in\r
334  let varno = List.length var_names in\r
335  let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; stepped=[]; phase=`One} in\r
336  (* initial sanity check *)\r
337  sanity p\r
338 ;;\r
339 \r
340 let solve p =\r
341  if eta_subterm p.div p.conv\r
342   then print_endline "!!! div is subterm of conv. Problem was not run !!!"\r
343   else check p (auto p)\r
344 ;;\r
345 \r
346 Problems.main (solve ++ problem_of);\r
347 \r
348 (* Example usage of interactive: *)\r
349 \r
350 (* let interactive div conv cmds =\r
351  let p = problem_of div conv in\r
352  try (\r
353  let p = List.fold_left (|>) p cmds in\r
354  let rec f p cmds =\r
355   let nth spl n = int_of_string (List.nth spl n) in\r
356   let read_cmd () =\r
357    let s = read_line () in\r
358    let spl = Str.split (Str.regexp " +") s in\r
359    s, let uno = List.hd spl in\r
360     try if uno = "eat" then eat\r
361     else if uno = "step" then step (nth spl 1) (nth spl 2)\r
362     else failwith "Wrong input."\r
363     with Failure s -> print_endline s; (fun x -> x) in\r
364   let str, cmd = read_cmd () in\r
365   let cmds = (" " ^ str ^ ";")::cmds in\r
366   try\r
367    let p = cmd p in f p cmds\r
368   with\r
369   | Done _ -> print_endline "Done! Commands history: "; List.iter print_endline (List.rev cmds)\r
370  in f p []\r
371  ) with Done _ -> ()\r
372 ;; *)\r
373 \r
374 (* interactive "x y"\r
375  "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat]\r
376 ;; *)\r