src/Pure/pure_thy.ML
author wenzelm
Thu, 14 Aug 2008 16:52:46 +0200
changeset 27865 27a8ad9612a3
parent 27739 cd1df29db620
child 28076 b2374a203b1c
permissions -rw-r--r--
moved basic thm operations from structure PureThy to Thm (cf. more_thm.ML);
     1 (*  Title:      Pure/pure_thy.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Theorem storage.  Pure theory syntax and logical content.
     6 *)
     7 
     8 signature PURE_THY =
     9 sig
    10   val facts_of: theory -> Facts.T
    11   val intern_fact: theory -> xstring -> string
    12   val defined_fact: theory -> string -> bool
    13   val hide_fact: bool -> string -> theory -> theory
    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: bstring * thm list -> theory -> thm list * theory
    27   val store_thm: bstring * thm -> theory -> thm * theory
    28   val store_thm_open: bstring * thm -> theory -> thm * theory
    29   val add_thms: ((bstring * thm) * attribute list) list -> theory -> thm list * theory
    30   val add_thm: (bstring * thm) * attribute list -> theory -> thm * theory
    31   val add_thmss: ((bstring * thm list) * attribute list) list -> theory -> thm list list * theory
    32   val add_thms_dynamic: bstring * (Context.generic -> thm list) -> theory -> theory
    33   val note_thmss: string -> ((bstring * attribute list) *
    34     (thm list * attribute list) list) list -> theory -> (bstring * thm list) list * theory
    35   val note_thmss_grouped: string -> string -> ((bstring * attribute list) *
    36     (thm list * attribute list) list) list -> theory -> (bstring * thm list) list * theory
    37   val add_axioms: ((bstring * term) * attribute list) list -> theory -> thm list * theory
    38   val add_axioms_cmd: ((bstring * string) * attribute list) list -> theory -> thm list * theory
    39   val add_defs: bool -> ((bstring * term) * attribute list) list ->
    40     theory -> thm list * theory
    41   val add_defs_unchecked: bool -> ((bstring * term) * attribute list) list ->
    42     theory -> thm list * theory
    43   val add_defs_unchecked_cmd: bool -> ((bstring * string) * attribute list) list ->
    44     theory -> thm list * theory
    45   val add_defs_cmd: bool -> ((bstring * string) * attribute list) list ->
    46     theory -> thm list * theory
    47   val old_appl_syntax: theory -> bool
    48   val old_appl_syntax_setup: theory -> theory
    49 end;
    50 
    51 structure PureThy: PURE_THY =
    52 struct
    53 
    54 
    55 (*** stored facts ***)
    56 
    57 (** theory data **)
    58 
    59 structure FactsData = TheoryDataFun
    60 (
    61   type T = Facts.T;
    62   val empty = Facts.empty;
    63   val copy = I;
    64   val extend = I;
    65   fun merge _ = Facts.merge;
    66 );
    67 
    68 val facts_of = FactsData.get;
    69 
    70 val intern_fact = Facts.intern o facts_of;
    71 val defined_fact = Facts.defined o facts_of;
    72 
    73 fun hide_fact fully name = FactsData.map (Facts.hide fully name);
    74 
    75 
    76 
    77 (** retrieve theorems **)
    78 
    79 fun get_fact context thy xthmref =
    80   let
    81     val xname = Facts.name_of_ref xthmref;
    82     val pos = Facts.pos_of_ref xthmref;
    83 
    84     val name = intern_fact thy xname;
    85     val res = Facts.lookup context (facts_of thy) name;
    86     val _ = Theory.check_thy thy;
    87   in
    88     (case res of
    89       NONE => error ("Unknown fact " ^ quote name ^ Position.str_of pos)
    90     | SOME (static, ths) =>
    91         (Position.report ((if static then Markup.fact else Markup.dynamic_fact) name) pos;
    92          Facts.select xthmref (map (Thm.transfer thy) ths)))
    93   end;
    94 
    95 fun get_thms thy = get_fact (Context.Theory thy) thy o Facts.named;
    96 fun get_thm thy name = Facts.the_single name (get_thms thy name);
    97 
    98 fun all_thms_of thy =
    99   Facts.fold_static (fn (_, ths) => append (map (`(Thm.get_name_hint)) ths)) (facts_of thy) [];
   100 
   101 
   102 
   103 (** store theorems **)
   104 
   105 (* fact specifications *)
   106 
   107 fun map_facts f = map (apsnd (map (apfst (map f))));
   108 fun burrow_fact f = split_list #>> burrow f #> op ~~;
   109 fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
   110 
   111 
   112 (* naming *)
   113 
   114 fun name_multi name [x] = [(name, x)]
   115   | name_multi "" xs = map (pair "") xs
   116   | name_multi name xs = map_index (fn (i, x) => (name ^ "_" ^ string_of_int (i + 1), x)) xs;
   117 
   118 fun name_thm pre official name thm = thm
   119   |> (if Thm.get_name thm <> "" andalso pre orelse not official then I else Thm.put_name name)
   120   |> (if Thm.has_name_hint thm andalso pre orelse name = "" then I else Thm.put_name_hint name)
   121   |> Thm.default_position (Position.thread_data ());
   122 
   123 fun name_thms pre official name xs =
   124   map (uncurry (name_thm pre official)) (name_multi name xs);
   125 
   126 fun name_thmss official name fact =
   127   burrow_fact (name_thms true official name) fact;
   128 
   129 
   130 (* enter_thms *)
   131 
   132 fun enter_thms _ _ app_att ("", thms) thy = app_att (thy, thms) |> swap
   133   | enter_thms pre_name post_name app_att (bname, thms) thy =
   134       let
   135         val naming = Sign.naming_of thy;
   136         val name = NameSpace.full naming bname;
   137         val (thy', thms') = apsnd (post_name name) (app_att (thy, pre_name name thms));
   138         val thms'' = map (Thm.transfer thy') thms';
   139         val thy'' = thy' |> FactsData.map (Facts.add_global naming (name, thms''));
   140       in (thms'', thy'') end;
   141 
   142 
   143 (* store_thm(s) *)
   144 
   145 val store_thms = enter_thms (name_thms true true) (name_thms false true) I;
   146 fun store_thm (name, th) = store_thms (name, [th]) #>> the_single;
   147 
   148 fun store_thm_open (name, th) =
   149   enter_thms (name_thms true false) (name_thms false false) I (name, [th]) #>> the_single;
   150 
   151 
   152 (* add_thms(s) *)
   153 
   154 fun add_thms_atts pre_name ((bname, thms), atts) =
   155   enter_thms pre_name (name_thms false true)
   156     (foldl_map (Thm.theory_attributes atts)) (bname, thms);
   157 
   158 fun gen_add_thmss pre_name =
   159   fold_map (add_thms_atts pre_name);
   160 
   161 fun gen_add_thms pre_name args =
   162   apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
   163 
   164 val add_thmss = gen_add_thmss (name_thms true true);
   165 val add_thms = gen_add_thms (name_thms true true);
   166 val add_thm = yield_singleton add_thms;
   167 
   168 
   169 (* add_thms_dynamic *)
   170 
   171 fun add_thms_dynamic (bname, f) thy =
   172   let val name = Sign.full_name thy bname
   173   in thy |> FactsData.map (Facts.add_dynamic (Sign.naming_of thy) (name, f)) end;
   174 
   175 
   176 (* note_thmss *)
   177 
   178 local
   179 
   180 fun gen_note_thmss tag = fold_map (fn ((bname, more_atts), ths_atts) => fn thy =>
   181   let
   182     fun app (x, (ths, atts)) = foldl_map (Thm.theory_attributes atts) (x, ths);
   183     val (thms, thy') = thy |> enter_thms
   184       (name_thmss true) (name_thms false true) (apsnd flat o foldl_map app)
   185       (bname, map (fn (ths, atts) => (ths, surround tag (atts @ more_atts))) ths_atts);
   186   in ((bname, thms), thy') end);
   187 
   188 in
   189 
   190 fun note_thmss k = gen_note_thmss (Thm.kind k);
   191 fun note_thmss_grouped k g = gen_note_thmss (Thm.kind k #> Thm.group g);
   192 
   193 end;
   194 
   195 
   196 (* store axioms as theorems *)
   197 
   198 local
   199   fun get_ax thy (name, _) = Thm.get_axiom_i thy (Sign.full_name thy name);
   200   fun get_axs thy named_axs = map (Thm.forall_elim_vars 0 o get_ax thy) named_axs;
   201   fun add_axm add = fold_map (fn ((name, ax), atts) => fn thy =>
   202     let
   203       val named_ax = [(name, ax)];
   204       val thy' = add named_ax thy;
   205       val thm = hd (get_axs thy' named_ax);
   206     in apfst hd (gen_add_thms (K I) [((name, thm), atts)] thy') end);
   207 in
   208   val add_defs               = add_axm o Theory.add_defs_i false;
   209   val add_defs_unchecked     = add_axm o Theory.add_defs_i true;
   210   val add_axioms             = add_axm Theory.add_axioms_i;
   211   val add_defs_cmd           = add_axm o Theory.add_defs false;
   212   val add_defs_unchecked_cmd = add_axm o Theory.add_defs true;
   213   val add_axioms_cmd         = add_axm Theory.add_axioms;
   214 end;
   215 
   216 
   217 
   218 (*** Pure theory syntax and logical content ***)
   219 
   220 val typ = SimpleSyntax.read_typ;
   221 val term = SimpleSyntax.read_term;
   222 val prop = SimpleSyntax.read_prop;
   223 
   224 
   225 (* application syntax variants *)
   226 
   227 val appl_syntax =
   228  [("_appl", typ "('b => 'a) => args => logic", Mixfix ("(1_/(1'(_')))", [1000, 0], 1000)),
   229   ("_appl", typ "('b => 'a) => args => aprop", Mixfix ("(1_/(1'(_')))", [1000, 0], 1000))];
   230 
   231 val applC_syntax =
   232  [("",       typ "'a => cargs",                  Delimfix "_"),
   233   ("_cargs", typ "'a => cargs => cargs",         Mixfix ("_/ _", [1000, 1000], 1000)),
   234   ("_applC", typ "('b => 'a) => cargs => logic", Mixfix ("(1_/ _)", [1000, 1000], 999)),
   235   ("_applC", typ "('b => 'a) => cargs => aprop", Mixfix ("(1_/ _)", [1000, 1000], 999))];
   236 
   237 structure OldApplSyntax = TheoryDataFun
   238 (
   239   type T = bool;
   240   val empty = false;
   241   val copy = I;
   242   val extend = I;
   243   fun merge _ (b1, b2) : T =
   244     if b1 = b2 then b1
   245     else error "Cannot merge theories with different application syntax";
   246 );
   247 
   248 val old_appl_syntax = OldApplSyntax.get;
   249 
   250 val old_appl_syntax_setup =
   251   OldApplSyntax.put true #>
   252   Sign.del_modesyntax_i Syntax.mode_default applC_syntax #>
   253   Sign.add_syntax_i appl_syntax;
   254 
   255 
   256 (* main content *)
   257 
   258 val _ = Context.>> (Context.map_theory
   259   (OldApplSyntax.init #>
   260    Sign.add_types
   261    [("fun", 2, NoSyn),
   262     ("prop", 0, NoSyn),
   263     ("itself", 1, NoSyn),
   264     ("dummy", 0, NoSyn)]
   265   #> Sign.add_nonterminals Syntax.basic_nonterms
   266   #> Sign.add_syntax_i
   267    [("_lambda",     typ "pttrns => 'a => logic",       Mixfix ("(3%_./ _)", [0, 3], 3)),
   268     ("_abs",        typ "'a",                          NoSyn),
   269     ("",            typ "'a => args",                  Delimfix "_"),
   270     ("_args",       typ "'a => args => args",          Delimfix "_,/ _"),
   271     ("",            typ "id => idt",                   Delimfix "_"),
   272     ("_idtdummy",   typ "idt",                         Delimfix "'_"),
   273     ("_idtyp",      typ "id => type => idt",           Mixfix ("_::_", [], 0)),
   274     ("_idtypdummy", typ "type => idt",                 Mixfix ("'_()::_", [], 0)),
   275     ("",            typ "idt => idt",                  Delimfix "'(_')"),
   276     ("",            typ "idt => idts",                 Delimfix "_"),
   277     ("_idts",       typ "idt => idts => idts",         Mixfix ("_/ _", [1, 0], 0)),
   278     ("",            typ "idt => pttrn",                Delimfix "_"),
   279     ("",            typ "pttrn => pttrns",             Delimfix "_"),
   280     ("_pttrns",     typ "pttrn => pttrns => pttrns",   Mixfix ("_/ _", [1, 0], 0)),
   281     ("",            typ "id => aprop",                 Delimfix "_"),
   282     ("",            typ "longid => aprop",             Delimfix "_"),
   283     ("",            typ "var => aprop",                Delimfix "_"),
   284     ("_DDDOT",      typ "aprop",                       Delimfix "..."),
   285     ("_aprop",      typ "aprop => prop",               Delimfix "PROP _"),
   286     ("_asm",        typ "prop => asms",                Delimfix "_"),
   287     ("_asms",       typ "prop => asms => asms",        Delimfix "_;/ _"),
   288     ("_bigimpl",    typ "asms => prop => prop",        Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
   289     ("_ofclass",    typ "type => logic => prop",       Delimfix "(1OFCLASS/(1'(_,/ _')))"),
   290     ("_mk_ofclass", typ "dummy",                       NoSyn),
   291     ("_TYPE",       typ "type => logic",               Delimfix "(1TYPE/(1'(_')))"),
   292     ("",            typ "id => logic",                 Delimfix "_"),
   293     ("",            typ "longid => logic",             Delimfix "_"),
   294     ("",            typ "var => logic",                Delimfix "_"),
   295     ("_DDDOT",      typ "logic",                       Delimfix "..."),
   296     ("_constify",   typ "num => num_const",            Delimfix "_"),
   297     ("_indexnum",   typ "num_const => index",          Delimfix "\\<^sub>_"),
   298     ("_index",      typ "logic => index",              Delimfix "(00\\<^bsub>_\\<^esub>)"),
   299     ("_indexdefault", typ "index",                     Delimfix ""),
   300     ("_indexvar",   typ "index",                       Delimfix "'\\<index>"),
   301     ("_struct",     typ "index => logic",              Mixfix ("\\<struct>_", [1000], 1000)),
   302     ("==>",         typ "prop => prop => prop",        Delimfix "op ==>"),
   303     (Term.dummy_patternN, typ "aprop",                 Delimfix "'_")]
   304   #> Sign.add_syntax_i applC_syntax
   305   #> Sign.add_modesyntax_i (Symbol.xsymbolsN, true)
   306    [("fun",      typ "type => type => type",   Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
   307     ("_bracket", typ "types => type => type",  Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
   308     ("_ofsort",  typ "tid => sort => type",    Mixfix ("_\\<Colon>_", [1000, 0], 1000)),
   309     ("_constrain", typ "'a => type => 'a",     Mixfix ("_\\<Colon>_", [4, 0], 3)),
   310     ("_idtyp",    typ "id => type => idt",     Mixfix ("_\\<Colon>_", [], 0)),
   311     ("_idtypdummy", typ "type => idt",         Mixfix ("'_()\\<Colon>_", [], 0)),
   312     ("_type_constraint_", typ "'a",            NoSyn),
   313     ("_lambda",  typ "pttrns => 'a => logic",  Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
   314     ("==",       typ "'a => 'a => prop",       InfixrName ("\\<equiv>", 2)),
   315     ("all_binder", typ "idts => prop => prop", Mixfix ("(3\\<And>_./ _)", [0, 0], 0)),
   316     ("==>",      typ "prop => prop => prop",   InfixrName ("\\<Longrightarrow>", 1)),
   317     ("_DDDOT",   typ "aprop",                  Delimfix "\\<dots>"),
   318     ("_bigimpl", typ "asms => prop => prop",   Mixfix ("((1\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
   319     ("_DDDOT",   typ "logic",                  Delimfix "\\<dots>")]
   320   #> Sign.add_modesyntax_i ("", false)
   321    [("prop", typ "prop => prop", Mixfix ("_", [0], 0)),
   322     ("Pure.term", typ "'a => prop", Delimfix "TERM _"),
   323     ("Pure.conjunction", typ "prop => prop => prop", InfixrName ("&&", 2))]
   324   #> Sign.add_modesyntax_i ("HTML", false)
   325    [("_lambda", typ "pttrns => 'a => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3))]
   326   #> Sign.add_consts_i
   327    [("==", typ "'a => 'a => prop", InfixrName ("==", 2)),
   328     ("==>", typ "prop => prop => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
   329     ("all", typ "('a => prop) => prop", Binder ("!!", 0, 0)),
   330     ("prop", typ "prop => prop", NoSyn),
   331     ("TYPE", typ "'a itself", NoSyn),
   332     (Term.dummy_patternN, typ "'a", Delimfix "'_")]
   333   #> Theory.add_deps "==" ("==", typ "'a => 'a => prop") []
   334   #> Theory.add_deps "==>" ("==>", typ "prop => prop => prop") []
   335   #> Theory.add_deps "all" ("all", typ "('a => prop) => prop") []
   336   #> Theory.add_deps "TYPE" ("TYPE", typ "'a itself") []
   337   #> Theory.add_deps Term.dummy_patternN (Term.dummy_patternN, typ "'a") []
   338   #> Sign.add_trfuns Syntax.pure_trfuns
   339   #> Sign.add_trfunsT Syntax.pure_trfunsT
   340   #> Sign.local_path
   341   #> Sign.add_consts_i
   342    [("term", typ "'a => prop", NoSyn),
   343     ("conjunction", typ "prop => prop => prop", NoSyn)]
   344   #> (add_defs false o map Thm.no_attributes)
   345    [("prop_def", prop "(CONST prop :: prop => prop) (A::prop) == A::prop"),
   346     ("term_def", prop "(CONST Pure.term :: 'a => prop) (x::'a) == (!!A::prop. A ==> A)"),
   347     ("conjunction_def", prop "(A && B) == (!!C::prop. (A ==> B ==> C) ==> C)")] #> snd
   348   #> Sign.hide_const false "Pure.conjunction"
   349   #> Sign.hide_const false "Pure.term"
   350   #> add_thmss [(("nothing", []), [])] #> snd
   351   #> Theory.add_axioms_i Proofterm.equality_axms));
   352 
   353 end;