src/Pure/Isar/locale.ML
author haftmann
Mon Nov 29 11:38:59 2010 +0100 (2010-11-29 ago)
changeset 40803 3f66ea311d44
parent 40782 aa533c5e3f48
child 41270 dea60d052923
permissions -rw-r--r--
tuned
haftmann@29361
     1
(*  Title:      Pure/Isar/locale.ML
haftmann@29361
     2
    Author:     Clemens Ballarin, TU Muenchen
haftmann@29361
     3
wenzelm@30725
     4
Locales -- managed Isar proof contexts, based on Pure predicates.
haftmann@29361
     5
haftmann@29361
     6
Draws basic ideas from Florian Kammueller's original version of
haftmann@29361
     7
locales, but uses the richer infrastructure of Isar instead of the raw
haftmann@29361
     8
meta-logic.  Furthermore, structured import of contexts (with merge
haftmann@29361
     9
and rename operations) are provided, as well as type-inference of the
haftmann@29361
    10
signature parts, and predicate definitions of the specification text.
haftmann@29361
    11
haftmann@29361
    12
Interpretation enables the reuse of theorems of locales in other
haftmann@29361
    13
contexts, namely those defined by theories, structured proofs and
haftmann@29361
    14
locales themselves.
haftmann@29361
    15
haftmann@29361
    16
See also:
haftmann@29361
    17
haftmann@29361
    18
[1] Clemens Ballarin. Locales and Locale Expressions in Isabelle/Isar.
haftmann@29361
    19
    In Stefano Berardi et al., Types for Proofs and Programs: International
haftmann@29361
    20
    Workshop, TYPES 2003, Torino, Italy, LNCS 3085, pages 34-50, 2004.
haftmann@29361
    21
[2] Clemens Ballarin. Interpretation of Locales in Isabelle: Managing
haftmann@29361
    22
    Dependencies between Locales. Technical Report TUM-I0607, Technische
haftmann@29361
    23
    Universitaet Muenchen, 2006.
haftmann@29361
    24
[3] Clemens Ballarin. Interpretation of Locales in Isabelle: Theories and
haftmann@29361
    25
    Proof Contexts. In J.M. Borwein and W.M. Farmer, MKM 2006, LNAI 4108,
haftmann@29361
    26
    pages 31-43, 2006.
haftmann@29361
    27
*)
haftmann@29361
    28
haftmann@29361
    29
signature LOCALE =
haftmann@29361
    30
sig
haftmann@29576
    31
  (* Locale specification *)
wenzelm@30344
    32
  val register_locale: binding ->
wenzelm@30755
    33
    (string * sort) list * ((string * typ) * mixfix) list ->
haftmann@29361
    34
    term option * term list ->
haftmann@29441
    35
    thm option * thm option -> thm list ->
wenzelm@35798
    36
    declaration list ->
wenzelm@30725
    37
    (string * (Attrib.binding * (thm list * Attrib.src list) list) list) list ->
wenzelm@30725
    38
    (string * morphism) list -> theory -> theory
haftmann@29361
    39
  val intern: theory -> xstring -> string
haftmann@29361
    40
  val extern: theory -> string -> xstring
haftmann@29392
    41
  val defined: theory -> string -> bool
wenzelm@30755
    42
  val params_of: theory -> string -> ((string * typ) * mixfix) list
haftmann@29441
    43
  val intros_of: theory -> string -> thm option * thm option
haftmann@29441
    44
  val axioms_of: theory -> string -> thm list
haftmann@29544
    45
  val instance_of: theory -> string -> morphism -> term list
haftmann@29361
    46
  val specification_of: theory -> string -> term option * term list
haftmann@29361
    47
haftmann@29361
    48
  (* Storing results *)
haftmann@29361
    49
  val add_thmss: string -> string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
haftmann@29361
    50
    Proof.context -> Proof.context
haftmann@29361
    51
  val add_declaration: string -> declaration -> Proof.context -> Proof.context
wenzelm@35798
    52
  val add_syntax_declaration: string -> declaration -> Proof.context -> Proof.context
haftmann@29361
    53
haftmann@29361
    54
  (* Activation *)
wenzelm@30764
    55
  val activate_declarations: string * morphism -> Proof.context -> Proof.context
ballarin@38316
    56
  val activate_facts: morphism option -> string * morphism -> Context.generic -> Context.generic
haftmann@29361
    57
  val init: string -> theory -> Proof.context
haftmann@29361
    58
haftmann@29361
    59
  (* Reasoning about locales *)
wenzelm@30725
    60
  val get_witnesses: Proof.context -> thm list
wenzelm@30725
    61
  val get_intros: Proof.context -> thm list
wenzelm@30725
    62
  val get_unfolds: Proof.context -> thm list
wenzelm@30725
    63
  val witness_add: attribute
wenzelm@30725
    64
  val intro_add: attribute
wenzelm@30725
    65
  val unfold_add: attribute
haftmann@29361
    66
  val intro_locales_tac: bool -> Proof.context -> thm list -> tactic
haftmann@29361
    67
haftmann@31988
    68
  (* Registrations and dependencies *)
ballarin@32845
    69
  val add_registration: string * morphism -> (morphism * bool) option ->
ballarin@38107
    70
    morphism -> Context.generic -> Context.generic
