src/HOL/Import/proof_kernel.ML
changeset 46803 f8875c15cbe1
parent 46800 9696218b02fe
equal deleted inserted replaced
46802:13a3657d0dac 46803:f8875c15cbe1
   114     val protect_factname : string -> string
   114     val protect_factname : string -> string
   115     val replay_protect_varname : string -> string -> unit
   115     val replay_protect_varname : string -> string -> unit
   116     val replay_add_dump : string -> theory -> theory
   116     val replay_add_dump : string -> theory -> theory
   117 end
   117 end
   118 
   118 
   119 structure ProofKernel :> ProofKernel =
   119 structure ProofKernel : ProofKernel =
   120 struct
   120 struct
   121 type hol_type = Term.typ
   121 type hol_type = Term.typ
   122 type term = Term.term
   122 type term = Term.term
   123 datatype tag = Tag of string list
   123 datatype tag = Tag of string list
   124 type ('a,'b) subst = ('a * 'b) list
   124 type ('a,'b) subst = ('a * 'b) list
   241 
   241 
   242 fun prth th = writeln (Print_Mode.setmp [] Display.string_of_thm_without_context th);
   242 fun prth th = writeln (Print_Mode.setmp [] Display.string_of_thm_without_context th);
   243 val topctxt = ML_Context.the_local_context ();
   243 val topctxt = ML_Context.the_local_context ();
   244 fun prin t = writeln (Print_Mode.setmp []
   244 fun prin t = writeln (Print_Mode.setmp []
   245   (fn () => Syntax.string_of_term topctxt t) ());
   245   (fn () => Syntax.string_of_term topctxt t) ());
   246 fun pth (HOLThm(ren,thm)) =
   246 fun pth (HOLThm(_,thm)) =
   247     let
   247     let
   248         (*val _ = writeln "Renaming:"
   248         (*val _ = writeln "Renaming:"
   249         val _ = app (fn(v,w) => (prin v; writeln " -->"; prin w)) ren*)
   249         val _ = app (fn(v,w) => (prin v; writeln " -->"; prin w)) ren*)
   250         val _ = prth thm
   250         val _ = prth thm
   251     in
   251     in
   347 val mk_var = Free
   347 val mk_var = Free
   348 
   348 
   349 fun mk_thy_const thy Thy Nam Ty = Const(intern_const_name Thy Nam thy,Ty)
   349 fun mk_thy_const thy Thy Nam Ty = Const(intern_const_name Thy Nam thy,Ty)
   350 
   350 
   351 local
   351 local
   352   fun get_const sg thyname name =
   352   fun get_const sg _ name =
   353     (case Sign.const_type sg name of
   353     (case Sign.const_type sg name of
   354       SOME ty => Const (name, ty)
   354       SOME ty => Const (name, ty)
   355     | NONE => raise ERR "get_type" (name ^ ": No such constant"))
   355     | NONE => raise ERR "get_type" (name ^ ": No such constant"))
   356 in
   356 in
   357 fun prim_mk_const thy Thy Nam =
   357 fun prim_mk_const thy Thy Nam =
   428 
   428 
   429 fun replacestr x y s =
   429 fun replacestr x y s =
   430 let
   430 let
   431   val xl = raw_explode x
   431   val xl = raw_explode x
   432   val yl = raw_explode y
   432   val yl = raw_explode y
   433   fun isprefix [] ys = true
   433   fun isprefix [] _ = true
   434     | isprefix (x::xs) (y::ys) = if x = y then isprefix xs ys else false
   434     | isprefix (x::xs) (y::ys) = if x = y then isprefix xs ys else false
   435     | isprefix _ _ = false
   435     | isprefix _ _ = false
   436   fun isp s = isprefix xl s
   436   fun isp s = isprefix xl s
   437   fun chg s = yl@(List.drop (s, List.length xl))
   437   fun chg s = yl@(List.drop (s, List.length xl))
   438   fun r [] = []
   438   fun r [] = []
   495           | gtfx _ = raise ERR "get_type" "Bad type"
   495           | gtfx _ = raise ERR "get_type" "Bad type"
   496     in
   496     in
   497         gtfx
   497         gtfx
   498     end
   498     end
   499 
   499 
   500 fun input_types thyname (Elem("tylist",[("i",i)],xtys)) =
   500 fun input_types _ (Elem("tylist",[("i",i)],xtys)) =
   501     let
   501     let
   502         val types = Array.array(the (Int.fromString i),XMLty (Elem("",[],[])))
   502         val types = Array.array(the (Int.fromString i),XMLty (Elem("",[],[])))
   503         fun IT _ [] = ()
   503         fun IT _ [] = ()
   504           | IT n (xty::xtys) =
   504           | IT n (xty::xtys) =
   505             (Array.update(types,n,XMLty xty);
   505             (Array.update(types,n,XMLty xty);
   526                            tm
   526                            tm
   527                        end)
   527                        end)
   528       | NONE => raise ERR "get_term_from_index" "Bad index"
   528       | NONE => raise ERR "get_term_from_index" "Bad index"
   529 and get_term_from_xml thy thyname types terms =
   529 and get_term_from_xml thy thyname types terms =
   530     let
   530     let
   531         fun get_type [] = NONE
       
   532           | get_type [ty] = SOME (TypeNet.get_type_from_xml thy thyname types ty)
       
   533           | get_type _ = raise ERR "get_term" "Bad type"
       
   534 
       
   535         fun gtfx (Elem("tmv",[("n",name),("t",tyi)],[])) =
   531         fun gtfx (Elem("tmv",[("n",name),("t",tyi)],[])) =
   536             mk_var(protect_varname name,TypeNet.get_type_from_index thy thyname types tyi)
   532             mk_var(protect_varname name,TypeNet.get_type_from_index thy thyname types tyi)
   537           | gtfx (Elem("tmc",atts,[])) =
   533           | gtfx (Elem("tmc",atts,[])) =
   538             let
   534             let
   539                 val segment = get_segment thyname atts
   535                 val segment = get_segment thyname atts
   562             raise ERR "get_term" ("Not a term: "^tag)
   558             raise ERR "get_term" ("Not a term: "^tag)
   563     in
   559     in
   564         gtfx
   560         gtfx
   565     end
   561     end
   566 
   562 
   567 fun input_terms thyname types (Elem("tmlist",[("i",i)],xtms)) =
   563 fun input_terms _ _ (Elem("tmlist",[("i",i)],xtms)) =
   568     let
   564     let
   569         val terms = Array.array(the (Int.fromString i), XMLtm (Elem("",[],[])))
   565         val terms = Array.array(the (Int.fromString i), XMLtm (Elem("",[],[])))
   570 
   566 
   571         fun IT _ [] = ()
   567         fun IT _ [] = ()
   572           | IT n (xtm::xtms) =
   568           | IT n (xtm::xtms) =
   682             in
   678             in
   683                 mk_proof (PTyIntro(seg,protect_tyname name,protect_constname abs_name,protect_constname rep_name,P,t,x2p p))
   679                 mk_proof (PTyIntro(seg,protect_tyname name,protect_constname abs_name,protect_constname rep_name,P,t,x2p p))
   684             end
   680             end
   685           | x2p (Elem("ptydef",[("s",seg),("n",name)],[p])) =
   681           | x2p (Elem("ptydef",[("s",seg),("n",name)],[p])) =
   686             mk_proof (PTyDef(seg,protect_tyname name,x2p p))
   682             mk_proof (PTyDef(seg,protect_tyname name,x2p p))
   687           | x2p (xml as Elem("poracle",[],chldr)) =
   683           | x2p (Elem("poracle",[],chldr)) =
   688             let
   684             let
   689                 val (oracles,terms) = List.partition (fn (Elem("oracle",_,_)) => true | _ => false) chldr
   685                 val (oracles,terms) = List.partition (fn (Elem("oracle",_,_)) => true | _ => false) chldr
   690                 val ors = map (fn (Elem("oracle",[("n",name)],[])) => name | xml => raise ERR "x2p" "bad oracle") oracles
   686                 val ors = map (fn (Elem("oracle",[("n",name)],[])) => name | _ => raise ERR "x2p" "bad oracle") oracles
   691                 val (c,asl) = case terms of
   687                 val (c,asl) = case terms of
   692                                   [] => raise ERR "x2p" "Bad oracle description"
   688                                   [] => raise ERR "x2p" "Bad oracle description"
   693                                 | (hd::tl) => (hd,tl)
   689                                 | (hd::tl) => (hd,tl)
   694                 val tg = fold_rev (Tag.merge o Tag.read) ors Tag.empty_tag
   690                 val tg = fold_rev (Tag.merge o Tag.read) ors Tag.empty_tag
   695             in
   691             in
   844                 val p = x2p prf
   840                 val p = x2p prf
   845                 val t = index_to_term is
   841                 val t = index_to_term is
   846             in
   842             in
   847                 mk_proof (PContr (p,t))
   843                 mk_proof (PContr (p,t))
   848             end
   844             end
   849           | x2p xml = raise ERR "x2p" "Bad proof"
   845           | x2p _ = raise ERR "x2p" "Bad proof"
   850     in
   846     in
   851         x2p prf
   847         x2p prf
   852     end
   848     end
   853 
   849 
   854 fun import_proof_concl thyname thmname thy =
   850 fun import_proof_concl thyname thmname thy =
   856         val is = TextIO.openIn(proof_file_name thyname thmname thy)
   852         val is = TextIO.openIn(proof_file_name thyname thmname thy)
   857         val proof_xml = xml_to_import_xml (XML.parse (TextIO.inputAll is))
   853         val proof_xml = xml_to_import_xml (XML.parse (TextIO.inputAll is))
   858         val _ = TextIO.closeIn is
   854         val _ = TextIO.closeIn is
   859     in
   855     in
   860         case proof_xml of
   856         case proof_xml of
   861             Elem("proof",[],xtypes::xterms::prf::rest) =>
   857             Elem("proof",[],xtypes::xterms::_::rest) =>
   862             let
   858             let
   863                 val types = TypeNet.input_types thyname xtypes
   859                 val types = TypeNet.input_types thyname xtypes
   864                 val terms = TermNet.input_terms thyname types xterms
   860                 val terms = TermNet.input_terms thyname types xterms
   865                 fun f xtm thy = TermNet.get_term_from_xml thy thyname types terms xtm
   861                 fun f xtm thy = TermNet.get_term_from_xml thy thyname types terms xtm
   866             in
   862             in
   969         val th2 = beta_eta_thm (Thm.forall_intr cv th1)
   965         val th2 = beta_eta_thm (Thm.forall_intr cv th1)
   970         val th3 = th2 COMP (beta_eta_thm (Drule.instantiate' [SOME cvty] [SOME clc] gen_thm))
   966         val th3 = th2 COMP (beta_eta_thm (Drule.instantiate' [SOME cvty] [SOME clc] gen_thm))
   971         val c = prop_of th3
   967         val c = prop_of th3
   972         val vname = fst(dest_Free v)
   968         val vname = fst(dest_Free v)
   973         val (cold,cnew) = case c of
   969         val (cold,cnew) = case c of
   974                               tpc $ (Const(@{const_name All},allT) $ Abs(oldname,ty,body)) =>
   970                               tpc $ (Const(@{const_name All},_) $ Abs(oldname,_,_)) =>
   975                               (Abs(oldname,dummyT,Bound 0),Abs(vname,dummyT,Bound 0))
   971                               (Abs(oldname,dummyT,Bound 0),Abs(vname,dummyT,Bound 0))
   976                             | tpc $ (Const(@{const_name All},allT) $ rest) => (tpc,tpc)
   972                             | tpc $ (Const(@{const_name All},_) $ _) => (tpc,tpc)
   977                             | _ => raise ERR "mk_GEN" "Unknown conclusion"
   973                             | _ => raise ERR "mk_GEN" "Unknown conclusion"
   978         val th4 = Thm.rename_boundvars cold cnew th3
   974         val th4 = Thm.rename_boundvars cold cnew th3
   979         val res = implies_intr_hyps th4
   975         val res = implies_intr_hyps th4
   980     in
   976     in
   981         res
   977         res
  1002 
   998 
  1003 (* Code for disambiguating variablenames (wrt. types) *)
   999 (* Code for disambiguating variablenames (wrt. types) *)
  1004 
  1000 
  1005 val disamb_info_empty = {vars=[],rens=[]}
  1001 val disamb_info_empty = {vars=[],rens=[]}
  1006 
  1002 
  1007 fun rens_of {vars,rens} = rens
  1003 fun rens_of { vars = _, rens = rens } = rens
  1008 
  1004 
  1009 fun disamb_term_from info tm = (info, tm)
  1005 fun disamb_term_from info tm = (info, tm)
  1010 
  1006 
  1011 fun has_ren (HOLThm _) = false
  1007 fun has_ren (HOLThm _) = false
  1012 
  1008 
  1018 
  1014 
  1019 fun disamb_term tm   = disamb_term_from disamb_info_empty tm
  1015 fun disamb_term tm   = disamb_term_from disamb_info_empty tm
  1020 fun disamb_thm thm   = disamb_thm_from disamb_info_empty thm
  1016 fun disamb_thm thm   = disamb_thm_from disamb_info_empty thm
  1021 fun disamb_thms thms = disamb_thms_from disamb_info_empty thms
  1017 fun disamb_thms thms = disamb_thms_from disamb_info_empty thms
  1022 
  1018 
  1023 fun norm_hthm thy (hth as HOLThm _) = hth
  1019 fun norm_hthm _ (hth as HOLThm _) = hth
  1024 
  1020 
  1025 (* End of disambiguating code *)
  1021 (* End of disambiguating code *)
  1026 
  1022 
  1027 fun disambiguate_frees thm =
  1023 fun disambiguate_frees thm =
  1028     let
  1024     let
  1041               new_name' (Symbol.bump_string bump) map n
  1037               new_name' (Symbol.bump_string bump) map n
  1042             else
  1038             else
  1043               n'
  1039               n'
  1044           end
  1040           end
  1045       val new_name = new_name' "a"
  1041       val new_name = new_name' "a"
  1046       fun replace_name n' (Free (n, t)) = Free (n', t)
  1042       fun replace_name n' (Free (_, t)) = Free (n', t)
  1047         | replace_name _ _ = ERR "replace_name"
  1043         | replace_name _ _ = ERR "replace_name"
  1048       (* map: old or fresh name -> old free,
  1044       (* map: old or fresh name -> old free,
  1049          invmap: old free which has fresh name assigned to it -> fresh name *)
  1045          invmap: old free which has fresh name assigned to it -> fresh name *)
  1050       fun dis v (mapping as (map,invmap)) =
  1046       fun dis v (mapping as (map,invmap)) =
  1051           let val n = name_of v in
  1047           let val n = name_of v in
  1185 fun get_isabelle_thm_and_warn thyname thmname importerconc thy =
  1181 fun get_isabelle_thm_and_warn thyname thmname importerconc thy =
  1186   let
  1182   let
  1187     val (a, b) = get_isabelle_thm thyname thmname importerconc thy
  1183     val (a, b) = get_isabelle_thm thyname thmname importerconc thy
  1188     fun warn () =
  1184     fun warn () =
  1189         let
  1185         let
  1190             val (info,importerconc') = disamb_term importerconc
  1186             val (_,importerconc') = disamb_term importerconc
  1191             val i2h_conc = Thm.symmetric (rewrite_importer_term (HOLogic.mk_Trueprop importerconc') thy)
  1187             val i2h_conc = Thm.symmetric (rewrite_importer_term (HOLogic.mk_Trueprop importerconc') thy)
  1192         in
  1188         in
  1193             case concl_of i2h_conc of
  1189             case concl_of i2h_conc of
  1194                 Const("==",_) $ lhs $ _ =>
  1190                 Const("==",_) $ lhs $ _ =>
  1195                 (warning ("Failed lookup of theorem '"^thmname^"':");
  1191                 (warning ("Failed lookup of theorem '"^thmname^"':");
  1209     case get_importer_thm thyname thmname thy of
  1205     case get_importer_thm thyname thmname thy of
  1210         SOME hth => (thy,SOME hth)
  1206         SOME hth => (thy,SOME hth)
  1211       | NONE => ((case import_proof_concl thyname thmname thy of
  1207       | NONE => ((case import_proof_concl thyname thmname thy of
  1212                       SOME f => get_isabelle_thm_and_warn thyname thmname (f thy) thy
  1208                       SOME f => get_isabelle_thm_and_warn thyname thmname (f thy) thy
  1213                     | NONE => (message "No conclusion"; (thy,NONE)))
  1209                     | NONE => (message "No conclusion"; (thy,NONE)))
  1214                  handle e as IO.Io _ => (message "IO exception"; (thy,NONE))
  1210                  handle IO.Io _ => (message "IO exception"; (thy,NONE))
  1215                       | e as PK _ => (message "PK exception"; (thy,NONE)))
  1211                       | PK _ => (message "PK exception"; (thy,NONE)))
  1216 
  1212 
  1217 fun rename_const thyname thy name =
  1213 fun rename_const thyname thy name =
  1218     case get_importer_const_renaming thyname name thy of
  1214     case get_importer_const_renaming thyname name thy of
  1219         SOME cname => cname
  1215         SOME cname => cname
  1220       | NONE => name
  1216       | NONE => name
  1282         val _ = if_debug pth res
  1278         val _ = if_debug pth res
  1283     in
  1279     in
  1284         (thy,res)
  1280         (thy,res)
  1285     end
  1281     end
  1286 
  1282 
  1287 fun INST_TYPE lambda (hth as HOLThm(rens,th)) thy =
  1283 fun INST_TYPE lambda (hth as HOLThm(_,th)) thy =
  1288     let
  1284     let
  1289         val _ = message "INST_TYPE:"
  1285         val _ = message "INST_TYPE:"
  1290         val _ = if_debug pth hth
  1286         val _ = if_debug pth hth
  1291         val tys_before = Misc_Legacy.add_term_tfrees (prop_of th,[])
  1287         val tys_before = Misc_Legacy.add_term_tfrees (prop_of th,[])
  1292         val th1 = Thm.varifyT_global th
  1288         val th1 = Thm.varifyT_global th
  1697         val _ = if_debug pth hth
  1693         val _ = if_debug pth hth
  1698         val (info,th) = disamb_thm hth
  1694         val (info,th) = disamb_thm hth
  1699         val (info',vlist') = disamb_terms_from info vlist
  1695         val (info',vlist') = disamb_terms_from info vlist
  1700         val th1 =
  1696         val th1 =
  1701             case copt of
  1697             case copt of
  1702                 SOME (c as Const(cname,cty)) =>
  1698                 SOME (Const(cname,cty)) =>
  1703                 let
  1699                 let
  1704                     fun inst_type ty1 ty2 (TVar _) = raise ERR "GEN_ABS" "Type variable found!"
  1700                     fun inst_type ty1 ty2 (TVar _) = raise ERR "GEN_ABS" "Type variable found!"
  1705                       | inst_type ty1 ty2 (ty as TFree _) = if ty1 = ty
  1701                       | inst_type ty1 ty2 (ty as TFree _) = if ty1 = ty
  1706                                                             then ty2
  1702                                                             then ty2
  1707                                                             else ty
  1703                                                             else ty
  1786     let
  1782     let
  1787         val constname = rename_const thyname thy constname
  1783         val constname = rename_const thyname thy constname
  1788         val redeclared = is_some (Sign.const_type thy (Sign.intern_const thy constname));
  1784         val redeclared = is_some (Sign.const_type thy (Sign.intern_const thy constname));
  1789         val _ = warning ("Introducing constant " ^ constname)
  1785         val _ = warning ("Introducing constant " ^ constname)
  1790         val (thmname,thy) = get_defname thyname constname thy
  1786         val (thmname,thy) = get_defname thyname constname thy
  1791         val (info,rhs') = disamb_term rhs
  1787         val (_,rhs') = disamb_term rhs
  1792         val ctype = type_of rhs'
  1788         val ctype = type_of rhs'
  1793         val csyn = mk_syn thy constname
  1789         val csyn = mk_syn thy constname
  1794         val thy1 = case Importer_DefThy.get thy of
  1790         val thy1 = case Importer_DefThy.get thy of
  1795                        Replaying _ => thy
  1791                        Replaying _ => thy
  1796                      | _ => Sign.add_consts_i [(Binding.name constname,ctype,csyn)] thy
  1792                      | _ => Sign.add_consts_i [(Binding.name constname,ctype,csyn)] thy
  1857                          | _ =>
  1853                          | _ =>
  1858                            let
  1854                            let
  1859                                fun dest_eta_abs (Abs(x,xT,body)) = (x,xT,body)
  1855                                fun dest_eta_abs (Abs(x,xT,body)) = (x,xT,body)
  1860                                  | dest_eta_abs body =
  1856                                  | dest_eta_abs body =
  1861                                    let
  1857                                    let
  1862                                        val (dT,rT) = Term.dest_funT (type_of body)
  1858                                        val (dT,_) = Term.dest_funT (type_of body)
  1863                                    in
  1859                                    in
  1864                                        ("x",dT,body $ Bound 0)
  1860                                        ("x",dT,body $ Bound 0)
  1865                                    end
  1861                                    end
  1866                                    handle TYPE _ => raise ERR "new_specification" "not an abstraction type"
  1862                                    handle TYPE _ => raise ERR "new_specification" "not an abstraction type"
  1867                                fun dest_exists (Const(@{const_name Ex},_) $ abody) =
  1863                                fun dest_exists (Const(@{const_name Ex},_) $ abody) =
  1868                                    dest_eta_abs abody
  1864                                    dest_eta_abs abody
  1869                                  | dest_exists tm =
  1865                                  | dest_exists _ =
  1870                                    raise ERR "new_specification" "Bad existential formula"
  1866                                    raise ERR "new_specification" "Bad existential formula"
  1871 
  1867 
  1872                                val (consts,_) = Library.foldl (fn ((cs,ex),cname) =>
  1868                                val (consts,_) = Library.foldl (fn ((cs,ex),cname) =>
  1873                                                           let
  1869                                                           let
  1874                                                               val (_,cT,p) = dest_exists ex
  1870                                                               val (_,cT,p) = dest_exists ex
  1918             val _ = if_debug pth hth
  1914             val _ = if_debug pth hth
  1919         in
  1915         in
  1920             intern_store_thm false thyname thmname hth thy''
  1916             intern_store_thm false thyname thmname hth thy''
  1921         end
  1917         end
  1922 
  1918 
  1923 fun new_axiom name tm thy = raise ERR "new_axiom" ("Oh, no you don't! (" ^ name ^ ")")
  1919 fun new_axiom name _ _ = raise ERR "new_axiom" ("Oh, no you don't! (" ^ name ^ ")")
  1924 
  1920 
  1925 fun to_isa_thm (hth as HOLThm(_,th)) =
  1921 fun to_isa_thm (hth as HOLThm(_,th)) =
  1926     let
  1922     let
  1927         val (HOLThm args) = norm_hthm (theory_of_thm th) hth
  1923         val (HOLThm args) = norm_hthm (theory_of_thm th) hth
  1928     in
  1924     in