]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/simple.ml
4a69c6e84b5ba2f046deac6281630a5b866f9897
[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 (* constant *)\r
16 ;;\r
17 \r
18 let delta = L(A(V 0, V 0));;\r
19 \r
20 let rec is_stuck = function\r
21  | C -> true\r
22  | A(t,_) -> is_stuck t\r
23  | _ -> false\r
24 ;;\r
25 \r
26 let eta_eq' =\r
27  let rec aux l1 l2 t1 t2 = match t1, t2 with\r
28   | _, _ when is_stuck t1 || is_stuck t2 -> true\r
29   | L t1, L t2 -> aux l1 l2 t1 t2\r
30   | L t1, t2 -> aux l1 (l2+1) t1 t2\r
31   | t1, L t2 -> aux (l1+1) l2 t1 t2\r
32   | V a, V b -> a + l1 = b + l2\r
33   | A(t1,t2), A(u1,u2) -> aux l1 l2 t1 u1 && aux l1 l2 t2 u2\r
34   | _, _ -> false\r
35  in aux ;;\r
36 let eta_eq = eta_eq' 0 0;;\r
37 \r
38 (* is arg1 eta-subterm of arg2 ? *)\r
39 let eta_subterm u =\r
40  let rec aux lev t = if t = C then false else (eta_eq' lev 0 u t || match t with\r
41  | L t -> aux (lev+1) t\r
42  | A(t1, t2) -> aux lev t1 || aux lev t2\r
43  | _ -> false) in\r
44  aux 0\r
45 ;;\r
46 \r
47 (* does NOT lift the argument *)\r
48 let mk_lams = fold_nat (fun x _ -> L x) ;;\r
49 \r
50 let string_of_t =\r
51   let string_of_bvar =\r
52    let bound_vars = ["x"; "y"; "z"; "w"; "q"] in\r
53    let bvarsno = List.length bound_vars in\r
54    fun nn -> if nn < bvarsno then List.nth bound_vars nn else "x" ^ (string_of_int (nn - bvarsno + 1)) in\r
55   let rec string_of_term_w_pars level = function\r
56     | V v -> if v >= level then "`" ^ string_of_int (v-level) else\r
57        string_of_bvar (level - v-1)\r
58     | C -> "C"\r
59     | A _\r
60     | L _ as t -> "(" ^ string_of_term_no_pars level t ^ ")"\r
61     | B -> "BOT"\r
62   and string_of_term_no_pars_app level = function\r
63     | A(t1,t2) -> string_of_term_no_pars_app level t1 ^ " " ^ string_of_term_w_pars level t2\r
64     | _ as t -> string_of_term_w_pars level t\r
65   and string_of_term_no_pars level = function\r
66     | L t -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t\r
67     | _ as t -> string_of_term_no_pars_app level t\r
68   in string_of_term_no_pars 0\r
69 ;;\r
70 \r
71 type problem = {\r
72    orig_freshno: int\r
73  ; freshno : int\r
74  ; div : t\r
75  ; conv : t\r
76  ; sigma : (var * t) list (* substitutions *)\r
77  ; stepped : var list\r
78  ; phase : [`One | `Two] (* :'( *)\r
79 }\r
80 \r
81 let string_of_problem p =\r
82  let lines = [\r
83   "[stepped] " ^ String.concat " " (List.map string_of_int p.stepped);\r
84   "[DV] " ^ string_of_t p.div;\r
85   "[CV] " ^ string_of_t p.conv;\r
86  ] in\r
87  String.concat "\n" lines\r
88 ;;\r
89 \r
90 exception Done of (var * t) list (* substitution *);;\r
91 exception Fail of int * string;;\r
92 \r
93 let problem_fail p reason =\r
94  print_endline "!!!!!!!!!!!!!!! FAIL !!!!!!!!!!!!!!!";\r
95  print_endline (string_of_problem p);\r
96  raise (Fail (-1, reason))\r
97 ;;\r
98 \r
99 let freshvar ({freshno} as p) =\r
100  {p with freshno=freshno+1}, freshno+1\r
101 ;;\r
102 \r
103 let rec is_inert =\r
104  function\r
105  | A(t,_) -> is_inert t\r
106  | V _ -> true\r
107  | C\r
108  | L _ | B -> false\r
109 ;;\r
110 \r
111 let rec get_inert = function\r
112  | V _ | C as t -> (t,0)\r
113  | A(t, _) -> let hd,args = get_inert t in hd,args+1\r
114  | _ -> assert false\r
115 ;;\r
116 \r
117 (* precomputes the number of leading lambdas in a term,\r
118    after replacing _v_ w/ a term starting with n lambdas *)\r
119 let rec no_leading_lambdas v n = function\r
120  | L t -> 1 + no_leading_lambdas (v+1) n t\r
121  | A _ as t -> let v', m = get_inert t in if V v = v' then max 0 (n - m) else 0\r
122  | V v' -> if v = v' then n else 0\r
123  | B | C -> 0\r
124 ;;\r
125 \r
126 let rec subst level delift sub =\r
127  function\r
128  | V v -> if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v)\r
129  | L t -> let t = subst (level + 1) delift sub t in if t = B then B else L t\r
130  | A (t1,t2) ->\r
131   let t1 = subst level delift sub t1 in\r
132   let t2 = subst level delift sub t2 in\r
133   mk_app t1 t2\r
134  | C | B as t -> t\r
135 and mk_app t1 t2 = if t2 = B || (t1 = delta && t2 = delta) then B\r
136  else match t1 with\r
137  | B -> B\r
138  | L t1 -> subst 0 true (0, t2) t1\r
139  | _ -> A (t1, t2)\r
140 and lift n =\r
141  let rec aux lev =\r
142   function\r
143   | V m -> V (if m >= lev then m + n else m)\r
144   | L t -> L (aux (lev+1) t)\r
145   | A (t1, t2) -> A (aux lev t1, aux lev t2)\r
146   | C  | B as t -> t\r
147  in aux 0\r
148 ;;\r
149 let subst = subst 0 false;;\r
150 \r
151 let subst_in_problem ((v, t) as sub) p =\r
152 print_endline ("-- SUBST " ^ string_of_t (V v) ^ " |-> " ^ string_of_t t);\r
153  {p with\r
154   div=subst sub p.div;\r
155   conv=subst sub p.conv;\r
156   stepped=v::p.stepped;\r
157   sigma=sub::p.sigma}\r
158 ;;\r
159 \r
160 let get_subterm_with_head_and_args hd_var n_args =\r
161  let rec aux lev = function\r
162  | C | 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' = V (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 (min_int/2)\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 _ -> []\r
229  | A(t1,t2), A(u1,u2) ->\r
230     print_endline (string_of_t t2 ^ " vs " ^ string_of_t u2);\r
231     if not (eta_eq t2 u2) then (k-1)::aux t1 u1 (k-1)\r
232     else aux t1 u1 (k-1)\r
233  | _, _ -> assert false\r
234  in aux p.div t argsno\r
235 ;;\r
236 \r
237 let compute_max_lambdas_at hd_var j =\r
238  let rec aux hd = function\r
239  | A(t1,t2) ->\r
240     (if get_inert t1 = (V hd, j)\r
241       then max ( (*FIXME*)\r
242        if is_inert t2 && let hd', j' = get_inert t2 in hd' = V hd\r
243         then let hd', j' = get_inert t2 in j - j'\r
244         else no_leading_lambdas hd_var j t2)\r
245       else id) (max (aux hd t1) (aux hd t2))\r
246  | L t -> aux (hd+1) t\r
247  | V _ | C -> 0\r
248  | _ -> assert false\r
249  in aux hd_var\r
250 ;;\r
251 \r
252 let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);;\r
253 \r
254 (* eat the arguments of the divergent and explode.\r
255  It does NOT perform any check, may fail if done unsafely *)\r
256 let eat p =\r
257 print_cmd "EAT" "";\r
258  let var, k = get_inert p.div in\r
259  match var with\r
260  | C | L _ | B | A _ -> assert false\r
261  | V var ->\r
262  let phase = p.phase in\r
263  let p =\r
264   match phase with\r
265   | `One ->\r
266       let n = 1 + max\r
267        (compute_max_lambdas_at var (k-1) p.div)\r
268        (compute_max_lambdas_at var (k-1) p.conv) in\r
269       (* apply fresh vars *)\r
270       let p, t = fold_nat (fun (p, t) _ ->\r
271         let p, v = freshvar p in\r
272         p, A(t, V (v + k))\r
273       ) (p, V 0) n in\r
274       let p = {p with phase=`Two} in\r
275       let t = A(t, delta) in\r
276       let t = fold_nat (fun t m -> A(t, V (k-m))) t (k-1) in\r
277       let subst = var, mk_lams t k in\r
278       let p = subst_in_problem subst p in\r
279       let _, args = get_inert p.div in\r
280       {p with div = inert_cut_at (args-k) p.div}\r
281   | `Two ->\r
282       let subst = var, mk_lams delta k in\r
283       subst_in_problem subst p in\r
284  sanity p\r
285 ;;\r
286 \r
287 (* step on the head of div, on the k-th argument, with n fresh vars *)\r
288 let step k n p =\r
289  let hd, _ = get_inert p.div in\r
290  match hd with\r
291  | C | L _ | B | A _  -> assert false\r
292  | V var ->\r
293 print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (on " ^ string_of_int (k+1) ^ "th)");\r
294  let p, t = (* apply fresh vars *)\r
295   fold_nat (fun (p, t) _ ->\r
296     let p, v = freshvar p in\r
297     p, A(t, V (v + k + 1))\r
298   ) (p, V 0) n in\r
299  let t = (* apply unused bound variables V_{k-1}..V_1 *)\r
300   fold_nat (fun t m -> A(t, V (k-m+1))) t k in\r
301  let t = mk_lams t (k+1) in (* make leading lambdas *)\r
302  let subst = var, t in\r
303  let p = subst_in_problem subst p in\r
304  sanity p\r
305 ;;\r
306 \r
307 let auto p =\r
308  let rec aux p =\r
309  let hd, n_args = get_inert p.div in\r
310  match hd with\r
311  | C | L _ | B | A _  -> assert false\r
312  | V hd_var ->\r
313  match get_subterm_with_head_and_args hd_var n_args p.conv with\r
314  | None ->\r
315     let phase = p.phase in\r
316      let p = eat p in\r
317      if phase = `Two\r
318       then problem_fail p "Auto.2 did not complete the problem"\r
319       else aux p\r
320  | Some t ->\r
321   let js = find_eta_difference p t n_args in\r
322   (* print_endline (String.concat ", " (List.map string_of_int js)); *)\r
323   if js = [] then problem_fail p "no eta difference found (div subterm of conv?)";\r
324   let js = List.rev js in\r
325    List.iter\r
326     (fun j ->\r
327      try\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       ignore (aux (step j k p))\r
332      with Fail(_, s) ->\r
333       print_endline ("Backtracking because: " ^ s)) js;\r
334    raise (Fail(-1, "no eta difference")) in\r
335   try\r
336    aux p\r
337   with Done sigma -> sigma\r
338 ;;\r
339 \r
340 let problem_of (label, div, convs, ps, var_names) =\r
341  print_hline ();\r
342  let rec aux lev = function\r
343  | `Lam(_, t) -> L (aux (lev+1) t)\r
344  | `I (v, args) -> Listx.fold_left (fun x y -> mk_app x (aux lev y)) (aux lev (`Var v)) args\r
345  | `Var(v,_) -> if v >= lev && List.nth var_names (v-lev) = "C" then C else V v\r
346  | `N _ | `Match _ -> assert false in\r
347  assert (List.length ps = 0);\r
348  let convs = List.rev convs in\r
349  let conv = List.fold_left (fun x y -> mk_app x (aux 0 (y :> Num.nf))) (V (List.length var_names)) convs in\r
350  let var_names = "@" :: var_names in\r
351  let div = match div with\r
352  | Some div -> aux 0 (div :> Num.nf)\r
353  | None -> assert false in\r
354  let varno = List.length var_names in\r
355  let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; stepped=[]; phase=`One} in\r
356  (* initial sanity check *)\r
357  sanity p\r
358 ;;\r
359 \r
360 let solve p =\r
361  if is_stuck p.div then print_endline "!!! div is stuck. Problem was not run !!!"\r
362  else if eta_subterm p.div p.conv\r
363   then print_endline "!!! div is subterm of conv. Problem was not run !!!"\r
364   else check p (auto p)\r
365 ;;\r
366 \r
367 Problems.main (solve ++ problem_of);\r
368 \r
369 (* Example usage of interactive: *)\r
370 \r
371 (* let interactive div conv cmds =\r
372  let p = problem_of div conv in\r
373  try (\r
374  let p = List.fold_left (|>) p cmds in\r
375  let rec f p cmds =\r
376   let nth spl n = int_of_string (List.nth spl n) in\r
377   let read_cmd () =\r
378    let s = read_line () in\r
379    let spl = Str.split (Str.regexp " +") s in\r
380    s, let uno = List.hd spl in\r
381     try if uno = "eat" then eat\r
382     else if uno = "step" then step (nth spl 1) (nth spl 2)\r
383     else failwith "Wrong input."\r
384     with Failure s -> print_endline s; (fun x -> x) in\r
385   let str, cmd = read_cmd () in\r
386   let cmds = (" " ^ str ^ ";")::cmds in\r
387   try\r
388    let p = cmd p in f p cmds\r
389   with\r
390   | Done _ -> print_endline "Done! Commands history: "; List.iter print_endline (List.rev cmds)\r
391  in f p []\r
392  ) with Done _ -> ()\r
393 ;; *)\r
394 \r
395 (* interactive "x y"\r
396  "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat]\r
397 ;; *)\r