ballarin@32845
    71
  val amend_registration: string * morphism -> morphism * bool ->
ballarin@38107
    72
    morphism -> Context.generic -> Context.generic
ballarin@38111
    73
  val registrations_of: Context.generic -> string -> (string * morphism) list
haftmann@32074
    74
  val add_dependency: string -> string * morphism -> morphism -> theory -> theory
haftmann@29361
    75
haftmann@29361
    76
  (* Diagnostic *)
haftmann@37471
    77
  val all_locales: theory -> string list
haftmann@29361
    78
  val print_locales: theory -> unit
wenzelm@30344
    79
  val print_locale: theory -> bool -> xstring -> unit
ballarin@38109
    80
  val print_registrations: Proof.context -> string -> unit
haftmann@37897
    81
  val locale_deps: theory ->
wenzelm@40782
    82
    {params: ((string * typ) * mixfix) list, axioms: term list, registrations: term list list} Graph.T
haftmann@37897
    83
      * term list list Symtab.table Symtab.table
haftmann@29361
    84
end;
haftmann@29361
    85
haftmann@29361
    86
structure Locale: LOCALE =
haftmann@29361
    87
struct
haftmann@29361
    88
haftmann@29361
    89
datatype ctxt = datatype Element.ctxt;
haftmann@29361
    90
haftmann@29392
    91
ballarin@38211
    92
(*** Locales ***)
haftmann@29361
    93
haftmann@29361
    94
