src/HOL/Import/proof_kernel.ML
changeset 17657 2f5f595eb618
parent 17652 b1ef33ebfa17
child 17894 f2fdd22accaa
equal deleted inserted replaced
17656:a8b83a82c4c6 17657:2f5f595eb618
    51 	   | PContr of proof * term
    51 	   | PContr of proof * term
    52 
    52 
    53     exception PK of string * string
    53     exception PK of string * string
    54 
    54 
    55     val get_proof_dir: string -> theory -> string option
    55     val get_proof_dir: string -> theory -> string option
       
    56     val disambiguate_frees : Thm.thm -> Thm.thm
    56     val debug : bool ref
    57     val debug : bool ref
    57     val disk_info_of : proof -> (string * string) option
    58     val disk_info_of : proof -> (string * string) option
    58     val set_disk_info_of : proof -> string -> string -> unit
    59     val set_disk_info_of : proof -> string -> string -> unit
    59     val mk_proof : proof_content -> proof
    60     val mk_proof : proof_content -> proof
    60     val content_of : proof -> proof_content
    61     val content_of : proof -> proof_content
  1117 
  1118 
  1118 fun norm_hthm sg (hth as HOLThm _) = hth
  1119 fun norm_hthm sg (hth as HOLThm _) = hth
  1119 
  1120 
  1120 (* End of disambiguating code *)
  1121 (* End of disambiguating code *)
  1121 
  1122 
       
  1123 fun disambiguate_frees thm =
       
  1124     let
       
  1125       fun ERR s = error ("Drule.disambiguate_frees: "^s)
       
  1126       val ct = cprop_of thm
       
  1127       val t = term_of ct
       
  1128       val thy = theory_of_cterm ct
       
  1129       val frees = term_frees t
       
  1130       val freenames = add_term_free_names (t, [])
       
  1131       fun is_old_name n = n mem_string freenames
       
  1132       fun name_of (Free (n, _)) = n
       
  1133         | name_of _ = ERR "name_of"
       
  1134       fun new_name' bump map n =
       
  1135           let val n' = n^bump in
       
  1136             if is_old_name n' orelse Symtab.lookup map n' <> NONE then 
       
  1137               new_name' (Symbol.bump_string bump) map n
       
  1138             else
       
  1139               n'
       
  1140           end              
       
  1141       val new_name = new_name' "a"
       
  1142       fun replace_name n' (Free (n, t)) = Free (n', t)
       
  1143         | replace_name n' _ = ERR "replace_name"
       
  1144       (* map: old oder fresh name -> old free, 
       
  1145          invmap: old free which has fresh name assigned to it -> fresh name *)
       
  1146       fun dis (v, mapping as (map,invmap)) =
       
  1147           let val n = name_of v in
       
  1148             case Symtab.lookup map n of
       
  1149               NONE => (Symtab.update (n, v) map, invmap)
       
  1150             | SOME v' => 
       
  1151               if v=v' then 
       
  1152                 mapping 
       
  1153               else
       
  1154                 let val n' = new_name map n in
       
  1155                   (Symtab.update (n', v) map, 
       
  1156                    Termtab.update (v, n') invmap)
       
  1157                 end
       
  1158           end
       
  1159     in
       
  1160       if (length freenames = length frees) then
       
  1161         thm
       
  1162       else
       
  1163         let 
       
  1164           val (_, invmap) = 
       
  1165               List.foldl dis (Symtab.empty, Termtab.empty) frees 
       
  1166           fun make_subst ((oldfree, newname), (intros, elims)) =
       
  1167               (cterm_of thy oldfree :: intros, 
       
  1168                cterm_of thy (replace_name newname oldfree) :: elims)
       
  1169           val (intros, elims) = List.foldl make_subst ([], []) (Termtab.dest invmap)
       
  1170         in 
       
  1171           forall_elim_list elims (forall_intr_list intros thm)
       
  1172         end     
       
  1173     end
       
  1174 
  1122 val debug = ref false
  1175 val debug = ref false
  1123 
  1176 
  1124 fun if_debug f x = if !debug then f x else ()
  1177 fun if_debug f x = if !debug then f x else ()
  1125 val message = if_debug writeln
  1178 val message = if_debug writeln
  1126 
  1179 
  1302 	val sg = sign_of thy
  1355 	val sg = sign_of thy
  1303 	val (hth' as HOLThm (args as (_,th))) = norm_hthm sg hth
  1356 	val (hth' as HOLThm (args as (_,th))) = norm_hthm sg hth
  1304 	val rew = rewrite_hol4_term (concl_of th) thy
  1357 	val rew = rewrite_hol4_term (concl_of th) thy
  1305 	val th  = equal_elim rew th
  1358 	val th  = equal_elim rew th
  1306 	val thy' = add_hol4_pending thyname thmname args thy
  1359 	val thy' = add_hol4_pending thyname thmname args thy
  1307         val th = Drule.disambiguate_frees th
  1360         val th = disambiguate_frees th
  1308 	val thy2 = if gen_output
  1361 	val thy2 = if gen_output
  1309 		   then add_dump ("lemma " ^ (quotename thmname) ^ ": " ^ 
  1362 		   then add_dump ("lemma " ^ (quotename thmname) ^ ": " ^ 
  1310                                   (smart_string_of_thm th) ^ "\n  by (import " ^ 
  1363                                   (smart_string_of_thm th) ^ "\n  by (import " ^ 
  1311                                   thyname ^ " " ^ (quotename thmname) ^ ")") thy'
  1364                                   thyname ^ " " ^ (quotename thmname) ^ ")") thy'
  1312 		   else thy'
  1365 		   else thy'