src/Pure/pure_thy.ML
author wenzelm
Thu Oct 23 15:28:01 2008 +0200 (2008-10-23)
changeset 28674 08a77c495dc1
parent 28622 1a0b845855ac
child 28841 5556c7976837
permissions -rw-r--r--
renamed Thm.get_axiom_i to Thm.axiom;
     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 -> Position.T -> string -> thm -> thm
    24   val name_thms: bool -> bool -> Position.T -> string -> thm list -> thm list
    25   val name_thmss: bool -> Position.T -> 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 -> ((Name.binding * attribute list) *
    34     (thm list * attribute list) list) list -> theory -> (string * thm list) list * theory
    35   val note_thmss_grouped: string -> string -> ((Name.binding * attribute list) *
    36     (thm list * attribute list) list) list -> theory -> (string * 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 pos 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 pos
   122   |> Thm.default_position (Position.thread_data ());
   123 
   124 fun name_thms pre official pos name xs =
   125   map (uncurry (name_thm pre official pos)) (name_multi name xs);
   126 
   127 fun name_thmss official pos name fact =
   128   burrow_fact (name_thms true official pos name) fact;
   129 
   130 
   131 (* enter_thms *)
   132 
   133 fun enter_thms _ _ app_att ("", thms) thy = app_att (thy, thms) |> swap
   134   | enter_thms pre_name post_name app_att (bname, thms) thy =
   135       let
   136         val naming = Sign.naming_of thy;
   137         val name = NameSpace.full naming bname;
   138         val (thy', thms') = apsnd (post_name name) (app_att (thy, pre_name name thms));
   139         val thms'' = map (Thm.transfer thy') thms';
   140         val thy'' = thy' |> FactsData.map (Facts.add_global naming (name, thms''));
   141       in (thms'', thy'') end;
   142 
   143 
   144 (* store_thm(s) *)
   145 
   146 val store_thms =
   147   enter_thms (name_thms true true Position.none) (name_thms false true Position.none) I;
   148 
   149 fun store_thm (name, th) = store_thms (name, [th]) #>> the_single;
   150 
   151 fun store_thm_open (name, th) =
   152   enter_thms (name_thms true false Position.none) (name_thms false false Position.none) I
   153     (name, [th]) #>> the_single;
   154 
   155 
   156 (* add_thms(s) *)
   157 
   158 fun add_thms_atts pre_name ((bname, thms), atts) =
   159   enter_thms pre_name (name_thms false true Position.none)
   160     (foldl_map (Thm.theory_attributes atts)) (bname, thms);
   161 
   162 fun gen_add_thmss pre_name =
   163   fold_map (add_thms_atts pre_name);
   164 
   165 fun gen_add_thms pre_name args =
   166   apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
   167 
   168 val add_thmss = gen_add_thmss (name_thms true true Position.none);
   169 val add_thms = gen_add_thms (name_thms true true Position.none);
   170 val add_thm = yield_singleton add_thms;
   171 
   172 
   173 (* add_thms_dynamic *)
   174 
   175 fun add_thms_dynamic (bname, f) thy =
   176   let val name = Sign.full_name thy bname
   177   in thy |> FactsData.map (Facts.add_dynamic (Sign.naming_of thy) (name, f)) end;
   178 
   179 
   180 (* note_thmss *)
   181 
   182 local
   183 
   184 fun gen_note_thmss tag = fold_map (fn ((binding, more_atts), ths_atts) => fn thy =>
   185   let
   186     val bname = Name.name_of binding;
   187     val pos = Name.pos_of binding;
   188     val name = Sign.full_name thy bname;
   189     val _ = Position.report (Markup.fact_decl name) pos;
   190 
   191     fun app (x, (ths, atts)) = foldl_map (Thm.theory_attributes atts) (x, ths);
   192     val (thms, thy') = thy |> enter_thms
   193       (name_thmss true pos) (name_thms false true pos) (apsnd flat o foldl_map app)
   194       (bname, map (fn (ths, atts) => (ths, surround tag (atts @ more_atts))) ths_atts);
   195   in ((name, thms), thy') end);
   196 
   197 in
   198 
   199 fun note_thmss k = gen_note_thmss (Thm.kind k);
   200 fun note_thmss_grouped k g = gen_note_thmss (Thm.kind k #> Thm.group g);
   201 
   202 end;
   203 
   204 
   205 (* store axioms as theorems *)
   206 
   207 local
   208   fun get_ax thy (name, _) = Thm.axiom thy (Sign.full_name thy name);
   209   fun get_axs thy named_axs = map (Thm.forall_elim_vars 0 o get_ax thy) named_axs;
   210   fun add_axm add = fold_map (fn ((name, ax), atts) => fn thy =>
   211     let
   212       val named_ax = [(name, ax)];
   213       val thy' = add named_ax thy;
   214       val thm = hd (get_axs thy' named_ax);
   215     in apfst hd (gen_add_thms (K I) [((name, thm), atts)] thy') end);
   216 in
   217   val add_defs               = add_axm o Theory.add_defs_i false;
   218   val add_defs_unchecked     = add_axm o Theory.add_defs_i true;
   219   val add_axioms             = add_axm Theory.add_axioms_i;
   220   val add_defs_cmd           = add_axm o Theory.add_defs false;
   221   val add_defs_unchecked_cmd = add_axm o Theory.add_defs true;
   222   val add_axioms_cmd         = add_axm Theory.add_axioms;
   223 end;
   224 
   225 
   226 
   227 (*** Pure theory syntax and logical content ***)
   228 
   229 val typ = SimpleSyntax.read_typ;
   230 val term = SimpleSyntax.read_term;
   231 val prop = SimpleSyntax.read_prop;
   232 
   233 
   234 (* application syntax variants *)
   235 
   236 val appl_syntax =
   237  [("_appl", typ "('b => 'a) => args => logic", Mixfix ("(1_/(1'(_')))", [1000, 0], 1000)),
   238   ("_appl", typ "('b => 'a) => args => aprop", Mixfix ("(1_/(1'(_')))", [1000, 0], 1000))];
   239 
   240 val applC_syntax =
   241  [("",       typ "'a => cargs",                  Delimfix "_"),
   242   ("_cargs", typ "'a => cargs => cargs",         Mixfix ("_/ _", [1000, 1000], 1000)),
   243   ("_applC", typ "('b => 'a) => cargs => logic", Mixfix ("(1_/ _)", [1000, 1000], 999)),
   244   ("_applC", typ "('b => 'a) => cargs => aprop", Mixfix ("(1_/ _)", [1000, 1000], 999))];
   245 
   246 structure OldApplSyntax = TheoryDataFun
   247 (
   248   type T = bool;
   249   val empty = false;
   250   val copy = I;
   251   val extend = I;
   252   fun merge _ (b1, b2) : T =
   253     if b1 = b2 then b1
   254     else error "Cannot merge theories with different application syntax";
   255 );
   256 
   257 val old_appl_syntax = OldApplSyntax.get;
   258 
   259 val old_appl_syntax_setup =
   260   OldApplSyntax.put true #>
   261   Sign.del_modesyntax_i Syntax.mode_default applC_syntax #>
   262   Sign.add_syntax_i appl_syntax;
   263 
   264 
   265 (* main content *)
   266 
   267 val _ = Context.>> (Context.map_theory
   268   (OldApplSyntax.init #>
   269    Sign.add_types
   270    [("fun", 2, NoSyn),
   271     ("prop", 0, NoSyn),
   272     ("itself", 1, NoSyn),
   273     ("dummy", 0, NoSyn)]
   274   #> Sign.add_nonterminals Syntax.basic_nonterms
   275   #> Sign.add_syntax_i
   276    [("_lambda",     typ "pttrns => 'a => logic",       Mixfix ("(3%_./ _)", [0, 3], 3)),
   277     ("_abs",        typ "'a",                          NoSyn),
   278     ("",            typ "'a => args",                  Delimfix "_"),
   279     ("_args",       typ "'a => args => args",          Delimfix "_,/ _"),
   280     ("",            typ "id => idt",                   Delimfix "_"),
   281     ("_idtdummy",   typ "idt",                         Delimfix "'_"),
   282     ("_idtyp",      typ "id => type => idt",           Mixfix ("_::_", [], 0)),
   283     ("_idtypdummy", typ "type => idt",                 Mixfix ("'_()::_", [], 0)),
   284     ("",            typ "idt => idt",                  Delimfix "'(_')"),
   285     ("",            typ "idt => idts",                 Delimfix "_"),
   286     ("_idts",       typ "idt => idts => idts",         Mixfix ("_/ _", [1, 0], 0)),
   287     ("",            typ "idt => pttrn",                Delimfix "_"),
   288     ("",            typ "pttrn => pttrns",             Delimfix "_"),
   289     ("_pttrns",     typ "pttrn => pttrns => pttrns",   Mixfix ("_/ _", [1, 0], 0)),
   290     ("",            typ "id => aprop",                 Delimfix "_"),
   291     ("",            typ "longid => aprop",             Delimfix "_"),
   292     ("",            typ "var => aprop",                Delimfix "_"),
   293     ("_DDDOT",      typ "aprop",                       Delimfix "..."),
   294     ("_aprop",      typ "aprop => prop",               Delimfix "PROP _"),
   295     ("_asm",        typ "prop => asms",                Delimfix "_"),
   296     ("_asms",       typ "prop => asms => asms",        Delimfix "_;/ _"),
   297     ("_bigimpl",    typ "asms => prop => prop",        Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
   298     ("_ofclass",    typ "type => logic => prop",       Delimfix "(1OFCLASS/(1'(_,/ _')))"),
   299     ("_mk_ofclass", typ "dummy",                       NoSyn),
   300     ("_TYPE",       typ "type => logic",               Delimfix "(1TYPE/(1'(_')))"),
   301     ("",            typ "id => logic",                 Delimfix "_"),
   302     ("",            typ "longid => logic",             Delimfix "_"),
   303     ("",            typ "var => logic",                Delimfix "_"),
   304     ("_DDDOT",      typ "logic",                       Delimfix "..."),
   305     ("_constify",   typ "num => num_const",            Delimfix "_"),
   306     ("_indexnum",   typ "num_const => index",          Delimfix "\\<^sub>_"),
   307     ("_index",      typ "logic => index",              Delimfix "(00\\<^bsub>_\\<^esub>)"),
   308     ("_indexdefault", typ "index",                     Delimfix ""),
   309     ("_indexvar",   typ "index",                       Delimfix "'\\<index>"),
   310     ("_struct",     typ "index => logic",              Mixfix ("\\<struct>_", [1000], 1000)),
   311     ("==>",         typ "prop => prop => prop",        Delimfix "op ==>"),
   312     (Term.dummy_patternN, typ "aprop",                 Delimfix "'_"),
   313     ("_sort_constraint", typ "type => prop",           Delimfix "(1SORT'_CONSTRAINT/(1'(_')))")]
   314   #> Sign.add_syntax_i applC_syntax
   315   #> Sign.add_modesyntax_i (Symbol.xsymbolsN, true)
   316    [("fun",      typ "type => type => type",   Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
   317     ("_bracket", typ "types => type => type",  Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
   318     ("_ofsort",  typ "tid => sort => type",    Mixfix ("_\\<Colon>_", [1000, 0], 1000)),
   319     ("_constrain", typ "'a => type => 'a",     Mixfix ("_\\<Colon>_", [4, 0], 3)),
   320     ("_idtyp",    typ "id => type => idt",     Mixfix ("_\\<Colon>_", [], 0)),
   321     ("_idtypdummy", typ "type => idt",         Mixfix ("'_()\\<Colon>_", [], 0)),
   322     ("_type_constraint_", typ "'a",            NoSyn),
   323     ("_lambda",  typ "pttrns => 'a => logic",  Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
   324     ("==",       typ "'a => 'a => prop",       InfixrName ("\\<equiv>", 2)),
   325     ("all_binder", typ "idts => prop => prop", Mixfix ("(3\\<And>_./ _)", [0, 0], 0)),
   326     ("==>",      typ "prop => prop => prop",   InfixrName ("\\<Longrightarrow>", 1)),
   327     ("_DDDOT",   typ "aprop",                  Delimfix "\\<dots>"),
   328     ("_bigimpl", typ "asms => prop => prop",   Mixfix ("((1\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
   329     ("_DDDOT",   typ "logic",                  Delimfix "\\<dots>")]
   330   #> Sign.add_modesyntax_i ("", false)
   331    [("prop", typ "prop => prop", Mixfix ("_", [0], 0)),
   332     ("Pure.term", typ "'a => prop", Delimfix "TERM _"),
   333     ("Pure.conjunction", typ "prop => prop => prop", InfixrName ("&&", 2))]
   334   #> Sign.add_modesyntax_i ("HTML", false)
   335    [("_lambda", typ "pttrns => 'a => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3))]
   336   #> Sign.add_consts_i
   337    [("==", typ "'a => 'a => prop", InfixrName ("==", 2)),
   338     ("==>", typ "prop => prop => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
   339     ("all", typ "('a => prop) => prop", Binder ("!!", 0, 0)),
   340     ("prop", typ "prop => prop", NoSyn),
   341     ("TYPE", typ "'a itself", NoSyn),
   342     (Term.dummy_patternN, typ "'a", Delimfix "'_")]
   343   #> Theory.add_deps "==" ("==", typ "'a => 'a => prop") []
   344   #> Theory.add_deps "==>" ("==>", typ "prop => prop => prop") []
   345   #> Theory.add_deps "all" ("all", typ "('a => prop) => prop") []
   346   #> Theory.add_deps "TYPE" ("TYPE", typ "'a itself") []
   347   #> Theory.add_deps Term.dummy_patternN (Term.dummy_patternN, typ "'a") []
   348   #> Sign.add_trfuns Syntax.pure_trfuns
   349   #> Sign.add_trfunsT Syntax.pure_trfunsT
   350   #> Sign.local_path
   351   #> Sign.add_consts_i
   352    [("term", typ "'a => prop", NoSyn),
   353     ("sort_constraint", typ "'a itself => prop", NoSyn),
   354     ("conjunction", typ "prop => prop => prop", NoSyn)]
   355   #> (add_defs false o map Thm.no_attributes)
   356    [("prop_def", prop "(CONST prop :: prop => prop) (A::prop) == A::prop"),
   357     ("term_def", prop "(CONST Pure.term :: 'a => prop) (x::'a) == (!!A::prop. A ==> A)"),
   358     ("sort_constraint_def",
   359       prop "(CONST Pure.sort_constraint :: 'a itself => prop) (CONST TYPE :: 'a itself) ==\
   360       \ (CONST Pure.term :: 'a itself => prop) (CONST TYPE :: 'a itself)"),
   361     ("conjunction_def", prop "(A && B) == (!!C::prop. (A ==> B ==> C) ==> C)")] #> snd
   362   #> Sign.hide_const false "Pure.term"
   363   #> Sign.hide_const false "Pure.sort_constraint"
   364   #> Sign.hide_const false "Pure.conjunction"
   365   #> add_thmss [(("nothing", []), [])] #> snd
   366   #> Theory.add_axioms_i Proofterm.equality_axms));
   367 
   368 end;