]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/andrea.ml
bd2ad0919520b15bebb73d849d81413afc02488c
[fireball-separation.git] / ocaml / andrea.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 type var = int;;\r
8 type t =\r
9  | V of var\r
10  | A of t * t\r
11  | L of t\r
12  | B (* bottom *)\r
13 ;;\r
14 \r
15 let eta_eq =\r
16  let rec aux l1 l2 t1 t2 = match t1, t2 with\r
17   | L t1, L t2 -> aux l1 l2 t1 t2\r
18   | L t1, t2 -> aux l1 (l2+1) t1 t2\r
19   | t1, L t2 -> aux (l1+1) l2 t1 t2\r
20   | V a, V b -> a + l1 = b + l2\r
21   | A(t1,t2), A(u1,u2) -> aux l1 l2 t1 u1 && aux l1 l2 t2 u2\r
22   | _, _ -> false\r
23  in aux 0 0\r
24 ;;\r
25 \r
26 (* does NOT lift t *)\r
27 let mk_lams = fold_nat (fun x _ -> L x) ;;\r
28 \r
29 let string_of_t =\r
30   let string_of_bvar =\r
31    let bound_vars = ["x"; "y"; "z"; "w"; "q"] in\r
32    let bvarsno = List.length bound_vars in\r
33    fun nn -> if nn < bvarsno then List.nth bound_vars nn else "x" ^ (string_of_int (nn - bvarsno + 1)) in\r
34   let rec string_of_term_w_pars level = function\r
35     | V v -> if v >= level then "`" ^ string_of_int (v-level) else\r
36        string_of_bvar (level - v-1)\r
37     | A _\r
38     | L _ as t -> "(" ^ string_of_term_no_pars level t ^ ")"\r
39     | B -> "BOT"\r
40   and string_of_term_no_pars_app level = function\r
41     | A(t1,t2) -> string_of_term_no_pars_app level t1 ^ " " ^ string_of_term_w_pars level t2\r
42     | _ as t -> string_of_term_w_pars level t\r
43   and string_of_term_no_pars level = function\r
44     | L t -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t\r
45     | _ as t -> string_of_term_no_pars_app level t\r
46   in string_of_term_no_pars 0\r
47 ;;\r
48 \r
49 type problem = {\r
50    orig_freshno: int\r
51  ; freshno : int\r
52  ; div : t\r
53  ; conv : t\r
54  ; sigma : (var * t) list (* substitutions *)\r
55  ; stepped : var list\r
56 }\r
57 \r
58 let string_of_problem p =\r
59  let lines = [\r
60   "[stepped] " ^ String.concat " " (List.map string_of_int p.stepped);\r
61   "[DV] " ^ string_of_t p.div;\r
62   "[CV] " ^ string_of_t p.conv;\r
63  ] in\r
64  String.concat "\n" lines\r
65 ;;\r
66 \r
67 exception Done of (var * t) list (* substitution *);;\r
68 exception Fail of int * string;;\r
69 \r
70 let problem_fail p reason =\r
71  print_endline "!!!!!!!!!!!!!!! FAIL !!!!!!!!!!!!!!!";\r
72  print_endline (string_of_problem p);\r
73  raise (Fail (-1, reason))\r
74 ;;\r
75 \r
76 let freshvar ({freshno} as p) =\r
77  {p with freshno=freshno+1}, freshno+1\r
78 ;;\r
79 \r
80 let rec is_inert =\r
81  function\r
82  | A(t,_) -> is_inert t\r
83  | V _ -> true\r
84  | L _ | B -> false\r
85 ;;\r
86 \r
87 let is_var = function V _ -> true | _ -> false;;\r
88 let is_lambda = function L _ -> true | _ -> false;;\r
89 \r
90 let rec get_inert = function\r
91  | V n -> (n,0)\r
92  | A(t, _) -> let hd,args = get_inert t in hd,args+1\r
93  | _ -> assert false\r
94 ;;\r
95 \r
96 let rec subst level delift sub =\r
97  function\r
98  | V v -> if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v)\r
99  | L t -> L (subst (level + 1) delift sub t)\r
100  | A (t1,t2) ->\r
101   let t1 = subst level delift sub t1 in\r
102   let t2 = subst level delift sub t2 in\r
103   mk_app t1 t2\r
104  | B -> B\r
105 and mk_app t1 t2 = match t1 with\r
106  | B | _ when t2 = B -> B\r
107  | L t1 -> subst 0 true (0, t2) t1\r
108  | t1 -> A (t1, t2)\r
109 and lift n =\r
110  let rec aux lev =\r
111   function\r
112   | V m -> V (if m >= lev then m + n else m)\r
113   | L t -> L (aux (lev+1) t)\r
114   | A (t1, t2) -> A (aux lev t1, aux lev t2)\r
115   | B -> B\r
116  in aux 0\r
117 ;;\r
118 let subst = subst 0 false;;\r
119 \r
120 let subst_in_problem (sub: var * t) (p: problem) =\r
121 print_endline ("-- SUBST " ^ string_of_t (V (fst sub)) ^ " |-> " ^ string_of_t (snd sub));\r
122  {p with\r
123   div=subst sub p.div;\r
124   conv=subst sub p.conv;\r
125   stepped=(fst sub)::p.stepped;\r
126   sigma=sub::p.sigma}\r
127 ;;\r
128 \r
129 let get_subterm_with_head_and_args hd_var n_args =\r
130  let rec aux lev = function\r
131  | V _ | B -> None\r
132  | L t -> aux (lev+1) t\r
133  | A(t1,t2) as t ->\r
134    let hd_var', n_args' = get_inert t1 in\r
135    if hd_var' = hd_var + lev && n_args <= 1 + n_args'\r
136     then Some (lift ~-lev t)\r
137     else match aux lev t2 with\r
138     | None -> aux lev t1\r
139     | Some _ as res -> res\r
140  in aux 0\r
141 ;;\r
142 \r
143 let sanity p =\r
144  print_endline (string_of_problem p); (* non cancellare *)\r
145  if p.conv = B then problem_fail p "p.conv diverged";\r
146  if p.div = B then raise (Done p.sigma);\r
147  if not (is_inert p.div) then problem_fail p "p.div converged"\r
148 ;;\r
149 \r
150 let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);;\r
151 \r
152 (* eat the arguments of the divergent and explode.\r
153  It does NOT perform any check, may fail if done unsafely *)\r
154 let eat p =\r
155 print_cmd "EAT" "";\r
156  let var, n = get_inert p.div in\r
157  let subst = var, mk_lams B n in\r
158  let p = subst_in_problem subst p in\r
159  sanity p; p\r
160 ;;\r
161 \r
162 (* step on the head of div, on the k-th argument, with n fresh vars *)\r
163 let step k n p =\r
164  let var, _ = get_inert p.div in\r
165 print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")");\r
166  let p, t = (* apply fresh vars *)\r
167   fold_nat (fun (p, t) _ ->\r
168     let p, v = freshvar p in\r
169     p, A(t, V (v + k + 1))\r
170   ) (p, V 0) n in\r
171  let t = (* apply unused bound variables V_{k-1}..V_1 *)\r
172   fold_nat (fun t m -> A(t, V (k-m+1))) t k in\r
173  let t = mk_lams t (k+1) in (* make leading lambdas *)\r
174  let subst = var, t in\r
175  let p = subst_in_problem subst p in\r
176  sanity p; p\r
177 ;;\r
178 \r
179 let parse strs =\r
180   let rec aux level = function\r
181   | Parser_andrea.Lam t -> L (aux (level + 1) t)\r
182   | Parser_andrea.App (t1, t2) ->\r
183    if level = 0 then mk_app (aux level t1) (aux level t2)\r
184     else A(aux level t1, aux level t2)\r
185   | Parser_andrea.Var v -> V v\r
186   in let (tms, free) = Parser_andrea.parse_many strs\r
187   in (List.map (aux 0) tms, free)\r
188 ;;\r
189 \r
190 let problem_of div conv =\r
191  print_hline ();\r
192  let all_tms, var_names = parse ([div; conv]) in\r
193  let div, conv = List.hd all_tms, List.hd (List.tl all_tms) in\r
194  let varno = List.length var_names in\r
195  let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; stepped=[]} in\r
196  (* activate bombs *)\r
197  let p = try\r
198   let subst = Util.index_of "BOMB" var_names, L B in\r
199    subst_in_problem subst p\r
200   with Not_found -> p in\r
201  (* initial sanity check *)\r
202  sanity p; p\r
203 ;;\r
204 \r
205 let exec div conv cmds =\r
206  let p = problem_of div conv in\r
207  try\r
208   problem_fail (List.fold_left (|>) p cmds) "Problem not completed"\r
209  with\r
210  | Done _ -> ()\r
211 ;;\r
212 \r
213 let inert_cut_at n t =\r
214  let rec aux t =\r
215   match t with\r
216   | V _ as t -> 0, t\r
217   | A(t1,_) as t ->\r
218     let k', t' = aux t1 in\r
219      if k' = n then n, t'\r
220       else k'+1, t\r
221   | _ -> assert false\r
222  in snd (aux t)\r
223 ;;\r
224 \r
225 let find_eta_difference p t n_args =\r
226  let t = inert_cut_at n_args t in\r
227  let rec aux t u k = match t, u with\r
228  | V _, V _ -> assert false (* div subterm of conv *)\r
229  | A(t1,t2), A(u1,u2) ->\r
230     if not (eta_eq t2 u2) then (print_endline((string_of_t t2) ^ " <> " ^ (string_of_t u2)); k)\r
231     else aux t1 u1 (k-1)\r
232  | _, _ -> assert false\r
233  in aux p.div t n_args\r
234 ;;\r
235 \r
236 let rec no_leading_lambdas = function\r
237  | L t -> 1 + no_leading_lambdas t\r
238  | _ -> 0\r
239 ;;\r
240 \r
241 let compute_max_lambdas_at hd_var j =\r
242  let rec aux hd = function\r
243  | A(t1,t2) ->\r
244     (if get_inert t1 = (hd, j)\r
245       then max ( (*FIXME*)\r
246        if is_inert t2 && let hd', j' = get_inert t2 in hd' = hd\r
247         then let hd', j' = get_inert t2 in j - j'\r
248         else no_leading_lambdas t2)\r
249       else id) (max (aux hd t1) (aux hd t2))\r
250  | L t -> aux (hd+1) t\r
251  | V _ -> 0\r
252  | _ -> assert false\r
253  in aux hd_var\r
254 ;;\r
255 \r
256 let rec auto p =\r
257  let hd_var, n_args = get_inert p.div in\r
258  match get_subterm_with_head_and_args hd_var n_args p.conv with\r
259  | None ->\r
260     (try problem_fail (eat p) "Auto did not complete the problem"  with Done _ -> ())\r
261  | Some t ->\r
262   let j = find_eta_difference p t n_args - 1 in\r
263   let k = 1 + max\r
264    (compute_max_lambdas_at hd_var j p.div)\r
265     (compute_max_lambdas_at hd_var j p.conv) in\r
266   let p = step j k p in\r
267   auto p\r
268 ;;\r
269 \r
270 let interactive div conv cmds =\r
271  let p = problem_of div conv in\r
272  try (\r
273  let p = List.fold_left (|>) p cmds in\r
274  let rec f p cmds =\r
275   let nth spl n = int_of_string (List.nth spl n) in\r
276   let read_cmd () =\r
277    let s = read_line () in\r
278    let spl = Str.split (Str.regexp " +") s in\r
279    s, let uno = List.hd spl in\r
280     try if uno = "eat" then eat\r
281     else if uno = "step" then step (nth spl 1) (nth spl 2)\r
282     else failwith "Wrong input."\r
283     with Failure s -> print_endline s; (fun x -> x) in\r
284   let str, cmd = read_cmd () in\r
285   let cmds = (" " ^ str ^ ";")::cmds in\r
286   try\r
287    let p = cmd p in f p cmds\r
288   with\r
289   | Done _ -> print_endline "Done! Commands history: "; List.iter print_endline (List.rev cmds)\r
290  in f p []\r
291  ) with Done _ -> ()\r
292 ;;\r
293 \r
294 let rec conv_join = function\r
295  | [] -> "@"\r
296  | x::xs -> conv_join xs ^ " ("^ x ^")"\r
297 ;;\r
298 \r
299 let auto' a b = auto (problem_of a (conv_join b));;\r
300 \r
301 (* Example usage of exec, interactive:\r
302 \r
303 exec\r
304  "x x"\r
305  (conv_join["x y"; "y y"; "y x"])\r
306  [ step 0 1; eat ]\r
307 ;;\r
308 \r
309 interactive "x y"\r
310  "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat]\r
311 ;;\r
312 \r
313 *)\r
314 \r
315 auto' "x x" ["x y"; "y y"; "y x"] ;;\r
316 auto' "x y" ["x (_. x)"; "y z"; "y x"] ;;\r
317 auto' "a (x. x b) (x. x c)" ["a (x. b b) @"; "a @ c"; "a (x. x x) a"; "a (a a a) (a c c)"] ;;\r
318 \r
319 auto' "x (y. x y y)" ["x (y. x y x)"] ;;\r
320 \r
321 auto' "x a a a a" [\r
322  "x b a a a";\r
323  "x a b a a";\r
324  "x a a b a";\r
325  "x a a a b";\r
326 ] ;;\r
327 \r
328 (* Controesempio ad usare un conto dei lambda che non considere le permutazioni *)\r
329 auto' "x a a a a (x (x. x x) @ @ (_._.x. x x) x) b b b" [\r
330  "x a a a a (_. a) b b b";\r
331  "x a a a a (_. _. _. _. x. y. x y)";\r
332 ] ;;\r
333 \r
334 \r
335 print_hline();\r
336 print_endline "ALL DONE. "\r