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