src/Pure/pure_thy.ML
author wenzelm
Thu Mar 20 16:04:30 2008 +0100 (2008-03-20)
changeset 26361 7946f459c6c8
parent 26344 04dacc6809b6
child 26367 06635166d211
permissions -rw-r--r--
Facts.Named: include position;
wenzelm@3987
     1
(*  Title:      Pure/pure_thy.ML
wenzelm@3987
     2
    ID:         $Id$
wenzelm@3987
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@3987
     4
wenzelm@16441
     5
Theorem storage.  The ProtoPure theory.
wenzelm@3987
     6
*)
wenzelm@3987
     7
wenzelm@4022
     8
signature BASIC_PURE_THY =
wenzelm@4022
     9
sig
wenzelm@5091
    10
  structure ProtoPure:
wenzelm@5091
    11
    sig
wenzelm@5091
    12
      val thy: theory
wenzelm@18031
    13
      val prop_def: thm
wenzelm@19775
    14
      val term_def: thm
wenzelm@19125
    15
      val conjunction_def: thm
wenzelm@5091
    16
    end
wenzelm@4853
    17
end;
wenzelm@4022
    18
wenzelm@3987
    19
signature PURE_THY =
wenzelm@3987
    20
sig
wenzelm@4022
    21
  include BASIC_PURE_THY
wenzelm@23657
    22
  val tag_rule: Markup.property -> thm -> thm
wenzelm@18801
    23
  val untag_rule: string -> thm -> thm
wenzelm@23657
    24
  val tag: Markup.property -> attribute
wenzelm@18801
    25
  val untag: string -> attribute
wenzelm@21964
    26
  val has_name_hint: thm -> bool
wenzelm@21646
    27
  val get_name_hint: thm -> string
wenzelm@21646
    28
  val put_name_hint: string -> thm -> thm
wenzelm@25981
    29
  val get_group: thm -> string option
wenzelm@25981
    30
  val put_group: string -> thm -> thm
wenzelm@25981
    31
  val group: string -> attribute
wenzelm@22251
    32
  val has_kind: thm -> bool
wenzelm@18801
    33
  val get_kind: thm -> string
wenzelm@18801
    34
  val kind_rule: string -> thm -> thm
wenzelm@18801
    35
  val kind: string -> attribute
wenzelm@18801
    36
  val kind_internal: attribute
wenzelm@23657
    37
  val has_internal: Markup.property list -> bool
wenzelm@18801
    38
  val is_internal: thm -> bool
wenzelm@26344
    39
  val get_fact: theory -> Facts.ref -> thm list
wenzelm@26344
    40
  val get_fact_silent: theory -> Facts.ref -> thm list
wenzelm@26344
    41
  val get_thms: theory -> xstring -> thm list
wenzelm@26344
    42
  val get_thm: theory -> xstring -> thm
wenzelm@17162
    43
  val theorems_of: theory -> thm list NameSpace.table
wenzelm@26282
    44
  val all_facts_of: theory -> Facts.T
wenzelm@16336
    45
  val thms_of: theory -> (string * thm) list
wenzelm@16336
    46
  val all_thms_of: theory -> (string * thm) list
wenzelm@12695
    47
  val hide_thms: bool -> string list -> theory -> theory
wenzelm@21580
    48
  val map_facts: ('a -> 'b) -> ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
wenzelm@21567
    49
  val burrow_fact: ('a list -> 'b list) -> ('a list * 'c) list -> ('b list * 'c) list
wenzelm@21580
    50
  val burrow_facts: ('a list -> 'b list) ->
wenzelm@21580
    51
    ('c * ('a list * 'd) list) list -> ('c * ('b list * 'd) list) list
wenzelm@21580
    52
  val name_multi: string -> 'a list -> (string * 'a) list
wenzelm@21646
    53
  val name_thm: bool -> bool -> string -> thm -> thm
wenzelm@21646
    54
  val name_thms: bool -> bool -> string -> thm list -> thm list
wenzelm@21646
    55
  val name_thmss: bool -> string -> (thm list * 'a) list -> (thm list * 'a) list
wenzelm@18728
    56
  val store_thm: (bstring * thm) * attribute list -> theory -> thm * theory
wenzelm@7405
    57
  val smart_store_thms: (bstring * thm list) -> thm list
wenzelm@12138
    58
  val smart_store_thms_open: (bstring * thm list) -> thm list
wenzelm@7899
    59
  val forall_elim_var: int -> thm -> thm
wenzelm@7899
    60
  val forall_elim_vars: int -> thm -> thm
wenzelm@18728
    61
  val add_thms: ((bstring * thm) * attribute list) list -> theory -> thm list * theory
wenzelm@24965
    62
  val add_thmss: ((bstring * thm list) * attribute list) list -> theory -> thm list list * theory
haftmann@25598
    63
  val note: string -> string * thm -> theory -> thm * theory
wenzelm@18801
    64
  val note_thmss: string -> ((bstring * attribute list) *
wenzelm@26336
    65
    (Facts.ref * attribute list) list) list -> theory -> (bstring * thm list) list * theory
wenzelm@18801
    66
  val note_thmss_i: string -> ((bstring * attribute list) *
wenzelm@18801
    67
    (thm list * attribute list) list) list -> theory -> (bstring * thm list) list * theory
