+
+let rec pp_proofterm name t context =
+ let rec skip_lambda tys ctx = function
+ | Cic.Lambda (n,s,t) -> skip_lambda (s::tys) ((Some n)::ctx) t
+ | t -> ctx,tys,t
+ in
+ let rename s name =
+ match name with
+ | Cic.Name s1 -> Cic.Name (s ^ s1)
+ | _ -> assert false
+ in
+ let rec skip_letin ctx = function
+ | Cic.LetIn (n,b,t) ->
+ pp_proofterm (Some (rename "Lemma " n)) b ctx::
+ skip_letin ((Some n)::ctx) t
+ | t ->
+ let ppterm t = CicPp.pp t ctx in
+ let rec pp inner = function
+ | Cic.Appl [Cic.Const (uri,[]);_;l;m;r;p1;p2]
+ when Pcre.pmatch ~pat:"trans_eq" (UriManager.string_of_uri uri)->
+ if not inner then
+ (" " ^ ppterm l) :: pp true p1 @
+ [ " = " ^ ppterm m ] @ pp true p2 @
+ [ " = " ^ ppterm r ]
+ else
+ pp true p1 @
+ [ " = " ^ ppterm m ] @ pp true p2
+ | Cic.Appl [Cic.Const (uri,[]);_;l;m;p]
+ when Pcre.pmatch ~pat:"sym_eq" (UriManager.string_of_uri uri)->
+ pp true p
+ | Cic.Appl [Cic.Const (uri,[]);_;_;_;_;_;p]
+ when Pcre.pmatch ~pat:"eq_f" (UriManager.string_of_uri uri)->
+ pp true p
+ | Cic.Appl [Cic.Const (uri,[]);_;_;_;_;_;p]
+ when Pcre.pmatch ~pat:"eq_OF_eq" (UriManager.string_of_uri uri)->
+ pp true p
+ | Cic.Appl [Cic.MutConstruct (uri,_,_,[]);_;_;t;p]
+ when Pcre.pmatch ~pat:"ex.ind" (UriManager.string_of_uri uri)->
+ [ "witness " ^ ppterm t ] @ pp true p
+ | Cic.Appl (t::_) ->[ " [by " ^ ppterm t ^ "]"]
+ | t ->[ " [by " ^ ppterm t ^ "]"]
+ in
+ let rec compat = function
+ | a::b::tl -> (b ^ a) :: compat tl
+ | h::[] -> [h]
+ | [] -> []
+ in
+ let compat l = List.hd l :: compat (List.tl l) in
+ compat (pp false t) @ ["";""]
+ in
+ let names, tys, body = skip_lambda [] context t in
+ let ppname name = (match name with Some (Cic.Name s) -> s | _ -> "") in
+ ppname name ^ ":\n" ^
+ (if context = [] then
+ let rec pp_l ctx = function
+ | (t,name)::tl ->
+ " " ^ ppname name ^ ": " ^ CicPp.pp t ctx ^ "\n" ^
+ pp_l (name::ctx) tl
+ | [] -> "\n\n"
+ in
+ pp_l [] (List.rev (List.combine tys names))
+ else "")
+ ^
+ String.concat "\n" (skip_letin names body)
+;;
+
+let pp_proofterm t =
+ "\n\n" ^
+ pp_proofterm (Some (Cic.Name "Hypothesis")) t []
+;;
+
+let initial_nameset_list = [
+ "x"; "y"; "z"; "t"; "u"; "v"; "a"; "b"; "c"; "d";
+ "e"; "l"; "m"; "n"; "o"; "p"; "q"; "r";
+]
+
+module S = Set.Make(String)
+
+let initial_nameset = List.fold_right S.add initial_nameset_list S.empty, [];;
+
+let freshname (nameset, subst) term =
+ let m = CicUtil.metas_of_term term in
+ let nameset, subst =
+ List.fold_left
+ (fun (set,rc) (m,_) ->
+ if List.mem_assoc m rc then set,rc else
+ let name = S.choose set in
+ let set = S.remove name set in
+ set,
+ (m,Cic.Const(UriManager.uri_of_string
+ ("cic:/"^name^".con"),[]))::rc)
+ (nameset,subst) m
+ in
+ let term =
+ ProofEngineReduction.replace
+ ~equality:(fun i t -> match t with Cic.Meta (j,_) -> i=j| _ -> false)
+ ~what:(List.map fst subst)
+ ~with_what:(List.map snd subst) ~where:term
+ in
+ (nameset, subst), term
+;;
+
+let remove_names_in_context (set,subst) names =
+ List.fold_left
+ (fun s n ->
+ match n with Some (Cic.Name n) -> S.remove n s | _ -> s)
+ set names, subst
+;;
+
+let string_of_id2 (id_to_eq,_) names nameset id =
+ if id = 0 then "" else
+ try
+ let (_,_,(_,l,r,_),_,_) = open_equality (Hashtbl.find id_to_eq id) in
+ let nameset, l = freshname nameset l in
+ let nameset, r = freshname nameset r in
+ Printf.sprintf "%s = %s" (CicPp.pp l names) (CicPp.pp r names)
+ with
+ Not_found -> assert false
+;;
+
+let draw_proof bag names goal_proof proof id =
+ let b = Buffer.create 100 in
+ let fmt = Format.formatter_of_buffer b in
+ let sint = string_of_int in
+ let fst3 (x,_,_) = x in
+ let visited = ref [] in
+ let nameset = remove_names_in_context initial_nameset names in
+ let rec fact id = function
+ | Exact t ->
+ if not (List.mem id !visited) then
+ begin
+ visited := id :: !visited;
+ let nameset, t = freshname nameset t in
+ let t = CicPp.pp t names in
+ GraphvizPp.Dot.node (sint id)
+ ~attrs:["label",t^":"^string_of_id2 bag names nameset id;
+ "shape","rectangle"] fmt;
+ end
+ | Step (_,(_,id1,(_,id2),_)) ->
+ GraphvizPp.Dot.edge (sint id) (sint id1) fmt;
+ GraphvizPp.Dot.edge (sint id) (sint id2) fmt;
+ let p1,_,_ = proof_of_id bag id1 in
+ let p2,_,_ = proof_of_id bag id2 in
+ fact id1 p1;
+ fact id2 p2;
+ if not (List.mem id !visited); then
+ begin
+ visited := id :: !visited;
+ GraphvizPp.Dot.node (sint id)
+ ~attrs:["label",sint id^":"^string_of_id2 bag names nameset id;
+ "shape","ellipse"] fmt
+ end
+ in
+ let sleft acc (_,_,id,_,_) =
+ if acc != 0 then GraphvizPp.Dot.edge (sint acc) (sint id) fmt;
+ fact id (fst3 (proof_of_id bag id));
+ id
+ in
+ GraphvizPp.Dot.header ~node_attrs:["fontsize","10"; ] fmt;
+ ignore(List.fold_left sleft id goal_proof);
+ GraphvizPp.Dot.trailer fmt;
+ let oc = open_out "/tmp/matita_paramod.dot" in
+ Buffer.output_buffer oc b;
+ close_out oc;
+ Utils.debug_print (lazy "dot!");
+ ignore(Unix.system
+ "dot -Tps -o /tmp/matita_paramod.eps /tmp/matita_paramod.dot"
+(* "cat /tmp/matita_paramod.dot| tred | dot -Tps -o /tmp/matita_paramod.eps" *)
+ );
+ ignore(Unix.system "gv /tmp/matita_paramod.eps");
+;;
+