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;;
15 type discriminating_set = (int * nf) list;;
19 ; div: i_var option (* None = bomb *)
20 ; conv: i_n_var list (* the inerts that must converge *)
21 ; ps: i_n_var list (* the n-th inert must become n *)
22 ; sigma: (int * nf) list (* the computed substitution *)
23 ; deltas: discriminating_set ref list (* collection of all branches *)
24 ; initialSpecialK: int
26 ; trail: discriminating_set list list
30 (match p.div with None -> [] | Some t -> [(t :> i_n_var)])
36 let rec aux = function
37 | `N _ | `Bottom | `Pacman -> 0
38 | `Var(_,ar) -> if ar = min_int then 0 else max 0 ar (*assert (ar >= 0); ar*)
40 | `I(v,args) -> aux (`Var v) + aux_many (Listx.to_list args :> nf list)
41 | `Match(u,(_,ar),_,_,args) -> aux (u :> nf) + (if ar = min_int then 0 else ar - 1) + aux_many (args :> nf list)
42 and aux_many tms = List.fold_right ((+) ++ aux) tms 0 in
43 aux_many (all_terms p :> nf list)
46 let problem_measure p = sum_arities p;;
47 let string_of_measure = string_of_int;;
49 let string_of_problem label ({freshno; div; conv; ps; deltas} as p) =
50 Console.print_hline ();
51 prerr_endline ("\n||||| Displaying problem: " ^ label ^ " |||||");
53 let deltas = String.concat nl (List.map (fun r -> String.concat " <> " (List.map (fun (i,_) -> string_of_int i) !r)) deltas) in
54 let l = Array.to_list (Array.init (freshno + 1) string_of_var) in
55 nl ^ "measure="^string_of_measure(problem_measure p)^" freshno = " ^ string_of_int freshno
56 ^nl^"\b> DISCRIMINATING SETS (deltas)"
57 ^nl^deltas^(if deltas = "" then "" else nl)
58 ^"\b (* DIVERGENT *)" ^ nl
59 ^" "^ (match div with None -> "None" | Some div -> "(Some\""^ print ~l (div :> nf) ^"\" ") ^ nl
60 ^"\b (* CONVERGENT *) [" ^ nl
61 ^ String.concat " " (List.map (fun t -> "_: " ^ (if t = `N (-1) then "_" else "\""^ print ~l (t :> nf) ^"\";")) conv) ^
62 (if conv = [] then "" else nl)
64 ^ String.concat "\n| " (List.mapi (fun i t -> string_of_int i ^ ": " ^ print ~l (t :> nf)) ps)
69 let failwithProblem p reason =
70 print_endline (string_of_problem "FAIL" p);
74 let make_fresh_var p arity =
75 let freshno = p.freshno + 1 in
76 {p with freshno}, `Var(freshno,arity)
79 let make_fresh_vars p arities =
81 (fun arity (p, vars) -> let p, var = make_fresh_var p arity in p, var::vars)
86 let simple_expand_match ps =
87 let rec aux_nob level = function
88 | #i_num_var as t -> aux_i_num_var level t
89 | `Lam(b,t) -> `Lam(b,aux (level+1) t)
91 and aux level = function
93 | #nf_nob as t -> aux_nob level t
94 and aux_i_num_var level = function
95 | `Match(u,v,bs_lift,bs,args) as torig ->
96 let u = aux_i_num_var level u in
97 bs := List.map (fun (n, x) -> n, aux 0 x) !bs;
101 let i = index_of (lift (-level) u) (ps :> nf list) (* can raise Not_found *)
102 in let t = mk_match (`N i) v bs_lift bs (args :> nf list) in
106 | _ -> raise Not_found)
108 mk_appl (`Match(cast_to_i_num_var u,v,bs_lift,bs,[])) (List.map (aux_nob level) args))
109 | `I(v,args) -> mk_appl (`Var v) (List.map (aux_nob level) (Listx.to_list args))
110 | `N _ | `Var _ as t -> t
115 let rec aux x = let x' = f x in if x <> x' then aux x' else x in aux
118 let rec super_simplify_ps ps =
119 fixpoint (List.map (cast_to_i_num_var ++ (simple_expand_match ps)))
122 let super_simplify ({div; ps; conv} as p) =
123 let ps = super_simplify_ps p.ps (p.ps :> i_num_var list) in
124 let conv = super_simplify_ps ps (p.conv :> i_num_var list) in
125 let div = option_map (fun div ->
126 let divs = super_simplify_ps p.ps ([div] :> i_num_var list) in
128 {p with div=option_map cast_to_i_var div; ps=List.map cast_to_i_n_var ps; conv=List.map cast_to_i_n_var conv}
130 let subst_in_problem x inst ({freshno; div; conv; ps; sigma} as p) =
131 let len_ps = List.length ps in
132 (*(let l = Array.to_list (Array.init (freshno + 1) string_of_var) in
133 prerr_endline ("# INST0: " ^ string_of_var x ^ " := " ^ print ~l inst));*)
134 let rec aux ((freshno,acc_ps,acc_new_ps) as acc) =
138 (*prerr_endline ("EXPAND t:" ^ print (t :> nf));*)
139 let t = subst false false x inst (t :> nf) in
140 (*prerr_endline ("SUBSTITUTED t:" ^ print (t :> nf));*)
141 let freshno,new_t,acc_new_ps =
142 expand_match (freshno,acc_ps@`Var(max_int/3,-666)::todo_ps,acc_new_ps) t
144 aux (freshno,acc_ps@[new_t],acc_new_ps) todo_ps
146 (* cut&paste from aux above *)
147 and aux' ps ((freshno,acc_conv,acc_new_ps) as acc) =
151 (*prerr_endline ("EXPAND t:" ^ print (t :> nf));*)
152 let t = subst false false x inst (t :> nf) in
153 (*prerr_endline ("SUBSTITUTED t:" ^ print (t :> nf));*)
154 let freshno,new_t,acc_new_ps =
155 expand_match (freshno,ps,acc_new_ps) t
157 aux' ps (freshno,acc_conv@[new_t],acc_new_ps) todo_conv
159 (* cut&paste from aux' above *)
160 and aux'' ps (freshno,acc_new_ps) =
162 | None -> freshno, None, acc_new_ps
164 let t = subst false false x inst (t :> nf) in
165 let freshno,new_t,acc_new_ps =
166 expand_match (freshno,ps,acc_new_ps) t
168 freshno,Some new_t,acc_new_ps
170 and expand_match ((freshno,acc_ps,acc_new_ps) as acc) t =
172 | `Match(u',orig,bs_lift,bs,args) ->
173 let freshno,u,acc_new_ps = expand_match acc (u' :> nf) in
176 | `N i -> acc_new_ps,i
178 let ps = List.map (fun t -> cast_to_i_num_var (subst false false x inst (t:> nf))) (acc_ps@acc_new_ps) in
179 let super_simplified_ps = super_simplify_ps ps ps in
180 (*prerr_endline ("CERCO u:" ^ print (fst u :> nf));
181 List.iter (fun x -> prerr_endline ("IN: " ^ print (fst x :> nf))) ps;
182 List.iter (fun x -> prerr_endline ("IN2: " ^ print (fst x :> nf))) super_simplified_ps;*)
183 match index_of_opt ~eq:eta_eq super_simplified_ps u with
184 Some i -> acc_new_ps, i
185 | None -> acc_new_ps@[u], len_ps + List.length acc_new_ps
188 if List.exists (fun (j,_) -> i=j) !bs then
191 let freshno,v = freshno+1, `Var (freshno+1, -666) in (* make_fresh_var freshno in *)
194 (*prerr_endlie ("t DA RIDURRE:" ^ print (`Match(`N i,arity,bs_lift,bs,args) :> nf) ^ " more_args=" ^ string_of_int more_args);*)
195 let t = mk_match (`N i) orig bs_lift bs (args :> nf list) in
196 (*prerr_endline ("NUOVO t:" ^ print (fst t :> nf) ^ " more_args=" ^ string_of_int (snd t));*)
197 expand_match (freshno,acc_ps,acc_new_ps) t
198 | `Lam _ | `Pacman -> assert false (* loose typing *)
199 | `Bottom -> assert false (* algorithm invariant *)
201 let x = simple_expand_match (acc_ps@acc_new_ps) x in
202 freshno,cast_to_i_num_var x,acc_new_ps in
204 let freshno,old_ps,new_ps = aux (freshno,[],[]) (ps :> i_num_var list) in
205 let freshno,conv,new_ps = aux' old_ps (freshno,[],new_ps) (conv :> i_num_var list) in
206 let freshno,div,new_ps = aux'' old_ps (freshno,new_ps) (div :> i_num_var option) in
207 let div = option_map cast_to_i_var div in
208 let ps = List.map cast_to_i_n_var (old_ps @ new_ps) in
209 let conv = List.map cast_to_i_n_var conv in
210 (let l = Array.to_list (Array.init (freshno + 1) string_of_var) in
211 prerr_endline ("# INST: " ^ string_of_var x ^ " := " ^ print ~l inst));
212 let p = {p with freshno; div; conv; ps} in
213 ( (* check if double substituting a variable *)
214 if List.exists (fun (x',_) -> x = x') sigma
215 then failwithProblem p ("Variable "^ string_of_var x ^"replaced twice")
217 let p = {p with sigma = sigma@[x,inst]} in
218 let p = super_simplify p in
219 prerr_endline (string_of_problem "instantiate" p);
225 let arity_of arities k =
226 let _,pos,y = List.find (fun (v,_,_) -> v=k) arities in
227 let arity = match y with `Var _ -> 0 | `I(_,args) -> Listx.length args | _ -> assert false in
228 arity + if pos = -1 then - 1 else 0
231 let rec dangerous arities showstoppers =
237 | `Match(t,_,liftno,bs,args) ->
238 (* CSC: XXX partial dependency on the encoding *)
240 `N _ -> List.iter (dangerous arities showstoppers) args
241 | `Match _ as t -> dangerous arities showstoppers t ; List.iter (dangerous arities showstoppers) args
242 | `Var(x,_) -> dangerous_inert arities showstoppers x args num_more_args
243 | `I((x,_),args') -> dangerous_inert arities showstoppers x (Listx.to_list args' @ args) num_more_args
245 | `I((k,_),args) -> dangerous_inert arities showstoppers k (Listx.to_list args) 0
247 and dangerous_inert arities showstoppers k args more_args =
248 List.iter (dangerous arities showstoppers) args ;
249 if List.mem k showstoppers then raise Dangerous else
251 let arity = arity_of arities k in
252 if List.length args + more_args > arity then raise Dangerous else ()
256 (* cut & paste from above *)
257 let rec dangerous_conv arities showstoppers =
263 | `Match(t,_,liftno,bs,args) ->
264 (* CSC: XXX partial dependency on the encoding *)
266 `N _ -> concat_map (dangerous_conv arities showstoppers) args
267 | `Match _ as t -> dangerous_conv arities showstoppers t @ concat_map (dangerous_conv arities showstoppers) args
268 | `Var(x,_) -> dangerous_inert_conv arities showstoppers x [] args 2
269 | `I((x,_),args') -> dangerous_inert_conv arities showstoppers x (Listx.to_list args') args 2
271 | `I((k,_),args) -> dangerous_inert_conv arities showstoppers k (Listx.to_list args) [] 0
273 and dangerous_inert_conv arities showstoppers k args match_args more_args =
274 let all_args = args @ match_args in
275 let dangerous_args = concat_map (dangerous_conv arities showstoppers) all_args in
276 let all_args = (all_args :> nf list) in
277 if dangerous_args = [] then (
278 if List.mem k showstoppers then k :: concat_map free_vars all_args else
280 let arity = arity_of arities k in
281 prerr_endline ("dangerous_inert_conv: ar=" ^ string_of_int arity ^ " k="^string_of_var k ^ " listlenargs=" ^ (string_of_int (List.length args)) ^ " more_args=" ^ string_of_int more_args);
282 if more_args > 0 (* match argument*) && List.length args = arity then []
283 else if List.length all_args + more_args > arity then k :: concat_map free_vars all_args else []
286 ) else k :: concat_map free_vars all_args
288 (* inefficient algorithm *)
289 let rec edible arities div ps conv showstoppers =
290 let rec aux showstoppers =
293 | x::xs when List.exists (fun y -> hd_of x = Some y) showstoppers ->
294 (* se la testa di x e' uno show-stopper *)
295 let new_showstoppers = sort_uniq (showstoppers @ free_vars (x :> nf)) in
296 (* aggiungi tutte le variabili libere di x *)
297 if List.length showstoppers <> List.length new_showstoppers then
298 aux new_showstoppers ps
303 None -> aux showstoppers xs
306 dangerous arities showstoppers (x : i_n_var :> nf_nob) ;
310 aux (sort_uniq (h::showstoppers)) ps
312 let showstoppers = sort_uniq (aux showstoppers ps) in
314 List.map (dangerous_conv arities showstoppers) conv in
316 prerr_endline ("dangerous_conv lenght:" ^ string_of_int (List.length dangerous_conv));
317 List.iter (fun l -> prerr_endline (String.concat " " (List.map string_of_var l))) dangerous_conv;
319 let showstoppers' = showstoppers @ List.concat dangerous_conv in
320 let showstoppers' = sort_uniq (match div with
321 | None -> showstoppers'
323 if List.exists ((=) (hd_of_i_var div)) showstoppers'
324 then showstoppers' @ free_vars (div :> nf) else showstoppers') in
325 if showstoppers <> showstoppers' then edible arities div ps conv showstoppers' else showstoppers', dangerous_conv
328 let precompute_edible_data {ps; div} xs =
329 (match div with None -> [] | Some div -> [hd_of_i_var div, -1, (div :> i_n_var)]) @
331 let i, tm = Util.findi (fun y -> hd_of y = Some hd) ps in
336 let critical_showstoppers p =
337 let p = super_simplify p in
338 let hd_of_div = match p.div with None -> [] | Some t -> [hd_of_i_var t] in
339 let showstoppers_step =
340 concat_map (fun bs ->
341 let heads = List.map (fun (i,_) -> List.nth p.ps i) !bs in
342 let heads = List.sort compare (hd_of_div @ filter_map hd_of heads) in
343 snd (split_duplicates heads)
345 if List.exists (fun t -> [hd_of t] = List.map (fun x -> Some x) hd_of_div) p.conv
346 then hd_of_div else [] in
347 let showstoppers_step = sort_uniq showstoppers_step in
348 let showstoppers_eat =
349 let heads_and_arities =
350 List.sort (fun (k,_) (h,_) -> compare k h)
351 (filter_map (function `Var(k,_) -> Some (k,0) | `I((k,_),args) -> Some (k,Listx.length args) | _ -> None ) p.ps) in
352 let rec multiple_arities =
356 | (x,i)::(y,j)::tl when x = y && i <> j ->
357 x::multiple_arities tl
358 | _::tl -> multiple_arities tl in
359 multiple_arities heads_and_arities in
361 let showstoppers_eat = sort_uniq showstoppers_eat in
362 let showstoppers_eat = List.filter
363 (fun x -> not (List.mem x showstoppers_step))
365 List.iter (fun v -> prerr_endline ("DANGEROUS STEP: " ^ string_of_var v)) showstoppers_step;
366 List.iter (fun v -> prerr_endline ("DANGEROUS EAT: " ^ string_of_var v)) showstoppers_eat;
367 p, showstoppers_step, showstoppers_eat
371 let ({ps} as p), showstoppers_step, showstoppers_eat = critical_showstoppers p in
372 let showstoppers = showstoppers_step @ showstoppers_eat in
373 let heads = List.sort compare (filter_map hd_of ps) in
374 let arities = precompute_edible_data p (uniq heads) in
375 let showstoppers, showstoppers_conv =
376 edible arities p.div ps (p.conv :> nf_nob list) showstoppers in
377 let l = List.filter (fun (x,_,_) -> not (List.mem x showstoppers)) arities in
379 List.fold_left (fun p (x,pos,(xx : i_n_var)) -> if pos = -1 then p else
380 let n = match xx with `I(_,args) -> Listx.length args | _ -> 0 in
382 let inst = make_lams v n in
383 (let l = Array.to_list (Array.init (p.freshno + 1) string_of_var) in
384 prerr_endline ("# INST_IN_EAT: " ^ string_of_var x ^ " := " ^ print ~l inst));
385 { p with sigma = p.sigma @ [x,inst] }
387 (* to avoid applied numbers in safe positions that
388 trigger assert failures subst_in_problem x inst p*)
392 let _,j,_ = List.find (fun (h,_,_) -> hd_of t = Some h) l in
396 let p = match p.div with
399 if List.mem (hd_of_i_var div) showstoppers
402 let n = match div with `I(_,args) -> Listx.length args | `Var _ -> 0 in
403 let p, bomb' = make_fresh_var p (-666) in
404 (if !bomb <> `Var (-1,-666) then
406 ("Bomb was duplicated! It was " ^ string_of_nf !bomb ^
407 ", tried to change it to " ^ string_of_nf bomb'));
409 prerr_endline ("Just created bomb var: " ^ string_of_nf !bomb);
410 let x = hd_of_i_var div in
411 let inst = make_lams !bomb n in
412 prerr_endline ("# INST (div): " ^ string_of_var x ^ " := " ^ string_of_nf inst);
413 let p = {p with div=None} in
414 (* subst_in_problem (hd_of_i_var div) inst p in *)
415 {p with sigma=p.sigma@[x,inst]} in
416 let dangerous_conv = showstoppers_conv in
417 let _ = prerr_endline ("dangerous_conv lenght:" ^ string_of_int (List.length dangerous_conv));
418 List.iter (fun l -> prerr_endline (String.concat " " (List.map string_of_var l))) dangerous_conv; in
420 List.map (function s,t ->
422 if s <> [] then t else (
423 (match t with | `Var _ -> raise Not_found | _ -> ());
424 let _ = List.find (fun h -> hd_of t = Some h) showstoppers in
426 with Not_found -> match hd_of t with
427 | None -> assert (t = `N ~-1); t
429 prerr_endline ("FREEZING " ^ string_of_var h);
430 `N ~-1 (* convergent dummy*)
431 ) (List.combine showstoppers_conv p.conv) in
437 match List.nth ps n with
442 let old_conv = p.conv in
443 let p = { p with ps; conv } in
444 if l <> [] || old_conv <> conv
445 then prerr_endline (string_of_problem "eat" p);
446 if List.for_all (function `N _ -> true | _ -> false) ps && p.div = None then
451 let instantiate p x perm n =
452 (if hd_of_i_var (cast_to_i_var !bomb) = x
453 then failwithProblem p ("BOMB (" ^ string_of_nf !bomb ^ ") cannot be instantiated!"));
454 let arity_of_x = max_arity_tms x (all_terms p) in
455 (if arity_of_x = None then failwithProblem p "step on var non occurring in problem");
456 let arity_of_x = Util.option_get(arity_of_x) in
457 (if arity_of_x = min_int then failwithProblem p "step on fake variable");
458 (if arity_of_x <= 0 then failwithProblem p "step on var of non-positive arity");
459 (if perm < 1 || perm > arity_of_x then
460 failwithProblem p ("Tried to permutate variable "^ string_of_var x ^" beyond its max arity"));
461 let n = (prerr_endline "WARNING: using constant initialSpecialK"); p.initialSpecialK in
462 let arities = Array.to_list (Array.make (n+1) min_int) in
463 let p,vars = make_fresh_vars p arities in
464 (* manual lifting of vars by perm in next line *)
465 let vars = List.map (function `Var (n,ar) -> `Var (n+perm,ar)) vars in
466 let args = Listx.from_list vars in
468 (* other_vars are the variables which are delayed and re-applied to the match *)
469 let other_vars = Array.mapi (fun n () -> `Var(n+1,min_int)) (Array.make (perm-1) ()) in
470 let other_vars = Array.to_list other_vars in
471 (* 666, since it will be replaced anyway during subst: *)
472 let inst = `Match(`I((0,min_int),args),(x,-666),perm,bs,other_vars) in
473 (* Add a number of 'perm' leading lambdas *)
474 let inst = Array.fold_left (fun t () -> `Lam(false, t)) inst (Array.make perm ()) in
475 let p = {p with deltas=bs::p.deltas} in
476 subst_in_problem x inst p
479 let compute_special_k tms =
480 let rec aux k (t: nf) = Pervasives.max k (match t with
481 | `Lam(b,t) -> aux (k + if b then 1 else 0) t
482 | `I(n, tms) -> Listx.max (Listx.map (aux 0) (tms :> nf Listx.listx))
483 | `Match(t, _, liftno, bs, args) ->
484 List.fold_left max 0 (List.map (aux 0) ((t :> nf)::(args :> nf list)@List.map snd !bs))
489 ) in Listx.max (Listx.map (aux 0) tms)
492 let choose_step (n,p) =
493 let p, showstoppers_step, showstoppers_eat = critical_showstoppers p in
495 match showstoppers_step, showstoppers_eat with
497 prerr_endline ("INSTANTIATING CRITICAL TO EAT " ^ string_of_var y); y
500 (* Choose only variables still alive (with arity > 0) *)
501 List.sort compare (filter_map (
502 fun t -> match t with `Var _ -> None | x -> if arity_of_hd x <= 0 then None else hd_of x
503 ) ((match p.div with Some t -> [(t :> i_n_var)] | _ -> []) @ p.ps)) in
507 fst (List.find (((<) 0) ++ snd) (concat_map free_vars' (p.conv :> nf list)))
509 Not_found -> assert false)
511 prerr_endline ("INSTANTIATING TO EAT " ^ string_of_var x);
514 prerr_endline ("INSTANTIATING " ^ string_of_var x);
516 (* Strategy that decreases the special_k to 0 first (round robin)
517 1:11m42 2:14m5 3:11m16s 4:14m46s 5:12m7s 6:6m31s *)
521 hd_of (List.find (fun t ->
522 compute_special_k (Listx.Nil (t :> nf)) > 0 && arity_of_hd t > 0
525 | None -> assert false
527 prerr_endline ("INSTANTIATING AND HOPING " ^ string_of_var x);
531 (* Instantiate in decreasing order of compute_special_k
532 1:15m14s 2:13m14s 3:4m55s 4:4m43s 5:4m34s 6:6m28s 7:3m31s
535 (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
538 prerr_endline ("INSTANTIATING AND HOPING " ^ string_of_var x);
544 compute_special_k (Listx.from_list (all_terms p :> nf list) )in
545 if special_k < n then
546 prerr_endline ("@@@@ NEW INSTANTIATE PHASE (" ^ string_of_int special_k ^ ") @@@@");
547 let arity_of_x = Util.option_get (max_arity_tms x (all_terms p)) in
548 x,arity_of_x,special_k
552 let first bound p var f =
553 let p = {p with trail = (List.map (!) p.deltas)::p.trail} in
556 (prerr_endline (">>>>>>>>>>>>>>>>>> BACKTRACKING ON "^ string_of_var var ^" J="^ string_of_int i ^" <<<<<<<<<<<<<<<<<<");
563 List.iter (fun (r,l) -> r := l) (List.combine p.deltas (List.hd p.trail)) ;
568 let rec auto_eat (n,p) =
569 prerr_endline "{{{{{{{{ Computing measure before auto_instantiate }}}}}}";
570 let m = problem_measure p in
571 let x, arity_of, n = choose_step (n,p) in
572 first arity_of p x (fun p j ->
573 let p' = instantiate p x j n in
577 prerr_endline "{{{{{{{{ Computing measure inafter auto_instantiate }}}}}}";
578 let delta = problem_measure p - m in
579 (* let delta = m - problem_measure p' in *)
583 ("Measure did not decrease (+=" ^ string_of_int delta ^ ")"))
584 else prerr_endline ("$ Measure decreased of " ^ string_of_int delta);
589 prerr_endline ("@@@@ FIRST INSTANTIATE PHASE (" ^ string_of_int n ^ ") @@@@");
592 | `Continue p -> auto_eat (n,p)
598 x y = y 0 a y = k k z = z 0 c y = k y u = u h1 h2 0 h2 a = h3
599 1 x a c 1 a 0 c 1 k c 1 c 0 1 k 1 k 1 k
600 2 x a y 2 a 0 y 2 k y 2 y 0 2 y 0 2 h2 0 2 h3
601 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)
602 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
603 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
604 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
607 b u = u l1 l2 0 e _ _ _ _ = f l3 n = n j 0
610 3 l2 0 (\u. u h1 (\w. h3) 0) 3 l3 (\u. u h1 (\w. h3) 0) 3 j h1 (\w. h3) 0 0
611 4 l2 0 c 4 l3 c 4 c j 0
612 5 e l1 l2 0 0 5 f 5 f
613 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
618 x a (b (a c)) a 0 = 1 ? (b (a c)) 8
619 x a (b d') a 0 = 1 ? (b d') 7
620 x b (a c) b 0 = 1 ? (a c) 4
621 x b (a c') b 0 = 1 ? (a c') 5
629 b 4 = 8 (* b (a c) *)
634 (************** Tests ************************)
636 let optimize_numerals p =
637 let replace_in_sigma perm =
638 let rec aux = function
639 | `N n -> `N (List.nth perm n)
641 | `I _ -> assert false
643 | `Lam(v,t) -> `Lam(v, aux t)
644 | `Match(_,_,_,bs,_) as t -> (bs := List.map (fun (n,t) -> (List.nth perm n, t)) !bs); t
646 in List.map (fun (n,t) -> (n,aux t))
648 let deltas' = List.mapi (fun n d -> (n, List.map fst !d)) p.deltas in
649 let maxs = Array.to_list (Array.init (List.length deltas') (fun _ -> 0)) in
650 let max = List.fold_left max 0 (concat_map snd deltas') in
651 let perm,_ = List.fold_left (fun (perm, maxs) (curr_n:int) ->
652 let containing = filter_map (fun (i, bs) -> if List.mem curr_n bs then Some i else None) deltas' in
653 (* (prerr_endline (string_of_int curr_n ^ " occurs in: " ^ (String.concat " " (List.map string_of_int containing)))); *)
654 let neww = List.fold_left Pervasives.max 0 (List.mapi (fun n max -> if List.mem n containing then max else 0) maxs) in
655 let maxs = List.mapi (fun i m -> if List.mem i containing then neww+1 else m) maxs in
657 ) ([],maxs) (Array.to_list (Array.init (max+1) (fun x -> x))) in
658 replace_in_sigma (List.rev perm) p.sigma
661 let env_of_sigma freshno sigma should_explode =
668 e,Pure.lift (-n-1) (snd (List.find (fun (i,_) -> i = n) sigma)),[]
671 if should_explode && n = hd_of_i_var (cast_to_i_var !bomb)
672 then ([], (let f t = Pure.A(t,t) in f (Pure.L (f (Pure.V 0)))), [])
673 else ([],Pure.V n,[]))::e
677 prerr_endline "########## main ##########";;
680 v ==> v := \a. a k1 .. kn \^m.0
681 + ==> v := \^k. numero for every v such that ...
682 * ==> tries v as long as possible and then +v as long as possible
685 let rec aux ({ps} as p) n l =
686 if List.for_all (function `N _ -> true | _ -> false) ps && p.div = None then begin
689 let _ = prerr_endline (string_of_problem "main" p) in
693 | [] -> read_line (),[] in
702 | `DoneWith -> assert false (*aux (eat p) n l*) (* CSC: TODO *)
703 | `Step x -> assert false
704 (* let x = var_of_string x in
705 aux (instantiate p x 1 n) n l *)
706 | `Auto -> aux (auto p n) n l
710 Console.print_hline();
711 bomb := `Var (-1,-666);
712 let p_finale = aux p n cmds in
713 let freshno,sigma = p_finale.freshno, p_finale.sigma in
714 prerr_endline ("------- <DONE> ------\n ");
715 (* prerr_endline (string_of_problem "Original problem" p); *)
716 prerr_endline "---------------------";
717 let l = Array.to_list (Array.init (freshno + 1) string_of_var) in
718 prerr_endline (" BOMB == " ^ print ~l !bomb);
719 prerr_endline "---------------------";
720 List.iter (fun (x,inst) -> prerr_endline (string_of_var x ^ " := " ^ print ~l inst)) sigma;
722 prerr_endline "----------------------";
724 List.fold_left (fun ps (x,inst) ->
725 (* CSC: XXXX Is the subst always sorted correctly? Otherwise, implement a recursive subst *)
726 (* In this non-recursive version, the intermediate states may containt Matchs *)
727 List.map (fun t -> let t = subst false x inst (t :> nf) in cast_to_i_num_var t) ps)
728 (p.ps :> i_num_var list) sigma in
729 prerr_endline (string_of_problem {p with ps= List.map (function t -> cast_to_i_n_var t) ps; freshno});
730 List.iteri (fun i (n,more_args) -> assert (more_args = 0 && n = `N i)) ps ;
732 prerr_endline "---------<OPT>----------";
733 let sigma = optimize_numerals p_finale in (* optimize numerals *)
734 let l = Array.to_list (Array.init (freshno + 1) string_of_var) in
735 List.iter (fun (x,inst) -> prerr_endline (string_of_var x ^ " := " ^ print ~l inst)) sigma;
736 prerr_endline "---------<PURE>---------";
737 let div = option_map (fun div -> ToScott.scott_of_nf (div :> nf)) p.div in
738 let conv = List.map (fun t -> ToScott.scott_of_nf (t :> nf)) p.conv in
739 let ps = List.map (fun t -> ToScott.scott_of_nf (t :> nf)) p.ps in
740 let sigma = List.map (fun (x,inst) -> x, ToScott.scott_of_nf inst) sigma in
741 (*let ps_ok = List.fold_left (fun ps (x,inst) ->
742 List.map (Pure.subst false x inst) ps) ps sigma in*)
743 let e = env_of_sigma freshno sigma true in
744 let e' = env_of_sigma freshno sigma false in
747 prerr_endline "---------<PPP>---------";
749 "[" ^ String.concat ";" (List.map (fun (e,t,[]) -> print_e e ^ ":" ^ Pure.print t) e) ^ "]"
751 prerr_endline (print_e e);
752 List.iter (fun (t,t_ok) ->
753 prerr_endline ("T0= " ^ Pure.print t ^ "\nTM= " ^ Pure.print (Pure.unwind (e,t,[])) ^ "\nOM= " ^ Pure.print t_ok);
754 (*assert (Pure.unwind (e,t,[]) = t_ok)*)
755 ) (List.combine ps ps_ok);
757 prerr_endline "--------<REDUCE>---------";
758 (function Some div ->
759 print_endline (Pure.print div);
760 let t = Pure.mwhd (e',div,[]) in
761 prerr_endline ("*:: " ^ (Pure.print t));
762 prerr_endline (print !bomb);
763 assert (t = ToScott.scott_of_nf (!bomb:>nf))
766 prerr_endline ("_::: " ^ (Pure.print n));
767 let t = Pure.mwhd (e,n,[]) in
768 prerr_endline ("_:: " ^ (Pure.print t))
770 List.iteri (fun i n ->
771 prerr_endline ((string_of_int i) ^ "::: " ^ (Pure.print n));
772 let t = Pure.mwhd (e,n,[]) in
773 prerr_endline ((string_of_int i) ^ ":: " ^ (Pure.print t));
774 assert (t = Scott.mk_n i)
776 prerr_endline "-------- </DONE> --------"
779 (********************** problems *******************)
781 let zero = `Var(0,0);;
786 | `Var _ as i -> cast_to_i_n_var (mk_app i zero)
790 type t = problem * int * string list;;
792 let magic_conv ~div ~conv ~nums cmds =
793 let all_tms = (match div with None -> [] | Some div -> [div]) @ nums @ conv in
794 let all_tms, var_names = parse' all_tms in
795 let div, (tms, conv) = match div with
796 | None -> None, list_cut (List.length nums, all_tms)
797 | Some _ -> Some (List.hd all_tms), list_cut (List.length nums, List.tl all_tms) in
799 if match div with None -> false | Some div -> List.exists (eta_subterm div) (tms@conv)
801 prerr_endline "--- TEST SKIPPED ---";
802 {freshno=0; div=None; conv=[]; ps=[]; sigma=[]; deltas=[]; initialSpecialK=0; trail=[]}, 0, []
804 let tms = sort_uniq ~compare:eta_compare tms in
805 let special_k = compute_special_k (Listx.from_list all_tms) in (* compute initial special K *)
807 let div = option_map cast_to_i_var div in
808 let conv = Util.filter_map (function #i_n_var as t -> Some (cast_to_i_n_var t) | _ -> None) conv in
809 let tms = List.map cast_to_i_n_var tms in
811 let ps = List.map append_zero tms in (* crea lista applicando zeri o dummies *)
812 let freshno = List.length var_names in
814 let dummy = `Var (max_int / 2, -666) in
815 [ ref (Array.to_list (Array.init (List.length ps) (fun i -> i, dummy))) ] in
817 {freshno; div; conv; ps; sigma=[] ; deltas; initialSpecialK=special_k; trail}, special_k, cmds
820 let magic strings cmds = magic_conv None [] strings cmds;;