wenzelm@25981
    68
  val note_thmss_grouped: string -> string -> ((bstring * attribute list) *
wenzelm@25981
    69
    (thm list * attribute list) list) list -> theory -> (bstring * thm list) list * theory
wenzelm@18801
    70
  val note_thmss_qualified: string -> string -> ((bstring * attribute list) *
wenzelm@18728
    71
    (thm list * attribute list) list) list -> theory -> (bstring * thm list) list * theory
wenzelm@18728
    72
  val add_axioms: ((bstring * string) * attribute list) list -> theory -> thm list * theory
wenzelm@18728
    73
  val add_axioms_i: ((bstring * term) * attribute list) list -> theory -> thm list * theory
wenzelm@18728
    74
  val add_axiomss: ((bstring * string list) * attribute list) list ->
wenzelm@18728
    75
    theory -> thm list list * theory
wenzelm@18728
    76
  val add_axiomss_i: ((bstring * term list) * attribute list) list ->
wenzelm@18728
    77
    theory -> thm list list * theory
wenzelm@18728
    78
  val add_defs: bool -> ((bstring * string) * attribute list) list ->
haftmann@18377
    79
    theory -> thm list * theory
wenzelm@18728
    80
  val add_defs_i: bool -> ((bstring * term) * attribute list) list ->
haftmann@18377
    81
    theory -> thm list * theory
wenzelm@19629
    82
  val add_defs_unchecked: bool -> ((bstring * string) * attribute list) list ->
wenzelm@19629
    83
    theory -> thm list * theory
wenzelm@19629
    84
  val add_defs_unchecked_i: bool -> ((bstring * term) * attribute list) list ->
wenzelm@19629
    85
    theory -> thm list * theory
wenzelm@24243
    86
  val appl_syntax: (string * typ * mixfix) list
wenzelm@24243
    87
  val applC_syntax: (string * typ * mixfix) list
wenzelm@3987
    88
end;
wenzelm@3987
    89
wenzelm@3987
    90
structure PureThy: PURE_THY =
wenzelm@3987
    91
struct
wenzelm@3987
    92
wenzelm@3987
    93
wenzelm@18801
    94
(*** theorem tags ***)
wenzelm@18801
    95
wenzelm@18801
    96
(* add / delete tags *)
wenzelm@18801
    97
wenzelm@21646
    98
fun tag_rule tg = Thm.map_tags (insert (op =) tg);
wenzelm@21646
    99
