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