6 let bomb = ref(`Var(-1,-666));;
9 The number of arguments which can applied to numbers
10 safely, depending on the encoding of numbers.
11 For Scott's encoding, two.
13 let num_more_args = 2;;
14 let _very_verbose = false;;
17 if _very_verbose then prerr_endline s
20 let convergent_dummy = `N ~-1;
24 ; div: i_var option (* None = bomb *)
25 ; conv: i_n_var list (* the inerts that must converge *)
26 ; ps: i_n_var list (* the n-th inert must become n *)
27 ; sigma: (int * nf) list (* the computed substitution *)
28 ; deltas: (int * nf) list ref list (* collection of all branches *)
29 ; initialSpecialK: int
31 ; var_names : string list (* names of the original free variables *)
34 let label_of_problem {label} = label;;
36 let string_of_var l x =
39 with Failure "nth" -> "`" ^ string_of_int x
41 let string_of_term p t = print ~l:p.var_names (t :> nf);;
44 (match p.div with None -> [] | Some t -> [(t :> i_n_var)])
49 let measure_of_term, measure_of_terms =
50 let rec aux = function
52 | `Var(_,ar) -> if ar = min_int then 0 else max 0 ar (*assert (ar >= 0); ar*)
54 | `I(v,args) -> aux (`Var v) + aux_many (Listx.to_list args :> nf list)
55 | `Match(u,(_,ar),_,_,args) -> aux (u :> nf) + (if ar <= 0 then 0 else ar - 1) + aux_many args
56 and aux_many tms = List.fold_right ((+) ++ aux) tms 0 in
57 (fun t -> aux (t :> nf)), (fun l -> aux_many (l :> nf list))
60 let sum_arities p = measure_of_terms (all_terms p)
62 let count_fakevars p =
63 let rec aux = function
65 | `Var(_,ar) -> if ar = min_int then 1 else 0
67 | `I(v,args) -> aux (`Var v) + aux_many (Listx.to_list args)
68 | `Match(u,v,_,_,args) -> aux (u :> nf) + aux (`Var v) + aux_many args
69 and aux_many tms = List.fold_right ((+) ++ aux) tms 0 in
70 aux_many (all_terms p :> nf list)
73 (* let problem_measure p = count_fakevars p, sum_arities p;;
74 let string_of_measure (a,b) = "(fakevars="^string_of_int a^",sum_arities="^string_of_int b^")" *)
76 let problem_measure p = sum_arities p;;
77 let string_of_measure = string_of_int;;
79 let string_of_problem label ({freshno; div; conv; ps; deltas} as p) =
80 let deltas = String.concat ("\n# ") (List.map (fun r -> String.concat " <> " (List.map (fun (i,_) -> string_of_int i) !r)) deltas) in
81 let l = p.var_names in
84 "# DISPLAY PROBLEM (" ^ label ^ ") " ^ "measure=" ^ string_of_measure (problem_measure p);
85 if List.length p.deltas > 1 then (
86 "# Discriminating sets (deltas):\n" ^
91 (match div with None -> "# no D" | Some div -> "D ("^string_of_int (measure_of_term div)^")"^ print ~l (div :> nf));
93 @ List.map (fun t -> if t = convergent_dummy then "#C" else "C ("^string_of_int (measure_of_term t)^") " ^ print ~l (t :> nf)) conv
94 @ List.mapi (fun i t -> string_of_int i ^ " ("^string_of_int (measure_of_term t)^") " ^ print ~l (t :> nf)) ps
99 let failwithProblem p reason =
100 print_endline (string_of_problem "FAIL" p);
104 let make_fresh_var p arity =
105 let freshno = p.freshno + 1 in
106 {p with freshno}, `Var(freshno,arity)
109 let make_fresh_vars p arities =
111 (fun arity (p, vars) -> let p, var = make_fresh_var p arity in p, var::vars)
116 let simple_expand_match ps =
117 let rec aux level = function
118 | #i_num_var as t -> (aux_i_num_var level t :> nf)
119 | `Lam(b,t) -> `Lam(b,aux (level+1) t)
120 and aux_i_num_var level = function
121 | `Match(u,v,bs_lift,bs,args) as torig ->
122 let (u : i_num_var) = aux_i_num_var level u in
123 bs := List.map (fun (n, x) -> n, aux 0 x) !bs;
127 let i = index_of ~eq:eta_eq (lift (-level) u) (ps :> nf list) in (* can raise Not_found *)
128 let t = cast_to_i_num_var (mk_match (`N i) v bs_lift bs (args :> nf list)) in
130 aux_i_num_var level t
132 | _ -> raise Not_found)
134 cast_to_i_num_var (mk_appl (`Match(u,v,bs_lift,bs,[])) (List.map (aux level) args)))
135 | `I(v,args) -> cast_to_i_num_var (mk_appl (`Var v) (List.map (aux level) (Listx.to_list args)))
136 | `N _ | `Var _ as t -> t
141 let rec aux x = let x' = f x in if x <> x' then aux x' else x in aux
144 let rec super_simplify_ps ps =
145 fixpoint (List.map (fun x -> cast_to_i_n_var (simple_expand_match ps (x :> i_num_var))))
148 let rec super_simplify_ps_with_match ps =
149 fixpoint (List.map (cast_to_i_num_var ++ (simple_expand_match ps)))
152 let super_simplify ({div; ps; conv} as p) =
153 let ps = super_simplify_ps p.ps p.ps in
154 let conv = super_simplify_ps ps p.conv in
155 let div = option_map (fun div ->
156 let divs = super_simplify_ps p.ps ([div] :> i_n_var list) in
158 {p with div=option_map cast_to_i_var div; ps; conv}
160 exception ExpandedToLambda;;
162 let subst_in_problem x inst ({freshno; div; conv; ps; sigma} as p) =
163 let len_ps = List.length ps in
164 (*(let l = Array.to_list (Array.init (freshno + 1) string_of_var) in
165 prerr_endline ("# INST0: " ^ string_of_var x ^ " := " ^ print ~l inst));*)
166 let rec aux ((freshno,acc_ps,acc_new_ps) as acc) =
170 (*prerr_endline ("EXPAND t:" ^ print (t :> nf));*)
171 let t = subst false false x inst (t :> nf) in
172 (*prerr_endline ("SUBSTITUTED t:" ^ print (t :> nf));*)
173 let freshno,new_t,acc_new_ps =
174 expand_match (freshno,acc_ps@`Var(max_int/3,-666)::todo_ps,acc_new_ps) t
176 aux (freshno,acc_ps@[new_t],acc_new_ps) todo_ps
178 (* cut&paste from aux above *)
179 and aux' ps ((freshno,acc_conv,acc_new_ps) as acc) =
183 (*prerr_endline ("EXPAND t:" ^ print (t :> nf));*)
185 let t = subst false false x inst (t :> nf) in
186 (*prerr_endline ("SUBSTITUTED t:" ^ print (t :> nf));*)
187 let freshno,new_t,acc_new_ps =
188 expand_match (freshno,ps,acc_new_ps) t
190 aux' ps (freshno,acc_conv@[new_t],acc_new_ps) todo_conv
191 (* with ExpandedToLambda -> aux' ps (freshno,acc_conv@[`N(-1)],acc_new_ps) todo_conv *)
193 (* cut&paste from aux' above *)
194 and aux'' ps (freshno,acc_new_ps) =
196 | None -> freshno, None, acc_new_ps
198 let t = subst false false x inst (t :> nf) in
199 let freshno,new_t,acc_new_ps =
200 expand_match (freshno,ps,acc_new_ps) t
202 freshno,Some new_t,acc_new_ps
204 and expand_match ((freshno,acc_ps,acc_new_ps) as acc) t =
206 | `Match(u',orig,bs_lift,bs,args) ->
207 let freshno,u,acc_new_ps = expand_match acc (u' :> nf) in
210 | `N i -> acc_new_ps,i
212 let ps = List.map (fun t -> cast_to_i_num_var (subst false false x inst (t:> nf))) (acc_ps@acc_new_ps) in
213 let super_simplified_ps = super_simplify_ps_with_match ps ps in
214 (*prerr_endline ("CERCO u:" ^ print (fst u :> nf));
215 List.iter (fun x -> prerr_endline ("IN: " ^ print (fst x :> nf))) ps;
216 List.iter (fun x -> prerr_endline ("IN2: " ^ print (fst x :> nf))) super_simplified_ps;*)
217 match index_of_opt ~eq:eta_eq super_simplified_ps u with
218 Some i -> acc_new_ps, i
219 | None -> acc_new_ps@[u], len_ps + List.length acc_new_ps
222 if List.exists (fun (j,_) -> i=j) !bs then
225 let freshno,v = freshno+1, `Var (freshno+1, -666) in (* make_fresh_var freshno in *)
228 (*prerr_endlie ("t DA RIDURRE:" ^ print (`Match(`N i,arity,bs_lift,bs,args) :> nf) ^ " more_args=" ^ string_of_int more_args);*)
229 let t = mk_match (`N i) orig bs_lift bs args in
230 (*prerr_endline ("NUOVO t:" ^ print (fst t :> nf) ^ " more_args=" ^ string_of_int (snd t));*)
231 expand_match (freshno,acc_ps,acc_new_ps) t
232 | `Lam _ -> raise ExpandedToLambda
234 let x = simple_expand_match (acc_ps@acc_new_ps) x in
235 freshno,cast_to_i_num_var x,acc_new_ps in
237 let freshno,old_ps,new_ps = aux (freshno,[],[]) (ps :> i_num_var list) in
238 let freshno,conv,new_ps = aux' old_ps (freshno,[],new_ps) (conv :> i_num_var list) in
239 let freshno,div,new_ps = aux'' old_ps (freshno,new_ps) (div :> i_num_var option) in
240 let div = option_map cast_to_i_var div in
241 let ps = List.map cast_to_i_n_var (old_ps @ new_ps) in
242 let conv = List.map cast_to_i_n_var conv in
243 let p = {p with freshno; div; conv; ps} in
244 prerr_endline ("# INST: " ^ string_of_var p.var_names x ^ " := " ^ string_of_term p inst);
245 ( (* check if double substituting a variable *)
246 if List.exists (fun (x',_) -> x = x') sigma
247 then failwithProblem p ("Variable "^ string_of_var p.var_names x ^"replaced twice")
249 let p = {p with sigma = sigma@[x,inst]} in
250 let p = super_simplify p in
251 prerr_endline (string_of_problem "instantiate" p);
257 let arity_of arities k =
258 let _,pos,y = List.find (fun (v,_,_) -> v=k) arities in
259 let arity = match y with `Var _ -> 0 | `I(_,args) -> Listx.length args | `N _ -> assert false in
260 arity + if pos = -1 then - 1 else 0
263 let rec dangerous arities showstoppers =
268 | `Match(t,_,liftno,bs,args) ->
269 (* CSC: XXX partial dependency on the encoding *)
271 `N _ -> List.iter (dangerous arities showstoppers) args
272 | `Match _ as t -> dangerous arities showstoppers t ; List.iter (dangerous arities showstoppers) args
273 | `Var(x,_) -> dangerous_inert arities showstoppers x args num_more_args
274 | `I((x,_),args') -> dangerous_inert arities showstoppers x (Listx.to_list args' @ args) num_more_args
276 | `I((k,_),args) -> dangerous_inert arities showstoppers k (Listx.to_list args) 0
278 and dangerous_inert arities showstoppers k args more_args =
279 List.iter (dangerous arities showstoppers) args ;
280 if List.mem k showstoppers then raise Dangerous else
282 let arity = arity_of arities k in
283 if List.length args + more_args > arity then raise Dangerous else ()
287 (* cut & paste from above *)
288 let rec dangerous_conv p arities showstoppers =
293 | `Match(t,_,liftno,bs,args) ->
294 (* CSC: XXX partial dependency on the encoding *)
296 `N _ -> concat_map (dangerous_conv p arities showstoppers) args
297 | `Match _ as t -> dangerous_conv p arities showstoppers t @ concat_map (dangerous_conv p arities showstoppers) args
298 | `Var(x,_) -> dangerous_inert_conv p arities showstoppers x [] args 2
299 | `I((x,_),args') -> dangerous_inert_conv p arities showstoppers x (Listx.to_list args') args 2
301 | `I((k,_),args) -> dangerous_inert_conv p arities showstoppers k (Listx.to_list args) [] 0
303 and dangerous_inert_conv p arities showstoppers k args match_args more_args =
304 let all_args = args @ match_args in
305 let dangerous_args = concat_map (dangerous_conv p arities showstoppers) all_args in
306 let all_args = (all_args :> nf list) in
307 if dangerous_args = [] then (
308 if List.mem k showstoppers then k :: concat_map free_vars all_args else
310 let arity = arity_of arities k in
311 prerr_endline ("dangerous_inert_conv: ar=" ^ string_of_int arity ^ " k="^string_of_var p.var_names k ^ " listlenargs=" ^ (string_of_int (List.length args)) ^ " more_args=" ^ string_of_int more_args);
312 if more_args > 0 (* match argument*) && List.length args = arity then []
313 else if List.length all_args + more_args > arity then k :: concat_map free_vars all_args else []
316 ) else k :: concat_map free_vars all_args
318 (* inefficient algorithm *)
319 let rec edible ({div; conv; ps} as p) arities showstoppers =
320 let rec aux showstoppers =
323 | x::xs when List.exists (fun y -> hd_of x = Some y) showstoppers ->
324 (* se la testa di x e' uno show-stopper *)
325 let new_showstoppers = sort_uniq (showstoppers @ free_vars (x :> nf)) in
326 (* aggiungi tutte le variabili libere di x *)
327 if List.length showstoppers <> List.length new_showstoppers then
328 aux new_showstoppers ps
333 None -> aux showstoppers xs
336 dangerous arities showstoppers (x : i_n_var :> nf) ;
340 aux (sort_uniq (h::showstoppers)) ps
342 let showstoppers = sort_uniq (aux showstoppers ps) in
344 List.map (dangerous_conv p arities showstoppers) (conv :> nf list) in
346 prerr_endline ("dangerous_conv lenght:" ^ string_of_int (List.length dangerous_conv));
347 List.iter (fun l -> prerr_endline (String.concat " " (List.map (string_of_var p.var_names) l))) dangerous_conv;
349 let showstoppers' = showstoppers @ List.concat dangerous_conv in
350 let showstoppers' = sort_uniq (match div with
351 | None -> showstoppers'
353 if List.exists ((=) (hd_of_i_var div)) showstoppers'
354 then showstoppers' @ free_vars (div :> nf) else showstoppers') in
355 if showstoppers <> showstoppers' then edible p arities showstoppers' else showstoppers', dangerous_conv
358 let precompute_edible_data {ps; div} xs =
359 (match div with None -> [] | Some div -> [hd_of_i_var div, -1, (div :> i_n_var)]) @
361 let i, tm = Util.findi (fun y -> hd_of y = Some hd) ps in
366 let critical_showstoppers p =
367 let p = super_simplify p in
368 let hd_of_div = match p.div with None -> [] | Some t -> [hd_of_i_var t] in
369 let showstoppers_step =
370 concat_map (fun bs ->
371 let heads = List.map (fun (i,_) -> List.nth p.ps i) !bs in
372 let heads = List.sort compare (hd_of_div @ filter_map hd_of heads) in
373 snd (split_duplicates heads)
375 if List.exists (fun t -> [hd_of t] = List.map (fun x -> Some x) hd_of_div) p.conv
376 then hd_of_div else [] in
377 let showstoppers_step = sort_uniq showstoppers_step in
378 let showstoppers_eat =
379 let heads_and_arities =
380 List.sort (fun (k,_) (h,_) -> compare k h)
381 (filter_map (function `Var(k,_) -> Some (k,0) | `I((k,_),args) -> Some (k,Listx.length args) | _ -> None ) p.ps) in
382 let rec multiple_arities =
386 | (x,i)::(y,j)::tl when x = y && i <> j ->
387 x::multiple_arities tl
388 | _::tl -> multiple_arities tl in
389 multiple_arities heads_and_arities in
391 let showstoppers_eat = sort_uniq showstoppers_eat in
392 let showstoppers_eat = List.filter
393 (fun x -> not (List.mem x showstoppers_step))
395 List.iter (fun v -> prerr_endline ("DANGEROUS STEP: " ^ (string_of_var p.var_names) v)) showstoppers_step;
396 List.iter (fun v -> prerr_endline ("DANGEROUS EAT: " ^ (string_of_var p.var_names) v)) showstoppers_eat;
397 p, showstoppers_step, showstoppers_eat
401 let ({ps} as p), showstoppers_step, showstoppers_eat = critical_showstoppers p in
402 let showstoppers = showstoppers_step @ showstoppers_eat in
403 let heads = List.sort compare (filter_map hd_of ps) in
404 let arities = precompute_edible_data p (uniq heads) in
405 let showstoppers, showstoppers_conv =
406 edible p arities showstoppers in
407 let l = List.filter (fun (x,_,_) -> not (List.mem x showstoppers)) arities in
409 List.fold_left (fun p (x,pos,(xx : i_n_var)) -> if pos = -1 then p else
410 let n = match xx with `I(_,args) -> Listx.length args | _ -> 0 in
412 let inst = make_lams v n in
413 prerr_endline ("# INST_IN_EAT: " ^ string_of_var p.var_names x ^ " := " ^ string_of_term p inst);
414 { p with sigma = p.sigma @ [x,inst] }
416 (* to avoid applied numbers in safe positions that
417 trigger assert failures subst_in_problem x inst p*)
421 let _,j,_ = List.find (fun (h,_,_) -> hd_of t = Some h) l in
425 let p = match p.div with
428 if List.mem (hd_of_i_var div) showstoppers
431 let n = match div with `I(_,args) -> Listx.length args | `Var _ -> 0 in
432 let p, bomb' = make_fresh_var p (-666) in
433 (if !bomb <> `Var (-1,-666) then
435 ("Bomb was duplicated! It was " ^ string_of_nf !bomb ^
436 ", tried to change it to " ^ string_of_nf bomb'));
438 prerr_endline ("Just created bomb var: " ^ string_of_nf !bomb);
439 let x = hd_of_i_var div in
440 let inst = make_lams !bomb n in
441 let p = {p with div=None} in
442 (* subst_in_problem (hd_of_i_var div) inst p in *)
443 {p with sigma=p.sigma@[x,inst]} in
444 let dangerous_conv = showstoppers_conv in
445 let _ = prerr_endline ("dangerous_conv lenght:" ^ string_of_int (List.length dangerous_conv));
446 List.iter (fun l -> prerr_endline (String.concat " " (List.map (string_of_var p.var_names) l))) dangerous_conv; in
448 List.map (function s,t ->
450 if s <> [] then t else (
451 (match t with | `Var _ -> raise Not_found | _ -> ());
452 let _ = List.find (fun h -> hd_of t = Some h) showstoppers in
454 with Not_found -> match hd_of t with
455 | None -> assert (t = convergent_dummy); t
457 prerr_endline ("FREEZING " ^ string_of_var p.var_names h);
459 ) (List.combine showstoppers_conv p.conv) in
465 match List.nth ps n with
470 let old_conv = p.conv in
471 let p = { p with ps; conv } in
472 if l <> [] || old_conv <> conv
473 then prerr_endline (string_of_problem "eat" p);
474 if List.for_all (function `N _ -> true | _ -> false) ps && p.div = None then
479 let instantiate p x n =
480 (if hd_of_i_var (cast_to_i_var !bomb) = x
481 then failwithProblem p ("BOMB (" ^ string_of_nf !bomb ^ ") cannot be instantiated!"));
482 let arity_of_x = max_arity_tms x (all_terms p) in
483 (if arity_of_x = None then failwithProblem p "step on var non occurring in problem");
484 (if Util.option_get(arity_of_x) = min_int then failwithProblem p "step on fake variable");
485 (if Util.option_get(arity_of_x) <= 0 then failwithProblem p "step on var of non-positive arity");
486 let n = (prerr_endline "WARNING: using constant initialSpecialK"); p.initialSpecialK in
487 (* AC: Once upon a time, it was:
488 let arities = Num.compute_arities x (n+1) (all_terms p :> nf list) in *)
489 (* let arities = Array.to_list (Array.make (n+1) 0) in *)
490 let arities = Array.to_list (Array.make (n+1) min_int) in
491 let p,vars = make_fresh_vars p arities in
492 let args = Listx.from_list (vars :> nf list) in
494 (* 666, since it will be replaced anyway during subst: *)
495 let inst = `Lam(false,`Match(`I((0,min_int),Listx.map (lift 1) args),(x,-666),1,bs,[])) in
496 let p = {p with deltas=bs::p.deltas} in
497 subst_in_problem x inst p
500 let compute_special_k tms =
501 let rec aux k (t: nf) = Pervasives.max k (match t with
502 | `Lam(b,t) -> aux (k + if b then 1 else 0) t
503 | `I(n, tms) -> Listx.max (Listx.map (aux 0) tms)
504 | `Match(t, _, liftno, bs, args) ->
505 List.fold_left max 0 (List.map (aux 0) ((t :> nf)::args@List.map snd !bs))
508 ) in Listx.max (Listx.map (aux 0) tms)
511 let auto_instantiate (n,p) =
512 let p, showstoppers_step, showstoppers_eat = critical_showstoppers p in
514 match showstoppers_step, showstoppers_eat with
516 prerr_endline ("INSTANTIATING CRITICAL TO EAT " ^ string_of_var p.var_names y); y
519 (* Choose only variables still alive (with arity > 0) *)
520 List.sort compare (filter_map (
521 fun t -> match t with `Var _ -> None | x -> if arity_of_hd x <= 0 then None else hd_of x
522 ) ((match p.div with Some t -> [(t :> i_n_var)] | _ -> []) @ p.ps)) in
526 fst (List.find (((<) 0) ++ snd) (concat_map free_vars' (p.conv :> nf list)))
528 Not_found -> assert false)
530 prerr_endline ("INSTANTIATING TO EAT " ^ string_of_var p.var_names x);
533 prerr_endline ("INSTANTIATING " ^ string_of_var p.var_names x);
535 (* Strategy that decreases the special_k to 0 first (round robin)
536 1:11m42 2:14m5 3:11m16s 4:14m46s 5:12m7s 6:6m31s *)
540 hd_of (List.find (fun t ->
541 compute_special_k (Listx.Nil (t :> nf)) > 0 && arity_of_hd t > 0
544 | None -> assert false
546 prerr_endline ("INSTANTIATING AND HOPING " ^ string_of_var p.var_names x);
550 let arity_of_x = max_arity_tms x (all_terms p) in
551 assert (Util.option_get arity_of_x > 0);
553 (* Instantiate in decreasing order of compute_special_k
554 1:15m14s 2:13m14s 3:4m55s 4:4m43s 5:4m34s 6:6m28s 7:3m31s
557 (match hd_of (snd (List.hd (List.sort (fun c1 c2 -> - compare (fst c1) (fst c2)) (filter_map (function `I _ as t -> Some (compute_special_k (Listx.Nil (t :> nf)),t) | _ -> None) (all_terms p))))) with
560 prerr_endline ("INSTANTIATING AND HOPING " ^ string_of_var x);
566 compute_special_k (Listx.from_list (all_terms p :> nf list) )in
567 if special_k < n then
568 prerr_endline ("@@@@ NEW INSTANTIATE PHASE (" ^ string_of_int special_k ^ ") @@@@");
569 let p = instantiate p x special_k in
573 let rec auto_eat (n,p) =
574 prerr_endline "{{{{{{{{ Computing measure before auto_instantiate }}}}}}";
575 let m = problem_measure p in
576 let (n,p') = auto_instantiate (n,p) in
580 prerr_endline "{{{{{{{{ Computing measure inafter auto_instantiate }}}}}}";
581 let delta = problem_measure p - m in
582 (* let delta = m - problem_measure p' in *)
586 ("Measure did not decrease (+=" ^ string_of_int delta ^ ")"))
587 else prerr_endline ("$ Measure decreased of " ^ string_of_int delta);
592 prerr_endline ("@@@@ FIRST INSTANTIATE PHASE (" ^ string_of_int n ^ ") @@@@");
595 | `Continue p -> auto_eat (n,p)
601 x y = y 0 a y = k k z = z 0 c y = k y u = u h1 h2 0 h2 a = h3
602 1 x a c 1 a 0 c 1 k c 1 c 0 1 k 1 k 1 k
603 2 x a y 2 a 0 y 2 k y 2 y 0 2 y 0 2 h2 0 2 h3
604 3 x b y 3 b 0 y 3 b 0 y 3 b 0 y 3 b 0 y 3 b 0 (\u. u h1 h2 0) 3 b 0 (\u. u h1 (\w.h3) 0)
605 4 x b c 4 b 0 c 4 b 0 c 4 b 0 c 4 b 0 c 4 b 0 c 4 b 0 c
606 5 x (b e) 5 b e 0 5 b e 0 5 b e 0 5 b e 0 5 b e 0 5 b e 0
607 6 y y 6 y y 6 y y 6 y y 6 y y 6 h1 h1 h2 0 h2 0 6 h1 h1 (\w. h3) 0 (\w. h3) 0
610 b u = u l1 l2 0 e _ _ _ _ = f l3 n = n j 0
613 3 l2 0 (\u. u h1 (\w. h3) 0) 3 l3 (\u. u h1 (\w. h3) 0) 3 j h1 (\w. h3) 0 0
614 4 l2 0 c 4 l3 c 4 c j 0
615 5 e l1 l2 0 0 5 f 5 f
616 6 h1 h1 (\w. h3) 0 (\w. h3) 0 6 h1 h1 (\w. h3) 0 (\w. h3) 0 6 h1 h1 (\w. h3) 0 (\w. h3) 0
621 x a (b (a c)) a 0 = 1 ? (b (a c)) 8
622 x a (b d') a 0 = 1 ? (b d') 7
623 x b (a c) b 0 = 1 ? (a c) 4
624 x b (a c') b 0 = 1 ? (a c') 5
632 b 4 = 8 (* b (a c) *)
637 (************** Tests ************************)
639 let optimize_numerals p =
640 let replace_in_sigma perm =
641 let rec aux = function
642 | `N n -> `N (List.nth perm n)
643 | `I _ -> assert false
645 | `Lam(v,t) -> `Lam(v, aux t)
646 | `Match(_,_,_,bs,_) as t -> (bs := List.map (fun (n,t) -> (List.nth perm n, t)) !bs); t
647 in List.map (fun (n,t) -> (n,aux t))
649 let deltas' = List.mapi (fun n d -> (n, List.map fst !d)) p.deltas in
650 let maxs = Array.to_list (Array.init (List.length deltas') (fun _ -> 0)) in
651 let max = List.fold_left max 0 (concat_map snd deltas') in
652 let perm,_ = List.fold_left (fun (perm, maxs) (curr_n:int) ->
653 let containing = filter_map (fun (i, bs) -> if List.mem curr_n bs then Some i else None) deltas' in
654 (* (prerr_endline (string_of_int curr_n ^ " occurs in: " ^ (String.concat " " (List.map string_of_int containing)))); *)
655 let neww = List.fold_left Pervasives.max 0 (List.mapi (fun n max -> if List.mem n containing then max else 0) maxs) in
656 let maxs = List.mapi (fun i m -> if List.mem i containing then neww+1 else m) maxs in
658 ) ([],maxs) (Array.to_list (Array.init (max+1) (fun x -> x))) in
659 replace_in_sigma (List.rev perm) p.sigma
662 let env_of_sigma freshno sigma should_explode =
669 e,Pure.lift (-n-1) (snd (List.find (fun (i,_) -> i = n) sigma)),[]
672 if should_explode && n = hd_of_i_var (cast_to_i_var !bomb)
673 then ([], (let f t = Pure.A(t,t) in f (Pure.L (f (Pure.V 0)))), [])
674 else ([],Pure.V n,[]))::e
677 (* ************************************************************************** *)
680 | `Separable of (int * Num.nf) list
681 | `Unseparable of string
685 (* TODO check if there are duplicates in p.ps
686 before it was: ps = sort_uniq ~compare:eta_compare (ps :> nf list) *)
687 (* FIXME what about initial fragments? *)
688 if (let rec f = function
690 | hd::tl -> List.exists (eta_eq hd) tl || f tl in
692 then Some "ps contains duplicate entries"
693 (* check if div occurs somewhere in ps@conv *)
694 else match p.div with
697 if (List.exists (eta_subterm div) (p.ps@p.conv))
698 then Some "div occurs as subterm in ps or conv"
703 prerr_endline (string_of_problem "main" p);
705 | Some s -> `Unseparable s
707 bomb := `Var(-1,-666);
708 Console.print_hline();
709 let p_finale = auto p p.initialSpecialK in
710 let freshno,sigma = p_finale.freshno, p_finale.sigma in
711 prerr_endline ("------- <DONE> ------ measure=. \n ");
712 List.iter (fun (x,inst) -> prerr_endline
713 (string_of_var p_finale.var_names x ^ " := " ^ string_of_term p_finale inst)) sigma;
715 prerr_endline "---------<OPT>----------";
716 let sigma = optimize_numerals p_finale in (* optimize numerals *)
717 List.iter (fun (x,inst) -> prerr_endline
718 (string_of_var p_finale.var_names x ^ " := " ^ string_of_term p_finale inst)) sigma;
720 prerr_endline "---------<PURE>---------";
721 let scott_of_nf t = ToScott.t_of_nf (t :> nf) in
722 let div = option_map scott_of_nf p.div in
723 let conv = List.map scott_of_nf p.conv in
724 let ps = List.map scott_of_nf p.ps in
726 let sigma' = List.map (fun (x,inst) -> x, scott_of_nf inst) sigma in
727 let e' = env_of_sigma freshno sigma' false (* FIXME shoudl_explode *) in
729 prerr_endline "--------<REDUCE>---------";
730 let pure_bomb = ToScott.t_of_nf (!bomb) in (* Pure.B *)
733 print_endline (Pure.print div);
734 let t = Pure.mwhd (e',div,[]) in
735 prerr_endline ("*:: " ^ (Pure.print t));
736 assert (t = pure_bomb)
739 verbose ("_::: " ^ (Pure.print n));
740 let t = Pure.mwhd (e',n,[]) in
741 verbose ("_:: " ^ (Pure.print t));
742 assert (t <> pure_bomb)
744 List.iteri (fun i n ->
745 verbose ((string_of_int i) ^ "::: " ^ (Pure.print n));
746 let t = Pure.mwhd (e',n,[]) in
747 verbose ((string_of_int i) ^ ":: " ^ (Pure.print t));
748 assert (t = Scott.mk_n i)
750 prerr_endline "-------- </DONE> --------";
751 `Separable p_finale.sigma
754 let problem_of (label, div, conv, ps, var_names) =
755 (* TODO: replace div with bottom in problem??? *)
756 let all_tms = (match div with None -> [] | Some div -> [(div :> i_n_var)]) @ ps @ conv in
757 if all_tms = [] then failwith "problem_of: empty problem";
758 let initialSpecialK = compute_special_k (Listx.from_list (all_tms :> nf list)) in
759 let freshno = List.length var_names in
761 let dummy = `Var (max_int / 2, -666) in
762 [ ref (Array.to_list (Array.init (List.length ps) (fun i -> i, dummy))) ] in
763 {freshno; div; conv; ps; sigma=[]; deltas; initialSpecialK; var_names; label}