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