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