src/Pure/global_theory.ML
changeset 70430 6ec97dc6670e
parent 70428 4537e82019d3
child 70431 dbb32c2d5c2c
equal deleted inserted replaced
70429:7f7f149d6843 70430:6ec97dc6670e
    19   val map_facts: ('a -> 'b) -> ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
    19   val map_facts: ('a -> 'b) -> ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
    20   val burrow_fact: ('a list -> 'b list) -> ('a list * 'c) list -> ('b list * 'c) list
    20   val burrow_fact: ('a list -> 'b list) -> ('a list * 'c) list -> ('b list * 'c) list
    21   val burrow_facts: ('a list -> 'b list) ->
    21   val burrow_facts: ('a list -> 'b list) ->
    22     ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
    22     ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
    23   val name_multi: string -> 'a list -> (string * 'a) list
    23   val name_multi: string -> 'a list -> (string * 'a) list
    24   type name_flags = {pre: bool, official: bool}
    24   type name_flags
       
    25   val unnamed: name_flags
    25   val official1: name_flags
    26   val official1: name_flags
    26   val official2: name_flags
    27   val official2: name_flags
    27   val unofficial1: name_flags
    28   val unofficial1: name_flags
    28   val unofficial2: name_flags
    29   val unofficial2: name_flags
    29   val name_thm: name_flags -> string -> thm -> thm
    30   val name_thm: name_flags -> string -> thm -> thm
   108 fun map_facts f = map (apsnd (map (apfst (map f))));
   109 fun map_facts f = map (apsnd (map (apfst (map f))));
   109 fun burrow_fact f = split_list #>> burrow f #> op ~~;
   110 fun burrow_fact f = split_list #>> burrow f #> op ~~;
   110 fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
   111 fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
   111 
   112 
   112 
   113 
   113 (* naming *)
   114 (* name theorems *)
   114 
   115 
   115 type name_flags = {pre: bool, official: bool};
   116 abstype name_flags = No_Name_Flags | Name_Flags of {pre: bool, official: bool}
   116 
   117 with
   117 val official1 : name_flags = {pre = true, official = true};
   118 
   118 val official2 : name_flags = {pre = false, official = true};
   119 val unnamed = No_Name_Flags;
   119 val unofficial1 : name_flags = {pre = true, official = false};
   120 val official1 = Name_Flags {pre = true, official = true};
   120 val unofficial2 : name_flags = {pre = false, official = false};
   121 val official2 = Name_Flags {pre = false, official = true};
   121 
   122 val unofficial1 = Name_Flags {pre = true, official = false};
   122 fun name_thm {pre, official} name thm = thm
   123 val unofficial2 = Name_Flags {pre = false, official = false};
   123   |> (official andalso not (pre andalso Thm.derivation_name thm <> "")) ?
   124 
   124       Thm.name_derivation name
   125 fun name_thm No_Name_Flags _ thm = thm
   125   |> (name <> "" andalso not (pre andalso Thm.has_name_hint thm)) ?
   126   | name_thm (Name_Flags {pre, official}) name thm = thm
   126       Thm.put_name_hint name;
   127       |> (official andalso not (pre andalso Thm.derivation_name thm <> "")) ?
   127 
   128           Thm.name_derivation name
       
   129       |> (name <> "" andalso not (pre andalso Thm.has_name_hint thm)) ?
       
   130           Thm.put_name_hint name;
       
   131 
       
   132 end;
   128 
   133 
   129 fun name_multi name [x] = [(name, x)]
   134 fun name_multi name [x] = [(name, x)]
   130   | name_multi "" xs = map (pair "") xs
   135   | name_multi "" xs = map (pair "") xs
   131   | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
   136   | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
   132 
   137 
   181     in thy |> Thm.register_proofs thms' |> add_facts (b, thms') end;
   186     in thy |> Thm.register_proofs thms' |> add_facts (b, thms') end;
   182 
   187 
   183 val app_facts =
   188 val app_facts =
   184   apfst flat oo fold_map (fn (thms, atts) => fold_map (Thm.theory_attributes atts) thms);
   189   apfst flat oo fold_map (fn (thms, atts) => fold_map (Thm.theory_attributes atts) thms);
   185 
   190 
   186 fun apply_facts pre_name post_name (b, facts) thy =
   191 fun apply_facts name_flags1 name_flags2 (b, facts) thy =
   187   if Binding.is_empty b then app_facts facts thy |-> register_proofs
   192   if Binding.is_empty b then app_facts facts thy |-> register_proofs
   188   else
   193   else
   189     let
   194     let
   190       val name = Sign.full_name thy b;
   195       val name = Sign.full_name thy b;
   191       val (thms', thy') = thy
   196       val (thms', thy') = thy
   192         |> app_facts (map (apfst (pre_name name)) facts)
   197         |> app_facts (map (apfst (name_thms name_flags1 name)) facts)
   193         |>> post_name name |-> register_proofs;
   198         |>> name_thms name_flags2 name |-> register_proofs;
   194       val thy'' = thy' |> add_facts (b, Lazy.value thms');
   199       val thy'' = thy' |> add_facts (b, Lazy.value thms');
   195     in (map (Thm.transfer thy'') thms', thy'') end;
   200     in (map (Thm.transfer thy'') thms', thy'') end;
   196 
   201 
   197 
   202 
   198 (* store_thm *)
   203 (* store_thm *)
   199 
   204 
   200 fun store_thm (b, th) =
   205 fun store_thm (b, th) =
   201   apply_facts (name_thms official1) (name_thms official2) (b, [([th], [])]) #>> the_single;
   206   apply_facts official1 official2 (b, [([th], [])]) #>> the_single;
   202 
   207 
   203 fun store_thm_open (b, th) =
   208 fun store_thm_open (b, th) =
   204   apply_facts (name_thms unofficial1) (name_thms unofficial2) (b, [([th], [])]) #>> the_single;
   209   apply_facts unofficial1 unofficial2 (b, [([th], [])]) #>> the_single;
   205 
   210 
   206 
   211 
   207 (* add_thms(s) *)
   212 (* add_thms(s) *)
   208 
   213 
   209 val add_thmss =
   214 val add_thmss =
   210   fold_map (fn ((b, thms), atts) =>
   215   fold_map (fn ((b, thms), atts) => apply_facts official1 official2 (b, [(thms, atts)]));
   211     apply_facts (name_thms official1) (name_thms official2) (b, [(thms, atts)]));
       
   212 
   216 
   213 fun add_thms args =
   217 fun add_thms args =
   214   add_thmss (map (apfst (apsnd single)) args) #>> map the_single;
   218   add_thmss (map (apfst (apsnd single)) args) #>> map the_single;
   215 
   219 
   216 val add_thm = yield_singleton add_thms;
   220 val add_thm = yield_singleton add_thms;
   230 
   234 
   231 fun note_thms kind ((b, more_atts), facts) thy =
   235 fun note_thms kind ((b, more_atts), facts) thy =
   232   let
   236   let
   233     val name = Sign.full_name thy b;
   237     val name = Sign.full_name thy b;
   234     val facts' = facts |> map (apsnd (fn atts => surround (Thm.kind kind) (atts @ more_atts)));
   238     val facts' = facts |> map (apsnd (fn atts => surround (Thm.kind kind) (atts @ more_atts)));
   235     val (thms', thy') = thy |> apply_facts (name_thms official1) (name_thms official2) (b, facts');
   239     val (thms', thy') = thy |> apply_facts official1 official2 (b, facts');
   236   in ((name, thms'), thy') end;
   240   in ((name, thms'), thy') end;
   237 
   241 
   238 val note_thmss = fold_map o note_thms;
   242 val note_thmss = fold_map o note_thms;
   239 
   243 
   240 
   244 
   248     val ((_, def), thy') = Thm.add_def context unchecked overloaded (b, prop) thy;
   252     val ((_, def), thy') = Thm.add_def context unchecked overloaded (b, prop) thy;
   249     val thm = def
   253     val thm = def
   250       |> Thm.forall_intr_frees
   254       |> Thm.forall_intr_frees
   251       |> Thm.forall_elim_vars 0
   255       |> Thm.forall_elim_vars 0
   252       |> Thm.varifyT_global;
   256       |> Thm.varifyT_global;
   253   in
   257   in thy' |> apply_facts unnamed official2 (b, [([thm], atts)]) |>> the_single end);
   254     thy'
       
   255     |> apply_facts (K I) (name_thms official2) (b, [([thm], atts)])
       
   256     |>> the_single
       
   257   end);
       
   258 
   258 
   259 in
   259 in
   260 
   260 
   261 val add_defs = add false;
   261 val add_defs = add false;
   262 val add_defs_unchecked = add true;
   262 val add_defs_unchecked = add true;