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