]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/simple.ml
Removed eat, p.stepped, p.phase and t.B
[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  | C (* constant *)\r
15 ;;\r
16 \r
17 let delta = L(A(V 0, V 0));;\r
18 \r
19 let rec is_stuck = function\r
20  | C -> true\r
21  | A(t,_) -> is_stuck t\r
22  | _ -> false\r
23 ;;\r
24 \r
25 let eta_eq' =\r
26  let rec aux l1 l2 t1 t2 = match t1, t2 with\r
27   | _, _ when is_stuck t1 || is_stuck t2 -> true\r
28   | L t1, L t2 -> aux l1 l2 t1 t2\r
29   | L t1, t2 -> aux l1 (l2+1) t1 t2\r
30   | t1, L t2 -> aux (l1+1) l2 t1 t2\r
31   | V a, V b -> a + l1 = b + l2\r
32   | A(t1,t2), A(u1,u2) -> aux l1 l2 t1 u1 && aux l1 l2 t2 u2\r
33   | _, _ -> false\r
34  in aux ;;\r
35 let eta_eq = eta_eq' 0 0;;\r
36 \r
37 (* is arg1 eta-subterm of arg2 ? *)\r
38 let eta_subterm u =\r
39  let rec aux lev t = if t = C then false else (eta_eq' lev 0 u t || match t with\r
40  | L t -> aux (lev+1) t\r
41  | A(t1, t2) -> aux lev t1 || aux lev t2\r
42  | _ -> false) in\r
43  aux 0\r
44 ;;\r
45 \r
46 (* does NOT lift the argument *)\r
47 let mk_lams = fold_nat (fun x _ -> L x) ;;\r
48 \r
49 let string_of_t =\r
50   let string_of_bvar =\r
51    let bound_vars = ["x"; "y"; "z"; "w"; "q"] in\r
52    let bvarsno = List.length bound_vars in\r
53    fun nn -> if nn < bvarsno then List.nth bound_vars nn else "x" ^ (string_of_int (nn - bvarsno + 1)) in\r
54   let rec string_of_term_w_pars level = function\r
55     | V v -> if v >= level then "`" ^ string_of_int (v-level) else\r
56        string_of_bvar (level - v-1)\r
57     | C -> "C"\r
58     | A _\r
59     | L _ as t -> "(" ^ string_of_term_no_pars level t ^ ")"\r
60   and string_of_term_no_pars_app level = function\r
61     | A(t1,t2) -> string_of_term_no_pars_app level t1 ^ " " ^ string_of_term_w_pars level t2\r
62     | _ as t -> string_of_term_w_pars level t\r
63   and string_of_term_no_pars level = function\r
64     | L t -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t\r
65     | _ as t -> string_of_term_no_pars_app level t\r
66   in string_of_term_no_pars 0\r
67 ;;\r
68 \r
69 type problem = {\r
70    orig_freshno: int\r
71  ; freshno : int\r
72  ; div : t\r
73  ; conv : t\r
74  ; sigma : (var * t) list (* substitutions *)\r
75 }\r
76 \r
77 let string_of_problem p =\r
78  let lines = [\r
79   "[DV] " ^ string_of_t p.div;\r
80   "[CV] " ^ string_of_t p.conv;\r
81  ] in\r
82  String.concat "\n" lines\r
83 ;;\r
84 \r
85 exception B;;\r
86 exception Done of (var * t) list (* substitution *);;\r
87 exception Fail of int * string;;\r
88 \r
89 let problem_fail p reason =\r
90  print_endline "!!!!!!!!!!!!!!! FAIL !!!!!!!!!!!!!!!";\r
91  print_endline (string_of_problem p);\r
92  raise (Fail (-1, reason))\r
93 ;;\r
94 \r
95 let freshvar ({freshno} as p) =\r
96  {p with freshno=freshno+1}, freshno+1\r
97 ;;\r
98 \r
99 (* CSC: rename? is an applied C an inert?\r
100    is_inert and get_inert work inconsistently *)\r
101 let rec is_inert =\r
102  function\r
103  | A(t,_) -> is_inert t\r
104  | V _ -> true\r
105  | C\r
106  | L _ -> false\r
107 ;;\r
108 \r
109 let rec is_constant =\r
110  function\r
111     C -> true\r
112   | V _ -> false\r
113   | A(t,_)\r
114   | L t -> is_constant t\r
115 ;;\r
116 \r
117 let rec get_inert = function\r
118  | V _ | C as t -> (t,0)\r
119  | A(t, _) -> let hd,args = get_inert t in hd,args+1\r
120  | _ -> assert false\r
121 ;;\r
122 \r
123 let args_of_inert =\r
124  let rec aux acc =\r
125   function\r
126    | V _ | C -> acc\r
127    | A(t, a) -> aux (a::acc) t\r
128    | _ -> assert false\r
129  in\r
130   aux []\r
131 ;;\r
132 \r
133 (* precomputes the number of leading lambdas in a term,\r
134    after replacing _v_ w/ a term starting with n lambdas *)\r
135 let rec no_leading_lambdas v n = function\r
136  | L t -> 1 + no_leading_lambdas (v+1) n t\r
137  | A _ as t -> let v', m = get_inert t in if V v = v' then max 0 (n - m) else 0\r
138  | V v' -> if v = v' then n else 0\r
139  | C -> 0\r
140 ;;\r
141 \r
142 let rec subst level delift sub =\r
143  function\r
144  | V v -> if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v)\r
145  | L t -> let t = subst (level + 1) delift sub t in if t = B then B else L t\r
146  | A (t1,t2) ->\r
147   let t1 = subst level delift sub t1 in\r
148   let t2 = subst level delift sub t2 in\r
149   mk_app t1 t2\r
150  | C -> C\r
151 and mk_app t1 t2 = if t1 = delta && t2 = delta then raise B\r
152  else match t1 with\r
153  | L t1 -> subst 0 true (0, t2) t1\r
154  | _ -> A (t1, t2)\r
155 and lift n =\r
156  let rec aux lev =\r
157   function\r
158   | V m -> V (if m >= lev then m + n else m)\r
159   | L t -> L (aux (lev+1) t)\r
160   | A (t1, t2) -> A (aux lev t1, aux lev t2)\r
161   | C -> C\r
162  in aux 0\r
163 ;;\r
164 let subst = subst 0 false;;\r
165 \r
166 let subst_in_problem ((v, t) as sub) p =\r
167 print_endline ("-- SUBST " ^ string_of_t (V v) ^ " |-> " ^ string_of_t t);\r
168  {p with\r
169   div=subst sub p.div;\r
170   conv=subst sub p.conv;\r
171   sigma=sub::p.sigma}\r
172 ;;\r
173 \r
174 let get_subterms_with_head hd_var =\r
175  let rec aux lev inert_done = function\r
176  | L t -> aux (lev+1) false t\r
177  | C | V _ -> []\r
178  | A(t1,t2) as t ->\r
179    let hd_var', n_args' = get_inert t1 in\r
180    if not inert_done && hd_var' = V (hd_var + lev)\r
181     then lift ~-lev t :: aux lev true t1 @ aux lev false t2\r
182     else aux lev true t1 @ aux lev false t2\r
183  in aux 0 false\r
184 ;;\r
185 \r
186 let rec purify = function\r
187  | L t -> Pure.L (purify t)\r
188  | A (t1,t2) -> Pure.A (purify t1, purify t2)\r
189  | V n -> Pure.V n\r
190  | C -> Pure.V (min_int/2)\r
191 ;;\r
192 \r
193 let check p sigma =\r
194  print_endline "Checking...";\r
195  let div = purify p.div in\r
196  let conv = purify p.conv in\r
197  let sigma = List.map (fun (v,t) -> v, purify t) sigma in\r
198  let freshno = List.fold_right (max ++ fst) sigma 0 in\r
199  let env = Pure.env_of_sigma freshno sigma in\r
200  assert (Pure.diverged (Pure.mwhd (env,div,[])));\r
201  print_endline " D diverged.";\r
202  assert (not (Pure.diverged (Pure.mwhd (env,conv,[]))));\r
203  print_endline " C converged.";\r
204  ()\r
205 ;;\r
206 \r
207 let sanity p =\r
208  print_endline (string_of_problem p); (* non cancellare *)\r
209  if not (is_inert p.div) then problem_fail p "p.div converged";\r
210  (* Trailing constant args can be removed because do not contribute to eta-diff *)\r
211  let rec remove_trailing_constant_args = function\r
212  | A(t1, t2) when is_constant t2 -> remove_trailing_constant_args t1\r
213  | _ as t -> t in\r
214  let p = {p with div=remove_trailing_constant_args p.div} in\r
215  p\r
216 ;;\r
217 \r
218 (* drops the arguments of t after the n-th *)\r
219 let inert_cut_at n t =\r
220  let rec aux t =\r
221   match t with\r
222   | V _ as t -> 0, t\r
223   | A(t1,_) as t ->\r
224     let k', t' = aux t1 in\r
225      if k' = n then n, t'\r
226       else k'+1, t\r
227   | _ -> assert false\r
228  in snd (aux t)\r
229 ;;\r
230 \r
231 (* return the index of the first argument with a difference\r
232    (the first argument is 0) *)\r
233 let find_eta_difference p t =\r
234  let divargs = args_of_inert p.div in\r
235  let conargs = args_of_inert t in\r
236  let rec aux k divargs conargs =\r
237   match divargs,conargs with\r
238      [],_ -> []\r
239    | _::_,[] -> [k]\r
240    | t1::divargs,t2::conargs ->\r
241       (if not (eta_eq t1 t2) then [k] else []) @ aux (k+1) divargs conargs\r
242  in\r
243   aux 0 divargs conargs\r
244 ;;\r
245 \r
246 let compute_max_lambdas_at hd_var j =\r
247  let rec aux hd = function\r
248  | A(t1,t2) ->\r
249     (if get_inert t1 = (V hd, j)\r
250       then max ( (*FIXME*)\r
251        if is_inert t2 && let hd', j' = get_inert t2 in hd' = V hd\r
252         then let hd', j' = get_inert t2 in j - j'\r
253         else no_leading_lambdas hd_var j t2)\r
254       else id) (max (aux hd t1) (aux hd t2))\r
255  | L t -> aux (hd+1) t\r
256  | V _ | C -> 0\r
257  in aux hd_var\r
258 ;;\r
259 \r
260 let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);;\r
261 \r
262 (* returns Some i if i is the smallest integer s.t. p holds for the i-th\r
263    element of the list in input *)\r
264 let smallest_such_that p =\r
265  let rec aux i =\r
266   function\r
267      [] -> None\r
268    | hd::_ when (print_endline (string_of_t hd) ; p hd) -> Some i\r
269    | _::tl -> aux (i+1) tl\r
270  in\r
271   aux 0\r
272 ;;\r
273 \r
274 (* step on the head of div, on the k-th argument, with n fresh vars *)\r
275 let step k n p =\r
276  let hd, _ = get_inert p.div in\r
277  match hd with\r
278  | C | L _ | A _  -> assert false\r
279  | V var ->\r
280 print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (on " ^ string_of_int (k+1) ^ "th)");\r
281  let p, t = (* apply fresh vars *)\r
282   fold_nat (fun (p, t) _ ->\r
283     let p, v = freshvar p in\r
284     p, A(t, V (v + k + 1))\r
285   ) (p, V 0) n in\r
286  let t = (* apply unused bound variables V_{k-1}..V_1 *)\r
287   fold_nat (fun t m -> A(t, V (k-m+1))) t k in\r
288  let t = mk_lams t (k+1) in (* make leading lambdas *)\r
289  let subst = var, t in\r
290  let p = subst_in_problem subst p in\r
291  sanity p\r
292 ;;\r
293 \r
294 let finish p =\r
295  (* one-step version of eat *)\r
296  let compute_max_arity =\r
297    let rec aux n = function\r
298    | A(t1,t2) -> max (aux (n+1) t1) (aux 0 t2)\r
299    | L t -> max n (aux 0 t)\r
300    | _ -> n\r
301  in aux 0 in\r
302 print_cmd "FINISH" "";\r
303  (* First, a step on the last argument of the divergent.\r
304     Because of the sanity check, it will never be a constant term. *)\r
305  let div_hd, div_nargs = get_inert p.div in\r
306  let div_hd = match div_hd with V n -> n | _ -> assert false in\r
307  let j = div_nargs - 1 in\r
308  let arity = compute_max_arity p.conv in\r
309  let n = 1 + arity + max\r
310   (compute_max_lambdas_at div_hd j p.div)\r
311   (compute_max_lambdas_at div_hd j p.conv) in\r
312  let p = step j n p in\r
313  (* Now, find first argument of div that is a variable never applied anywhere.\r
314  It must exist because of some invariant, since we just did a step,\r
315  and because of the arity of the divergent *)\r
316  let div_hd, div_nargs = get_inert p.div in\r
317  let div_hd = match div_hd with V n -> n | _ -> assert false in\r
318  let rec aux m = function\r
319  | A(t, V delta_var) ->\r
320    if delta_var <> div_hd && get_subterms_with_head delta_var p.conv = []\r
321    then m, delta_var\r
322    else aux (m-1) t\r
323  | A(t,_) -> aux (m-1) t\r
324  | _ -> assert false in\r
325  let m, delta_var = aux div_nargs p.div in\r
326  let p = subst_in_problem (delta_var, delta) p in\r
327  let p = subst_in_problem (div_hd, mk_lams delta (m-1)) p in\r
328  sanity p\r
329 ;;\r
330 \r
331 let auto p =\r
332  let rec aux p =\r
333  let hd, n_args = get_inert p.div in\r
334  match hd with\r
335  | C | L _ | A _  -> assert false\r
336  | V hd_var ->\r
337  let tms = get_subterms_with_head hd_var p.conv in\r
338  if List.exists (fun t -> snd (get_inert t) >= n_args) tms\r
339   then (\r
340     (* let tms = List.sort (fun t1 t2 -> - compare (snd (get_inert t1)) (snd (get_inert t2))) tms in *)\r
341     List.iter (fun t -> try\r
342       let js = find_eta_difference p t in\r
343       (* print_endline (String.concat ", " (List.map string_of_int js)); *)\r
344       if js = [] then problem_fail p "no eta difference found (div subterm of conv?)";\r
345       let js = List.rev js in\r
346        List.iter\r
347         (fun j ->\r
348          try\r
349           let k = 1 + max\r
350            (compute_max_lambdas_at hd_var j p.div)\r
351             (compute_max_lambdas_at hd_var j p.conv) in\r
352           ignore (aux (step j k p))\r
353          with Fail(_, s) ->\r
354           print_endline ("Backtracking (eta_diff) because: " ^ s)) js;\r
355        raise (Fail(-1, "no eta difference"))\r
356       with Fail(_, s) ->\r
357        print_endline ("Backtracking (get_subterms) because: " ^ s)) tms;\r
358      raise (Fail(-1, "no similar terms"))\r
359    )\r
360   else\r
361     problem_fail (finish p) "Finish did not complete the problem"\r
362   in\r
363   try\r
364    aux p\r
365   with Done sigma -> sigma\r
366 ;;\r
367 \r
368 let problem_of (label, div, convs, ps, var_names) =\r
369  print_hline ();\r
370  let rec aux lev = function\r
371  | `Lam(_, t) -> L (aux (lev+1) t, [])\r
372  | `I (v, args) -> Listx.fold_left (fun x y -> fst (mk_app x (aux lev y))) (aux lev (`Var v)) args\r
373  | `Var(v,_) -> if v >= lev && List.nth var_names (v-lev) = "C" then C else V v\r
374  | `N _ | `Match _ -> assert false in\r
375  assert (List.length ps = 0);\r
376  let convs = List.rev convs in\r
377  let conv = List.fold_left (fun x y -> fst (mk_app x (aux 0 (y :> Num.nf)))) (V (List.length var_names)) convs in\r
378  let var_names = "@" :: var_names in\r
379  let div = match div with\r
380  | Some div -> aux 0 (div :> Num.nf)\r
381  | None -> assert false in\r
382  let varno = List.length var_names in\r
383  {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]}\r
384 ;;\r
385 \r
386 let solve p =\r
387  if is_constant p.div\r
388   then print_endline "!!! div is stuck. Problem was not run !!!"\r
389  else if eta_subterm p.div p.conv\r
390   then print_endline "!!! div is subterm of conv. Problem was not run !!!"\r
391   else let p = sanity p (* initial sanity check *) in check p (auto p)\r
392 ;;\r
393 \r
394 Problems.main (solve ++ problem_of);\r
395 \r
396 (* Example usage of interactive: *)\r
397 \r
398 (* let interactive div conv cmds =\r
399  let p = problem_of div conv in\r
400  try (\r
401  let p = List.fold_left (|>) p cmds in\r
402  let rec f p cmds =\r
403   let nth spl n = int_of_string (List.nth spl n) in\r
404   let read_cmd () =\r
405    let s = read_line () in\r
406    let spl = Str.split (Str.regexp " +") s in\r
407    s, let uno = List.hd spl in\r
408     try if uno = "eat" then eat\r
409     else if uno = "step" then step (nth spl 1) (nth spl 2)\r
410     else failwith "Wrong input."\r
411     with Failure s -> print_endline s; (fun x -> x) in\r
412   let str, cmd = read_cmd () in\r
413   let cmds = (" " ^ str ^ ";")::cmds in\r
414   try\r
415    let p = cmd p in f p cmds\r
416   with\r
417   | Done _ -> print_endline "Done! Commands history: "; List.iter print_endline (List.rev cmds)\r
418  in f p []\r
419  ) with Done _ -> ()\r
420 ;; *)\r
421 \r
422 (* interactive "x y"\r
423  "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat]\r
424 ;; *)\r