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