-let auto_main flags signature elems cache =
- let rec aux (elems, cache : 'a auto_status) =
-(* processa le hint dell'utente *)
-(* let cache, elems = filter_prune_hint cache elems in *)
- match elems with
-(* USER HINT
- | (m, s, size, don, todo, fl)::orlist when !hint <> None ->
- debug_print (lazy "skip");
- (match !hint with
- | Some i when condition_for_hint i todo ->
- aux tables flags cache orlist
- | _ ->
- hint := None;
- aux tables flags cache elems)
-*)
- | [] ->
- debug_print (lazy "gave up");
- Gaveup
- | (s, _, _, [],_)::orlist ->
- debug_print (lazy "success");
- Proved (s, (orlist, cache))
- | (s, size, don, (D (_,_,T))::todo, fl)::orlist
- when not flags.do_types ->
- debug_print (lazy "skip goal in Type");
- aux ((s, size, don, todo, fl)::orlist, cache)
- | (s, size, don, (S(g, key, c) as op)::todo, fl)::orlist ->
- let cache, orlist, fl, sibling_pruned =
- add_to_cache_and_del_from_orlist_if_green_cut
- g s cache key todo orlist fl size
- in
- let fl = remove_s_from_fl g fl in
- let don = if sibling_pruned then don else op::don in
- let s = NTactics.unfocus_tac s in
- aux ((s, size, don, todo, fl)::orlist, cache)
- | (s, size, don, todo, fl)::orlist
- when List.length(prop_only (d_goals todo)) > flags.maxwidth ->
- debug_print (lazy ("FAIL: WIDTH: " ^
- String.concat ", "
- (List.map (fun (x,_,_) -> string_of_int x) (d_goals todo))));
- aux (orlist, cache)
- | (s, size, don, todo, fl)::orlist when size > flags.maxsize ->
- debug_print (lazy ("FAIL: SIZE: "^string_of_int size ^
- " > " ^ string_of_int flags.maxsize ^ ": " ^
- String.concat ", "
- (List.map (fun (x,_,_) -> string_of_int x) (d_goals todo))));
- aux (orlist, cache)
- | _ when Unix.gettimeofday () > flags.timeout ->
- debug_print (lazy ("FAIL: TIMEOUT"));
+module T = ZipTree
+module Z = AndOrTree
+
+let img_counter = ref 0 ;;
+let show pos =
+ incr img_counter;
+ let file = ("/tmp/a"^string_of_int !img_counter^".dot") in
+ debug_print (lazy("generating " ^ file));
+ debug_do (fun () ->
+ let oc = open_out file in
+ let fmt = Format.formatter_of_out_channel oc in
+ GraphvizPp.Dot.header fmt;
+ Z.dump pp_item pos fmt;
+ GraphvizPp.Dot.trailer fmt;
+ Format.fprintf fmt "@?";
+ close_out oc;
+ ignore(Sys.command ("dot -Tpng "^file^" > "^file^".png"));
+ (*ignore(Sys.command ("eog "^file^".png"))*))
+;;
+
+let rightmost_bro pred pos =
+ let rec last acc pos =
+ let acc = if pred pos then Some pos else acc in
+ match Z.right pos with
+ | None -> acc
+ | Some pos -> last acc pos
+ in
+ last None pos
+;;
+
+let leftmost_bro pred pos =
+ let rec fst pos =
+ if pred pos then Some pos else
+ match Z.right pos with
+ | None -> None
+ | Some pos -> fst pos
+ in
+ fst pos
+;;
+
+let rec first_left_mark_L_as_D pred pos =
+ if pred pos then
+ Some pos
+ else
+ let pos =
+ match Z.getA pos with
+ | s,L (g,_) ->
+ Z.inject T.Nil (Z.setA s (D g) pos)
+ | _ -> pos
+ in
+ match Z.left pos with
+ | None -> None
+ | Some pos ->
+ first_left_mark_L_as_D pred pos
+;;
+
+let is_oS pos =
+ match Z.getO pos with
+ | S _ -> true
+ | D _ | L _ -> false
+;;
+
+
+let is_aS pos =
+ match Z.getA pos with
+ | _,S _ -> true
+ | _,D _ | _,L _ -> false
+;;
+
+let is_not_oS x = not (is_oS x);;
+let is_not_aS x = not (is_aS x);;
+
+let is_oL pos = match Z.getO pos with L _ -> true | _ -> false ;;
+let is_aL pos = match Z.getA pos with _,L _ -> true | _ -> false ;;
+
+let is_not_oL x = not (is_oL x) ;;
+let is_not_aL x = not (is_aL x) ;;
+
+let rec forall_left pred pos =
+ match Z.left pos with
+ | None -> true
+ | Some pos -> if pred pos then forall_left pred pos else false
+;;
+
+
+let rec product = function
+ | [] -> []
+ | ((g,s) :: tl) as l -> (s,List.map fst l) :: product tl
+;;
+
+let has_no_alternatives (pos : 'a and_pos) =
+ match Z.getA pos with
+ | _, L _ -> true
+ | _ -> false
+;;
+
+let rec collect_left_up (pos : 'a and_pos) =
+ match Z.left pos with
+ | Some pos ->
+ (match Z.getA pos with
+ | _, L (g,s) -> (g,s) :: collect_left_up pos
+ | _ -> [])
+ | None ->
+ match Z.upA pos with
+ | None -> [] (* root *)
+ | Some pos -> collect_left_up (Z.upO pos)
+;;
+
+let compute_failed_goals (pos : 'a and_pos) =
+ let curr = match Z.getA pos with (s,_,_),D g -> (g,s) | _ -> assert false in
+ product (List.rev (curr :: collect_left_up pos) )
+;;
+
+let pp_failures l =
+ debug_print (lazy("CACHE FAILURES/UNDERINSPECTION"));
+ List.iter (fun (s,gl) ->
+ debug_print (lazy("FAIL: " ^
+ String.concat " , " (List.map (fun g ->
+ match calculate_goal_ty g s with
+ | None ->
+ (try
+ let (i,_) = g in
+ let _,_,_,subst,_ = s#obj in
+ let _,cc,_,ty = NCicUtils.lookup_subst i subst in
+ let ty = mk_cic_term cc ty in
+ string_of_int i^":"^ppterm s ty
+ with NCicUtils.Subst_not_found _ -> "XXXX")
+ | Some gty ->
+ let s, gty = apply_subst s (ctx_of gty) gty in
+ string_of_int (fst g)^":"^ppterm s gty) gl))))
+ l
+;;
+
+let is_closed pos =
+ match Z.getA pos with
+ | (s,_,_),S (g,_)
+ | (s,_,_),D g ->
+ (match calculate_goal_ty g s with
+ | None -> true
+ | Some gty -> metas_of_term s gty = [])
+ | _, L _ -> assert false
+;;
+
+let auto_main flags signature (pos : 'a and_pos) cache =
+ let solved g depth size s (pos : 'a and_pos) =
+ Z.inject (T.Node(`Or,[D g,T.Node(`And(s,depth,size),[])])) pos
+ in
+ let failed (pos : 'a and_pos) =
+ pp_failures (compute_failed_goals pos);
+ Z.inject (T.Node(`Or,[])) pos
+ in
+
+ let rec next ~unfocus (pos : 'a and_pos) cache =
+ (* TODO: process USER HINT is any *)
+ match Z.downA pos with
+ | Z.Unexplored -> attack pos cache (Z.getA pos)
+ | Z.Alternatives pos -> nextO ~unfocus pos cache
+
+ and nextO ~unfocus (pos : 'a or_pos) cache =
+ match Z.getO pos with
+ | S _ | L _ -> assert false (* XXX set to Nil when backtrack *)
+ | D g ->
+ match Z.downO pos with
+ | Z.Solution (s,_,_) -> move_solution_up ~unfocus true s pos cache
+ | Z.Todo pos -> next ~unfocus:true pos cache
+
+ and next_choice_point (pos : 'a and_pos) cache =
+
+ let rec global_choice_point (pos : 'a and_pos) : 'a auto_result =
+(* prerr_endline "global"; show pos; *)
+ match Z.upA pos with
+ | None -> Gaveup
+ | Some alts ->
+ let alts = Z.inject T.Nil alts in
+ let alts =
+ match Z.getO alts with
+ | S (s,g) -> Z.setO (L (s,g)) alts
+ | D (g) -> Z.setO (L (g,Obj.magic g)) alts
+ (* L (and other marks) for OR should have no arguments *)
+ | L _ -> assert false
+ in
+ match Z.right alts with
+ | None ->
+ let upalts = Z.upO alts in
+ let upalts = Z.inject T.Nil upalts in
+ let upalts =
+ match Z.getA upalts with
+ | s,S (a,b) -> Z.setA s (L (a,b)) upalts
+ | _,L _ -> assert false
+ | s,D (a) -> Z.setA s (L (a,Obj.magic a)) upalts
+ in
+ backtrack upalts
+ | Some pos ->
+ match Z.downO pos with
+ | Z.Solution (s,_,_) ->
+ move_solution_up ~unfocus:false true s pos cache
+ | Z.Todo pos -> next ~unfocus:true pos cache
+
+ and backtrack (pos : 'a and_pos) : 'a auto_result =
+(* prerr_endline "backtrack"; show pos; *)
+ let pos = Z.inject T.Nil pos in
+ let pos =
+ match Z.getA pos with
+ | s,D g | s, S (g,_) | s,L(g,_) -> Z.setA s (D g) pos
+ in
+ match first_left_mark_L_as_D is_aS pos with
+ | None -> global_choice_point pos
+ | Some pos ->
+ let rec local_choice_point pos =
+(* prerr_endline "local"; show pos; *)
+ match Z.downA pos with
+ | Z.Unexplored -> attack pos cache (Z.getA pos)
+ | Z.Alternatives alts ->
+ match leftmost_bro is_not_oL alts with
+ | None -> assert false (* is not L, thus has alternatives *)
+ | Some pos ->
+ let is_D = is_not_oS pos in
+ match if is_D then Z.downO pos else Z.downOr pos with
+ | Z.Solution (s,_,_) -> assert(is_D);
+ move_solution_up ~unfocus:false true s pos cache
+ | Z.Todo pos when is_D -> attack pos cache (Z.getA pos)
+ | Z.Todo pos ->
+ match first_left_mark_L_as_D is_aS pos with
+ | Some pos -> local_choice_point pos
+ | None -> assert false
+ in
+ local_choice_point pos
+ in
+ backtrack pos
+
+ and next_choice (pos : 'a and_pos) cache =
+ next_choice_point pos cache
+
+ and move_solution_up
+ ~unfocus are_sons_L
+ (status : #tac_status as 'a) (pos : 'a or_pos) cache
+ =
+ let pos = (* mark as solved *)
+ match Z.getO pos with
+ | L _ -> assert false (* XXX *)
+ | S (g,_)
+ | D g ->
+ if are_sons_L then
+ Z.inject T.Nil (Z.setO (L (g,status)) pos)
+ else
+ Z.setO (S (g,status)) pos
+ in
+ let has_alternative_or = match Z.right pos with None -> false | _ -> true in
+ let pos = Z.upO pos in
+ let are_all_lbro_L = forall_left is_aL pos in
+ let has_no_alternative =
+ ((not has_alternative_or) && are_sons_L) ||
+ is_closed pos
+ in
+ match Z.getA pos with
+ | _, L _ -> assert false
+ | (_, size, depth), S (g,_)
+ (* S if already solved and then solved again because of a backtrack *)
+ | (_, size, depth), D g ->
+ let newg =
+ if has_no_alternative then (L (g,status)) else (S (g,status))in
+ (* TODO: cache success g *)
+ let pos = if has_no_alternative then Z.inject T.Nil pos else pos in
+ let status = if unfocus then NTactics.unfocus_tac status else status
+ in
+ let news = status,size,depth in
+ let pos = Z.setA news newg pos in
+ match Z.right pos with
+ | Some pos -> next ~unfocus:true pos cache
+ | None ->
+ match Z.upA pos with
+ | None -> Proved (status, Some (pos,cache))
+ | Some pos ->
+ move_solution_up
+ ~unfocus:true (has_no_alternative && are_all_lbro_L)
+ status pos cache
+
+ and attack pos cache and_item =
+ (* show pos; uncomment to show the tree *)
+ match and_item with
+ | _, S _ -> assert false (* next would close the proof or give a D *)
+ | _, L _ -> assert false (* L is a final solution *)
+ | (_, depth, _),_ when Unix.gettimeofday () > flags.timeout ->
+ debug_print ~depth (lazy ("fail timeout"));