1 let (++) f g x = f (g 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
5 let print_hline = Console.print_hline;;
\r
18 let delta = L(A(V 0, V 0));;
\r
20 let rec is_stuck = function
\r
22 | A(t,_) -> is_stuck t
\r
27 let rec aux l1 l2 t1 t2 = match t1, t2 with
\r
28 | _, _ when is_stuck t1 || is_stuck t2 -> true
\r
29 | L t1, L t2 -> aux l1 l2 t1 t2
\r
30 | L t1, t2 -> aux l1 (l2+1) t1 t2
\r
31 | t1, L t2 -> aux (l1+1) l2 t1 t2
\r
32 | V a, V b -> a + l1 = b + l2
\r
33 | A(t1,t2), A(u1,u2) -> aux l1 l2 t1 u1 && aux l1 l2 t2 u2
\r
36 let eta_eq = eta_eq' 0 0;;
\r
38 (* is arg1 eta-subterm of arg2 ? *)
\r
40 let rec aux lev t = if t = C then false else (eta_eq' lev 0 u t || match t with
\r
41 | L t -> aux (lev+1) t
\r
42 | A(t1, t2) -> aux lev t1 || aux lev t2
\r
47 (* does NOT lift the argument *)
\r
48 let mk_lams = fold_nat (fun x _ -> L x) ;;
\r
51 let string_of_bvar =
\r
52 let bound_vars = ["x"; "y"; "z"; "w"; "q"] in
\r
53 let bvarsno = List.length bound_vars in
\r
54 fun nn -> if nn < bvarsno then List.nth bound_vars nn else "x" ^ (string_of_int (nn - bvarsno + 1)) in
\r
55 let rec string_of_term_w_pars level = function
\r
56 | V v -> if v >= level then "`" ^ string_of_int (v-level) else
\r
57 string_of_bvar (level - v-1)
\r
60 | 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 -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t
\r
67 | _ as t -> string_of_term_no_pars_app level t
\r
68 in string_of_term_no_pars 0
\r
76 ; sigma : (var * t) list (* substitutions *)
\r
77 ; stepped : var list
\r
78 ; phase : [`One | `Two] (* :'( *)
\r
81 let string_of_problem p =
\r
83 "[stepped] " ^ String.concat " " (List.map string_of_int p.stepped);
\r
84 "[DV] " ^ string_of_t p.div;
\r
85 "[CV] " ^ string_of_t p.conv;
\r
87 String.concat "\n" lines
\r
90 exception Done of (var * t) list (* substitution *);;
\r
91 exception Fail of int * string;;
\r
93 let problem_fail p reason =
\r
94 print_endline "!!!!!!!!!!!!!!! FAIL !!!!!!!!!!!!!!!";
\r
95 print_endline (string_of_problem p);
\r
96 raise (Fail (-1, reason))
\r
99 let freshvar ({freshno} as p) =
\r
100 {p with freshno=freshno+1}, freshno+1
\r
105 | A(t,_) -> is_inert t
\r
111 let rec get_inert = function
\r
112 | V _ | C as t -> (t,0)
\r
113 | A(t, _) -> let hd,args = get_inert t in hd,args+1
\r
114 | _ -> assert false
\r
117 let args_of_inert =
\r
121 | A(t, a) -> aux (a::acc) t
\r
122 | _ -> assert false
\r
127 (* precomputes the number of leading lambdas in a term,
\r
128 after replacing _v_ w/ a term starting with n lambdas *)
\r
129 let rec no_leading_lambdas v n = function
\r
130 | L t -> 1 + no_leading_lambdas (v+1) n t
\r
131 | A _ as t -> let v', m = get_inert t in if V v = v' then max 0 (n - m) else 0
\r
132 | V v' -> if v = v' then n else 0
\r
136 let rec subst level delift sub =
\r
138 | V v -> if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v)
\r
139 | L t -> let t = subst (level + 1) delift sub t in if t = B then B else L t
\r
141 let t1 = subst level delift sub t1 in
\r
142 let t2 = subst level delift sub t2 in
\r
145 and mk_app t1 t2 = if t2 = B || (t1 = delta && t2 = delta) then B
\r
148 | L t1 -> subst 0 true (0, t2) t1
\r
153 | V m -> V (if m >= lev then m + n else m)
\r
154 | L t -> L (aux (lev+1) t)
\r
155 | A (t1, t2) -> A (aux lev t1, aux lev t2)
\r
159 let subst = subst 0 false;;
\r
161 let subst_in_problem ((v, t) as sub) p =
\r
162 print_endline ("-- SUBST " ^ string_of_t (V v) ^ " |-> " ^ string_of_t t);
\r
164 div=subst sub p.div;
\r
165 conv=subst sub p.conv;
\r
166 stepped=v::p.stepped;
\r
167 sigma=sub::p.sigma}
\r
170 let get_subterms_with_head hd_var =
\r
171 let rec aux lev inert_done = function
\r
172 | C | V _ | B -> []
\r
173 | L t -> aux (lev+1) false t
\r
175 let hd_var', n_args' = get_inert t1 in
\r
176 if not inert_done && hd_var' = V (hd_var + lev)
\r
177 then lift ~-lev t :: aux lev true t1 @ aux lev false t2
\r
178 else aux lev true t1 @ aux lev false t2
\r
182 let rec purify = function
\r
183 | L t -> Pure.L (purify t)
\r
184 | A (t1,t2) -> Pure.A (purify t1, purify t2)
\r
186 | C -> Pure.V (min_int/2)
\r
190 let check p sigma =
\r
191 print_endline "Checking...";
\r
192 let div = purify p.div in
\r
193 let conv = purify p.conv in
\r
194 let sigma = List.map (fun (v,t) -> v, purify t) sigma in
\r
195 let freshno = List.fold_right (max ++ fst) sigma 0 in
\r
196 let env = Pure.env_of_sigma freshno sigma in
\r
197 assert (Pure.diverged (Pure.mwhd (env,div,[])));
\r
198 print_endline " D diverged.";
\r
199 assert (not (Pure.diverged (Pure.mwhd (env,conv,[]))));
\r
200 print_endline " C converged.";
\r
205 print_endline (string_of_problem p); (* non cancellare *)
\r
206 if p.conv = B then problem_fail p "p.conv diverged";
\r
207 if p.div = B then raise (Done p.sigma);
\r
208 if p.phase = `Two && p.div = delta then raise (Done p.sigma);
\r
209 if not (is_inert p.div) then problem_fail p "p.div converged";
\r
213 (* drops the arguments of t after the n-th *)
\r
214 (* FIXME! E' usato in modo improprio contando sul fatto
\r
215 errato che ritorna un inerte lungo esattamente n *)
\r
216 let inert_cut_at n t =
\r
221 let k', t' = aux t1 in
\r
222 if k' = n then n, t'
\r
224 | _ -> assert false
\r
228 (* return the index of the first argument with a difference
\r
229 (the first argument is 0) *)
\r
230 let find_eta_difference p t =
\r
231 let divargs = args_of_inert p.div in
\r
232 let conargs = args_of_inert t in
\r
233 let rec aux k divargs conargs =
\r
234 match divargs,conargs with
\r
237 | t1::divargs,t2::conargs ->
\r
238 (if not (eta_eq t1 t2) then [k] else []) @ aux (k+1) divargs conargs
\r
240 aux 0 divargs conargs
\r
243 let compute_max_lambdas_at hd_var j =
\r
244 let rec aux hd = function
\r
246 (if get_inert t1 = (V hd, j)
\r
247 then max ( (*FIXME*)
\r
248 if is_inert t2 && let hd', j' = get_inert t2 in hd' = V hd
\r
249 then let hd', j' = get_inert t2 in j - j'
\r
250 else no_leading_lambdas hd_var j t2)
\r
251 else id) (max (aux hd t1) (aux hd t2))
\r
252 | L t -> aux (hd+1) t
\r
254 | _ -> assert false
\r
258 let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);;
\r
260 (* eat the arguments of the divergent and explode.
\r
261 It does NOT perform any check, may fail if done unsafely *)
\r
263 print_cmd "EAT" "";
\r
264 let var, k = get_inert p.div in
\r
266 | C | L _ | B | A _ -> assert false
\r
268 let phase = p.phase in
\r
273 (compute_max_lambdas_at var (k-1) p.div)
\r
274 (compute_max_lambdas_at var (k-1) p.conv) in
\r
275 (* apply fresh vars *)
\r
276 let p, t = fold_nat (fun (p, t) _ ->
\r
277 let p, v = freshvar p in
\r
280 let p = {p with phase=`Two} in
\r
281 let t = A(t, delta) in
\r
282 let t = fold_nat (fun t m -> A(t, V (k-m))) t (k-1) in
\r
283 let subst = var, mk_lams t k in
\r
284 let p = subst_in_problem subst p in
\r
285 let _, args = get_inert p.div in
\r
286 {p with div = inert_cut_at (args-k) p.div}
\r
288 let subst = var, mk_lams delta k in
\r
289 subst_in_problem subst p in
\r
293 (* step on the head of div, on the k-th argument, with n fresh vars *)
\r
295 let hd, _ = get_inert p.div in
\r
297 | C | L _ | B | A _ -> assert false
\r
299 print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (on " ^ string_of_int (k+1) ^ "th)");
\r
300 let p, t = (* apply fresh vars *)
\r
301 fold_nat (fun (p, t) _ ->
\r
302 let p, v = freshvar p in
\r
303 p, A(t, V (v + k + 1))
\r
305 let t = (* apply unused bound variables V_{k-1}..V_1 *)
\r
306 fold_nat (fun t m -> A(t, V (k-m+1))) t k in
\r
307 let t = mk_lams t (k+1) in (* make leading lambdas *)
\r
308 let subst = var, t in
\r
309 let p = subst_in_problem subst p in
\r
315 let hd, n_args = get_inert p.div in
\r
317 | C | L _ | B | A _ -> assert false
\r
319 let tms = get_subterms_with_head hd_var p.conv in
\r
320 if List.exists (fun t -> snd (get_inert t) >= n_args) tms
\r
322 (* let tms = List.sort (fun t1 t2 -> - compare (snd (get_inert t1)) (snd (get_inert t2))) tms in *)
\r
323 List.iter (fun t -> try
\r
324 let js = find_eta_difference p t in
\r
325 (* print_endline (String.concat ", " (List.map string_of_int js)); *)
\r
326 if js = [] then problem_fail p "no eta difference found (div subterm of conv?)";
\r
327 let js = List.rev js in
\r
332 (compute_max_lambdas_at hd_var j p.div)
\r
333 (compute_max_lambdas_at hd_var j p.conv) in
\r
334 ignore (aux (step j k p))
\r
336 print_endline ("Backtracking (eta_diff) because: " ^ s)) js;
\r
337 raise (Fail(-1, "no eta difference"))
\r
339 print_endline ("Backtracking (get_subterms) because: " ^ s)) tms;
\r
340 raise (Fail(-1, "no similar terms"))
\r
343 (let phase = p.phase in
\r
346 then problem_fail p "Auto.2 did not complete the problem"
\r
351 with Done sigma -> sigma
\r
354 let problem_of (label, div, convs, ps, var_names) =
\r
356 let rec aux lev = function
\r
357 | `Lam(_, t) -> L (aux (lev+1) t)
\r
358 | `I (v, args) -> Listx.fold_left (fun x y -> mk_app x (aux lev y)) (aux lev (`Var v)) args
\r
359 | `Var(v,_) -> if v >= lev && List.nth var_names (v-lev) = "C" then C else V v
\r
360 | `N _ | `Match _ -> assert false in
\r
361 assert (List.length ps = 0);
\r
362 let convs = List.rev convs in
\r
363 let conv = List.fold_left (fun x y -> mk_app x (aux 0 (y :> Num.nf))) (V (List.length var_names)) convs in
\r
364 let var_names = "@" :: var_names in
\r
365 let div = match div with
\r
366 | Some div -> aux 0 (div :> Num.nf)
\r
367 | None -> assert false in
\r
368 let varno = List.length var_names in
\r
369 let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; stepped=[]; phase=`One} in
\r
370 (* initial sanity check *)
\r
375 if is_stuck p.div then print_endline "!!! div is stuck. Problem was not run !!!"
\r
376 else if eta_subterm p.div p.conv
\r
377 then print_endline "!!! div is subterm of conv. Problem was not run !!!"
\r
378 else check p (auto p)
\r
381 Problems.main (solve ++ problem_of);
\r
383 (* Example usage of interactive: *)
\r
385 (* let interactive div conv cmds =
\r
386 let p = problem_of div conv in
\r
388 let p = List.fold_left (|>) p cmds in
\r
390 let nth spl n = int_of_string (List.nth spl n) in
\r
392 let s = read_line () in
\r
393 let spl = Str.split (Str.regexp " +") s in
\r
394 s, let uno = List.hd spl in
\r
395 try if uno = "eat" then eat
\r
396 else if uno = "step" then step (nth spl 1) (nth spl 2)
\r
397 else failwith "Wrong input."
\r
398 with Failure s -> print_endline s; (fun x -> x) in
\r
399 let str, cmd = read_cmd () in
\r
400 let cmds = (" " ^ str ^ ";")::cmds in
\r
402 let p = cmd p in f p cmds
\r
404 | Done _ -> print_endline "Done! Commands history: "; List.iter print_endline (List.rev cmds)
\r
406 ) with Done _ -> ()
\r
409 (* interactive "x y"
\r
410 "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat]
\r