9 exception Backtrack of string
12 The number of arguments which can applied to numbers
13 safely, depending on the encoding of numbers.
14 For Scott's encoding, two.
16 let num_more_args = 2;;
17 let _very_verbose = false;;
20 if _very_verbose then prerr_endline s
23 let convergent_dummy = `N(-1);;
25 type discriminating_set = (int * nf) list;;
29 ; div: i_var option (* None = bomb *)
30 ; conv: i_n_var list (* the inerts that must converge *)
31 ; ps: i_n_var list (* the n-th inert must become n *)
32 ; sigma: (int * nf) list (* the computed substitution *)
33 ; deltas: discriminating_set ref list (* collection of all branches *)
34 ; initialSpecialK: int
36 ; var_names : string list (* names of the original free variables *)
38 ; trail: discriminating_set list list
41 let label_of_problem {label} = label;;
43 let string_of_var l x =
46 with Failure "nth" -> "`" ^ string_of_int x
48 let string_of_term p t = print ~l:p.var_names (t :> nf);;
50 let first bound p var f =
51 let p = {p with trail = (List.map (!) p.deltas)::p.trail} in
54 raise (Backtrack ("no more alternatives for " ^ string_of_var p.var_names var))
59 prerr_endline (">>>>>> BACKTRACK (reason: " ^ s ^") measure=$ ");
60 List.iter (fun (r,l) -> r := l) (List.combine p.deltas (List.hd p.trail)) ;
61 prerr_endline("Now trying var="^string_of_var p.var_names var^" i="^string_of_int i);
68 (match p.div with None -> [] | Some t -> [(t :> i_n_var)])
73 let measure_of_term, measure_of_terms =
74 let rec aux = function
75 | `N _ | `Bottom | `Pacman -> 0
76 | `Var(_,ar) -> if ar = min_int then 0 else max 0 ar (*assert (ar >= 0); ar*)
78 | `I(v,args) -> aux (`Var v) + aux_many (Listx.to_list args :> nf list)
79 | `Match(u,(_,ar),_,_,args) -> aux (u :> nf) + (if ar <= 0 then 0 else ar - 1) + aux_many (args :> nf list)
80 and aux_many tms = List.fold_right ((+) ++ aux) tms 0 in
81 (fun t -> aux (t :> nf)), (fun l -> aux_many (l :> nf list))
84 let sum_arities p = measure_of_terms (all_terms p)
86 let problem_measure p = sum_arities p;;
87 let string_of_measure = string_of_int;;
89 let string_of_problem label ({freshno; div; conv; ps; deltas} as p) =
90 let deltas = String.concat ("\n# ") (List.map (fun r -> String.concat " <> " (List.map (fun (i,_) -> string_of_int i) !r)) deltas) in
91 let l = p.var_names in
94 "# DISPLAY PROBLEM (" ^ label ^ ") " ^ "measure=" ^ string_of_measure (problem_measure p);
95 if List.length p.deltas > 1 then (
96 "# Discriminating sets (deltas):\n" ^
101 (match div with None -> "# no D" | Some div -> "D ("^string_of_int (measure_of_term div)^")"^ print ~l (div :> nf));
103 @ List.map (fun t -> if t = convergent_dummy then "#C" else "C ("^string_of_int (measure_of_term t)^") " ^ print ~l (t :> nf)) conv
104 @ List.mapi (fun i t -> string_of_int i ^ " ("^string_of_int (measure_of_term t)^") " ^ print ~l (t :> nf)) ps
109 let failwithProblem p reason =
110 print_endline (string_of_problem "FAIL" p);
114 let make_fresh_var p arity =
115 let freshno = p.freshno + 1 in
116 {p with freshno}, `Var(freshno,arity)
119 let make_fresh_vars p arities =
121 (fun arity (p, vars) -> let p, var = make_fresh_var p arity in p, var::vars)
126 let simple_expand_match ps =
127 let rec aux_nob level = function
128 | #i_num_var as t -> (aux_i_num_var level t :> nf)
129 | `Lam(b,t) -> `Lam(b,aux (level+1) t)
131 and aux level = function
133 | #nf_nob as t -> aux_nob level t
134 and aux_i_num_var level = function
135 | `Match(u,v,bs_lift,bs,args) as torig ->
136 let (u : i_num_var) = aux_i_num_var level u in
137 bs := List.map (fun (n, x) -> n, aux 0 x) !bs;
141 let i = index_of ~eq:eta_eq (lift (-level) u) (ps :> nf list) in (* can raise Not_found *)
142 let t = cast_to_i_num_var (mk_match (`N i) v bs_lift bs (args :> nf list)) in
144 aux_i_num_var level t
146 | _ -> raise Not_found)
148 cast_to_i_num_var (mk_appl (`Match(u,v,bs_lift,bs,[])) (List.map (aux_nob level) args)))
149 | `I(v,args) -> cast_to_i_num_var (mk_appl (`Var v) (List.map (aux_nob level) (Listx.to_list args)))
150 | `N _ | `Var _ as t -> t
155 let rec aux x = let x' = f x in if x <> x' then aux x' else x in aux
158 let rec super_simplify_ps ps =
159 fixpoint (List.map (fun x -> cast_to_i_n_var (simple_expand_match ps (x :> i_num_var))))
162 let rec super_simplify_ps_with_match ps =
163 fixpoint (List.map (cast_to_i_num_var ++ (simple_expand_match ps)))
166 let super_simplify ({div; ps; conv} as p) =
167 let ps = super_simplify_ps p.ps p.ps in
168 let conv = super_simplify_ps ps p.conv in
169 let div = option_map (fun div ->
170 let divs = super_simplify_ps p.ps ([div] :> i_n_var list) in
172 {p with div=option_map cast_to_i_var div; ps; conv}
174 let cast_to_ps_with_match =
176 #i_num_var as y -> (y : i_num_var)
177 | `Bottom | `Pacman -> raise (Backtrack "BOT/PAC in ps")
179 prerr_endline (print (t :> nf));
180 assert false (* algorithm failed *)
182 let subst_in_problem x inst ({freshno; div; conv; ps; sigma} as p) =
183 let len_ps = List.length ps in
184 (*(let l = Array.to_list (Array.init (freshno + 1) string_of_var) in
185 prerr_endline ("# INST0: " ^ string_of_var x ^ " := " ^ print ~l inst));*)
186 let rec aux_ps ((freshno,acc_ps,acc_new_ps) as acc) =
190 (*prerr_endline ("EXPAND t:" ^ print (t :> nf));*)
191 let t = subst false false x inst (t :> nf) in
192 (*prerr_endline ("SUBSTITUTED t:" ^ print (t :> nf));*)
193 let freshno,new_t,acc_new_ps =
195 expand_match (freshno,acc_ps@`Var(max_int/3,-666)::todo_ps,acc_new_ps) t
196 with Pacman | Bottom -> raise (Backtrack "Pacman/Bottom in ps")
198 aux_ps (freshno,acc_ps@[new_t],acc_new_ps) todo_ps
200 (* cut&paste from aux above *)
201 and aux_conv ps ((freshno,acc_conv,acc_new_ps) as acc) =
205 (*prerr_endline ("EXPAND t:" ^ print (t :> nf));*)
206 let t = subst false false x inst (t :> nf) in
207 (*prerr_endline ("SUBSTITUTED t:" ^ print (t :> nf));*)
208 let freshno,new_t,acc_new_ps =
210 expand_match (freshno,ps,acc_new_ps) t
211 with Pacman -> freshno,convergent_dummy,acc_new_ps
212 | Bottom -> raise (Backtrack "Bottom in conv") in
213 aux_conv ps (freshno,acc_conv@[new_t],acc_new_ps) todo_conv
215 (* cut&paste from aux' above *)
216 and aux_div ps (freshno,acc_new_ps) =
218 | None -> freshno, None, acc_new_ps
220 let t = subst false false x inst (t :> nf) in
222 let freshno,new_t,acc_new_ps = expand_match (freshno,ps,acc_new_ps) t in
223 (* backtrack if it is a number or a variable *)
225 | `N _ -> raise (Backtrack "div=`N")
227 | `I _ as new_t -> freshno, Some(new_t), acc_new_ps
229 | Bottom -> freshno, None, acc_new_ps
230 | Pacman -> raise (Backtrack "div=PAC")
232 and expand_match ((freshno,acc_ps,acc_new_ps) as acc) t =
234 | `Match(u',orig,bs_lift,bs,args) ->
235 let freshno,u,acc_new_ps = expand_match acc (u' :> nf) in
238 | `N i -> acc_new_ps,i
240 let ps = List.map (fun t -> cast_to_ps_with_match (subst false false x inst (t:> nf))) (acc_ps@acc_new_ps) in
241 let super_simplified_ps = super_simplify_ps_with_match ps ps in
242 (*prerr_endline ("CERCO u:" ^ print (fst u :> nf));
243 List.iter (fun x -> prerr_endline ("IN: " ^ print (fst x :> nf))) ps;
244 List.iter (fun x -> prerr_endline ("IN2: " ^ print (fst x :> nf))) super_simplified_ps;*)
245 match index_of_opt ~eq:eta_eq super_simplified_ps (u :> i_num_var) with
246 Some i -> acc_new_ps, i
247 | None -> acc_new_ps@[u], len_ps + List.length acc_new_ps
250 if List.exists (fun (j,_) -> i=j) !bs then
253 let freshno,v = freshno+1, `Var (freshno+1, -666) in (* make_fresh_var freshno in *)
256 (*prerr_endlie ("t DA RIDURRE:" ^ print (`Match(`N i,arity,bs_lift,bs,args) :> nf) ^ " more_args=" ^ string_of_int more_args);*)
257 let t = mk_match (`N i) orig bs_lift bs (args :> nf list) in
258 (*prerr_endline ("NUOVO t:" ^ print (fst t :> nf) ^ " more_args=" ^ string_of_int (snd t));*)
259 expand_match (freshno,acc_ps,acc_new_ps) t
260 | `Lam _ -> assert false (* algorithm invariant/loose typing *)
261 | `Bottom -> raise Bottom
262 | `Pacman -> raise Pacman
264 let x = simple_expand_match (acc_ps@acc_new_ps) x in
265 freshno,cast_to_i_n_var x,acc_new_ps in
267 let freshno,old_ps,new_ps = aux_ps (freshno,[],[]) ps in
268 let freshno,conv,new_ps = aux_conv old_ps (freshno,[],new_ps) conv in
269 let freshno,div,new_ps = aux_div old_ps (freshno,new_ps) (div :> i_num_var option) in
271 let ps = List.map cast_to_i_n_var (old_ps @ new_ps) in
272 let conv = List.map cast_to_i_n_var conv in
273 let p = {p with freshno; div; conv; ps} in
274 prerr_endline ("# INST: " ^ string_of_var p.var_names x ^ " := " ^ string_of_term p inst);
275 ( (* check if double substituting a variable *)
276 if List.exists (fun (x',_) -> x = x') sigma
277 then failwithProblem p ("Variable "^ string_of_var p.var_names x ^"replaced twice")
279 let p = {p with sigma = sigma@[x,inst]} in
280 let p = super_simplify p in
281 prerr_endline (string_of_problem "instantiate" p);
287 let arity_of arities hd =
288 let pos,_,nargs = List.find (fun (_,hd',_) -> hd=hd') arities in
289 nargs + if pos = -1 then - 1 else 0
292 let rec dangerous arities showstoppers =
298 | `Match(t,_,liftno,bs,args) ->
299 (* CSC: XXX partial dependency on the encoding *)
301 `N _ -> List.iter (dangerous arities showstoppers) args
302 | `Match _ as t -> dangerous arities showstoppers t ; List.iter (dangerous arities showstoppers) args
303 | `Var(x,_) -> dangerous_inert arities showstoppers x args num_more_args
304 | `I((x,_),args') -> dangerous_inert arities showstoppers x (Listx.to_list args' @ args) num_more_args
306 | `I((k,_),args) -> dangerous_inert arities showstoppers k (Listx.to_list args) 0
308 and dangerous_inert arities showstoppers k args more_args =
309 List.iter (dangerous arities showstoppers) args ;
310 if List.mem k showstoppers then raise Dangerous else
312 let arity = arity_of arities k in
313 if List.length args + more_args > arity then raise Dangerous else ()
317 (* cut & paste from above *)
318 let rec dangerous_conv p arities showstoppers =
324 | `Match(t,_,liftno,bs,args) ->
325 (* CSC: XXX partial dependency on the encoding *)
327 `N _ -> concat_map (dangerous_conv p arities showstoppers) args
328 | `Match _ as t -> dangerous_conv p arities showstoppers t @ concat_map (dangerous_conv p arities showstoppers) args
329 | `Var(x,_) -> dangerous_inert_conv p arities showstoppers x [] args 2
330 | `I((x,_),args') -> dangerous_inert_conv p arities showstoppers x (Listx.to_list args') args 2
332 | `I((k,_),args) -> dangerous_inert_conv p arities showstoppers k (Listx.to_list args) [] 0
334 and dangerous_inert_conv p arities showstoppers k args match_args more_args =
335 let all_args = args @ match_args in
336 let dangerous_args = concat_map (dangerous_conv p arities showstoppers) all_args in
337 let all_args = (all_args :> nf list) in
338 if dangerous_args = [] then (
339 if List.mem k showstoppers then k :: concat_map free_vars all_args else
341 let arity = arity_of arities k in
342 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);
343 if more_args > 0 (* match argument*) && List.length args = arity then []
344 else if List.length all_args + more_args > arity then k :: concat_map free_vars all_args else []
347 ) else k :: concat_map free_vars all_args
349 (* inefficient algorithm *)
350 let rec edible ({div; conv; ps} as p) arities showstoppers =
351 let rec aux showstoppers =
354 | x::xs when List.exists (fun y -> hd_of x = Some y) showstoppers ->
355 (* se la testa di x e' uno show-stopper *)
356 let new_showstoppers = sort_uniq (showstoppers @ free_vars (x :> nf)) in
357 (* aggiungi tutte le variabili libere di x *)
358 if List.length showstoppers <> List.length new_showstoppers then
359 aux new_showstoppers ps
364 None -> aux showstoppers xs
367 dangerous arities showstoppers (x : i_n_var :> nf_nob) ;
371 aux (sort_uniq (h::showstoppers)) ps
373 let showstoppers = sort_uniq (aux showstoppers ps) in
375 List.map (dangerous_conv p arities showstoppers) (conv :> nf_nob list) in
377 prerr_endline ("dangerous_conv lenght:" ^ string_of_int (List.length dangerous_conv));
378 List.iter (fun l -> prerr_endline (String.concat " " (List.map (string_of_var p.var_names) l))) dangerous_conv;
380 let showstoppers' = showstoppers @ List.concat dangerous_conv in
381 let showstoppers' = sort_uniq (match div with
382 | None -> showstoppers'
384 if List.exists ((=) (hd_of_i_var div)) showstoppers'
385 then showstoppers' @ free_vars (div :> nf) else showstoppers') in
386 if showstoppers <> showstoppers' then edible p arities showstoppers' else showstoppers', dangerous_conv
389 let precompute_edible_data {ps; div} xs =
390 let aux t = match t with `Var _ -> 0 | `I(_, args) -> Listx.length args | `N _ -> assert false in
391 (fun l -> match div with
393 | Some div -> (-1, hd_of_i_var div, aux div) :: l)
395 let i, tm = Util.findi (fun y -> hd_of y = Some hd) ps in
400 (** Returns (p, showstoppers_step, showstoppers_eat) where:
401 - showstoppers_step are the heads occurring twice
402 in the same discriminating set
403 - showstoppers_eat are the heads in ps having different number
405 let critical_showstoppers p =
406 let p = super_simplify p in
407 let hd_of_div = match p.div with None -> [] | Some t -> [hd_of_i_var t] in
408 let showstoppers_step =
409 concat_map (fun bs ->
410 let heads = List.map (fun (i,_) -> List.nth p.ps i) !bs in
411 let heads = List.sort compare (hd_of_div @ filter_map hd_of heads) in
412 snd (split_duplicates heads)
414 if List.exists (fun t -> [hd_of t] = List.map (fun x -> Some x) hd_of_div) p.conv
415 then hd_of_div else [] in
416 let showstoppers_step = sort_uniq showstoppers_step in
417 let showstoppers_eat =
418 let heads_and_arities =
419 List.sort (fun (k,_) (h,_) -> compare k h)
420 (filter_map (function `Var(k,_) -> Some (k,0) | `I((k,_),args) -> Some (k,Listx.length args) | _ -> None ) p.ps) in
421 let rec multiple_arities =
425 | (x,i)::(y,j)::tl when x = y && i <> j ->
426 x::multiple_arities tl
427 | _::tl -> multiple_arities tl in
428 multiple_arities heads_and_arities in
430 let showstoppers_eat = sort_uniq showstoppers_eat in
431 let showstoppers_eat = List.filter
432 (fun x -> not (List.mem x showstoppers_step))
434 List.iter (fun v -> prerr_endline ("DANGEROUS STEP: " ^ (string_of_var p.var_names) v)) showstoppers_step;
435 List.iter (fun v -> prerr_endline ("DANGEROUS EAT: " ^ (string_of_var p.var_names) v)) showstoppers_eat;
436 p, showstoppers_step, showstoppers_eat
440 let ({ps} as p), showstoppers_step, showstoppers_eat = critical_showstoppers p in
441 let showstoppers = showstoppers_step @ showstoppers_eat in
442 let heads = List.sort compare (filter_map hd_of ps) in
443 let arities = precompute_edible_data p (uniq heads) in
444 let inedible, showstoppers_conv = edible p arities showstoppers in
445 let l = List.filter (fun (_,hd,_) -> not (List.mem hd inedible)) arities in
447 List.fold_left (fun p (pos,hd,nargs) -> if pos = -1 then p else
449 let inst = make_lams v nargs in
450 prerr_endline ("# [INST_IN_EAT] eating: " ^ string_of_var p.var_names hd ^ " := " ^ string_of_term p inst);
451 { p with sigma = p.sigma @ [hd,inst] }
453 (* to avoid applied numbers in safe positions that
454 trigger assert failures subst_in_problem x inst p*)
458 let j,_,_ = List.find (fun (_,hd,_) -> hd_of t = Some hd) l in
462 let p = match p.div with
465 if List.mem (hd_of_i_var div) inedible
468 let n = match div with `I(_,args) -> Listx.length args | `Var _ -> 0 in
469 let x = hd_of_i_var div in
470 let inst = make_lams `Bottom n in
471 subst_in_problem x inst p in
472 let dangerous_conv = showstoppers_conv in
473 prerr_endline ("dangerous_conv lenght:" ^ string_of_int (List.length dangerous_conv));
474 List.iter (fun l -> prerr_endline (String.concat " " (List.map (string_of_var p.var_names) l))) dangerous_conv;
476 List.map (function s,t ->
478 if s <> [] then t else (
479 (match t with | `Var _ -> raise Not_found | _ -> ());
480 let _ = List.find (fun h -> hd_of t = Some h) inedible in
482 with Not_found -> match hd_of t with
483 | None -> assert (t = convergent_dummy); t
485 prerr_endline ("FREEZING " ^ string_of_var p.var_names h);
487 ) (List.combine showstoppers_conv p.conv) in
493 match List.nth ps n with
498 let old_conv = p.conv in
499 let p = { p with ps; conv } in
500 if l <> [] || old_conv <> conv
501 then prerr_endline (string_of_problem "eat" p);
502 if List.for_all (function `N _ -> true | _ -> false) ps && p.div = None then
508 let safe_arity_of_var p x =
509 (* Compute the minimum number of arguments when x is in head
510 position at p.div or p.ps *)
512 | `Var(y,_) -> if x = y then 0 else max_int
513 | `I((y,_),args) -> if x = y then Listx.length args else max_int
515 let tms = ((match p.div with None -> [] | Some t -> [(t :> i_n_var)]) @ p.ps) in
516 List.fold_left (fun acc t -> Pervasives.min acc (aux t)) max_int tms
519 let instantiate p x perm =
520 let n = (prerr_endline ("WARNING: using constant initialSpecialK=" ^ string_of_int p.initialSpecialK)); p.initialSpecialK in
521 let arities = Array.to_list (Array.make (n+1) min_int) in
522 let p,vars = make_fresh_vars p arities in
523 (* manual lifting of vars by perm in next line *)
524 let vars = List.map (function `Var (n,ar) -> `Var (n+perm,ar)) vars in
525 let args = Listx.from_list vars in
527 (* other_vars are the variables which are delayed and re-applied to the match *)
528 let other_vars = Array.mapi (fun n () -> `Var(n+1,min_int)) (Array.make (perm-1) ()) in
529 let other_vars = Array.to_list other_vars in
530 (* 666, since it will be replaced anyway during subst: *)
531 let inst = `Match(`I((0,min_int),args),(x,-666),perm,bs,other_vars) in
532 (* Add a number of 'perm' leading lambdas *)
533 let inst = Array.fold_left (fun t () -> `Lam(false, t)) inst (Array.make perm ()) in
534 let p = {p with deltas=bs::p.deltas} in
535 subst_in_problem x inst p
538 let compute_special_k tms =
539 let rec aux k (t: nf) = Pervasives.max k (match t with
540 | `Lam(b,t) -> aux (k + if b then 1 else 0) t
541 | `I(n, tms) -> Listx.max (Listx.map (aux 0) (tms :> nf Listx.listx))
542 | `Match(t, _, liftno, bs, args) ->
543 List.fold_left max 0 (List.map (aux 0) ((t :> nf)::(args :> nf list)@List.map snd !bs))
549 let rec aux' top t = match t with
550 | `Lam(_,t) -> aux' false t
551 | `I((_,ar), tms) -> max ar
552 (Listx.max (Listx.map (aux' false) (tms :> nf Listx.listx)))
553 | `Match(t, _, liftno, bs, args) ->
554 List.fold_left max 0 (List.map (aux' false) ((t :> nf)::(args :> nf list)@List.map snd !bs))
559 Listx.max (Listx.map (fun t -> max (aux 0 t) (aux' true t)) tms)
563 let p, showstoppers_step, showstoppers_eat = critical_showstoppers p in
565 match showstoppers_step, showstoppers_eat with
567 prerr_endline ("INSTANTIATING (critical eat) : " ^ string_of_var p.var_names y); y
569 prerr_endline ("INSTANTIATING (critical step): " ^ string_of_var p.var_names x); x
572 (* Choose only variables still alive (with arity > 0) *)
573 List.sort compare (filter_map (
574 fun t -> match t with `Var _ -> None | x -> if arity_of_hd x <= 0 then None else hd_of x
575 ) ((match p.div with Some t -> [(t :> i_n_var)] | _ -> []) @ p.ps)) in
579 fst (List.find (((<) 0) ++ snd) (concat_map free_vars' (p.conv :> nf list)))
581 Not_found -> assert false)
583 prerr_endline ("INSTANTIATING TO EAT " ^ string_of_var p.var_names x);
585 let arity_of_x = Util.option_get (max_arity_tms x (all_terms p)) in
586 let safe_arity_of_x = safe_arity_of_var p x in
587 x, min arity_of_x safe_arity_of_x
591 prerr_endline "{{{{{{{{ Computing measure before auto_instantiate }}}}}}";
592 let m = problem_measure p in
593 let x, arity_of = choose_step p in
594 first arity_of p x (fun p j ->
595 let p' = instantiate p x j in
599 prerr_endline "{{{{{{{{ Computing measure inafter auto_instantiate }}}}}}";
600 let delta = problem_measure p - m in
604 ("Measure did not decrease (+=" ^ string_of_int delta ^ ")"))
605 else prerr_endline ("$ Measure decreased: " ^ string_of_int delta);
610 prerr_endline ("@@@@ FIRST INSTANTIATE PHASE (" ^ string_of_int n ^ ") @@@@");
613 | `Continue p -> auto_eat p
617 (******************************************************************************)
619 let optimize_numerals p =
620 let replace_in_sigma perm =
621 let rec aux = function
622 | `N n -> `N (List.nth perm n)
624 | `I _ -> assert false
626 | `Lam(v,t) -> `Lam(v, aux t)
627 | `Match(_,_,_,bs,_) as t -> (bs := List.map (fun (n,t) -> (List.nth perm n, t)) !bs); t
629 in List.map (fun (n,t) -> (n,aux t))
631 let deltas' = List.mapi (fun n d -> (n, List.map fst !d)) p.deltas in
632 let maxs = Array.to_list (Array.init (List.length deltas') (fun _ -> 0)) in
633 let max = List.fold_left max 0 (concat_map snd deltas') in
634 let perm,_ = List.fold_left (fun (perm, maxs) (curr_n:int) ->
635 let containing = filter_map (fun (i, bs) -> if List.mem curr_n bs then Some i else None) deltas' in
636 (* (prerr_endline (string_of_int curr_n ^ " occurs in: " ^ (String.concat " " (List.map string_of_int containing)))); *)
637 let neww = List.fold_left Pervasives.max 0 (List.mapi (fun n max -> if List.mem n containing then max else 0) maxs) in
638 let maxs = List.mapi (fun i m -> if List.mem i containing then neww+1 else m) maxs in
640 ) ([],maxs) (Array.to_list (Array.init (max+1) (fun x -> x))) in
641 replace_in_sigma (List.rev perm) p.sigma
644 let env_of_sigma freshno sigma =
651 e,Pure.lift (-n-1) (snd (List.find (fun (i,_) -> i = n) sigma)),[]
653 Not_found -> ([],Pure.V n,[]))::e
656 (* ************************************************************************** *)
659 | `CompleteSeparable of string
660 | `CompleteUnseparable of string
665 `Complete | `Uncomplete
667 | `Separable of (int * Num.nf) list
668 | `Unseparable of string
672 Console.print_hline();
673 prerr_endline (string_of_problem "main" p);
674 let p_finale = auto p p.initialSpecialK in
675 let freshno,sigma = p_finale.freshno, p_finale.sigma in
676 prerr_endline ("------- <DONE> ------ measure=. \n ");
677 List.iter (fun (x,inst) -> prerr_endline (string_of_var p_finale.var_names x ^ " := " ^ string_of_term p_finale inst)) sigma;
679 prerr_endline "---------<OPT>----------";
680 let sigma = optimize_numerals p_finale in (* optimize numerals *)
681 List.iter (fun (x,inst) -> prerr_endline (string_of_var p_finale.var_names x ^ " := " ^ string_of_term p_finale inst)) sigma;
683 prerr_endline "---------<PURE>---------";
684 let scott_of_nf t = ToScott.scott_of_nf (t :> nf) in
685 let div = option_map scott_of_nf p.div in
686 let conv = List.map scott_of_nf p.conv in
687 let ps = List.map scott_of_nf p.ps in
689 let sigma' = List.map (fun (x,inst) -> x, ToScott.scott_of_nf inst) sigma in
690 let e' = env_of_sigma freshno sigma' in
692 prerr_endline "--------<REDUCE>---------";
693 (function Some div ->
694 print_endline (Pure.print div);
695 let t = Pure.mwhd (e',div,[]) in
696 prerr_endline ("*:: " ^ (Pure.print t));
700 verbose ("_::: " ^ (Pure.print n));
701 let t = Pure.mwhd (e',n,[]) in
702 verbose ("_:: " ^ (Pure.print t));
705 List.iteri (fun i n ->
706 verbose ((string_of_int i) ^ "::: " ^ (Pure.print n));
707 let t = Pure.mwhd (e',n,[]) in
708 verbose ((string_of_int i) ^ ":: " ^ (Pure.print t));
709 assert (t = Scott.mk_n i)
711 prerr_endline "-------- </DONE> --------";
715 let solve (p, todo) =
716 let completeness, to_run =
718 | `CompleteUnseparable s -> `Complete, `False s
719 | `CompleteSeparable _ -> `Complete, `True
720 | `Uncomplete -> `Uncomplete, `True in
721 completeness, match to_run with
722 | `False s -> `Unseparable s
727 Backtrack _ -> `Unseparable "backtrack"
730 let no_bombs_pacmans p =
731 not (List.exists (eta_subterm `Bottom) (p.ps@p.conv))
732 && not (List.exists (eta_subterm `Pacman) p.ps)
733 && Util.option_map (eta_subterm `Pacman) p.div <> Some true
737 if (let rec f = function
739 | hd::tl -> List.exists (eta_eq hd) tl || f tl in
740 f p.ps) (* FIXME what about initial fragments of numbers? *)
741 then `CompleteUnseparable "ps contains duplicates"
742 (* check if div occurs somewhere in ps@conv *)
743 else if (match p.div with
745 | Some div -> not (List.exists (eta_subterm div) (p.ps@p.conv))
746 ) && no_bombs_pacmans p
747 then `CompleteSeparable "no bombs, pacmans and div"
748 (* il check seguente e' spostato nel parser e lancia un ParsingError *)
749 (* else if false (* TODO bombs or div fuori da lambda in ps@conv *)
750 then `CompleteUnseparable "bombs or div fuori da lambda in ps@conv" *)
752 then `CompleteSeparable "no div"
756 let problem_of (label, div, conv, ps, var_names) =
757 (* TODO: replace div with bottom in problem??? *)
758 let all_tms = (match div with None -> [] | Some div -> [(div :> i_n_var)]) @ ps @ conv in
759 if all_tms = [] then failwith "problem_of: empty problem";
760 let initialSpecialK = compute_special_k (Listx.from_list (all_tms :> nf list)) in
761 let freshno = List.length var_names in
763 let dummy = `Var (max_int / 2, -666) in
764 [ ref (Array.to_list (Array.init (List.length ps) (fun i -> i, dummy))) ] in
767 let p = {freshno; div; conv; ps; sigma; deltas; initialSpecialK; trail; var_names; label} in