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