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
16 let rec measure_of_t = function
\r
18 | A(b,t1,t2) -> (if b then 1 else 0) + measure_of_t t1 + measure_of_t t2
\r
19 | L(b,t) -> if b then measure_of_t t else 0
\r
23 let string_of_bvar =
\r
24 let bound_vars = ["x"; "y"; "z"; "w"; "q"] in
\r
25 let bvarsno = List.length bound_vars in
\r
26 fun nn -> if nn < bvarsno then List.nth bound_vars nn else "x" ^ (string_of_int (nn - bvarsno + 1)) in
\r
27 let rec string_of_term_w_pars level = function
\r
28 | V v -> if v >= level then "`" ^ string_of_int (v-level) else
\r
29 string_of_bvar (level - v-1)
\r
31 | L _ as t -> "(" ^ string_of_term_no_pars level t ^ ")"
\r
32 and string_of_term_no_pars_app level = function
\r
33 | A(b,t1,t2) -> string_of_term_no_pars_app level t1 ^ (if b then "," else " ") ^ string_of_term_w_pars level t2
\r
34 | _ as t -> string_of_term_w_pars level t
\r
35 and string_of_term_no_pars level = function
\r
36 | L(_,t) -> "λ" ^ string_of_bvar level ^ ". " ^ string_of_term_no_pars (level+1) t
\r
37 | _ as t -> string_of_term_no_pars_app level t
\r
38 in string_of_term_no_pars 0
\r
42 let delta = L(true,A(true,V 0, V 0));;
\r
44 (* does NOT lift the argument *)
\r
45 let mk_lams = fold_nat (fun x _ -> L(false,x)) ;;
\r
52 ; sigma : (var * t) list (* substitutions *)
\r
53 ; phase : [`One | `Two] (* :'( *)
\r
56 let string_of_problem p =
\r
58 "[measure] " ^ string_of_int (measure_of_t p.div);
\r
59 "[DV] " ^ string_of_t p.div;
\r
60 "[CV] " ^ string_of_t p.conv;
\r
62 String.concat "\n" lines
\r
66 exception Done of (var * t) list (* substitution *);;
\r
67 exception Fail of int * string;;
\r
69 let problem_fail p reason =
\r
70 print_endline "!!!!!!!!!!!!!!! FAIL !!!!!!!!!!!!!!!";
\r
71 print_endline (string_of_problem p);
\r
72 raise (Fail (-1, reason))
\r
75 let freshvar ({freshno} as p) =
\r
76 {p with freshno=freshno+1}, freshno+1
\r
81 | A(_,t,_) -> is_inert t
\r
86 let is_var = function V _ -> true | _ -> false;;
\r
87 let is_lambda = function L _ -> true | _ -> false;;
\r
89 let rec get_inert = function
\r
91 | A(_,t,_) -> let hd,args = get_inert t in hd,args+1
\r
95 (* precomputes the number of leading lambdas in a term,
\r
96 after replacing _v_ w/ a term starting with n lambdas *)
\r
97 let rec no_leading_lambdas v n = function
\r
98 | L(_,t) -> 1 + no_leading_lambdas (v+1) n t
\r
99 | A _ as t -> let v', m = get_inert t in if v = v' then max 0 (n - m) else 0
\r
100 | V v' -> if v = v' then n else 0
\r
103 (* b' defaults to false *)
\r
104 let rec subst b' level delift sub =
\r
106 | V v -> if v = level + fst sub then lift level (snd sub) else V (if delift && v > level then v-1 else v)
\r
107 | L(b,t) -> L(b, subst b' (level + 1) delift sub t)
\r
108 | A(_,t1,(V v as t2)) when b' && v = level + fst sub ->
\r
109 mk_app b' (subst b' level delift sub t1) (subst b' level delift sub t2)
\r
111 mk_app b (subst b' level delift sub t1) (subst b' level delift sub t2)
\r
112 and mk_app b' t1 t2 = if t1 = delta && t2 = delta then raise B
\r
114 | L(b,t1) -> subst (b' && not b) 0 true (0, t2) t1
\r
115 | _ -> A (b', t1, t2)
\r
119 | V m -> V (if m >= lev then m + n else m)
\r
120 | L(b,t) -> L(b,aux (lev+1) t)
\r
121 | A (b,t1, t2) -> A (b,aux lev t1, aux lev t2)
\r
124 let subst = subst false 0 false;;
\r
125 let mk_app = mk_app true;;
\r
128 let rec aux t1 t2 = match t1, t2 with
\r
129 | L(_,t1), L(_,t2) -> aux t1 t2
\r
130 | L(_,t1), t2 -> aux t1 (A(true,lift 1 t2,V 0))
\r
131 | t1, L(_,t2) -> aux (A(true,lift 1 t1,V 0)) t2
\r
132 | V a, V b -> a = b
\r
133 | A(_,t1,t2), A(_,u1,u2) -> aux t1 u1 && aux t2 u2
\r
137 (* is arg1 eta-subterm of arg2 ? *)
\r
138 let eta_subterm u =
\r
139 let rec aux lev t = eta_eq u (lift lev t) || match t with
\r
140 | L(_, t) -> aux (lev+1) t
\r
141 | A(_, t1, t2) -> aux lev t1 || aux lev t2
\r
146 let subst_in_problem ((v, t) as sub) p =
\r
147 print_endline ("-- SUBST " ^ string_of_t (V v) ^ " |-> " ^ string_of_t t);
\r
148 let sigma = sub::p.sigma in
\r
149 let div = try subst sub p.div with B -> raise (Done sigma) in
\r
150 let conv = try subst sub p.conv with B -> raise (Fail(-1,"p.conv diverged")) in
\r
151 {p with div; conv; sigma}
\r
154 let get_subterm_with_head_and_args hd_var n_args =
\r
155 let rec aux lev = function
\r
157 | L(_,t) -> aux (lev+1) t
\r
158 | A(_,t1,t2) as t ->
\r
159 let hd_var', n_args' = get_inert t1 in
\r
160 if hd_var' = hd_var + lev && n_args <= 1 + n_args'
\r
161 (* the `+1` above is because of t2 *)
\r
162 then Some (lift ~-lev t)
\r
163 else match aux lev t2 with
\r
164 | None -> aux lev t1
\r
165 | Some _ as res -> res
\r
169 let rec purify = function
\r
170 | L(_,t) -> Pure.L (purify t)
\r
171 | A(_,t1,t2) -> Pure.A (purify t1, purify t2)
\r
175 let check p sigma =
\r
176 print_endline "Checking...";
\r
177 let div = purify p.div in
\r
178 let conv = purify p.conv in
\r
179 let sigma = List.map (fun (v,t) -> v, purify t) sigma in
\r
180 let freshno = List.fold_right (max ++ fst) sigma 0 in
\r
181 let env = Pure.env_of_sigma freshno sigma in
\r
182 assert (Pure.diverged (Pure.mwhd (env,div,[])));
\r
183 print_endline " D diverged.";
\r
184 assert (not (Pure.diverged (Pure.mwhd (env,conv,[]))));
\r
185 print_endline " C converged.";
\r
190 print_endline (string_of_problem p); (* non cancellare *)
\r
191 if p.phase = `Two && p.div = delta then raise (Done p.sigma);
\r
192 if not (is_inert p.div) then problem_fail p "p.div converged";
\r
196 (* drops the arguments of t after the n-th *)
\r
197 (* FIXME! E' usato in modo improprio contando sul fatto
\r
198 errato che ritorna un inerte lungo esattamente n *)
\r
199 let inert_cut_at n t =
\r
203 | A(_,t1,_) as t ->
\r
204 let k', t' = aux t1 in
\r
205 if k' = n then n, t'
\r
207 | _ -> assert false
\r
211 (* return the index of the first argument with a difference
\r
212 (the first argument is 0)
\r
213 precondition: p.div and t have n+1 arguments
\r
215 let find_eta_difference p t argsno =
\r
216 let t = inert_cut_at argsno t in
\r
217 let rec aux t u k = match t, u with
\r
219 | A(_,t1,t2), A(_,u1,u2) ->
\r
220 (match aux t1 u1 (k-1) with
\r
222 if not (eta_eq t2 u2) then Some (k-1)
\r
224 | Some j -> Some j)
\r
225 | _, _ -> assert false
\r
226 in match aux p.div t argsno with
\r
227 | None -> problem_fail p "no eta difference found (div subterm of conv?)"
\r
231 let compute_max_lambdas_at hd_var j =
\r
232 let rec aux hd = function
\r
234 (if get_inert t1 = (hd, j)
\r
235 then max ( (*FIXME*)
\r
236 if is_inert t2 && let hd', j' = get_inert t2 in hd' = hd
\r
237 then let hd', j' = get_inert t2 in j - j'
\r
238 else no_leading_lambdas hd_var j t2)
\r
239 else id) (max (aux hd t1) (aux hd t2))
\r
240 | L(_,t) -> aux (hd+1) t
\r
245 let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);;
\r
247 (* eat the arguments of the divergent and explode.
\r
248 It does NOT perform any check, may fail if done unsafely *)
\r
250 print_cmd "EAT" "";
\r
251 let var, k = get_inert p.div in
\r
252 let phase = p.phase in
\r
257 (compute_max_lambdas_at var (k-1) p.div)
\r
258 (compute_max_lambdas_at var (k-1) p.conv) in
\r
259 (* apply fresh vars *)
\r
260 let p, t = fold_nat (fun (p, t) _ ->
\r
261 let p, v = freshvar p in
\r
262 p, A(false, t, V (v + k))
\r
264 let p = {p with phase=`Two} in
\r
265 let t = A(false, t, delta) in
\r
266 let t = fold_nat (fun t m -> A(false, t, V (k-m))) t (k-1) in
\r
267 let subst = var, mk_lams t k in
\r
268 let p = subst_in_problem subst p in
\r
269 let _, args = get_inert p.div in
\r
270 {p with div = inert_cut_at (args-k) p.div}
\r
272 let subst = var, mk_lams delta k in
\r
273 subst_in_problem subst p in
\r
277 (* step on the head of div, on the k-th argument, with n fresh vars *)
\r
279 let var, _ = get_inert p.div in
\r
280 print_cmd "STEP" ("on " ^ string_of_t (V var) ^ " (of:" ^ string_of_int n ^ ")");
\r
281 let p, t = (* apply fresh vars *)
\r
282 fold_nat (fun (p, t) _ ->
\r
283 let p, v = freshvar p in
\r
284 p, A(false, t, V (v + k + 1))
\r
286 let t = (* apply bound variables V_k..V_0 *)
\r
287 fold_nat (fun t m -> A(false, t, V (k-m+1))) t (k+1) in
\r
288 let t = mk_lams t (k+1) in (* make leading lambdas *)
\r
289 let subst = var, t in
\r
290 let p = subst_in_problem subst p in
\r
295 let compute_max_arity =
\r
296 let rec aux n = function
\r
297 | A(_,t1,t2) -> max (aux (n+1) t1) (aux 0 t2)
\r
298 | L(_,t) -> max n (aux 0 t)
\r
301 print_cmd "FINISH" "";
\r
302 let div_hd, div_nargs = get_inert p.div in
\r
303 let j = div_nargs - 1 in
\r
304 let arity = compute_max_arity p.conv in
\r
305 let n = 1 + arity + max
\r
306 (compute_max_lambdas_at div_hd j p.div)
\r
307 (compute_max_lambdas_at div_hd j p.conv) in
\r
308 let p = step j n p in
\r
309 let div_hd, div_nargs = get_inert p.div in
\r
310 let rec aux m = function
\r
311 A(_,t1,t2) -> if is_var t2 then
\r
312 (let delta_var, _ = get_inert t2 in
\r
313 if delta_var <> div_hd && get_subterm_with_head_and_args delta_var 1 p.conv = None
\r
315 else aux (m-1) t1) else aux (m-1) t1
\r
316 | _ -> assert false in
\r
317 let m, delta_var = aux div_nargs p.div in
\r
318 let p = subst_in_problem (delta_var, delta) p in
\r
319 let p = subst_in_problem (div_hd, mk_lams delta (m-1)) p in
\r
324 let hd_var, n_args = get_inert p.div in
\r
325 match get_subterm_with_head_and_args hd_var n_args p.conv with
\r
327 (try problem_fail (finish p) "Auto.2 did not complete the problem"
\r
328 with Done sigma -> sigma)
\r
331 let phase = p.phase in
\r
334 then problem_fail p "Auto.2 did not complete the problem"
\r
336 with Done sigma -> sigma)
\r
339 let j = find_eta_difference p t n_args in
\r
341 (compute_max_lambdas_at hd_var j p.div)
\r
342 (compute_max_lambdas_at hd_var j p.conv) in
\r
343 let m1 = measure_of_t p.div in
\r
344 let p = step j k p in
\r
345 let m2 = measure_of_t p.div in
\r
347 (print_string "WARNING! Measure did not decrease (press <Enter>)";
\r
348 ignore(read_line())));
\r
352 let problem_of (label, div, convs, ps, var_names) =
\r
354 let rec aux = function
\r
355 | `Lam(_, t) -> L (true,aux t)
\r
356 | `I ((v,_), args) -> Listx.fold_left (fun x y -> mk_app x (aux y)) (V v) args
\r
358 | `N _ | `Match _ -> assert false in
\r
359 assert (List.length ps = 0);
\r
360 let convs = List.rev convs in
\r
361 let conv = List.fold_left (fun x y -> mk_app x (aux (y :> Num.nf))) (V (List.length var_names)) convs in
\r
362 let var_names = "@" :: var_names in
\r
363 let div = match div with
\r
364 | Some div -> aux (div :> Num.nf)
\r
365 | None -> assert false in
\r
366 let varno = List.length var_names in
\r
367 let p = {orig_freshno=varno; freshno=1+varno; div; conv; sigma=[]; phase=`One} in
\r
368 (* initial sanity check *)
\r
373 if eta_subterm p.div p.conv
\r
374 then print_endline "!!! div is subterm of conv. Problem was not run !!!"
\r
375 else check p (auto p)
\r
378 Problems.main (solve ++ problem_of);
\r
380 (* Example usage of interactive: *)
\r
382 (* let interactive div conv cmds =
\r
383 let p = problem_of div conv in
\r
385 let p = List.fold_left (|>) p cmds in
\r
387 let nth spl n = int_of_string (List.nth spl n) in
\r
389 let s = read_line () in
\r
390 let spl = Str.split (Str.regexp " +") s in
\r
391 s, let uno = List.hd spl in
\r
392 try if uno = "eat" then eat
\r
393 else if uno = "step" then step (nth spl 1) (nth spl 2)
\r
394 else failwith "Wrong input."
\r
395 with Failure s -> print_endline s; (fun x -> x) in
\r
396 let str, cmd = read_cmd () in
\r
397 let cmds = (" " ^ str ^ ";")::cmds in
\r
399 let p = cmd p in f p cmds
\r
401 | Done _ -> print_endline "Done! Commands history: "; List.iter print_endline (List.rev cmds)
\r
403 ) with Done _ -> ()
\r
406 (* interactive "x y"
\r
407 "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat]
\r