src/Pure/Isar/locale.ML
author wenzelm
Thu Mar 19 13:28:55 2009 +0100 (2009-03-19)
changeset 30585 6b2ba4666336
parent 30515 bca05b17b618
child 30725 c23a5b3cd1b9
permissions -rw-r--r--
use Name.of_binding for basic logical entities without name space (fixes, case names etc.);
haftmann@29361
     1
(*  Title:      Pure/Isar/locale.ML
haftmann@29361
     2
    Author:     Clemens Ballarin, TU Muenchen
haftmann@29361
     3
haftmann@29361
     4
Locales -- Isar proof contexts as meta-level predicates, with local
haftmann@29361
     5
syntax and implicit structures.
haftmann@29361
     6
haftmann@29361
     7
Draws basic ideas from Florian Kammueller's original version of
haftmann@29361
     8
locales, but uses the richer infrastructure of Isar instead of the raw
haftmann@29361
     9
meta-logic.  Furthermore, structured import of contexts (with merge
haftmann@29361
    10
and rename operations) are provided, as well as type-inference of the
haftmann@29361
    11
signature parts, and predicate definitions of the specification text.
haftmann@29361
    12
haftmann@29361
    13
Interpretation enables the reuse of theorems of locales in other
haftmann@29361
    14
contexts, namely those defined by theories, structured proofs and
haftmann@29361
    15
locales themselves.
haftmann@29361
    16
haftmann@29361
    17
See also:
haftmann@29361
    18
haftmann@29361
    19
[1] Clemens Ballarin. Locales and Locale Expressions in Isabelle/Isar.
haftmann@29361
    20
    In Stefano Berardi et al., Types for Proofs and Programs: International
haftmann@29361
    21
    Workshop, TYPES 2003, Torino, Italy, LNCS 3085, pages 34-50, 2004.
haftmann@29361
    22
[2] Clemens Ballarin. Interpretation of Locales in Isabelle: Managing
haftmann@29361
    23
    Dependencies between Locales. Technical Report TUM-I0607, Technische
haftmann@29361
    24
    Universitaet Muenchen, 2006.
haftmann@29361
    25
[3] Clemens Ballarin. Interpretation of Locales in Isabelle: Theories and
haftmann@29361
    26
    Proof Contexts. In J.M. Borwein and W.M. Farmer, MKM 2006, LNAI 4108,
haftmann@29361
    27
    pages 31-43, 2006.
haftmann@29361
    28
*)
haftmann@29361
    29
haftmann@29361
    30
signature LOCALE =
haftmann@29361
    31
sig
haftmann@29576
    32
  (* Locale specification *)
wenzelm@30344
    33
  val register_locale: binding ->
haftmann@29576
    34
    (string * sort) list * (binding * typ option * mixfix) list ->
haftmann@29361
    35
    term option * term list ->
haftmann@29441
    36
    thm option * thm option -> thm list ->
haftmann@29361
    37
    (declaration * stamp) list * (declaration * stamp) list ->
