src/Pure/pure_thy.ML
author wenzelm
Fri Sep 17 20:18:27 2010 +0200 (2010-09-17)
changeset 39507 839873937ddd
parent 38975 ef13a2cc97be
child 39557 fe5722fce758
permissions -rw-r--r--
tuned signature of (Context_)Position.report variants;
     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 -> 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
    44   val old_appl_syntax_setup: theory -> theory
    45 end;
    46 
    47 structure PureThy: PURE_THY =
    48 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 
   233 val typ = Simple_Syntax.read_typ;
   234 val prop = Simple_Syntax.read_prop;
   235 
   236 val tycon = Syntax.mark_type;
   237 val const = Syntax.mark_const;
   238 
   239 val typeT = Syntax.typeT;
   240 val spropT = Syntax.spropT;
   241 
   242 
   243 (* application syntax variants *)
   244 
   245 val appl_syntax =
   246  [("_appl", typ "('b => 'a) => args => logic", Mixfix ("(1_/(1'(_')))", [1000, 0], 1000)),
   247   ("_appl", typ "('b => 'a) => args => aprop", Mixfix ("(1_/(1'(_')))", [1000, 0], 1000))];
   248 
   249 val applC_syntax =
   250  [("",       typ "'a => cargs",                  Delimfix "_"),
   251   ("_cargs", typ "'a => cargs => cargs",         Mixfix ("_/ _", [1000, 1000], 1000)),
   252   ("_applC", typ "('b => 'a) => cargs => logic", Mixfix ("(1_/ _)", [1000, 1000], 999)),
   253   ("_applC", typ "('b => 'a) => cargs => aprop", Mixfix ("(1_/ _)", [1000, 1000], 999))];
   254 
   255 structure Old_Appl_Syntax = Theory_Data
   256 (
   257   type T = bool;
   258   val empty = false;
   259   val extend = I;
   260   fun merge (b1, b2) : T =
   261     if b1 = b2 then b1
   262     else error "Cannot merge theories with different application syntax";
   263 );
   264 
   265 val old_appl_syntax = Old_Appl_Syntax.get;
   266 
   267 val old_appl_syntax_setup =
   268   Old_Appl_Syntax.put true #>
   269   Sign.del_modesyntax_i Syntax.mode_default applC_syntax #>
   270   Sign.add_syntax_i appl_syntax;
   271 
   272 
   273 (* main content *)
   274 
   275 val _ = Context.>> (Context.map_theory
   276   (Sign.map_naming (Name_Space.set_theory_name Context.PureN) #>
   277    Old_Appl_Syntax.put false #>
   278    Sign.add_types
   279    [(Binding.name "fun", 2, NoSyn),
   280     (Binding.name "prop", 0, NoSyn),
   281     (Binding.name "itself", 1, NoSyn),
   282     (Binding.name "dummy", 0, NoSyn)]
   283   #> Sign.add_nonterminals (map Binding.name Syntax.basic_nonterms)
   284   #> Sign.add_syntax_i
   285    [("_lambda",     typ "pttrns => 'a => logic",       Mixfix ("(3%_./ _)", [0, 3], 3)),
   286     ("_abs",        typ "'a",                          NoSyn),
   287     ("",            typ "'a => args",                  Delimfix "_"),
   288     ("_args",       typ "'a => args => args",          Delimfix "_,/ _"),
   289     ("",            typ "id => idt",                   Delimfix "_"),
   290     ("_idtdummy",   typ "idt",                         Delimfix "'_"),
   291     ("_idtyp",      typ "id => type => idt",           Mixfix ("_::_", [], 0)),
   292     ("_idtypdummy", typ "type => idt",                 Mixfix ("'_()::_", [], 0)),
   293     ("",            typ "idt => idt",                  Delimfix "'(_')"),
   294     ("",            typ "idt => idts",                 Delimfix "_"),
   295     ("_idts",       typ "idt => idts => idts",         Mixfix ("_/ _", [1, 0], 0)),
   296     ("",            typ "idt => pttrn",                Delimfix "_"),
   297     ("",            typ "pttrn => pttrns",             Delimfix "_"),
   298     ("_pttrns",     typ "pttrn => pttrns => pttrns",   Mixfix ("_/ _", [1, 0], 0)),
   299     ("",            typ "aprop => aprop",              Delimfix "'(_')"),
   300     ("",            typ "id => aprop",                 Delimfix "_"),
   301     ("",            typ "longid => aprop",             Delimfix "_"),
   302     ("",            typ "var => aprop",                Delimfix "_"),
   303     ("_DDDOT",      typ "aprop",                       Delimfix "..."),
   304     ("_aprop",      typ "aprop => prop",               Delimfix "PROP _"),
   305     ("_asm",        typ "prop => asms",                Delimfix "_"),
   306     ("_asms",       typ "prop => asms => asms",        Delimfix "_;/ _"),
   307     ("_bigimpl",    typ "asms => prop => prop",        Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
   308     ("_ofclass",    typ "type => logic => prop",       Delimfix "(1OFCLASS/(1'(_,/ _')))"),
   309     ("_mk_ofclass", typ "dummy",                       NoSyn),
   310     ("_TYPE",       typ "type => logic",               Delimfix "(1TYPE/(1'(_')))"),
   311     ("",            typ "id => logic",                 Delimfix "_"),
   312     ("",            typ "longid => logic",             Delimfix "_"),
   313     ("",            typ "var => logic",                Delimfix "_"),
   314     ("_DDDOT",      typ "logic",                       Delimfix "..."),
   315     ("_constify",   typ "num => num_const",            Delimfix "_"),
   316     ("_constify",   typ "float_token => float_const",  Delimfix "_"),
   317     ("_indexnum",   typ "num_const => index",          Delimfix "\\<^sub>_"),
   318     ("_index",      typ "logic => index",              Delimfix "(00\\<^bsub>_\\<^esub>)"),
   319     ("_indexdefault", typ "index",                     Delimfix ""),
   320     ("_indexvar",   typ "index",                       Delimfix "'\\<index>"),
   321     ("_struct",     typ "index => logic",              Mixfix ("\\<struct>_", [1000], 1000)),
   322     ("_update_name", typ "idt",                        NoSyn),
   323     (Syntax.constrainAbsC, typ "'a",                   NoSyn),
   324     (const "==>",   typ "prop => prop => prop",        Delimfix "op ==>"),
   325     (const Term.dummy_patternN, typ "aprop",           Delimfix "'_"),
   326     ("_sort_constraint", typ "type => prop",           Delimfix "(1SORT'_CONSTRAINT/(1'(_')))"),
   327     (const "Pure.term", typ "logic => prop",           Delimfix "TERM _"),
   328     (const "Pure.conjunction", typ "prop => prop => prop", Infixr ("&&&", 2))]
   329   #> Sign.add_syntax_i applC_syntax
   330   #> Sign.add_modesyntax_i (Symbol.xsymbolsN, true)
   331    [(tycon "fun",         typ "type => type => type",   Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
   332     ("_bracket",          typ "types => type => type",  Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
   333     ("_ofsort",           typ "tid => sort => type",    Mixfix ("_\\<Colon>_", [1000, 0], 1000)),
   334     ("_constrain",        typ "logic => type => logic", Mixfix ("_\\<Colon>_", [4, 0], 3)),
   335     ("_constrain",        [spropT, typeT] ---> spropT,  Mixfix ("_\\<Colon>_", [4, 0], 3)),
   336     ("_idtyp",            typ "id => type => idt",      Mixfix ("_\\<Colon>_", [], 0)),
   337     ("_idtypdummy",       typ "type => idt",            Mixfix ("'_()\\<Colon>_", [], 0)),
   338     ("_type_constraint_", typ "'a",                     NoSyn),
   339     ("_lambda",           typ "pttrns => 'a => logic",  Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
   340     (const "==",          typ "'a => 'a => prop",       Infixr ("\\<equiv>", 2)),
   341     (const "all_binder",  typ "idts => prop => prop",   Mixfix ("(3\\<And>_./ _)", [0, 0], 0)),
   342     (const "==>",         typ "prop => prop => prop",   Infixr ("\\<Longrightarrow>", 1)),
   343     ("_DDDOT",            typ "aprop",                  Delimfix "\\<dots>"),
   344     ("_bigimpl",          typ "asms => prop => prop",   Mixfix ("((1\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
   345     ("_DDDOT",            typ "logic",                  Delimfix "\\<dots>")]
   346   #> Sign.add_modesyntax_i ("", false)
   347    [(const "prop", typ "prop => prop", Mixfix ("_", [0], 0))]
   348   #> Sign.add_modesyntax_i ("HTML", false)
   349    [("_lambda", typ "pttrns => 'a => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3))]
   350   #> Sign.add_consts_i
   351    [(Binding.name "==", typ "'a => 'a => prop", Infixr ("==", 2)),
   352     (Binding.name "==>", typ "prop => prop => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
   353     (Binding.name "all", typ "('a => prop) => prop", Binder ("!!", 0, 0)),
   354     (Binding.name "prop", typ "prop => prop", NoSyn),
   355     (Binding.name "TYPE", typ "'a itself", NoSyn),
   356     (Binding.name Term.dummy_patternN, typ "'a", Delimfix "'_")]
   357   #> Theory.add_deps "==" ("==", typ "'a => 'a => prop") []
   358   #> Theory.add_deps "==>" ("==>", typ "prop => prop => prop") []
   359   #> Theory.add_deps "all" ("all", typ "('a => prop) => prop") []
   360   #> Theory.add_deps "TYPE" ("TYPE", typ "'a itself") []
   361   #> Theory.add_deps Term.dummy_patternN (Term.dummy_patternN, typ "'a") []
   362   #> Sign.add_trfuns Syntax.pure_trfuns
   363   #> Sign.add_trfunsT Syntax.pure_trfunsT
   364   #> Sign.local_path
   365   #> Sign.add_consts_i
   366    [(Binding.name "term", typ "'a => prop", NoSyn),
   367     (Binding.name "sort_constraint", typ "'a itself => prop", NoSyn),
   368     (Binding.name "conjunction", typ "prop => prop => prop", NoSyn)]
   369   #> (add_defs false o map Thm.no_attributes)
   370    [(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)"),
   372     (Binding.name "sort_constraint_def",
   373       prop "(CONST Pure.sort_constraint :: 'a itself => prop) (CONST TYPE :: 'a itself) ==\
   374       \ (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
   376   #> Sign.hide_const false "Pure.term"
   377   #> Sign.hide_const false "Pure.sort_constraint"
   378   #> Sign.hide_const false "Pure.conjunction"
   379   #> add_thmss [((Binding.name "nothing", []), [])] #> snd
   380   #> fold (fn (a, prop) => snd o Thm.add_axiom (Binding.name a, prop)) Proofterm.equality_axms));
   381 
   382 end;