fun untag_rule s = Thm.map_tags (filter_out (fn (s', _) => s = s'));
wenzelm@18801
   100
wenzelm@18801
   101
fun tag tg x = Thm.rule_attribute (K (tag_rule tg)) x;
wenzelm@18801
   102
fun untag s x = Thm.rule_attribute (K (untag_rule s)) x;
wenzelm@18801
   103
wenzelm@18801
   104
wenzelm@21646
   105
(* unofficial theorem names *)
wenzelm@21646
   106
wenzelm@23657
   107
fun the_name_hint thm = the (AList.lookup (op =) (Thm.get_tags thm) Markup.nameN);
wenzelm@22251
   108
wenzelm@22251
   109
val has_name_hint = can the_name_hint;
wenzelm@22251
   110
val get_name_hint = the_default "??.unknown" o try the_name_hint;
wenzelm@21646
   111
wenzelm@23657
   112
fun put_name_hint name = untag_rule Markup.nameN #> tag_rule (Markup.nameN, name);
wenzelm@21964
   113
wenzelm@21646
   114
wenzelm@25981
   115
(* theorem groups *)
wenzelm@25981
   116
wenzelm@25981
   117
fun get_group thm = AList.lookup (op =) (Thm.get_tags thm) Markup.groupN;
wenzelm@25981
   118
wenzelm@25981
   119
fun put_group name =
wenzelm@25981
   120
  if name = "" then I else Thm.map_tags (AList.update (op =) (Markup.groupN, name));
wenzelm@25981
   121
wenzelm@25981
   122
fun group name = Thm.rule_attribute (K (put_group name));
wenzelm@25981
   123
wenzelm@25981
   124
wenzelm@18801
   125
(* theorem kinds *)
wenzelm@18801
   126
wenzelm@23657
   127
fun the_kind thm = the (AList.lookup (op =) (Thm.get_tags thm) Markup.kindN);
wenzelm@22251
   128
wenzelm@22251
   129
val has_kind = can the_kind;
wenzelm@25981
   130
val get_kind = the_default "" o try the_kind;
wenzelm@18801
   131
wenzelm@23657
   132
fun kind_rule k = tag_rule (Markup.kindN, k) o untag_rule Markup.kindN;
wenzelm@18801
   133
fun kind k x = if k = "" then x else Thm.rule_attribute (K (kind_rule k)) x;
wenzelm@22363
   134
fun kind_internal x = kind Thm.internalK x;
wenzelm@23657
   135
fun has_internal tags = exists (fn tg => tg = (Markup.kindN, Thm.internalK)) tags;
wenzelm@21646
   136
val is_internal = has_internal o Thm.get_tags;
wenzelm@18801
   137
wenzelm@18801
   138
wenzelm@18801
   139
wenzelm@4922
   140
(*** theorem database ***)
wenzelm@3987
   141
wenzelm@16441
   142
(** dataype theorems **)
wenzelm@3987
   143
wenzelm@26282
   144
datatype thms = Thms of
wenzelm@26282
   145
 {theorems: thm list NameSpace.table,   (* FIXME legacy *)
wenzelm@26282
   146
  all_facts: Facts.T};
wenzelm@26282
   147
wenzelm@26282
   148
fun make_thms theorems all_facts = Thms {theorems = theorems, all_facts = all_facts};
wenzelm@26282
   149
wenzelm@16441
   150
structure TheoremsData = TheoryDataFun
wenzelm@24713
   151
(
wenzelm@26282
   152
  type T = thms ref;   (* FIXME legacy *)
wenzelm@26282
   153
  val empty = ref (make_thms NameSpace.empty_table Facts.empty);
wenzelm@6547
   154
  fun copy (ref x) = ref x;
wenzelm@26282
   155
  fun extend (ref (Thms {theorems = _, all_facts})) = ref (make_thms NameSpace.empty_table all_facts);
wenzelm@26282
   156
  fun merge _
wenzelm@26282
   157
     (ref (Thms {theorems = _, all_facts = all_facts1}),
wenzelm@26282
   158
      ref (Thms {theorems = _, all_facts = all_facts2})) =
wenzelm@26282
   159
    ref (make_thms NameSpace.empty_table (Facts.merge (all_facts1, all_facts2)));
wenzelm@24713
   160
);
wenzelm@3987
   161
wenzelm@16493
   162
val get_theorems_ref = TheoremsData.get;
wenzelm@26282
   163
val get_theorems = (fn Thms args => args) o ! o get_theorems_ref;
wenzelm@17162
   164
val theorems_of = #theorems o get_theorems;
wenzelm@26282
   165
val all_facts_of = #all_facts o get_theorems;
wenzelm@16023
   166
wenzelm@6367
   167
wenzelm@3987
   168
wenzelm@4022
   169
(** retrieve theorems **)
wenzelm@3987
   170
skalberg@15531
   171
fun the_thms _ (SOME thms) = thms
skalberg@15531
   172
  | the_thms name NONE = error ("Unknown theorem(s) " ^ quote name);
wenzelm@4037
   173
wenzelm@26292
   174
local
wenzelm@26292
   175
wenzelm@26292
   176
fun lookup_thms thy xname =
wenzelm@9564
   177
  let
wenzelm@16493
   178
    val (space, thms) = #theorems (get_theorems thy);
wenzelm@26292
   179
    val name = NameSpace.intern space xname;
wenzelm@26292
   180
  in Option.map (pair name) (Symtab.lookup thms name) end;
wenzelm@26292
   181
wenzelm@26292
   182
fun lookup_fact thy xname =
wenzelm@26292
   183
  let
wenzelm@26292
   184
    val facts = all_facts_of thy;
wenzelm@26292
   185
    val name = NameSpace.intern (Facts.space_of facts) xname;
wenzelm@26292
   186
  in Option.map (pair name) (Facts.lookup facts name) end;
wenzelm@26292
   187
wenzelm@26292
   188
fun show_result NONE = "none"
wenzelm@26292
   189
  | show_result (SOME (name, _)) = quote name;
wenzelm@26292
   190
wenzelm@26344
   191
fun get silent theory thmref =
wenzelm@26292
   192
  let
wenzelm@26336
   193
    val name = Facts.name_of_ref thmref;
wenzelm@26292
   194
    val new_res = lookup_fact theory name;
wenzelm@26292
   195
    val old_res = get_first (fn thy => lookup_thms thy name) (theory :: Theory.ancestors_of theory);
wenzelm@26292
   196
    val is_same =
wenzelm@26292
   197
      (case (new_res, old_res) of
wenzelm@26292
   198
        (NONE, NONE) => true
wenzelm@26292
   199
      | (SOME (name1, ths1), SOME (name2, ths2)) => name1 = name2 andalso Thm.eq_thms (ths1, ths2)
wenzelm@26292
   200
      | _ => false);
wenzelm@26292
   201
    val _ =
wenzelm@26320
   202
      if is_same orelse silent then ()
wenzelm@26292
   203
      else legacy_feature ("Fact lookup differs from old-style thm database:\n" ^
wenzelm@26292
   204
        show_result new_res ^ " vs " ^ show_result old_res ^
wenzelm@26292
   205
        Position.str_of (Position.thread_data ()));
wenzelm@26336
   206
  in Option.map #2 old_res |> the_thms name |> Facts.select thmref |> map (Thm.transfer theory) end;
wenzelm@4022
   207
wenzelm@26320
   208
in
wenzelm@26320
   209
wenzelm@26344
   210
val get_fact_silent = get true;
wenzelm@26344
   211
val get_fact = get false;
wenzelm@26344
   212
wenzelm@26361
   213
fun get_thms thy = get_fact thy o Facts.named;
wenzelm@26344
   214
fun get_thm thy name = Facts.the_single name (get_thms thy name);
wenzelm@4783
   215
wenzelm@26292
   216
end;
wenzelm@26292
   217
wenzelm@4022
   218
wenzelm@16336
   219
(* thms_of etc. *)
kleing@15882
   220
wenzelm@16023
   221
fun thms_of thy =
wenzelm@17162
   222
  let val thms = #2 (theorems_of thy)
wenzelm@21646
   223
  in map (`(get_name_hint)) (maps snd (Symtab.dest thms)) end;
wenzelm@15703
   224
wenzelm@19482
   225
fun all_thms_of thy = maps thms_of (thy :: Theory.ancestors_of thy);
wenzelm@16336
   226
wenzelm@4022
   227
wenzelm@4022
   228
wenzelm@4022
   229
(** store theorems **)                    (*DESTRUCTIVE*)
wenzelm@3987
   230
wenzelm@16441
   231
(* hiding -- affects current theory node only *)
wenzelm@12695
   232
wenzelm@23933
   233
fun hide_thms fully names thy = CRITICAL (fn () =>
wenzelm@12695
   234
  let
wenzelm@26282
   235
    val r as ref (Thms {theorems = (space, thms), all_facts}) = get_theorems_ref thy;
wenzelm@16132
   236
    val space' = fold (NameSpace.hide fully) names space;
wenzelm@26282
   237
  in r := make_thms (space', thms) all_facts; thy end);
wenzelm@12695
   238
wenzelm@12695
   239
wenzelm@21580
   240
(* fact specifications *)
wenzelm@21580
   241
wenzelm@21580
   242
fun map_facts f = map (apsnd (map (apfst (map f))));
wenzelm@21580
   243
fun burrow_fact f = split_list #>> burrow f #> op ~~;
wenzelm@21580
   244
fun burrow_facts f = split_list ##> burrow (burrow_fact f) #> op ~~;
wenzelm@21580
   245
wenzelm@21580
   246
wenzelm@4853
   247
(* naming *)
wenzelm@4853
   248
wenzelm@18614
   249
fun gen_names _ len "" = replicate len ""
wenzelm@18614
   250
  | gen_names j len name = map (fn i => name ^ "_" ^ string_of_int i) (j + 1 upto j + len);
wenzelm@4853
   251
wenzelm@18801
   252
fun name_multi name [x] = [(name, x)]
wenzelm@18801
   253
  | name_multi name xs = gen_names 0 (length xs) name ~~ xs;
berghofe@12235
   254
wenzelm@21646
   255
fun name_thm pre official name thm = thm
wenzelm@21646
   256
  |> (if Thm.get_name thm <> "" andalso pre orelse not official then I else Thm.put_name name)
wenzelm@26050
   257
  |> (if has_name_hint thm andalso pre orelse name = "" then I else put_name_hint name)
wenzelm@26050
   258
  |> Thm.map_tags (Position.default_properties (Position.thread_data ()));
berghofe@12872
   259
wenzelm@21646
   260
fun name_thms pre official name xs =
wenzelm@21646
   261
  map (uncurry (name_thm pre official)) (name_multi name xs);
berghofe@12235
   262
wenzelm@21646
   263
fun name_thmss official name fact =
wenzelm@21646
   264
  burrow_fact (name_thms true official name) fact;
wenzelm@4853
   265
wenzelm@4853
   266
berghofe@11998
   267
(* enter_thms *)
wenzelm@4853
   268
wenzelm@7470
   269
fun warn_overwrite name = warning ("Replaced old copy of theorems " ^ quote name);
wenzelm@7470
   270
fun warn_same name = warning ("Theorem database already contains a copy of " ^ quote name);
wenzelm@3987
   271
haftmann@18418
   272
fun enter_thms _ _ app_att ("", thms) thy = app_att (thy, thms) |> swap
wenzelm@23933
   273
  | enter_thms pre_name post_name app_att (bname, thms) thy = CRITICAL (fn () =>
wenzelm@7470
   274
      let
wenzelm@16441
   275
        val name = Sign.full_name thy bname;
wenzelm@16441
   276
        val (thy', thms') = apsnd (post_name name) (app_att (thy, pre_name name thms));
wenzelm@26282
   277
        val r as ref (Thms {theorems = (space, theorems), all_facts}) = get_theorems_ref thy';
wenzelm@16513
   278
        val space' = Sign.declare_name thy' name space;
wenzelm@17418
   279
        val theorems' = Symtab.update (name, thms') theorems;
wenzelm@26308
   280
        val all_facts' = Facts.add_global (Sign.naming_of thy') (name, thms') all_facts;
wenzelm@13274
   281
      in
wenzelm@17418
   282
        (case Symtab.lookup theorems name of
skalberg@15531
   283
          NONE => ()
wenzelm@16441
   284
        | SOME thms'' =>
wenzelm@16441
   285
            if Thm.eq_thms (thms', thms'') then warn_same name
wenzelm@13274
   286
            else warn_overwrite name);
wenzelm@26282
   287
        r := make_thms (space', theorems') all_facts';
haftmann@18418
   288
        (thms', thy')
wenzelm@23933
   289
      end);
wenzelm@3987
   290
wenzelm@16023
   291
wenzelm@6091
   292
(* add_thms(s) *)
wenzelm@4853
   293
wenzelm@16441
   294
fun add_thms_atts pre_name ((bname, thms), atts) =
wenzelm@21646
   295
  enter_thms pre_name (name_thms false true)
wenzelm@21646
   296
    (foldl_map (Thm.theory_attributes atts)) (bname, thms);
wenzelm@4853
   297
haftmann@18377
   298
fun gen_add_thmss pre_name =
haftmann@18377
   299
  fold_map (add_thms_atts pre_name);
wenzelm@5907
   300
berghofe@12235
   301
fun gen_add_thms pre_name args =
haftmann@18377
   302
  apfst (map hd) o gen_add_thmss pre_name (map (apfst (apsnd single)) args);
berghofe@12235
   303
wenzelm@21646
   304
val add_thmss = gen_add_thmss (name_thms true true);
wenzelm@21646
   305
val add_thms = gen_add_thms (name_thms true true);
wenzelm@5907
   306
wenzelm@5907
   307
wenzelm@14564
   308
(* note_thmss(_i) *)
wenzelm@5907
   309
wenzelm@9192
   310
local
wenzelm@12711
   311
wenzelm@25981
   312
fun gen_note_thmss get tag = fold_map (fn ((bname, more_atts), ths_atts) => fn thy =>
wenzelm@12711
   313
  let
wenzelm@18728
   314
    fun app (x, (ths, atts)) = foldl_map (Thm.theory_attributes atts) (x, ths);
haftmann@18418
   315
    val (thms, thy') = thy |> enter_thms
wenzelm@21646
   316
      (name_thmss true) (name_thms false true) (apsnd flat o foldl_map app)
wenzelm@25981
   317
      (bname, map (fn (ths, atts) => (get thy ths, surround tag (atts @ more_atts))) ths_atts);
wenzelm@18801
   318
  in ((bname, thms), thy') end);
wenzelm@12711
   319
wenzelm@9192
   320
in
wenzelm@12711
   321
wenzelm@26344
   322
fun note_thmss k = gen_note_thmss get_fact (kind k);
wenzelm@25981
   323
fun note_thmss_i k = gen_note_thmss (K I) (kind k);
wenzelm@25981
   324
fun note_thmss_grouped k g = gen_note_thmss (K I) (kind k #> group g);
wenzelm@12711
   325
wenzelm@21438
   326
end;
wenzelm@21438
   327
haftmann@25598
   328
fun note kind (name, thm) =
haftmann@25598
   329
  note_thmss_i kind [((name, []), [([thm], [])])]
haftmann@25598
   330
  #>> (fn [(_, [thm])] => thm);
haftmann@25598
   331
wenzelm@18801
   332
fun note_thmss_qualified k path facts thy =
wenzelm@18801
   333
  thy
wenzelm@22796
   334
  |> Sign.add_path path
wenzelm@22796
   335
  |> Sign.no_base_names
wenzelm@18801
   336
  |> note_thmss_i k facts
wenzelm@22796
   337
  ||> Sign.restore_naming thy;
wenzelm@18801
   338
wenzelm@5280
   339
wenzelm@6091
   340
(* store_thm *)
wenzelm@5280
   341
berghofe@11998
   342
fun store_thm ((bname, thm), atts) thy =
wenzelm@21646
   343
  let val ([th'], thy') = add_thms_atts (name_thms true true) ((bname, [thm]), atts) thy
haftmann@18358
   344
  in (th', thy') end;
wenzelm@3987
   345
wenzelm@3987
   346
wenzelm@16441
   347
(* smart_store_thms(_open) *)
wenzelm@3987
   348
wenzelm@16441
   349
local
wenzelm@16441
   350
wenzelm@16441
   351
fun smart_store _ (name, []) =
berghofe@11516
   352
      error ("Cannot store empty list of theorems: " ^ quote name)
wenzelm@21646
   353
  | smart_store official (name, [thm]) =
wenzelm@21646
   354
      fst (enter_thms (name_thms true official) (name_thms false official) I (name, [thm])
wenzelm@21646
   355
        (Thm.theory_of_thm thm))
wenzelm@21646
   356
  | smart_store official (name, thms) =
wenzelm@21646
   357
      let val thy = Theory.merge_list (map Thm.theory_of_thm thms) in
wenzelm@21646
   358
        fst (enter_thms (name_thms true official) (name_thms false official) I (name, thms) thy)
wenzelm@21646
   359
      end;
berghofe@11516
   360
wenzelm@16441
   361
in
wenzelm@16441
   362
wenzelm@21646
   363
val smart_store_thms = smart_store true;
wenzelm@21646
   364
val smart_store_thms_open = smart_store false;
wenzelm@16441
   365
wenzelm@16441
   366
end;
wenzelm@3987
   367
wenzelm@3987
   368
wenzelm@16722
   369
(* forall_elim_var(s) -- belongs to drule.ML *)
wenzelm@7899
   370
wenzelm@16722
   371
fun forall_elim_vars_aux strip_vars i th =
wenzelm@16722
   372
  let
wenzelm@16722
   373
    val {thy, tpairs, prop, ...} = Thm.rep_thm th;
haftmann@16787
   374
    val add_used = Term.fold_aterms
haftmann@20853
   375
      (fn Var ((x, j), _) => if i = j then insert (op =) x else I | _ => I);
wenzelm@16722
   376
    val used = fold (fn (t, u) => add_used t o add_used u) tpairs (add_used prop []);
wenzelm@16722
   377
    val vars = strip_vars prop;
wenzelm@20071
   378
    val cvars = (Name.variant_list used (map #1 vars), vars)
wenzelm@16722
   379
      |> ListPair.map (fn (x, (_, T)) => Thm.cterm_of thy (Var ((x, i), T)));
wenzelm@16722
   380
  in fold Thm.forall_elim cvars th end;
wenzelm@7899
   381
wenzelm@16722
   382
val forall_elim_vars = forall_elim_vars_aux Term.strip_all_vars;
wenzelm@16722
   383
wenzelm@16722
   384
fun forall_elim_var i th = forall_elim_vars_aux
wenzelm@16722
   385
  (fn Const ("all", _) $ Abs (a, T, _) => [(a, T)]
wenzelm@16722
   386
  | _ => raise THM ("forall_elim_vars", i, [th])) i th;
wenzelm@7899
   387
wenzelm@7899
   388
wenzelm@4022
   389
(* store axioms as theorems *)
wenzelm@4022
   390
wenzelm@4853
   391
local
wenzelm@17418
   392
  fun get_ax thy (name, _) = Thm.get_axiom_i thy (Sign.full_name thy name);
wenzelm@17418
   393
  fun get_axs thy named_axs = map (forall_elim_vars 0 o get_ax thy) named_axs;
haftmann@18377
   394
  fun add_single add ((name, ax), atts) thy =
wenzelm@4853
   395
    let
berghofe@11998
   396
      val named_ax = [(name, ax)];
wenzelm@7753
   397
      val thy' = add named_ax thy;
wenzelm@7753
   398
      val thm = hd (get_axs thy' named_ax);
haftmann@18377
   399
    in apfst hd (gen_add_thms (K I) [((name, thm), atts)] thy') end;
haftmann@18377
   400
  fun add_multi add ((name, axs), atts) thy =
wenzelm@7753
   401
    let
wenzelm@7753
   402
      val named_axs = name_multi name axs;
wenzelm@4853
   403
      val thy' = add named_axs thy;
wenzelm@7753
   404
      val thms = get_axs thy' named_axs;
haftmann@18377
   405
    in apfst hd (gen_add_thmss (K I) [((name, thms), atts)] thy') end;
haftmann@18377
   406
  fun add_singles add = fold_map (add_single add);
haftmann@18377
   407
  fun add_multis add = fold_map (add_multi add);
wenzelm@4853
   408
in
wenzelm@19629
   409
  val add_axioms           = add_singles Theory.add_axioms;
wenzelm@19629
   410
  val add_axioms_i         = add_singles Theory.add_axioms_i;
wenzelm@19629
   411
  val add_axiomss          = add_multis Theory.add_axioms;
wenzelm@19629
   412
  val add_axiomss_i        = add_multis Theory.add_axioms_i;
wenzelm@22796
   413
  val add_defs             = add_singles o Theory.add_defs false;
wenzelm@22796
   414
  val add_defs_i           = add_singles o Theory.add_defs_i false;
wenzelm@22796
   415
  val add_defs_unchecked   = add_singles o Theory.add_defs true;
wenzelm@22796
   416
  val add_defs_unchecked_i = add_singles o Theory.add_defs_i true;
wenzelm@4853
   417
end;
wenzelm@4022
   418
wenzelm@4022
   419
wenzelm@3987
   420
wenzelm@5091
   421
(*** the ProtoPure theory ***)
wenzelm@3987
   422
wenzelm@24243
   423
val typ = SimpleSyntax.read_typ;
wenzelm@24243
   424
val term = SimpleSyntax.read_term;
wenzelm@24243
   425
val prop = SimpleSyntax.read_prop;
wenzelm@24243
   426
wenzelm@24243
   427
val appl_syntax =
wenzelm@24243
   428
 [("_appl", typ "('b => 'a) => args => logic", Mixfix ("(1_/(1'(_')))", [1000, 0], 1000)),
wenzelm@24243
   429
  ("_appl", typ "('b => 'a) => args => aprop", Mixfix ("(1_/(1'(_')))", [1000, 0], 1000))];
wenzelm@24243
   430
wenzelm@24243
   431
val applC_syntax =
wenzelm@24243
   432
 [("",       typ "'a => cargs",                  Delimfix "_"),
wenzelm@24243
   433
  ("_cargs", typ "'a => cargs => cargs",         Mixfix ("_/ _", [1000, 1000], 1000)),
wenzelm@24243
   434
  ("_applC", typ "('b => 'a) => cargs => logic", Mixfix ("(1_/ _)", [1000, 1000], 999)),
wenzelm@24243
   435
  ("_applC", typ "('b => 'a) => cargs => aprop", Mixfix ("(1_/ _)", [1000, 1000], 999))];
wenzelm@16441
   436
wenzelm@3987
   437
val proto_pure =
wenzelm@16493
   438
  Context.pre_pure_thy
wenzelm@16987
   439
  |> Compress.init_data
wenzelm@16023
   440
  |> TheoremsData.init
wenzelm@22796
   441
  |> Sign.add_types
wenzelm@4922
   442
   [("fun", 2, NoSyn),
wenzelm@4922
   443
    ("prop", 0, NoSyn),
wenzelm@4922
   444
    ("itself", 1, NoSyn),
wenzelm@4922
   445
    ("dummy", 0, NoSyn)]
wenzelm@22796
   446
  |> Sign.add_nonterminals Syntax.basic_nonterms
wenzelm@24243
   447
  |> Sign.add_syntax_i
wenzelm@24243
   448
   [("_lambda",     typ "pttrns => 'a => logic",     Mixfix ("(3%_./ _)", [0, 3], 3)),
wenzelm@24243
   449
    ("_abs",        typ "'a",                        NoSyn),
wenzelm@24243
   450
    ("",            typ "'a => args",                Delimfix "_"),
wenzelm@24243
   451
    ("_args",       typ "'a => args => args",        Delimfix "_,/ _"),
wenzelm@24243
   452
    ("",            typ "id => idt",                 Delimfix "_"),
wenzelm@24243
   453
    ("_idtdummy",   typ "idt",                       Delimfix "'_"),
wenzelm@24243
   454
    ("_idtyp",      typ "id => type => idt",         Mixfix ("_::_", [], 0)),
wenzelm@24243
   455
    ("_idtypdummy", typ "type => idt",               Mixfix ("'_()::_", [], 0)),
wenzelm@24243
   456
    ("",            typ "idt => idt",                Delimfix "'(_')"),
wenzelm@24243
   457
    ("",            typ "idt => idts",               Delimfix "_"),
wenzelm@24243
   458
    ("_idts",       typ "idt => idts => idts",       Mixfix ("_/ _", [1, 0], 0)),
wenzelm@24243
   459
    ("",            typ "idt => pttrn",              Delimfix "_"),
wenzelm@24243
   460
    ("",            typ "pttrn => pttrns",           Delimfix "_"),
wenzelm@24243
   461
    ("_pttrns",     typ "pttrn => pttrns => pttrns", Mixfix ("_/ _", [1, 0], 0)),
wenzelm@24243
   462
    ("",            typ "id => aprop",               Delimfix "_"),
wenzelm@24243
   463
    ("",            typ "longid => aprop",           Delimfix "_"),
wenzelm@24243
   464
    ("",            typ "var => aprop",              Delimfix "_"),
wenzelm@24243
   465
    ("_DDDOT",      typ "aprop",                     Delimfix "..."),
wenzelm@24243
   466
    ("_aprop",      typ "aprop => prop",             Delimfix "PROP _"),
wenzelm@24243
   467
    ("_asm",        typ "prop => asms",              Delimfix "_"),
wenzelm@24243
   468
    ("_asms",       typ "prop => asms => asms",      Delimfix "_;/ _"),
wenzelm@24243
   469
    ("_bigimpl",    typ "asms => prop => prop",      Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
wenzelm@24243
   470
    ("_ofclass",    typ "type => logic => prop",     Delimfix "(1OFCLASS/(1'(_,/ _')))"),
wenzelm@24243
   471
    ("_mk_ofclass", typ "dummy",                     NoSyn),
wenzelm@24243
   472
    ("_TYPE",       typ "type => logic",             Delimfix "(1TYPE/(1'(_')))"),
wenzelm@24243
   473
    ("",            typ "id => logic",               Delimfix "_"),
wenzelm@24243
   474
    ("",            typ "longid => logic",           Delimfix "_"),
wenzelm@24243
   475
    ("",            typ "var => logic",              Delimfix "_"),
wenzelm@24243
   476
    ("_DDDOT",      typ "logic",                     Delimfix "..."),
wenzelm@24243
   477
    ("_constify",   typ "num => num_const",          Delimfix "_"),
wenzelm@24243
   478
    ("_indexnum",   typ "num_const => index",        Delimfix "\\<^sub>_"),
wenzelm@24243
   479
    ("_index",      typ "logic => index",            Delimfix "(00\\<^bsub>_\\<^esub>)"),
wenzelm@24243
   480
    ("_indexdefault", typ "index",                   Delimfix ""),
wenzelm@24243
   481
    ("_indexvar",   typ "index",                     Delimfix "'\\<index>"),
wenzelm@24243
   482
    ("_struct",     typ "index => logic",            Mixfix ("\\<struct>_", [1000], 1000)),
wenzelm@24243
   483
    ("==>",         typ "prop => prop => prop",      Delimfix "op ==>"),
wenzelm@24243
   484
    (Term.dummy_patternN, typ "aprop",               Delimfix "'_")]
wenzelm@24243
   485
  |> Sign.add_syntax_i appl_syntax
wenzelm@24243
   486
  |> Sign.add_modesyntax_i (Symbol.xsymbolsN, true)
wenzelm@24243
   487
   [("fun",      typ "type => type => type",   Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
wenzelm@24243
   488
    ("_bracket", typ "types => type => type",  Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
wenzelm@24243
   489
    ("_ofsort",  typ "tid => sort => type",    Mixfix ("_\\<Colon>_", [1000, 0], 1000)),
wenzelm@24243
   490
    ("_constrain", typ "'a => type => 'a",     Mixfix ("_\\<Colon>_", [4, 0], 3)),
wenzelm@24243
   491
    ("_idtyp",    typ "id => type => idt",     Mixfix ("_\\<Colon>_", [], 0)),
wenzelm@24243
   492
    ("_idtypdummy", typ "type => idt",         Mixfix ("'_()\\<Colon>_", [], 0)),
wenzelm@24243
   493
    ("_type_constraint_", typ "'a",            NoSyn),
wenzelm@24243
   494
    ("_lambda",  typ "pttrns => 'a => logic",  Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
wenzelm@24243
   495
    ("==",       typ "'a => 'a => prop",       InfixrName ("\\<equiv>", 2)),
wenzelm@24243
   496
    ("all_binder", typ "idts => prop => prop", Mixfix ("(3\\<And>_./ _)", [0, 0], 0)),
wenzelm@24243
   497
    ("==>",      typ "prop => prop => prop",   InfixrName ("\\<Longrightarrow>", 1)),
wenzelm@24243
   498
    ("_DDDOT",   typ "aprop",                  Delimfix "\\<dots>"),
wenzelm@24243
   499
    ("_bigimpl", typ "asms => prop => prop",   Mixfix ("((1\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
wenzelm@24243
   500
    ("_DDDOT",   typ "logic",                  Delimfix "\\<dots>")]
wenzelm@24243
   501
  |> Sign.add_modesyntax_i ("", false)
wenzelm@24243
   502
   [("prop", typ "prop => prop", Mixfix ("_", [0], 0)),
wenzelm@24243
   503
    ("ProtoPure.term", typ "'a => prop", Delimfix "TERM _"),
wenzelm@24243
   504
    ("ProtoPure.conjunction", typ "prop => prop => prop", InfixrName ("&&", 2))]
wenzelm@24243
   505
  |> Sign.add_modesyntax_i ("HTML", false)
wenzelm@24243
   506
   [("_lambda", typ "pttrns => 'a => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3))]
wenzelm@24243
   507
  |> Sign.add_consts_i
wenzelm@24243
   508
   [("==", typ "'a => 'a => prop", InfixrName ("==", 2)),
wenzelm@24243
   509
    ("==>", typ "prop => prop => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
wenzelm@24243
   510
    ("all", typ "('a => prop) => prop", Binder ("!!", 0, 0)),
wenzelm@24243
   511
    ("prop", typ "prop => prop", NoSyn),
wenzelm@24243
   512
    ("TYPE", typ "'a itself", NoSyn),
wenzelm@24243
   513
    (Term.dummy_patternN, typ "'a", Delimfix "'_")]
wenzelm@25018
   514
  |> Theory.add_deps "==" ("==", typ "'a => 'a => prop") []
wenzelm@25018
   515
  |> Theory.add_deps "==>" ("==>", typ "prop => prop => prop") []
wenzelm@25018
   516
  |> Theory.add_deps "all" ("all", typ "('a => prop) => prop") []
wenzelm@25018
   517
  |> Theory.add_deps "TYPE" ("TYPE", typ "'a itself") []
wenzelm@25018
   518
  |> Theory.add_deps Term.dummy_patternN (Term.dummy_patternN, typ "'a") []
wenzelm@22796
   519
  |> Sign.add_trfuns Syntax.pure_trfuns
wenzelm@22796
   520
  |> Sign.add_trfunsT Syntax.pure_trfunsT
wenzelm@16441
   521
  |> Sign.local_path
wenzelm@24243
   522
  |> Sign.add_consts_i
wenzelm@24243
   523
   [("term", typ "'a => prop", NoSyn),
wenzelm@24243
   524
    ("conjunction", typ "prop => prop => prop", NoSyn)]
wenzelm@24243
   525
  |> (add_defs_i false o map Thm.no_attributes)
wenzelm@24243
   526
   [("prop_def", prop "(CONST prop :: prop => prop) (A::prop) == A::prop"),
wenzelm@24243
   527
    ("term_def", prop "(CONST ProtoPure.term :: 'a => prop) (x::'a) == (!!A::prop. A ==> A)"),
wenzelm@24243
   528
    ("conjunction_def", prop "(A && B) == (!!C::prop. (A ==> B ==> C) ==> C)")] |> snd
wenzelm@19775
   529
  |> Sign.hide_consts false ["conjunction", "term"]
haftmann@18377
   530
  |> add_thmss [(("nothing", []), [])] |> snd
berghofe@11516
   531
  |> Theory.add_axioms_i Proofterm.equality_axms
wenzelm@16493
   532
  |> Theory.end_theory;
wenzelm@3987
   533
wenzelm@5091
   534
structure ProtoPure =
wenzelm@5091
   535
struct
wenzelm@5091
   536
  val thy = proto_pure;
wenzelm@18031
   537
  val prop_def = get_axiom thy "prop_def";
wenzelm@19775
   538
  val term_def = get_axiom thy "term_def";
wenzelm@19125
   539
  val conjunction_def = get_axiom thy "conjunction_def";
wenzelm@5091
   540
end;
wenzelm@3987
   541
wenzelm@3987
   542
end;
wenzelm@3987
   543
wenzelm@4022
   544
structure BasicPureThy: BASIC_PURE_THY = PureThy;
wenzelm@4022
   545
open BasicPureThy;