haftmann@29361
    38
    ((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * stamp) list ->
haftmann@29544
    39
    ((string * morphism) * stamp) list -> theory -> theory
haftmann@29361
    40
  val intern: theory -> xstring -> string
haftmann@29361
    41
  val extern: theory -> string -> xstring
haftmann@29392
    42
  val defined: theory -> string -> bool
haftmann@29576
    43
  val params_of: theory -> string -> (binding * typ option * mixfix) list
haftmann@29441
    44
  val intros_of: theory -> string -> thm option * thm option
haftmann@29441
    45
  val axioms_of: theory -> string -> thm list
haftmann@29544
    46
  val instance_of: theory -> string -> morphism -> term list
haftmann@29361
    47
  val specification_of: theory -> string -> term option * term list
haftmann@29361
    48
  val declarations_of: theory -> string -> declaration list * declaration list
haftmann@29544
    49
  val dependencies_of: theory -> string -> (string * morphism) list
haftmann@29361
    50
haftmann@29361
    51
  (* Storing results *)
haftmann@29361
    52
  val add_thmss: string -> string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
haftmann@29361
    53
    Proof.context -> Proof.context
haftmann@29361
    54
  val add_type_syntax: string -> declaration -> Proof.context -> Proof.context
haftmann@29361
    55
  val add_term_syntax: string -> declaration -> Proof.context -> Proof.context
haftmann@29361
    56
  val add_declaration: string -> declaration -> Proof.context -> Proof.context
haftmann@29544
    57
  val add_dependency: string -> string * morphism -> theory -> theory
haftmann@29361
    58
haftmann@29361
    59
  (* Activation *)
haftmann@29544
    60
  val activate_declarations: theory -> string * morphism ->
haftmann@29361
    61
    Proof.context -> Proof.context
haftmann@29544
    62
  val activate_global_facts: string * morphism -> theory -> theory
haftmann@29544
    63
  val activate_local_facts: string * morphism -> Proof.context -> Proof.context
haftmann@29361
    64
  val init: string -> theory -> Proof.context
haftmann@29361
    65
haftmann@29361
    66
  (* Reasoning about locales *)
haftmann@29361
    67
  val witness_attrib: attribute
haftmann@29361
    68
  val intro_attrib: attribute
haftmann@29361
    69
  val unfold_attrib: attribute
haftmann@29361
    70
  val intro_locales_tac: bool -> Proof.context -> thm list -> tactic
haftmann@29361
    71
haftmann@29361
    72
  (* Registrations *)
haftmann@29544
    73
  val add_registration: string * (morphism * morphism) ->
haftmann@29361
    74
    theory -> theory
haftmann@29544
    75
  val amend_registration: morphism -> string * morphism ->
haftmann@29361
    76
    theory -> theory
haftmann@29544
    77
  val get_registrations: theory -> (string * morphism) list
haftmann@29361
    78
haftmann@29361
    79
  (* Diagnostic *)
haftmann@29361
    80
  val print_locales: theory -> unit
wenzelm@30344
    81
  val print_locale: theory -> bool -> xstring -> unit
haftmann@29361
    82
end;
haftmann@29361
    83
haftmann@29361
    84
haftmann@29361
    85
(*** Theorem list extensible via attribute --- to control intro_locales_tac ***)
haftmann@29361
    86
haftmann@29361
    87
(* FIXME: consider moving named_thms.ML up in the build hierarchy and using that *)
haftmann@29361
    88
functor ThmsFun() =
haftmann@29361
    89
struct
haftmann@29361
    90
haftmann@29361
    91
structure Data = GenericDataFun
haftmann@29361
    92
(
haftmann@29361
    93
  type T = thm list;
haftmann@29361
    94
  val empty = [];
haftmann@29361
    95
  val extend = I;
haftmann@29361
    96
  fun merge _ = Thm.merge_thms;
haftmann@29361
    97
);
haftmann@29361
    98
haftmann@29361
    99
val get = Data.get o Context.Proof;
haftmann@29361
   100
val add = Thm.declaration_attribute (Data.map o Thm.add_thm);
haftmann@29361
   101
haftmann@29361
   102
end;
haftmann@29361
   103
haftmann@29361
   104
haftmann@29361
   105
structure Locale: LOCALE =
haftmann@29361
   106
struct
haftmann@29361
   107
haftmann@29361
   108
datatype ctxt = datatype Element.ctxt;
haftmann@29361
   109
haftmann@29392
   110
haftmann@29392
   111
(*** Theory data ***)
haftmann@29361
   112
haftmann@29361
   113
datatype locale = Loc of {
haftmann@29392
   114
  (** static part **)
haftmann@29576
   115
  parameters: (string * sort) list * (binding * typ option * mixfix) list,
haftmann@29361
   116
    (* type and term parameters *)
haftmann@29361
   117
  spec: term option * term list,
haftmann@29361
   118
    (* assumptions (as a single predicate expression) and defines *)
haftmann@29441
   119
  intros: thm option * thm option,
haftmann@29441
   120
  axioms: thm list,
haftmann@29392
   121
  (** dynamic part **)
haftmann@29361
   122
  decls: (declaration * stamp) list * (declaration * stamp) list,
haftmann@29361
   123
    (* type and term syntax declarations *)
haftmann@29361
   124
  notes: ((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * stamp) list,
haftmann@29361
   125
    (* theorem declarations *)
haftmann@29544
   126
  dependencies: ((string * morphism) * stamp) list
haftmann@29361
   127
    (* locale dependencies (sublocale relation) *)
haftmann@29392
   128
};
haftmann@29361
   129
haftmann@29441
   130
fun mk_locale ((parameters, spec, intros, axioms), ((decls, notes), dependencies)) =
haftmann@29441
   131
  Loc {parameters = parameters, intros = intros, axioms = axioms, spec = spec,
haftmann@29441
   132
    decls = decls, notes = notes, dependencies = dependencies};
haftmann@29441
   133
fun map_locale f (Loc {parameters, spec, intros, axioms, decls, notes, dependencies}) =
haftmann@29441
   134
  mk_locale (f ((parameters, spec, intros, axioms), ((decls, notes), dependencies)));
haftmann@29441
   135
fun merge_locale (Loc {parameters, spec, intros, axioms, decls = (decls1, decls2),
haftmann@29441
   136
  notes, dependencies}, Loc {decls = (decls1', decls2'), notes = notes',
haftmann@29441
   137
    dependencies = dependencies', ...}) = mk_locale
haftmann@29441
   138
      ((parameters, spec, intros, axioms),
haftmann@29441
   139
        (((merge (eq_snd op =) (decls1, decls1'), merge (eq_snd op =) (decls2, decls2')),
haftmann@29441
   140
          merge (eq_snd op =) (notes, notes')),
haftmann@29441
   141
            merge (eq_snd op =) (dependencies, dependencies')));
haftmann@29361
   142
haftmann@29361
   143
structure LocalesData = TheoryDataFun
haftmann@29361
   144
(
haftmann@29392
   145
  type T = locale NameSpace.table;
haftmann@29361
   146
  val empty = NameSpace.empty_table;
haftmann@29361
   147
  val copy = I;
haftmann@29361
   148
  val extend = I;
haftmann@29392
   149
  fun merge _ = NameSpace.join_tables (K merge_locale);
haftmann@29361
   150
);
haftmann@29361
   151
haftmann@29361
   152
val intern = NameSpace.intern o #1 o LocalesData.get;
haftmann@29361
   153
val extern = NameSpace.extern o #1 o LocalesData.get;
haftmann@29361
   154
haftmann@29392
   155
fun get_locale thy = Symtab.lookup (#2 (LocalesData.get thy));
haftmann@29392
   156
haftmann@29392
   157
fun defined thy = is_some o get_locale thy;
haftmann@29361
   158
haftmann@29361
   159
fun the_locale thy name = case get_locale thy name
haftmann@29392
   160
 of SOME (Loc loc) => loc
haftmann@29361
   161
  | NONE => error ("Unknown locale " ^ quote name);
haftmann@29361
   162
wenzelm@30344
   163
fun register_locale binding parameters spec intros axioms decls notes dependencies thy =
wenzelm@30466
   164
  thy |> LocalesData.map (NameSpace.define (Sign.naming_of thy) (binding,
haftmann@29441
   165
    mk_locale ((parameters, spec, intros, axioms), ((decls, notes), dependencies))) #> snd);
haftmann@29361
   166
haftmann@29392
   167
fun change_locale name =
haftmann@29392
   168
  LocalesData.map o apsnd o Symtab.map_entry name o map_locale o apsnd;
haftmann@29361
   169
haftmann@29361
   170
fun print_locales thy =
haftmann@29392
   171
  Pretty.strs ("locales:" :: map #1 (NameSpace.extern_table (LocalesData.get thy)))
haftmann@29392
   172
  |> Pretty.writeln;
haftmann@29361
   173
haftmann@29361
   174
haftmann@29361
   175
(*** Primitive operations ***)
haftmann@29361
   176
haftmann@29392
   177
fun params_of thy = snd o #parameters o the_locale thy;
haftmann@29361
   178
haftmann@29441
   179
fun intros_of thy = #intros o the_locale thy;
haftmann@29441
   180
haftmann@29441
   181
fun axioms_of thy = #axioms o the_locale thy;
haftmann@29441
   182
haftmann@29392
   183
fun instance_of thy name morph = params_of thy name |>
wenzelm@30585
   184
  map ((fn (b, T, _) => Free (Name.of_binding b, the T)) #> Morphism.term morph);
haftmann@29361
   185
haftmann@29392
   186
fun specification_of thy = #spec o the_locale thy;
haftmann@29361
   187
haftmann@29392
   188
fun declarations_of thy name = the_locale thy name |>
haftmann@29392
   189
  #decls |> apfst (map fst) |> apsnd (map fst);
haftmann@29361
   190
haftmann@29544
   191
fun dependencies_of thy name = the_locale thy name |>
haftmann@29544
   192
  #dependencies |> map fst;
haftmann@29544
   193
haftmann@29361
   194
haftmann@29361
   195
(*** Activate context elements of locale ***)
haftmann@29361
   196
haftmann@29361
   197
(** Identifiers: activated locales in theory or proof context **)
haftmann@29361
   198
haftmann@29361
   199
type identifiers = (string * term list) list;
haftmann@29361
   200
haftmann@29361
   201
val empty = ([] : identifiers);
haftmann@29361
   202
haftmann@29361
   203
fun ident_eq thy ((n: string, ts), (m, ss)) = (m = n) andalso Pattern.matchess thy (ss, ts);
haftmann@29361
   204
haftmann@29361
   205
local
haftmann@29361
   206
haftmann@29361
   207
datatype 'a delayed = Ready of 'a | ToDo of ('a delayed * 'a delayed);
haftmann@29361
   208
haftmann@29361
   209
structure IdentifiersData = GenericDataFun
haftmann@29361
   210
(
haftmann@29361
   211
  type T = identifiers delayed;
haftmann@29361
   212
  val empty = Ready empty;
haftmann@29361
   213
  val extend = I;
haftmann@29361
   214
  fun merge _ = ToDo;
haftmann@29361
   215
);
haftmann@29361
   216
haftmann@29361
   217
in
haftmann@29361
   218
haftmann@29361
   219
fun finish thy (ToDo (i1, i2)) = merge (ident_eq thy) (finish thy i1, finish thy i2)
haftmann@29361
   220
  | finish _ (Ready ids) = ids;
haftmann@29361
   221
haftmann@29361
   222
val _ = Context.>> (Context.map_theory (Theory.at_begin (fn thy =>
haftmann@29361
   223
  (case IdentifiersData.get (Context.Theory thy) of
haftmann@29361
   224
    Ready _ => NONE |
haftmann@29361
   225
    ids => SOME (Context.theory_map (IdentifiersData.put (Ready (finish thy ids))) thy))
haftmann@29361
   226
  )));
haftmann@29361
   227
haftmann@29361
   228
fun get_global_idents thy =
haftmann@29361
   229
  let val (Ready ids) = (IdentifiersData.get o Context.Theory) thy in ids end;
haftmann@29361
   230
val put_global_idents = Context.theory_map o IdentifiersData.put o Ready;
haftmann@29361
   231
haftmann@29361
   232
fun get_local_idents ctxt =
haftmann@29361
   233
  let val (Ready ids) = (IdentifiersData.get o Context.Proof) ctxt in ids end;
haftmann@29361
   234
val put_local_idents = Context.proof_map o IdentifiersData.put o Ready;
haftmann@29361
   235
haftmann@29361
   236
end;
haftmann@29361
   237
haftmann@29361
   238
haftmann@29361
   239
(** Resolve locale dependencies in a depth-first fashion **)
haftmann@29361
   240
haftmann@29361
   241
local
haftmann@29361
   242
haftmann@29361
   243
val roundup_bound = 120;
haftmann@29361
   244
haftmann@29361
   245
fun add thy depth (name, morph) (deps, marked) =
haftmann@29361
   246
  if depth > roundup_bound
haftmann@29361
   247
  then error "Roundup bound exceeded (sublocale relation probably not terminating)."
haftmann@29361
   248
  else
haftmann@29361
   249
    let
haftmann@29392
   250
      val {parameters = (_, params), dependencies, ...} = the_locale thy name;
haftmann@29361
   251
      val instance = instance_of thy name morph;
haftmann@29361
   252
    in
haftmann@29361
   253
      if member (ident_eq thy) marked (name, instance)
haftmann@29361
   254
      then (deps, marked)
haftmann@29361
   255
      else
haftmann@29361
   256
        let
haftmann@29361
   257
          val dependencies' =
haftmann@29361
   258
            map (fn ((name, morph'), _) => (name, morph' $>  morph)) dependencies;
haftmann@29361
   259
          val marked' = (name, instance) :: marked;
haftmann@29361
   260
          val (deps', marked'') = fold_rev (add thy (depth + 1)) dependencies' ([], marked');
haftmann@29361
   261
        in
haftmann@29361
   262
          ((name, morph) :: deps' @ deps, marked'')
haftmann@29361
   263
        end
haftmann@29361
   264
    end;
haftmann@29361
   265
haftmann@29361
   266
in
haftmann@29361
   267
haftmann@29361
   268
fun roundup thy activate_dep (name, morph) (marked, input) =
haftmann@29361
   269
  let
haftmann@29361
   270
    (* Find all dependencies incuding new ones (which are dependencies enriching
haftmann@29361
   271
      existing registrations). *)
haftmann@29361
   272
    val (dependencies, marked') = add thy 0 (name, morph) ([], empty);
haftmann@29361
   273
    (* Filter out exisiting fragments. *)
haftmann@29361
   274
    val dependencies' = filter_out (fn (name, morph) =>
haftmann@29361
   275
      member (ident_eq thy) marked (name, instance_of thy name morph)) dependencies;
haftmann@29361
   276
  in
haftmann@29361
   277
    (merge (ident_eq thy) (marked, marked'), input |> fold_rev (activate_dep thy) dependencies')
haftmann@29361
   278
  end;
haftmann@29361
   279
haftmann@29361
   280
end;
haftmann@29361
   281
haftmann@29361
   282
haftmann@29361
   283
(* Declarations, facts and entire locale content *)
haftmann@29361
   284
haftmann@29361
   285
fun activate_decls thy (name, morph) ctxt =
haftmann@29361
   286
  let
haftmann@29392
   287
    val {decls = (typ_decls, term_decls), ...} = the_locale thy name;
haftmann@29361
   288
  in
haftmann@29361
   289
    ctxt |> fold_rev (fn (decl, _) => Context.proof_map (decl morph)) typ_decls |>
haftmann@29361
   290
      fold_rev (fn (decl, _) => Context.proof_map (decl morph)) term_decls
haftmann@29361
   291
  end;
haftmann@29361
   292
haftmann@29361
   293
fun activate_notes activ_elem transfer thy (name, morph) input =
haftmann@29361
   294
  let
haftmann@29392
   295
    val {notes, ...} = the_locale thy name;
haftmann@29361
   296
    fun activate ((kind, facts), _) input =
haftmann@29361
   297
      let
haftmann@29361
   298
        val facts' = facts |> Element.facts_map (Element.morph_ctxt (transfer input $> morph))
haftmann@29361
   299
      in activ_elem (Notes (kind, facts')) input end;
haftmann@29361
   300
  in
haftmann@29361
   301
    fold_rev activate notes input
haftmann@29361
   302
  end;
haftmann@29361
   303
haftmann@29361
   304
fun activate_all name thy activ_elem transfer (marked, input) =
haftmann@29361
   305
  let
haftmann@29392
   306
    val {parameters = (_, params), spec = (asm, defs), ...} =
haftmann@29361
   307
      the_locale thy name;
haftmann@29361
   308
  in
haftmann@29361
   309
    input |>
haftmann@29361
   310
      (if not (null params) then activ_elem (Fixes params) else I) |>
haftmann@29361
   311
      (* FIXME type parameters *)
haftmann@29361
   312
      (if is_some asm then activ_elem (Assumes [(Attrib.empty_binding, [(the asm, [])])]) else I) |>
haftmann@29361
   313
      (if not (null defs)
haftmann@29361
   314
        then activ_elem (Defines (map (fn def => (Attrib.empty_binding, (def, []))) defs))
haftmann@29361
   315
        else I) |>
haftmann@29361
   316
      pair marked |> roundup thy (activate_notes activ_elem transfer) (name, Morphism.identity)
haftmann@29361
   317
  end;
haftmann@29361
   318
haftmann@29361
   319
haftmann@29361
   320
(** Public activation functions **)
haftmann@29361
   321
haftmann@29361
   322
local
haftmann@29361
   323
haftmann@29361
   324
fun init_global_elem (Notes (kind, facts)) thy =
haftmann@29392
   325
  let
haftmann@29392
   326
    val facts' = Attrib.map_facts (Attrib.attribute_i thy) facts
wenzelm@30438
   327
  in PureThy.note_thmss kind facts' thy |> snd end
haftmann@29361
   328
haftmann@29361
   329
fun init_local_elem (Fixes fixes) ctxt = ctxt |>
haftmann@29361
   330
      ProofContext.add_fixes_i fixes |> snd
haftmann@29361
   331
  | init_local_elem (Assumes assms) ctxt =
haftmann@29361
   332
      let
haftmann@29361
   333
        val assms' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) assms
haftmann@29361
   334
      in
haftmann@29361
   335
        ctxt |> fold Variable.auto_fixes (maps (map fst o snd) assms') |>
haftmann@29361
   336
          ProofContext.add_assms_i Assumption.assume_export assms' |> snd
haftmann@29361
   337
     end
haftmann@29361
   338
  | init_local_elem (Defines defs) ctxt =
haftmann@29361
   339
      let
haftmann@29361
   340
        val defs' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) defs
haftmann@29361
   341
      in
haftmann@29361
   342
        ctxt |> fold Variable.auto_fixes (map (fst o snd) defs') |>
haftmann@29361
   343
          ProofContext.add_assms_i LocalDefs.def_export (map (fn (attn, t) => (attn, [t])) defs') |>
haftmann@29361
   344
          snd
haftmann@29361
   345
      end
haftmann@29361
   346
  | init_local_elem (Notes (kind, facts)) ctxt =
haftmann@29361
   347
      let
haftmann@29361
   348
        val facts' = Attrib.map_facts (Attrib.attribute_i (ProofContext.theory_of ctxt)) facts
wenzelm@30438
   349
      in ProofContext.note_thmss_i kind facts' ctxt |> snd end
haftmann@29361
   350
haftmann@29361
   351
fun cons_elem false (Notes notes) elems = elems
haftmann@29361
   352
  | cons_elem _ elem elems = elem :: elems
haftmann@29361
   353
haftmann@29361
   354
in
haftmann@29361
   355
haftmann@29361
   356
fun activate_declarations thy dep ctxt =
haftmann@29502
   357
  roundup thy activate_decls dep (get_local_idents ctxt, ctxt) |-> put_local_idents;
haftmann@29361
   358
haftmann@29361
   359
fun activate_global_facts dep thy =
haftmann@29361
   360
  roundup thy (activate_notes init_global_elem Element.transfer_morphism)
haftmann@29502
   361
    dep (get_global_idents thy, thy) |-> put_global_idents;
haftmann@29361
   362
haftmann@29361
   363
fun activate_local_facts dep ctxt =
haftmann@29361
   364
  roundup (ProofContext.theory_of ctxt)
haftmann@29361
   365
  (activate_notes init_local_elem (Element.transfer_morphism o ProofContext.theory_of)) dep
haftmann@29502
   366
    (get_local_idents ctxt, ctxt) |-> put_local_idents;
haftmann@29361
   367
haftmann@29361
   368
fun init name thy =
haftmann@29361
   369
  activate_all name thy init_local_elem (Element.transfer_morphism o ProofContext.theory_of)
haftmann@29502
   370
    (empty, ProofContext.init thy) |-> put_local_idents;
haftmann@29361
   371
haftmann@29361
   372
fun print_locale thy show_facts name =
haftmann@29361
   373
  let
haftmann@29361
   374
    val name' = intern thy name;
haftmann@29361
   375
    val ctxt = init name' thy
haftmann@29361
   376
  in
haftmann@29361
   377
    Pretty.big_list "locale elements:"
haftmann@29361
   378
      (activate_all name' thy (cons_elem show_facts) (K (Element.transfer_morphism thy))
haftmann@29361
   379
        (empty, []) |> snd |> rev |>
haftmann@29361
   380
        map (Element.pretty_ctxt ctxt) |> map Pretty.chunks) |> Pretty.writeln
haftmann@29361
   381
  end
haftmann@29361
   382
haftmann@29361
   383
end;
haftmann@29361
   384
haftmann@29361
   385
haftmann@29361
   386
(*** Registrations: interpretations in theories ***)
haftmann@29361
   387
haftmann@29392
   388
structure RegistrationsData = TheoryDataFun
haftmann@29361
   389
(
haftmann@29544
   390
  type T = ((string * (morphism * morphism)) * stamp) list;
haftmann@29392
   391
    (* FIXME mixins need to be stamped *)
haftmann@29361
   392
    (* registrations, in reverse order of declaration *)
haftmann@29361
   393
  val empty = [];
haftmann@29361
   394
  val extend = I;
haftmann@29392
   395
  val copy = I;
haftmann@29361
   396
  fun merge _ data : T = Library.merge (eq_snd op =) data;
haftmann@29361
   397
    (* FIXME consolidate with dependencies, consider one data slot only *)
haftmann@29361
   398
);
haftmann@29361
   399
haftmann@29392
   400
val get_registrations =
haftmann@29392
   401
  RegistrationsData.get #> map fst #> map (apsnd op $>);
haftmann@29361
   402
haftmann@29392
   403
fun add_registration (name, (base_morph, export)) thy =
haftmann@29392
   404
  roundup thy (fn _ => fn (name', morph') =>
haftmann@29392
   405
    (RegistrationsData.map o cons) ((name', (morph', export)), stamp ()))
haftmann@29502
   406
    (name, base_morph) (get_global_idents thy, thy) |> snd
haftmann@29502
   407
    (* FIXME |-> put_global_idents ?*);
haftmann@29361
   408
haftmann@29392
   409
fun amend_registration morph (name, base_morph) thy =
haftmann@29361
   410
  let
haftmann@29392
   411
    val regs = (RegistrationsData.get #> map fst) thy;
haftmann@29361
   412
    val base = instance_of thy name base_morph;
haftmann@29361
   413
    fun match (name', (morph', _)) =
haftmann@29361
   414
      name = name' andalso eq_list (op aconv) (base, instance_of thy name' morph');
haftmann@29361
   415
    val i = find_index match (rev regs);
haftmann@29392
   416
    val _ = if i = ~1 then error ("No registration of locale " ^
haftmann@29361
   417
        quote (extern thy name) ^ " and parameter instantiation " ^
haftmann@29361
   418
        space_implode " " (map (quote o Syntax.string_of_term_global thy) base) ^ " available.")
haftmann@29361
   419
      else ();
haftmann@29361
   420
  in
haftmann@29392
   421
    RegistrationsData.map (nth_map (length regs - 1 - i)
haftmann@29361
   422
      (fn ((name, (base, export)), stamp) => ((name, (base $> morph, export)), stamp))) thy
haftmann@29361
   423
  end;
haftmann@29361
   424
haftmann@29361
   425
haftmann@29502
   426
haftmann@29361
   427
(*** Storing results ***)
haftmann@29361
   428
haftmann@29361
   429
(* Theorems *)
haftmann@29361
   430
haftmann@29361
   431
fun add_thmss loc kind args ctxt =
haftmann@29361
   432
  let
haftmann@29361
   433
    val ([Notes args'], ctxt') = Element.activate_i [Notes (kind, args)] ctxt;
haftmann@29361
   434
    val ctxt'' = ctxt' |> ProofContext.theory (
haftmann@29392
   435
      (change_locale loc o apfst o apsnd) (cons (args', stamp ()))
haftmann@29392
   436
        #>
haftmann@29361
   437
      (* Registrations *)
haftmann@29361
   438
      (fn thy => fold_rev (fn (name, morph) =>
haftmann@29361
   439
            let
haftmann@29361
   440
              val args'' = snd args' |> Element.facts_map (Element.morph_ctxt morph) |>
haftmann@29361
   441
                Attrib.map_facts (Attrib.attribute_i thy)
wenzelm@30438
   442
            in PureThy.note_thmss kind args'' #> snd end)
haftmann@29392
   443
        (get_registrations thy |> filter (fn (name, _) => name = loc)) thy))
haftmann@29361
   444
  in ctxt'' end;
haftmann@29361
   445
haftmann@29361
   446
haftmann@29361
   447
(* Declarations *)
haftmann@29361
   448
haftmann@29361
   449
local
haftmann@29361
   450
haftmann@29361
   451
fun decl_attrib decl phi = Thm.declaration_attribute (K (decl phi));
haftmann@29361
   452
haftmann@29361
   453
fun add_decls add loc decl =
wenzelm@30223
   454
  ProofContext.theory ((change_locale loc o apfst o apfst) (add (decl, stamp ()))) #>
haftmann@29361
   455
  add_thmss loc Thm.internalK
haftmann@29361
   456
    [((Binding.empty, [Attrib.internal (decl_attrib decl)]), [([Drule.dummy_thm], [])])];
haftmann@29361
   457
haftmann@29361
   458
in
haftmann@29361
   459
haftmann@29361
   460
val add_type_syntax = add_decls (apfst o cons);
haftmann@29361
   461
val add_term_syntax = add_decls (apsnd o cons);
haftmann@29361
   462
val add_declaration = add_decls (K I);
haftmann@29361
   463
haftmann@29361
   464
end;
haftmann@29361
   465
haftmann@29361
   466
(* Dependencies *)
haftmann@29361
   467
haftmann@29392
   468
fun add_dependency loc dep = (change_locale loc o apsnd) (cons (dep, stamp ()));
haftmann@29361
   469
haftmann@29361
   470
haftmann@29361
   471
(*** Reasoning about locales ***)
haftmann@29361
   472
haftmann@29361
   473
(** Storage for witnesses, intro and unfold rules **)
haftmann@29361
   474
haftmann@29361
   475
structure Witnesses = ThmsFun();
haftmann@29361
   476
structure Intros = ThmsFun();
haftmann@29361
   477
structure Unfolds = ThmsFun();
haftmann@29361
   478
haftmann@29361
   479
val witness_attrib = Witnesses.add;
haftmann@29361
   480
val intro_attrib = Intros.add;
haftmann@29361
   481
val unfold_attrib = Unfolds.add;
haftmann@29361
   482
haftmann@29361
   483
(** Tactic **)
haftmann@29361
   484
haftmann@29361
   485
fun intro_locales_tac eager ctxt facts st =
haftmann@29361
   486
  Method.intros_tac
haftmann@29361
   487
    (Witnesses.get ctxt @ Intros.get ctxt @ (if eager then Unfolds.get ctxt else [])) facts st;
haftmann@29361
   488
haftmann@29361
   489
val _ = Context.>> (Context.map_theory
wenzelm@30515
   490
 (Method.setup (Binding.name "intro_locales") (Scan.succeed (METHOD o intro_locales_tac false))
wenzelm@30515
   491
    "back-chain introduction rules of locales without unfolding predicates" #>
wenzelm@30515
   492
  Method.setup (Binding.name "unfold_locales") (Scan.succeed (METHOD o intro_locales_tac true))
wenzelm@30515
   493
    "back-chain all introduction rules of locales"));
haftmann@29361
   494
haftmann@29361
   495
end;
haftmann@29361
   496