src/Pure/pure_thy.ML
changeset 39557 fe5722fce758
parent 39507 839873937ddd
child 42048 afd11ca8e018
equal deleted inserted replaced
39556:32a00ff29d1a 39557:fe5722fce758
     1 (*  Title:      Pure/pure_thy.ML
     1 (*  Title:      Pure/pure_thy.ML
     2     Author:     Markus Wenzel, TU Muenchen
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     3 
     4 Theorem storage.  Pure theory syntax and logical content.
     4 Pure theory syntax and further logical content.
     5 *)
     5 *)
     6 
     6 
     7 signature PURE_THY =
     7 signature PURE_THY =
     8 sig
     8 sig
     9   val facts_of: theory -> Facts.T
       
    10   val intern_fact: theory -> xstring -> string
       
    11   val defined_fact: theory -> string -> bool
       
    12   val hide_fact: bool -> string -> theory -> theory
       
    13   val join_proofs: theory -> unit
       
    14   val get_fact: Context.generic -> theory -> Facts.ref -> thm list
       
    15   val get_thms: theory -> xstring -> thm list
       
    16   val get_thm: theory -> xstring -> thm
       
    17   val all_thms_of: theory -> (string * thm) list
       
    18   val map_facts: ('a -> 'b) -> ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
       
    19   val burrow_fact: ('a list -> 'b list) -> ('a list * 'c) list -> ('b list * 'c) list
       
    20   val burrow_facts: ('a list -> 'b list) ->
       
    21     ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
       
    22   val name_multi: string -> 'a list -> (string * 'a) list
       
    23   val name_thm: bool -> bool -> string -> thm -> thm
       
    24   val name_thms: bool -> bool -> string -> thm list -> thm list
       
    25   val name_thmss: bool -> string -> (thm list * 'a) list -> (thm list * 'a) list
       
    26   val store_thms: binding * thm list -> theory -> thm list * theory
       
    27   val store_thm: binding * thm -> theory -> thm * theory
       
    28   val store_thm_open: binding * thm -> theory -> thm * theory
       
    29   val add_thms: ((binding * thm) * attribute list) list -> theory -> thm list * theory
       
    30   val add_thm: (binding * thm) * attribute list -> theory -> thm * theory
       
    31   val add_thmss: ((binding * thm list) * attribute list) list -> theory -> thm list list * theory
       
    32   val add_thms_dynamic: binding * (Context.generic -> thm list) -> theory -> theory
       
    33   val note_thmss: string -> (Thm.binding * (thm list * attribute list) list) list
       
    34     -> theory -> (string * thm list) list * theory
       
    35   val add_defs: bool -> ((binding * term) * attribute list) list ->
       
    36     theory -> thm list * theory
       
    37   val add_defs_unchecked: bool -> ((binding * term) * attribute list) list ->
       
    38     theory -> thm list * theory
       
    39   val add_defs_cmd: bool -> ((binding * string) * attribute list) list ->
       
    40     theory -> thm list * theory
       
    41   val add_defs_unchecked_cmd: bool -> ((binding * string) * attribute list) list ->
       
    42     theory -> thm list * theory
       
    43   val old_appl_syntax: theory -> bool
     9   val old_appl_syntax: theory -> bool
    44   val old_appl_syntax_setup: theory -> theory
    10   val old_appl_syntax_setup: theory -> theory
    45 end;
    11 end;
    46 
    12 
    47 structure PureThy: PURE_THY =
    13 structure Pure_Thy: PURE_THY =
    48 struct
    14 struct
    49 
       
    50 
       
    51 (*** stored facts ***)
       
    52 
       
    53 (** theory data **)
       
    54 
       
    55 structure Global_Facts = Theory_Data
       
    56 (
       
    57   type T = Facts.T * thm list;
       
    58   val empty = (Facts.empty, []);
       
    59   fun extend (facts, _) = (facts, []);
       
    60   fun merge ((facts1, _), (facts2, _)) = (Facts.merge (facts1, facts2), []);
       
    61 );
       
    62 
       
    63 
       
    64 (* facts *)
       
    65 
       
    66 val facts_of = #1 o Global_Facts.get;
       
    67 
       
    68 val intern_fact = Facts.intern o facts_of;
       
    69 val defined_fact = Facts.defined o facts_of;
       
    70 
       
    71 fun hide_fact fully name = Global_Facts.map (apfst (Facts.hide fully name));
       
    72 
       
    73 
       
    74 (* proofs *)
       
    75 
       
    76 fun register_proofs (thy, thms) = (Global_Facts.map (apsnd (append thms)) thy, thms);
       
    77 
       
    78 fun join_proofs thy = Thm.join_proofs (rev (#2 (Global_Facts.get thy)));
       
    79 
       
    80 
       
    81 
       
    82 (** retrieve theorems **)
       
    83 
       
    84 fun get_fact context thy xthmref =
       
    85   let
       
    86     val xname = Facts.name_of_ref xthmref;
       
    87     val pos = Facts.pos_of_ref xthmref;
       
    88 
       
    89     val name = intern_fact thy xname;
       
    90     val res = Facts.lookup context (facts_of thy) name;
       
    91     val _ = Theory.check_thy thy;
       
    92   in
       
    93     (case res of
       
    94       NONE => error ("Unknown fact " ^ quote name ^ Position.str_of pos)
       
    95     | SOME (static, ths) =>
       
    96         (Position.report pos ((if static then Markup.fact else Markup.dynamic_fact) name);
       
    97          Facts.select xthmref (map (Thm.transfer thy) ths)))
       
    98   end;
       
    99 
       
   100 fun get_thms thy = get_fact (Context.Theory thy) thy o Facts.named;
       
   101 fun get_thm thy name = Facts.the_single name (get_thms thy name);
       
   102 
       
   103 fun all_thms_of thy =
       
   104   Facts.fold_static (fn (_, ths) => append (map (`(Thm.get_name_hint)) ths)) (facts_of thy) [];
       
   105 
       
   106 
       
   107 
       
   108 (** store theorems **)
       
   109 
       
   110 (* fact specifications *)
       
   111 
       
   112 fun map_facts f = map (apsnd (map (apfst (map f))));
       
   113 fun burrow_fact f = split_list #>> burrow f #> op ~~;
       
   114 fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
       
   115 
       
   116 
       
   117 (* naming *)
       
   118 
       
   119 fun name_multi name [x] = [(name, x)]
       
   120   | name_multi "" xs = map (pair "") xs
       
   121   | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
       
   122 
       
   123 fun name_thm pre official name thm = thm
       
   124   |> not (Thm.derivation_name thm <> "" andalso pre orelse not official) ? Thm.name_derivation name
       
   125   |> (if Thm.has_name_hint thm andalso pre orelse name = "" then I else Thm.put_name_hint name);
       
   126 
       
   127 fun name_thms pre official name xs =
       
   128   map (uncurry (name_thm pre official)) (name_multi name xs);
       
   129 
       
   130 fun name_thmss official name fact =
       
   131   burrow_fact (name_thms true official name) fact;
       
   132 
       
   133 
       
   134 (* enter_thms *)
       
   135 
       
   136 fun enter_thms pre_name post_name app_att (b, thms) thy =
       
   137   if Binding.is_empty b
       
   138   then swap (register_proofs (app_att (thy, thms)))
       
   139   else
       
   140     let
       
   141       val naming = Sign.naming_of thy;
       
   142       val name = Name_Space.full_name naming b;
       
   143       val (thy', thms') =
       
   144         register_proofs (apsnd (post_name name) (app_att (thy, pre_name name thms)));
       
   145       val thms'' = map (Thm.transfer thy') thms';
       
   146       val thy'' = thy' |> (Global_Facts.map o apfst) (Facts.add_global naming (b, thms'') #> snd);
       
   147     in (thms'', thy'') end;
       
   148 
       
   149 
       
   150 (* store_thm(s) *)
       
   151 
       
   152 fun store_thms (b, thms) =
       
   153   enter_thms (name_thms true true) (name_thms false true) I (b, thms);
       
   154 
       
   155 fun store_thm (b, th) = store_thms (b, [th]) #>> the_single;
       
   156 
       
   157 fun store_thm_open (b, th) =
       
   158   enter_thms (name_thms true false) (name_thms false false) I (b, [th]) #>> the_single;
       
   159 
       
   160 
       
   161 (* add_thms(s) *)
       
   162 
       
   163 fun add_thms_atts pre_name ((b, thms), atts) =
       
   164   enter_thms pre_name (name_thms false true)
       
   165     (Library.foldl_map (Thm.theory_attributes atts)) (b, thms);
       
   166 
       
   167 fun gen_add_thmss pre_name =
       
   168   fold_map (add_thms_atts pre_name);
       
   169 
       
   170 fun gen_add_thms pre_name args =
       
   171   apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
       
   172 
       
   173 val add_thmss = gen_add_thmss (name_thms true true);
       
   174 val add_thms = gen_add_thms (name_thms true true);
       
   175 val add_thm = yield_singleton add_thms;
       
   176 
       
   177 
       
   178 (* add_thms_dynamic *)
       
   179 
       
   180 fun add_thms_dynamic (b, f) thy = thy
       
   181   |> (Global_Facts.map o apfst)
       
   182       (Facts.add_dynamic (Sign.naming_of thy) (b, f) #> snd);
       
   183 
       
   184 
       
   185 (* note_thmss *)
       
   186 
       
   187 fun note_thmss kind = fold_map (fn ((b, more_atts), ths_atts) => fn thy =>
       
   188   let
       
   189     val pos = Binding.pos_of b;
       
   190     val name = Sign.full_name thy b;
       
   191     val _ = Position.report pos (Markup.fact_decl name);
       
   192 
       
   193     fun app (x, (ths, atts)) = Library.foldl_map (Thm.theory_attributes atts) (x, ths);
       
   194     val (thms, thy') = thy |> enter_thms
       
   195       (name_thmss true) (name_thms false true) (apsnd flat o Library.foldl_map app)
       
   196       (b, map (fn (ths, atts) => (ths, surround (Thm.kind kind) (atts @ more_atts))) ths_atts);
       
   197   in ((name, thms), thy') end);
       
   198 
       
   199 
       
   200 (* store axioms as theorems *)
       
   201 
       
   202 local
       
   203 
       
   204 fun no_read _ (_, t) = t;
       
   205 
       
   206 fun read thy (b, str) =
       
   207   Syntax.read_prop_global thy str handle ERROR msg =>
       
   208     cat_error msg ("The error(s) above occurred in definition " ^ quote (Binding.str_of b));
       
   209 
       
   210 fun add prep unchecked overloaded = fold_map (fn ((b, raw_prop), atts) => fn thy =>
       
   211   let
       
   212     val prop = prep thy (b, raw_prop);
       
   213     val ((_, def), thy') = Thm.add_def unchecked overloaded (b, prop) thy;
       
   214     val thm = def
       
   215       |> Thm.forall_intr_frees
       
   216       |> Thm.forall_elim_vars 0
       
   217       |> Thm.varifyT_global;
       
   218   in yield_singleton (gen_add_thms (K I)) ((b, thm), atts) thy' end);
       
   219 
       
   220 in
       
   221 
       
   222 val add_defs = add no_read false;
       
   223 val add_defs_unchecked = add no_read true;
       
   224 val add_defs_cmd = add read false;
       
   225 val add_defs_unchecked_cmd = add read true;
       
   226 
       
   227 end;
       
   228 
       
   229 
       
   230 
       
   231 (*** Pure theory syntax and logical content ***)
       
   232 
    15 
   233 val typ = Simple_Syntax.read_typ;
    16 val typ = Simple_Syntax.read_typ;
   234 val prop = Simple_Syntax.read_prop;
    17 val prop = Simple_Syntax.read_prop;
   235 
    18 
   236 val tycon = Syntax.mark_type;
    19 val tycon = Syntax.mark_type;
   364   #> Sign.local_path
   147   #> Sign.local_path
   365   #> Sign.add_consts_i
   148   #> Sign.add_consts_i
   366    [(Binding.name "term", typ "'a => prop", NoSyn),
   149    [(Binding.name "term", typ "'a => prop", NoSyn),
   367     (Binding.name "sort_constraint", typ "'a itself => prop", NoSyn),
   150     (Binding.name "sort_constraint", typ "'a itself => prop", NoSyn),
   368     (Binding.name "conjunction", typ "prop => prop => prop", NoSyn)]
   151     (Binding.name "conjunction", typ "prop => prop => prop", NoSyn)]
   369   #> (add_defs false o map Thm.no_attributes)
   152   #> (Global_Theory.add_defs false o map Thm.no_attributes)
   370    [(Binding.name "prop_def", prop "(CONST prop :: prop => prop) (A::prop) == A::prop"),
   153    [(Binding.name "prop_def", prop "(CONST prop :: prop => prop) (A::prop) == A::prop"),
   371     (Binding.name "term_def", prop "(CONST Pure.term :: 'a => prop) (x::'a) == (!!A::prop. A ==> A)"),
   154     (Binding.name "term_def", prop "(CONST Pure.term :: 'a => prop) (x::'a) == (!!A::prop. A ==> A)"),
   372     (Binding.name "sort_constraint_def",
   155     (Binding.name "sort_constraint_def",
   373       prop "(CONST Pure.sort_constraint :: 'a itself => prop) (CONST TYPE :: 'a itself) ==\
   156       prop "(CONST Pure.sort_constraint :: 'a itself => prop) (CONST TYPE :: 'a itself) ==\
   374       \ (CONST Pure.term :: 'a itself => prop) (CONST TYPE :: 'a itself)"),
   157       \ (CONST Pure.term :: 'a itself => prop) (CONST TYPE :: 'a itself)"),
   375     (Binding.name "conjunction_def", prop "(A &&& B) == (!!C::prop. (A ==> B ==> C) ==> C)")] #> snd
   158     (Binding.name "conjunction_def", prop "(A &&& B) == (!!C::prop. (A ==> B ==> C) ==> C)")] #> snd
   376   #> Sign.hide_const false "Pure.term"
   159   #> Sign.hide_const false "Pure.term"
   377   #> Sign.hide_const false "Pure.sort_constraint"
   160   #> Sign.hide_const false "Pure.sort_constraint"
   378   #> Sign.hide_const false "Pure.conjunction"
   161   #> Sign.hide_const false "Pure.conjunction"
   379   #> add_thmss [((Binding.name "nothing", []), [])] #> snd
   162   #> Global_Theory.add_thmss [((Binding.name "nothing", []), [])] #> snd
   380   #> fold (fn (a, prop) => snd o Thm.add_axiom (Binding.name a, prop)) Proofterm.equality_axms));
   163   #> fold (fn (a, prop) => snd o Thm.add_axiom (Binding.name a, prop)) Proofterm.equality_axms));
   381 
   164 
   382 end;
   165 end;