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 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
26 (* does NOT lift t *)
\r
27 let mk_lams = fold_nat (fun x _ -> L x) ;;
\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
38 | L _ as t -> "(" ^ string_of_term_no_pars level t ^ ")"
\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
54 ; sigma : (var * t) list (* substitutions *)
\r
55 ; stepped : var list
\r
58 let string_of_problem p =
\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
64 String.concat "\n" lines
\r
67 exception Done of (var * t) list (* substitution *);;
\r
68 exception Fail of int * string;;
\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
76 let freshvar ({freshno} as p) =
\r
77 {p with freshno=freshno+1}, freshno+1
\r
82 | A(t,_) -> is_inert t
\r
87 let is_var = function V _ -> true | _ -> false;;
\r
88 let is_lambda = function L _ -> true | _ -> false;;
\r
90 let rec get_inert = function
\r
92 | A(t, _) -> let hd,args = get_inert t in hd,args+1
\r
96 let rec subst level delift sub =
\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
101 let t1 = subst level delift sub t1 in
\r
102 let t2 = subst level delift sub t2 in
\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
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
118 let subst = subst 0 false;;
\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
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
129 let get_subterm_with_head_and_args hd_var n_args =
\r
130 let rec aux lev = function
\r
132 | L t -> aux (lev+1) 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
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
150 let print_cmd s1 s2 = print_endline (">> " ^ s1 ^ " " ^ s2);;
\r
152 (* eat the arguments of the divergent and explode.
\r
153 It does NOT perform any check, may fail if done unsafely *)
\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
162 (* step on the head of div, on the k-th argument, with n fresh vars *)
\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
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
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
190 let problem_of div conv =
\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
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
205 let exec div conv cmds =
\r
206 let p = problem_of div conv in
\r
208 problem_fail (List.fold_left (|>) p cmds) "Problem not completed"
\r
213 let inert_cut_at n t =
\r
218 let k', t' = aux t1 in
\r
219 if k' = n then n, t'
\r
221 | _ -> assert false
\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
236 let rec no_leading_lambdas = function
\r
237 | L t -> 1 + no_leading_lambdas t
\r
241 let compute_max_lambdas_at hd_var j =
\r
242 let rec aux hd = function
\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
252 | _ -> assert false
\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
260 (try problem_fail (eat p) "Auto did not complete the problem" with Done _ -> ())
\r
262 let j = find_eta_difference p t n_args - 1 in
\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
270 let interactive div conv cmds =
\r
271 let p = problem_of div conv in
\r
273 let p = List.fold_left (|>) p cmds in
\r
275 let nth spl n = int_of_string (List.nth spl n) in
\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
287 let p = cmd p in f p cmds
\r
289 | Done _ -> print_endline "Done! Commands history: "; List.iter print_endline (List.rev cmds)
\r
291 ) with Done _ -> ()
\r
294 let rec conv_join = function
\r
296 | x::xs -> conv_join xs ^ " ("^ x ^")"
\r
299 let auto' a b = auto (problem_of a (conv_join b));;
\r
301 (* Example usage of exec, interactive:
\r
305 (conv_join["x y"; "y y"; "y x"])
\r
310 "@ (x x) (y x) (y z)" [step 0 1; step 0 2; eat]
\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
319 auto' "x (y. x y y)" ["x (y. x y x)"] ;;
\r
321 auto' "x a a a a" [
\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
336 print_endline "ALL DONE. "
\r