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