datatype locale = Loc of {
haftmann@29392
    95
  (** static part **)
wenzelm@30755
    96
  parameters: (string * sort) list * ((string * typ) * mixfix) list,
haftmann@29361
    97
    (* type and term parameters *)
haftmann@29361
    98
  spec: term option * term list,
haftmann@29361
    99
    (* assumptions (as a single predicate expression) and defines *)
haftmann@29441
   100
  intros: thm option * thm option,
haftmann@29441
   101
  axioms: thm list,
haftmann@29392
   102
  (** dynamic part **)
ballarin@36096
   103
  syntax_decls: (declaration * serial) list,
wenzelm@35798
   104
    (* syntax declarations *)
ballarin@36096
   105
  notes: ((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * serial) list,
haftmann@29361
   106
    (* theorem declarations *)
ballarin@36651
   107
  dependencies: ((string * (morphism * morphism)) * serial) list
haftmann@29361
   108
    (* locale dependencies (sublocale relation) *)
haftmann@29392
   109
};
haftmann@29361
   110
wenzelm@35798
   111
fun mk_locale ((parameters, spec, intros, axioms), ((syntax_decls, notes), dependencies)) =
haftmann@29441
   112
  Loc {parameters = parameters, intros = intros, axioms = axioms, spec = spec,
wenzelm@35798
   113
    syntax_decls = syntax_decls, notes = notes, dependencies = dependencies};
wenzelm@35798
   114
wenzelm@35798
   115
fun map_locale f (Loc {parameters, spec, intros, axioms, syntax_decls, notes, dependencies}) =
wenzelm@35798
   116
  mk_locale (f ((parameters, spec, intros, axioms), ((syntax_decls, notes), dependencies)));
wenzelm@30754
   117
wenzelm@35798
   118
fun merge_locale
wenzelm@35798
   119
 (Loc {parameters, spec, intros, axioms, syntax_decls, notes, dependencies},
wenzelm@35798
   120
  Loc {syntax_decls = syntax_decls', notes = notes', dependencies = dependencies', ...}) =
wenzelm@35798
   121
    mk_locale
haftmann@29441
   122
      ((parameters, spec, intros, axioms),
wenzelm@35798
   123
        ((merge (eq_snd op =) (syntax_decls, syntax_decls'),
haftmann@29441
   124
          merge (eq_snd op =) (notes, notes')),
haftmann@29441
   125
            merge (eq_snd op =) (dependencies, dependencies')));
haftmann@29361
   126
wenzelm@33522
   127
structure Locales = Theory_Data
haftmann@29361
   128
(
wenzelm@33095
   129
  type T = locale Name_Space.table;
wenzelm@33159
   130
  val empty : T = Name_Space.empty_table "locale";
haftmann@29361
   131
  val extend = I;
wenzelm@33522
   132
  val merge = Name_Space.join_tables (K merge_locale);
haftmann@29361
   133
);
haftmann@29361
   134
wenzelm@33095
   135
val intern = Name_Space.intern o #1 o Locales.get;
wenzelm@33095
   136
val extern = Name_Space.extern o #1 o Locales.get;
haftmann@29392
   137
wenzelm@30725
   138
val get_locale = Symtab.lookup o #2 o Locales.get;
wenzelm@30725
   139
val defined = Symtab.defined o #2 o Locales.get;
haftmann@29361
   140
wenzelm@30725
   141
fun the_locale thy name =
wenzelm@30725
   142
  (case get_locale thy name of
wenzelm@30725
   143
    SOME (Loc loc) => loc
wenzelm@30725
   144
  | NONE => error ("Unknown locale " ^ quote name));
haftmann@29361
   145
wenzelm@35798
   146
fun register_locale binding parameters spec intros axioms syntax_decls notes dependencies thy =
wenzelm@33095
   147
  thy |> Locales.map (Name_Space.define true (Sign.naming_of thy)
wenzelm@30725
   148
    (binding,
wenzelm@30725
   149
      mk_locale ((parameters, spec, intros, axioms),
ballarin@36096
   150
        ((map (fn decl => (decl, serial ())) syntax_decls, map (fn n => (n, serial ())) notes),
ballarin@36651
   151
          map (fn d => (d |> apsnd (rpair Morphism.identity), serial ())) dependencies))) #> snd);
ballarin@36651
   152
          (* FIXME *)
haftmann@29361
   153
haftmann@29392
   154
fun change_locale name =
wenzelm@30725
   155
  Locales.map o apsnd o Symtab.map_entry name o map_locale o apsnd;
haftmann@29361
   156
haftmann@29361
   157
ballarin@38211
   158
(** Primitive operations **)
haftmann@29361
   159
haftmann@29392
   160
fun params_of thy = snd o #parameters o the_locale thy;
haftmann@29361
   161
haftmann@29441
   162
fun intros_of thy = #intros o the_locale thy;
haftmann@29441
   163
haftmann@29441
   164
fun axioms_of thy = #axioms o the_locale thy;
haftmann@29441
   165
haftmann@29392
   166
fun instance_of thy name morph = params_of thy name |>
wenzelm@30755
   167
  map (Morphism.term morph o Free o #1);
haftmann@29361
   168
haftmann@29392
   169
fun specification_of thy = #spec o the_locale thy;
haftmann@29361
   170
haftmann@29544
   171
fun dependencies_of thy name = the_locale thy name |>
haftmann@29544
   172
  #dependencies |> map fst;
haftmann@29544
   173
ballarin@37133
   174
(* Print instance and qualifiers *)
ballarin@37133
   175
ballarin@37133
   176
fun pretty_reg thy (name, morph) =
ballarin@37133
   177
  let
ballarin@37133
   178
    val name' = extern thy name;
ballarin@37133
   179
    val ctxt = ProofContext.init_global thy;
ballarin@37133
   180
    fun prt_qual (qual, mand) = Pretty.str (qual ^ (if mand then "!" else "?"));
ballarin@37133
   181
    fun prt_quals qs = Pretty.separate "." (map prt_qual qs) |> Pretty.block;
ballarin@37133
   182
    val prt_term = Pretty.quote o Syntax.pretty_term ctxt;
wenzelm@39134
   183
    fun prt_term' t =
wenzelm@39134
   184
      if Config.get ctxt show_types
ballarin@37133
   185
      then Pretty.block [prt_term t, Pretty.brk 1, Pretty.str "::",
ballarin@37133
   186
        Pretty.brk 1, (Pretty.quote o Syntax.pretty_typ ctxt) (type_of t)]
ballarin@37133
   187
      else prt_term t;
ballarin@37133
   188
    fun prt_inst ts =
ballarin@37133
   189
      Pretty.block (Pretty.breaks (Pretty.str name' :: map prt_term' ts));
ballarin@37133
   190
ballarin@37133
   191
    val qs = Binding.name "x" |> Morphism.binding morph |> Binding.prefix_of;
ballarin@37133
   192
    val ts = instance_of thy name morph;
ballarin@37133
   193
  in
wenzelm@40782
   194
    (case qs of
wenzelm@40782
   195
      [] => prt_inst ts
wenzelm@40782
   196
    | qs => Pretty.block [prt_quals qs, Pretty.brk 1, Pretty.str ":", Pretty.brk 1, prt_inst ts])
ballarin@37133
   197
  end;
ballarin@37133
   198
haftmann@29361
   199
ballarin@38211
   200
(*** Identifiers: activated locales in theory or proof context ***)
haftmann@29361
   201
ballarin@37105
   202
(* subsumption *)
ballarin@37103
   203
fun ident_le thy ((n: string, ts), (m, ss)) = (m = n) andalso Pattern.matchess thy (ss, ts);
ballarin@37105
   204
  (* smaller term is more general *)
ballarin@37105
   205
ballarin@37105
   206
(* total order *)
ballarin@37105
   207
fun ident_ord ((n: string, ts), (m, ss)) =
wenzelm@40782
   208
  (case fast_string_ord (m, n) of
wenzelm@40782
   209
    EQUAL => list_ord Term_Ord.fast_term_ord (ts, ss)
wenzelm@40782
   210
  | ord => ord);
haftmann@29361
   211
haftmann@29361
   212
local
haftmann@29361
   213
wenzelm@30754
   214
datatype 'a delayed = Ready of 'a | ToDo of 'a delayed * 'a delayed;
haftmann@29361
   215
wenzelm@33519
   216
structure Identifiers = Generic_Data
haftmann@29361
   217
(
wenzelm@30754
   218
  type T = (string * term list) list delayed;
wenzelm@30754
   219
  val empty = Ready [];
haftmann@29361
   220
  val extend = I;
wenzelm@33519
   221
  val merge = ToDo;
haftmann@29361
   222
);
haftmann@29361
   223
ballarin@37103
   224
fun finish thy (ToDo (i1, i2)) = merge (ident_le thy) (finish thy i1, finish thy i2)
haftmann@29361
   225
  | finish _ (Ready ids) = ids;
haftmann@29361
   226
haftmann@29361
   227
val _ = Context.>> (Context.map_theory (Theory.at_begin (fn thy =>
wenzelm@30725
   228
  (case Identifiers.get (Context.Theory thy) of
wenzelm@30725
   229
    Ready _ => NONE
wenzelm@30725
   230
  | ids => SOME (Context.theory_map (Identifiers.put (Ready (finish thy ids))) thy)))));
haftmann@29361
   231
wenzelm@30764
   232
in
haftmann@29361
   233
wenzelm@30764
   234
val get_idents = (fn Ready ids => ids) o Identifiers.get;
wenzelm@30764
   235
val put_idents = Identifiers.put o Ready;
haftmann@29361
   236
haftmann@29361
   237
end;
haftmann@29361
   238
haftmann@29361
   239
haftmann@29361
   240
(** Resolve locale dependencies in a depth-first fashion **)
haftmann@29361
   241
haftmann@29361
   242
local
haftmann@29361
   243
haftmann@29361
   244
val roundup_bound = 120;
haftmann@29361
   245
ballarin@32803
   246
fun add thy depth export (name, morph) (deps, marked) =
haftmann@29361
   247
  if depth > roundup_bound
haftmann@29361
   248
  then error "Roundup bound exceeded (sublocale relation probably not terminating)."
haftmann@29361
   249
  else
haftmann@29361
   250
    let
wenzelm@30754
   251
      val dependencies = dependencies_of thy name;
ballarin@32803
   252
      val instance = instance_of thy name (morph $> export);
haftmann@29361
   253
    in
ballarin@37103
   254
      if member (ident_le thy) marked (name, instance)
haftmann@29361
   255
      then (deps, marked)
haftmann@29361
   256
      else
haftmann@29361
   257
        let
wenzelm@40782
   258
          val dependencies' =
wenzelm@40782
   259
            map (fn (name, (morph', export')) => (name, morph' $> export' $> morph)) dependencies;
haftmann@29361
   260
          val marked' = (name, instance) :: marked;
ballarin@32803
   261
          val (deps', marked'') = fold_rev (add thy (depth + 1) export) dependencies' ([], marked');
haftmann@29361
   262
        in
haftmann@29361
   263
          ((name, morph) :: deps' @ deps, marked'')
haftmann@29361
   264
        end
haftmann@29361
   265
    end;
haftmann@29361
   266
haftmann@29361
   267
in
haftmann@29361
   268
ballarin@33541
   269
(* Note that while identifiers always have the external (exported) view, activate_dep
ballarin@32803
   270
  is presented with the internal view. *)
ballarin@32803
   271
ballarin@32803
   272
fun roundup thy activate_dep export (name, morph) (marked, input) =
haftmann@29361
   273
  let
haftmann@29361
   274
    (* Find all dependencies incuding new ones (which are dependencies enriching
haftmann@29361
   275
      existing registrations). *)
ballarin@32803
   276
    val (dependencies, marked') = add thy 0 export (name, morph) ([], []);
ballarin@32800
   277
    (* Filter out fragments from marked; these won't be activated. *)
haftmann@29361
   278
    val dependencies' = filter_out (fn (name, morph) =>
ballarin@37103
   279
      member (ident_le thy) marked (name, instance_of thy name (morph $> export))) dependencies;
haftmann@29361
   280
  in
ballarin@37103
   281
    (merge (ident_le thy) (marked, marked'), input |> fold_rev activate_dep dependencies')
haftmann@29361
   282
  end;
haftmann@29361
   283
haftmann@29361
   284
end;
haftmann@29361
   285
haftmann@29361
   286
ballarin@38211
   287
(*** Registrations: interpretations in theories or proof contexts ***)
haftmann@29361
   288
ballarin@37105
   289
structure Idtab = Table(type key = string * term list val ord = ident_ord);
ballarin@37105
   290
ballarin@38107
   291
structure Registrations = Generic_Data
haftmann@29361
   292
(
ballarin@37105
   293
  type T = ((morphism * morphism) * serial) Idtab.table *
ballarin@37105
   294
    (* registrations, indexed by locale name and instance;
ballarin@36090
   295
       serial points to mixin list *)
ballarin@37105
   296
    (((morphism * bool) * serial) list) Inttab.table;
ballarin@37105
   297
    (* table of mixin lists, per list mixins in reverse order of declaration;
ballarin@36090
   298
       lists indexed by registration serial,
ballarin@36090
   299
       entries for empty lists may be omitted *)
ballarin@37105
   300
  val empty = (Idtab.empty, Inttab.empty);
haftmann@29361
   301
  val extend = I;
ballarin@37133
   302
  fun merge ((reg1, mix1), (reg2, mix2)) : T =
ballarin@37133
   303
    (Idtab.join (fn id => fn (r1 as (_, s1), r2 as (_, s2)) =>
ballarin@37133
   304
        if s1 = s2 then raise Idtab.SAME else raise Idtab.DUP id) (reg1, reg2),
ballarin@37133
   305
      Inttab.join (K (Library.merge (eq_snd op =))) (mix1, mix2))
ballarin@37133
   306
    handle Idtab.DUP id =>
ballarin@37133
   307
      (* distinct interpretations with same base: merge their mixins *)
ballarin@37133
   308
      let
ballarin@37133
   309
        val (_, s1) = Idtab.lookup reg1 id |> the;
ballarin@37133
   310
        val (morph2, s2) = Idtab.lookup reg2 id |> the;
ballarin@37133
   311
        val reg2' = Idtab.update (id, (morph2, s1)) reg2;
wenzelm@40782
   312
        val mix2' =
wenzelm@40782
   313
          (case Inttab.lookup mix2 s2 of
wenzelm@40782
   314
            NONE => mix2
wenzelm@40782
   315
          | SOME mxs => Inttab.delete s2 mix2 |> Inttab.update_new (s1, mxs));
ballarin@37133
   316
        val _ = warning "Removed duplicate interpretation after retrieving its mixins.";
ballarin@37133
   317
        (* FIXME print interpretations,
ballarin@37133
   318
           which is not straightforward without theory context *)
ballarin@37133
   319
      in merge ((reg1, mix1), (reg2', mix2')) end;
haftmann@29361
   320
    (* FIXME consolidate with dependencies, consider one data slot only *)
haftmann@29361
   321
);
haftmann@29361
   322
ballarin@32801
   323
ballarin@32801
   324
(* Primitive operations *)
ballarin@32801
   325
ballarin@37104
   326
fun add_reg thy export (name, morph) =
ballarin@37105
   327
  Registrations.map (apfst (Idtab.insert (K false)
ballarin@37105
   328
    ((name, instance_of thy name (morph $> export)), ((morph, export), serial ()))));
haftmann@31988
   329
ballarin@36095
   330
fun add_mixin serial' mixin =
ballarin@36095
   331
  (* registration to be amended identified by its serial id *)
ballarin@37105
   332
  Registrations.map (apsnd (Inttab.map_default (serial', []) (cons (mixin, serial ()))));
ballarin@32801
   333
ballarin@38107
   334
fun get_mixins context (name, morph) =
ballarin@32804
   335
  let
ballarin@38107
   336
    val thy = Context.theory_of context;
ballarin@38107
   337
    val (regs, mixins) = Registrations.get context;
ballarin@32804
   338
  in
wenzelm@40782
   339
    (case Idtab.lookup regs (name, instance_of thy name morph) of
ballarin@32804
   340
      NONE => []
wenzelm@40782
   341
    | SOME (_, serial) => the_default [] (Inttab.lookup mixins serial))
ballarin@32804
   342
  end;
ballarin@32804
   343
ballarin@36091
   344
fun compose_mixins mixins =
ballarin@36091
   345
  fold_rev Morphism.compose (map (fst o fst) mixins) Morphism.identity;
ballarin@36091
   346
ballarin@38107
   347
fun collect_mixins context (name, morph) =
ballarin@38107
   348
  let
ballarin@38107
   349
    val thy = Context.theory_of context;
ballarin@38107
   350
  in
ballarin@38107
   351
    roundup thy (fn dep => fn mixins => merge (eq_snd op =) (mixins, get_mixins context dep))
ballarin@38107
   352
      Morphism.identity (name, morph) ([(name, instance_of thy name morph)], [])
ballarin@38107
   353
    |> snd |> filter (snd o fst)  (* only inheritable mixins *)
ballarin@38107
   354
    |> (fn x => merge (eq_snd op =) (x, get_mixins context (name, morph)))
ballarin@38107
   355
    |> compose_mixins
ballarin@38107
   356
  end;
ballarin@36091
   357
ballarin@38107
   358
fun get_registrations context select = Registrations.get context
ballarin@37491
   359
  |>> Idtab.dest |>> select
ballarin@36095
   360
  (* with inherited mixins *)
ballarin@37105
   361
  |-> (fn regs => fn _ => map (fn ((name, _), ((base, export) ,_)) =>
ballarin@38107
   362
    (name, base $> (collect_mixins context (name, base $> export)) $> export)) regs);
ballarin@36091
   363
ballarin@38111
   364
fun registrations_of context name =
ballarin@38107
   365
  get_registrations context (filter (curry (op =) name o fst o fst));
ballarin@37491
   366
ballarin@38107
   367
fun all_registrations context = get_registrations context I;
haftmann@37973
   368
ballarin@38211
   369
ballarin@38211
   370
(*** Activate context elements of locale ***)
ballarin@38211
   371
ballarin@38211
   372
(* Declarations, facts and entire locale content *)
ballarin@38211
   373
ballarin@38211
   374
fun activate_syntax_decls (name, morph) context =
ballarin@38211
   375
  let
ballarin@38211
   376
    val thy = Context.theory_of context;
ballarin@38211
   377
    val {syntax_decls, ...} = the_locale thy name;
ballarin@38211
   378
  in
ballarin@38211
   379
    context
ballarin@38211
   380
    |> fold_rev (fn (decl, _) => decl morph) syntax_decls
ballarin@38211
   381
  end;
ballarin@38211
   382
ballarin@38211
   383
fun activate_notes activ_elem transfer context export' (name, morph) input =
ballarin@32804
   384
  let
ballarin@38107
   385
    val thy = Context.theory_of context;
ballarin@32804
   386
    val {notes, ...} = the_locale thy name;
ballarin@32804
   387
    fun activate ((kind, facts), _) input =
ballarin@32804
   388
      let
wenzelm@40782
   389
        val mixin =
wenzelm@40782
   390
          (case export' of
wenzelm@40782
   391
            NONE => Morphism.identity
wenzelm@40782
   392
          | SOME export => collect_mixins context (name, morph $> export) $> export);
wenzelm@40782
   393
        val facts' = facts
wenzelm@40782
   394
          |> Element.facts_map (Element.morph_ctxt (transfer input $> morph $> mixin));
ballarin@32804
   395
      in activ_elem (Notes (kind, facts')) input end;
ballarin@32804
   396
  in
ballarin@32804
   397
    fold_rev activate notes input
ballarin@32804
   398
  end;
ballarin@32804
   399
ballarin@38211
   400
fun activate_all name thy activ_elem transfer (marked, input) =
ballarin@38211
   401
  let
ballarin@38211
   402
    val {parameters = (_, params), spec = (asm, defs), ...} = the_locale thy name;
ballarin@38211
   403
    val input' = input |>
ballarin@38211
   404
      (not (null params) ?
ballarin@38211
   405
        activ_elem (Fixes (map (fn ((x, T), mx) => (Binding.name x, SOME T, mx)) params))) |>
ballarin@38211
   406
      (* FIXME type parameters *)
ballarin@38211
   407
      (case asm of SOME A => activ_elem (Assumes [(Attrib.empty_binding, [(A, [])])]) | _ => I) |>
wenzelm@40782
   408
      (not (null defs) ?
wenzelm@40782
   409
        activ_elem (Defines (map (fn def => (Attrib.empty_binding, (def, []))) defs)));
ballarin@38211
   410
    val activate = activate_notes activ_elem transfer (Context.Theory thy) NONE;
ballarin@38211
   411
  in
ballarin@38211
   412
    roundup thy activate Morphism.identity (name, Morphism.identity) (marked, input')
ballarin@38211
   413
  end;
ballarin@38211
   414
ballarin@38211
   415
ballarin@38211
   416
(** Public activation functions **)
ballarin@38211
   417
ballarin@38211
   418
fun activate_declarations dep = Context.proof_map (fn context =>
ballarin@32804
   419
  let
ballarin@32804
   420
    val thy = Context.theory_of context;
ballarin@38211
   421
  in
ballarin@38211
   422
    roundup thy activate_syntax_decls Morphism.identity dep (get_idents context, context)
ballarin@38211
   423
    |-> put_idents
ballarin@38211
   424
  end);
ballarin@38211
   425
ballarin@38211
   426
fun activate_facts export dep context =
ballarin@38211
   427
  let
ballarin@38211
   428
    val thy = Context.theory_of context;
ballarin@38316
   429
    val activate = activate_notes Element.init (Element.transfer_morphism o Context.theory_of) context export;
ballarin@38316
   430
  in
ballarin@38316
   431
    roundup thy activate (case export of NONE => Morphism.identity | SOME export => export)
ballarin@38316
   432
      dep (get_idents context, context)
ballarin@38316
   433
    |-> put_idents
ballarin@38316
   434
  end;
ballarin@32804
   435
ballarin@38211
   436
fun init name thy =
ballarin@38211
   437
  activate_all name thy Element.init (Element.transfer_morphism o Context.theory_of)
ballarin@38211
   438
    ([], Context.Proof (ProofContext.init_global thy)) |-> put_idents |> Context.proof_of;
ballarin@32804
   439
ballarin@38211
   440
ballarin@38211
   441
(*** Add and extend registrations ***)
ballarin@32801
   442
ballarin@38107
   443
fun amend_registration (name, morph) mixin export context =
ballarin@32804
   444
  let
ballarin@38107
   445
    val thy = Context.theory_of context;
ballarin@38107
   446
    val regs = Registrations.get context |> fst;
ballarin@32804
   447
    val base = instance_of thy name (morph $> export);
ballarin@32804
   448
  in
wenzelm@40782
   449
    (case Idtab.lookup regs (name, base) of
wenzelm@40782
   450
      NONE =>
wenzelm@40782
   451
        error ("No interpretation of locale " ^
ballarin@32804
   452
          quote (extern thy name) ^ " and\nparameter instantiation " ^
ballarin@32804
   453
          space_implode " " (map (quote o Syntax.string_of_term_global thy) base) ^
ballarin@32804
   454
          " available")
wenzelm@40782
   455
    | SOME (_, serial') => add_mixin serial' mixin context)
ballarin@32804
   456
  end;
ballarin@32804
   457
ballarin@32801
   458
(* Note that a registration that would be subsumed by an existing one will not be
ballarin@32801
   459
   generated, and it will not be possible to amend it. *)
ballarin@32801
   460
ballarin@38107
   461
fun add_registration (name, base_morph) mixin export context =
ballarin@32801
   462
  let
ballarin@38107
   463
    val thy = Context.theory_of context;
wenzelm@40782
   464
    val mix = (case mixin of NONE => Morphism.identity | SOME (mix, _) => mix);
ballarin@37102
   465
    val morph = base_morph $> mix;
ballarin@37102
   466
    val inst = instance_of thy name morph;
ballarin@32801
   467
  in
ballarin@38107
   468
    if member (ident_le thy) (get_idents context) (name, inst)
ballarin@38107
   469
    then context
ballarin@32801
   470
    else
ballarin@38107
   471
      (get_idents context, context)
ballarin@36095
   472
      (* add new registrations with inherited mixins *)
ballarin@37104
   473
      |> roundup thy (add_reg thy export) export (name, morph)
ballarin@36095
   474
      |> snd
ballarin@36095
   475
      (* add mixin *)
wenzelm@40782
   476
      |>
wenzelm@40782
   477
        (case mixin of
wenzelm@40782
   478
          NONE => I
wenzelm@40782
   479
        | SOME mixin => amend_registration (name, morph) mixin export)
ballarin@36095
   480
      (* activate import hierarchy as far as not already active *)
ballarin@38316
   481
      |> activate_facts (SOME export) (name, morph)
ballarin@32801
   482
  end;
ballarin@32801
   483
haftmann@29361
   484
haftmann@31988
   485
(*** Dependencies ***)
haftmann@31988
   486
ballarin@37102
   487
fun add_dependency loc (name, morph) export thy =
ballarin@37102
   488
  let
ballarin@37102
   489
    val thy' = (change_locale loc o apsnd) (cons ((name, (morph, export)), serial ())) thy;
ballarin@38107
   490
    val context' = Context.Theory thy';
ballarin@37102
   491
    val (_, regs) = fold_rev (roundup thy' cons export)
ballarin@38783
   492
      (registrations_of context' loc) (get_idents (context'), []);
ballarin@37102
   493
  in
ballarin@37102
   494
    thy'
ballarin@38107
   495
    |> fold_rev (fn dep => Context.theory_map (add_registration dep NONE export)) regs
ballarin@37102
   496
  end;
haftmann@31988
   497
haftmann@31988
   498
haftmann@29361
   499
(*** Storing results ***)
haftmann@29361
   500
haftmann@29361
   501
(* Theorems *)
haftmann@29361
   502
haftmann@29361
   503
fun add_thmss loc kind args ctxt =
haftmann@29361
   504
  let
wenzelm@30777
   505
    val (Notes args', ctxt') = Element.activate_i (Notes (kind, args)) ctxt;
wenzelm@38757
   506
    val ctxt'' = ctxt' |> ProofContext.background_theory
wenzelm@38757
   507
     ((change_locale loc o apfst o apsnd) (cons (args', serial ()))
haftmann@29392
   508
        #>
haftmann@29361
   509
      (* Registrations *)
haftmann@31988
   510
      (fn thy => fold_rev (fn (_, morph) =>
haftmann@29361
   511
            let
haftmann@29361
   512
              val args'' = snd args' |> Element.facts_map (Element.morph_ctxt morph) |>
haftmann@29361
   513
                Attrib.map_facts (Attrib.attribute_i thy)
wenzelm@39557
   514
            in Global_Theory.note_thmss kind args'' #> snd end)
ballarin@38111
   515
        (registrations_of (Context.Theory thy) loc) thy))
haftmann@29361
   516
  in ctxt'' end;
haftmann@29361
   517
haftmann@29361
   518
haftmann@29361
   519
(* Declarations *)
haftmann@29361
   520
wenzelm@35798
   521
fun add_declaration loc decl =
wenzelm@33643
   522
  add_thmss loc ""
wenzelm@35798
   523
    [((Binding.conceal Binding.empty,
wenzelm@35798
   524
        [Attrib.internal (fn phi => Thm.declaration_attribute (K (decl phi)))]),
wenzelm@33278
   525
      [([Drule.dummy_thm], [])])];
haftmann@29361
   526
wenzelm@35798
   527
fun add_syntax_declaration loc decl =
wenzelm@38756
   528
  ProofContext.background_theory ((change_locale loc o apfst o apfst) (cons (decl, serial ())))
wenzelm@35798
   529
  #> add_declaration loc decl;
haftmann@29361
   530
wenzelm@30725
   531
haftmann@29361
   532
(*** Reasoning about locales ***)
haftmann@29361
   533
wenzelm@30725
   534
(* Storage for witnesses, intro and unfold rules *)
haftmann@29361
   535
wenzelm@33519
   536
structure Thms = Generic_Data
wenzelm@30725
   537
(
wenzelm@30725
   538
  type T = thm list * thm list * thm list;
wenzelm@30725
   539
  val empty = ([], [], []);
wenzelm@30725
   540
  val extend = I;
wenzelm@33519
   541
  fun merge ((witnesses1, intros1, unfolds1), (witnesses2, intros2, unfolds2)) =
wenzelm@30725
   542
   (Thm.merge_thms (witnesses1, witnesses2),
wenzelm@30725
   543
    Thm.merge_thms (intros1, intros2),
wenzelm@30725
   544
    Thm.merge_thms (unfolds1, unfolds2));
wenzelm@30725
   545
);
haftmann@29361
   546
wenzelm@30725
   547
val get_witnesses = #1 o Thms.get o Context.Proof;
wenzelm@30725
   548
val get_intros = #2 o Thms.get o Context.Proof;
wenzelm@30725
   549
val get_unfolds = #3 o Thms.get o Context.Proof;
haftmann@29361
   550
wenzelm@30725
   551
val witness_add =
wenzelm@30725
   552
  Thm.declaration_attribute (fn th => Thms.map (fn (x, y, z) => (Thm.add_thm th x, y, z)));
wenzelm@30725
   553
val intro_add =
wenzelm@30725
   554
  Thm.declaration_attribute (fn th => Thms.map (fn (x, y, z) => (x, Thm.add_thm th y, z)));
wenzelm@30725
   555
val unfold_add =
wenzelm@30725
   556
  Thm.declaration_attribute (fn th => Thms.map (fn (x, y, z) => (x, y, Thm.add_thm th z)));
haftmann@29361
   557
wenzelm@30725
   558
wenzelm@40782
   559
(* Tactics *)
wenzelm@30725
   560
ballarin@36093
   561
fun gen_intro_locales_tac intros_tac eager ctxt =
ballarin@36093
   562
  intros_tac
wenzelm@30725
   563
    (get_witnesses ctxt @ get_intros ctxt @ (if eager then get_unfolds ctxt else []));
haftmann@29361
   564
ballarin@36093
   565
val intro_locales_tac = gen_intro_locales_tac Method.intros_tac;
ballarin@36093
   566
val try_intro_locales_tac= gen_intro_locales_tac Method.try_intros_tac;
ballarin@36093
   567
haftmann@29361
   568
val _ = Context.>> (Context.map_theory
ballarin@36093
   569
 (Method.setup (Binding.name "intro_locales") (Scan.succeed (METHOD o try_intro_locales_tac false))
wenzelm@30515
   570
    "back-chain introduction rules of locales without unfolding predicates" #>
ballarin@36093
   571
  Method.setup (Binding.name "unfold_locales") (Scan.succeed (METHOD o try_intro_locales_tac true))
wenzelm@30515
   572
    "back-chain all introduction rules of locales"));
haftmann@29361
   573
haftmann@37471
   574
haftmann@37471
   575
(*** diagnostic commands and interfaces ***)
haftmann@37471
   576
haftmann@37897
   577
val all_locales = Symtab.keys o snd o Locales.get;
haftmann@37471
   578
haftmann@37471
   579
fun print_locales thy =
haftmann@37897
   580
  Pretty.strs ("locales:" :: map #1 (Name_Space.extern_table (Locales.get thy)))
haftmann@37471
   581
  |> Pretty.writeln;
haftmann@37471
   582
haftmann@37471
   583
fun print_locale thy show_facts raw_name =
haftmann@37471
   584
  let
haftmann@37471
   585
    val name = intern thy raw_name;
haftmann@37471
   586
    val ctxt = init name thy;
haftmann@37471
   587
    fun cons_elem (elem as Notes _) = show_facts ? cons elem
haftmann@37471
   588
      | cons_elem elem = cons elem;
haftmann@37471
   589
    val elems =
haftmann@37471
   590
      activate_all name thy cons_elem (K (Element.transfer_morphism thy)) ([], [])
haftmann@37471
   591
      |> snd |> rev;
haftmann@37471
   592
  in
haftmann@37471
   593
    Pretty.big_list "locale elements:" (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)
haftmann@37471
   594
    |> Pretty.writeln
haftmann@37471
   595
  end;
haftmann@37471
   596
ballarin@38109
   597
fun print_registrations ctxt raw_name =
ballarin@38109
   598
  let
ballarin@38109
   599
    val thy = ProofContext.theory_of ctxt;
ballarin@38109
   600
  in
wenzelm@40782
   601
    (case registrations_of (Context.Proof ctxt) (* FIXME *) (intern thy raw_name) of
wenzelm@40782
   602
      [] => Pretty.str ("no interpretations")
wenzelm@40782
   603
    | regs => Pretty.big_list "interpretations:" (map (pretty_reg thy) (rev regs)))
wenzelm@40782
   604
  end |> Pretty.writeln;
haftmann@37471
   605
haftmann@37897
   606
fun locale_deps thy =
haftmann@37897
   607
  let
haftmann@37897
   608
    val names = all_locales thy
haftmann@37897
   609
    fun add_locale_node name =
haftmann@37897
   610
      let
haftmann@37897
   611
        val params = params_of thy name;
wenzelm@40782
   612
        val axioms =
wenzelm@40782
   613
          these (Option.map (Logic.strip_imp_prems o Thm.prop_of) (fst (intros_of thy name)));
wenzelm@40782
   614
        val registrations =
wenzelm@40782
   615
          map (instance_of thy name o snd) (registrations_of (Context.Theory thy) name);
wenzelm@40782
   616
      in
wenzelm@40782
   617
        Graph.new_node (name, {params = params, axioms = axioms, registrations = registrations})
haftmann@37897
   618
      end;
haftmann@37897
   619
    fun add_locale_deps name =
haftmann@37897
   620
      let
wenzelm@40782
   621
        val dependencies =
wenzelm@40782
   622
          (map o apsnd) (instance_of thy name o op $>) (dependencies_of thy name);
haftmann@37897
   623
      in
haftmann@37897
   624
        fold (fn (super, ts) => fn (gr, deps) => (gr |> Graph.add_edge (super, name),
haftmann@37897
   625
          deps |> Symtab.map_default (super, Symtab.empty) (Symtab.cons_list (name, ts))))
haftmann@37897
   626
            dependencies
haftmann@37897
   627
      end;
haftmann@37897
   628
  in
haftmann@37897
   629
    Graph.empty
haftmann@37897
   630
    |> fold add_locale_node names
haftmann@37897
   631
    |> rpair Symtab.empty
haftmann@37897
   632
    |> fold add_locale_deps names
haftmann@37897
   633
  end;
haftmann@37897
   634
haftmann@29361
   635
end;