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