src/Pure/Isar/locale.ML
author wenzelm
Wed Nov 21 20:20:18 2001 +0100 (2001-11-21)
changeset 12263 6f2acf10e2a2
parent 12143 dc42d17c5b53
child 12273 7fb9840d358d
permissions -rw-r--r--
locale expressions;
wenzelm@12014
     1
(*  Title:      Pure/Isar/locale.ML
wenzelm@11896
     2
    ID:         $Id$
wenzelm@11896
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@11896
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@11896
     5
wenzelm@12058
     6
Locales -- Isar proof contexts as meta-level predicates, with local
wenzelm@12063
     7
syntax and implicit structures.  Draws basic ideas from Florian
wenzelm@12263
     8
Kammüller's original version of locales, but uses the rich
wenzelm@12063
     9
infrastructure of Isar instead of the raw meta-logic.
wenzelm@11896
    10
*)
wenzelm@11896
    11
wenzelm@11896
    12
signature BASIC_LOCALE =
wenzelm@11896
    13
sig
wenzelm@11896
    14
  val print_locales: theory -> unit
wenzelm@12063
    15
  val print_locale: theory -> xstring -> unit
wenzelm@11896
    16
end;
wenzelm@11896
    17
wenzelm@11896
    18
signature LOCALE =
wenzelm@11896
    19
sig
wenzelm@11896
    20
  include BASIC_LOCALE
wenzelm@12046
    21
  type context
wenzelm@12263
    22
  datatype expression =
wenzelm@12263
    23
    Locale of string |
wenzelm@12263
    24
    Merge of expression list |
wenzelm@12263
    25
    Rename of expression * string option list
wenzelm@12046
    26
  datatype ('typ, 'term, 'fact, 'att) elem =
wenzelm@12058
    27
    Fixes of (string * 'typ option * mixfix option) list |
wenzelm@12046
    28
    Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list |
wenzelm@12046
    29
    Defines of ((string * 'att list) * ('term * 'term list)) list |
wenzelm@12046
    30
    Notes of ((string * 'att list) * ('fact * 'att list) list) list |
wenzelm@12046
    31
    Uses of expression
wenzelm@12046
    32
  type 'att element
wenzelm@12046
    33
  type 'att element_i
wenzelm@12046
    34
  type locale
wenzelm@12046
    35
  val intern: Sign.sg -> xstring -> string
wenzelm@12014
    36
  val cond_extern: Sign.sg -> string -> xstring
wenzelm@12063
    37
  val attribute: ('att -> context attribute) ->
wenzelm@12046
    38
    ('typ, 'term, 'thm, 'att) elem -> ('typ, 'term, 'thm, context attribute) elem
wenzelm@12046
    39
  val activate_elements: context attribute element list -> context -> context
wenzelm@12046
    40
  val activate_elements_i: context attribute element_i list -> context -> context
wenzelm@12046
    41
  val activate_locale: xstring -> context -> context
wenzelm@12046
    42
  val activate_locale_i: string -> context -> context
wenzelm@12063
    43
  val add_locale: bstring -> xstring list -> context attribute element list -> theory -> theory
wenzelm@12063
    44
  val add_locale_i: bstring -> xstring list -> context attribute element_i list -> theory -> theory
wenzelm@12143
    45
  val add_thmss: string -> ((string * thm list) * context attribute list) list
wenzelm@12143
    46
    -> theory -> theory
wenzelm@11896
    47
  val setup: (theory -> theory) list
wenzelm@11896
    48
end;
wenzelm@11896
    49
wenzelm@11896
    50
structure Locale: LOCALE =
wenzelm@11896
    51
struct
wenzelm@11896
    52
wenzelm@11896
    53
wenzelm@12014
    54
(** locale elements and locales **)
wenzelm@11896
    55
wenzelm@12014
    56
type context = ProofContext.context;
wenzelm@11896
    57
wenzelm@12263
    58
datatype expression =
wenzelm@12263
    59
  Locale of string |
wenzelm@12263
    60
  Merge of expression list |
wenzelm@12263
    61
  Rename of expression * string option list;
wenzelm@11896
    62
wenzelm@12046
    63
datatype ('typ, 'term, 'fact, 'att) elem =
wenzelm@12058
    64
  Fixes of (string * 'typ option * mixfix option) list |
wenzelm@12046
    65
  Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list |
wenzelm@12046
    66
  Defines of ((string * 'att list) * ('term * 'term list)) list |
wenzelm@12046
    67
  Notes of ((string * 'att list) * ('fact * 'att list) list) list |
wenzelm@12014
    68
  Uses of expression;
wenzelm@11896
    69
wenzelm@12046
    70
type 'att element = (string, string, string, 'att) elem;
wenzelm@12046
    71
type 'att element_i = (typ, term, thm list, 'att) elem;
wenzelm@12070
    72
wenzelm@12070
    73
type locale =
wenzelm@12263
    74
 {imports: string list,
wenzelm@12263
    75
  elements: context attribute element_i list,
wenzelm@12263
    76
  params: (string * typ) list,
wenzelm@12263
    77
  text: ((string * typ) list * term) option,
wenzelm@12263
    78
  closed: bool};
wenzelm@12063
    79
wenzelm@12263
    80
fun make_locale imports elements params text closed =
wenzelm@12263
    81
 {imports = imports, elements = elements, params = params,
wenzelm@12263
    82
  text = text, closed = closed}: locale;
wenzelm@12063
    83
wenzelm@11896
    84
wenzelm@11896
    85
wenzelm@11896
    86
(** theory data **)
wenzelm@11896
    87
wenzelm@11896
    88
(* data kind 'Pure/locales' *)
wenzelm@11896
    89
wenzelm@11896
    90
structure LocalesArgs =
wenzelm@11896
    91
struct
wenzelm@12014
    92
  val name = "Isar/locales";
wenzelm@12063
    93
  type T = NameSpace.T * locale Symtab.table;
wenzelm@11896
    94
wenzelm@12063
    95
  val empty = (NameSpace.empty, Symtab.empty);
wenzelm@12063
    96
  val copy = I;
wenzelm@12263
    97
wenzelm@12263
    98
  fun close {imports, elements, params, text, closed = _} =
wenzelm@12263
    99
    make_locale imports elements params text true;
wenzelm@12263
   100
  fun finish (space, locales) = (space, Symtab.map close locales);
wenzelm@12263
   101
wenzelm@12118
   102
  val prep_ext = I;
wenzelm@12063
   103
  fun merge ((space1, locales1), (space2, locales2)) =
wenzelm@12063
   104
      (NameSpace.merge (space1, space2), Symtab.merge (K true) (locales1, locales2));
wenzelm@11896
   105
wenzelm@12063
   106
  fun print _ (space, locales) =
wenzelm@12014
   107
    Pretty.strs ("locales:" :: map (NameSpace.cond_extern space o #1) (Symtab.dest locales))
wenzelm@12014
   108
    |> Pretty.writeln;
wenzelm@11896
   109
end;
wenzelm@11896
   110
wenzelm@11896
   111
structure LocalesData = TheoryDataFun(LocalesArgs);
wenzelm@11896
   112
val print_locales = LocalesData.print;
wenzelm@11896
   113
wenzelm@12063
   114
val intern = NameSpace.intern o #1 o LocalesData.get_sg;
wenzelm@12063
   115
val cond_extern = NameSpace.cond_extern o #1 o LocalesData.get_sg;
wenzelm@12014
   116
wenzelm@11896
   117
wenzelm@11896
   118
(* access locales *)
wenzelm@11896
   119
wenzelm@12063
   120
fun declare_locale name =
wenzelm@12063
   121
  LocalesData.map (apfst (fn space => (NameSpace.extend (space, [name]))));
wenzelm@11896
   122
wenzelm@12063
   123
fun put_locale name locale =
wenzelm@12063
   124
  LocalesData.map (apsnd (fn locales => Symtab.update ((name, locale), locales)));
wenzelm@12063
   125
wenzelm@12063
   126
fun get_locale thy name = Symtab.lookup (#2 (LocalesData.get thy), name);
wenzelm@11896
   127
wenzelm@12014
   128
fun the_locale thy name =
wenzelm@12014
   129
  (case get_locale thy name of
wenzelm@12014
   130
    Some loc => loc
wenzelm@12014
   131
  | None => error ("Unknown locale " ^ quote name));
wenzelm@11896
   132
wenzelm@11896
   133
wenzelm@12046
   134
wenzelm@12063
   135
(** internalize elements **)
wenzelm@12063
   136
wenzelm@12263
   137
(* read *)
wenzelm@12263
   138
wenzelm@12263
   139
fun read_expression ctxt (Locale xname) = Locale (intern (ProofContext.sign_of ctxt) xname)
wenzelm@12263
   140
  | read_expression ctxt (Merge exprs) = Merge (map (read_expression ctxt) exprs)
wenzelm@12263
   141
  | read_expression ctxt (Rename (expr, xs)) = Rename (read_expression ctxt expr, xs);
wenzelm@12046
   142
wenzelm@12063
   143
fun read_elem ctxt =
wenzelm@12063
   144
 fn Fixes fixes =>
wenzelm@12063
   145
      let val vars =
wenzelm@12063
   146
        #2 (foldl_map ProofContext.read_vars (ctxt, map (fn (x, T, _) => ([x], T)) fixes))
wenzelm@12063
   147
      in Fixes (map2 (fn (([x'], T'), (_, _, mx)) => (x', T', mx)) (vars, fixes)) end
wenzelm@12063
   148
  | Assumes asms =>
wenzelm@12063
   149
      Assumes (map #1 asms ~~ #2 (ProofContext.read_propp (ctxt, map #2 asms)))
wenzelm@12063
   150
  | Defines defs =>
wenzelm@12063
   151
      let val propps =
wenzelm@12063
   152
        #2 (ProofContext.read_propp (ctxt, map (fn (_, (t, ps)) => [(t, (ps, []))]) defs))
wenzelm@12063
   153
      in Defines (map #1 defs ~~ map (fn [(t', (ps', []))] => (t', ps')) propps) end
wenzelm@12063
   154
  | Notes facts =>
wenzelm@12063
   155
      Notes (map (apsnd (map (apfst (ProofContext.get_thms ctxt)))) facts)
wenzelm@12263
   156
  | Uses expr => Uses (read_expression ctxt expr);
wenzelm@12063
   157
wenzelm@12063
   158
wenzelm@12063
   159
(* prepare attributes *)
wenzelm@12063
   160
wenzelm@12063
   161
local fun int_att attrib (x, srcs) = (x, map attrib srcs) in
wenzelm@12063
   162
wenzelm@12063
   163
fun attribute _ (Fixes fixes) = Fixes fixes
wenzelm@12063
   164
  | attribute attrib (Assumes asms) = Assumes (map (apfst (int_att attrib)) asms)
wenzelm@12063
   165
  | attribute attrib (Defines defs) = Defines (map (apfst (int_att attrib)) defs)
wenzelm@12063
   166
  | attribute attrib (Notes facts) =
wenzelm@12046
   167
      Notes (map (apfst (int_att attrib) o apsnd (map (int_att attrib))) facts)
wenzelm@12263
   168
  | attribute _ (Uses expr) = Uses expr;
wenzelm@12063
   169
wenzelm@12063
   170
end;
wenzelm@12046
   171
wenzelm@12046
   172
wenzelm@12263
   173
(** renaming **)
wenzelm@12263
   174
wenzelm@12263
   175
fun rename ren x = if_none (assoc_string (ren, x)) x;
wenzelm@12263
   176
wenzelm@12263
   177
fun rename_term ren (Free (x, T)) = Free (rename ren x, T)
wenzelm@12263
   178
  | rename_term ren (t $ u) = rename_term ren t $ rename_term ren u
wenzelm@12263
   179
  | rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t)
wenzelm@12263
   180
  | rename_term _ a = a;
wenzelm@12263
   181
wenzelm@12263
   182
fun rename_thm ren th =
wenzelm@12263
   183
  let
wenzelm@12263
   184
    val {sign, hyps, prop, maxidx, ...} = Thm.rep_thm th;
wenzelm@12263
   185
    val cert = Thm.cterm_of sign;
wenzelm@12263
   186
    val (xs, Ts) = Library.split_list (foldl Drule.add_frees ([], prop :: hyps));
wenzelm@12263
   187
    val xs' = map (rename ren) xs;
wenzelm@12263
   188
    fun cert_frees names = map (cert o Free) (names ~~ Ts);
wenzelm@12263
   189
    fun cert_vars names = map (cert o Var o apfst (rpair (maxidx + 1))) (names ~~ Ts);
wenzelm@12263
   190
  in
wenzelm@12263
   191
    if xs = xs' then th
wenzelm@12263
   192
    else
wenzelm@12263
   193
      th
wenzelm@12263
   194
      |> Drule.implies_intr_list (map cert hyps)
wenzelm@12263
   195
      |> Drule.forall_intr_list (cert_frees xs)
wenzelm@12263
   196
      |> Drule.forall_elim_list (cert_vars xs)
wenzelm@12263
   197
      |> Thm.instantiate ([], cert_vars xs ~~ cert_frees xs')
wenzelm@12263
   198
      |> (fn th' => Drule.implies_elim_list th' (map (Thm.assume o cert o rename_term ren) hyps))
wenzelm@12263
   199
  end;
wenzelm@12263
   200
wenzelm@12263
   201
wenzelm@12263
   202
fun rename_elem ren (Fixes fixes) = Fixes (map (fn (x, T, mx) =>
wenzelm@12263
   203
      (rename ren x, T, if mx = None then mx else Some Syntax.NoSyn)) fixes)
wenzelm@12263
   204
  | rename_elem ren (Assumes asms) = Assumes (map (apsnd (map (fn (t, (ps, qs)) =>
wenzelm@12263
   205
      (rename_term ren t, (map (rename_term ren) ps, map (rename_term ren) qs))))) asms)
wenzelm@12263
   206
  | rename_elem ren (Defines defs) = Defines (map (apsnd (fn (t, ps) =>
wenzelm@12263
   207
      (rename_term ren t, map (rename_term ren) ps))) defs)
wenzelm@12263
   208
  | rename_elem ren (Notes facts) = Notes (map (apsnd (map (apfst (map (rename_thm ren))))) facts)
wenzelm@12263
   209
  | rename_elem ren (Uses expr) = sys_error "FIXME";
wenzelm@12263
   210
wenzelm@12263
   211
wenzelm@11896
   212
wenzelm@12014
   213
(** activate locales **)
wenzelm@11896
   214
wenzelm@12046
   215
fun activate (ctxt, Fixes fixes) =
wenzelm@12070
   216
      ctxt |> ProofContext.fix_direct (map (fn (x, T, mx) => ([x], T)) fixes)
wenzelm@12070
   217
      |> ProofContext.add_syntax fixes
wenzelm@12058
   218
  | activate (ctxt, Assumes asms) =
wenzelm@12058
   219
      ctxt |> ProofContext.fix_frees (flat (map (map #1 o #2) asms))
wenzelm@12058
   220
      |> ProofContext.assume_i ProofContext.export_assume asms |> #1
wenzelm@12046
   221
  | activate (ctxt, Defines defs) = #1 (ProofContext.assume_i ProofContext.export_def
wenzelm@12084
   222
      (map (fn ((name, atts), (t, ps)) =>
wenzelm@12084
   223
        let val (c, t') = ProofContext.cert_def ctxt t
wenzelm@12084
   224
        in ((if name = "" then Thm.def_name c else name, atts), [(t', (ps, []))]) end) defs) ctxt)
wenzelm@12046
   225
  | activate (ctxt, Notes facts) = #1 (ProofContext.have_thmss facts ctxt)
wenzelm@12263
   226
  | activate (ctxt, Uses expr) = activate_expression [] (ctxt, expr)
wenzelm@11896
   227
wenzelm@12070
   228
and activate_elements_i elems ctxt = foldl activate (ctxt, elems)
wenzelm@12046
   229
wenzelm@12263
   230
and activate_expression rs (ctxt, Locale name) = activate_loc rs name ctxt
wenzelm@12263
   231
  | activate_expression rs (ctxt, Merge exprs) = foldl (activate_expression rs) (ctxt, exprs)
wenzelm@12263
   232
  | activate_expression rs (ctxt, Rename (expr, xs)) = activate_expression (xs :: rs) (ctxt, expr)
wenzelm@12263
   233
wenzelm@12263
   234
and activate_locale_elements rs (ctxt, name) =
wenzelm@12014
   235
  let
wenzelm@12014
   236
    val thy = ProofContext.theory_of ctxt;
wenzelm@12263
   237
    val {elements, params, ...} = the_locale thy name;    (*exception ERROR*)
wenzelm@12263
   238
    val param_names = map #1 params;
wenzelm@12263
   239
wenzelm@12263
   240
    fun replace (opt_x :: xs, y :: ys) = if_none opt_x y :: replace (xs, ys)
wenzelm@12263
   241
      | replace ([], ys) = ys
wenzelm@12263
   242
      | replace (_ :: _, []) = raise ProofContext.CONTEXT
wenzelm@12263
   243
          ("Too many parameters in renaming of locale " ^ quote name, ctxt);
wenzelm@12263
   244
wenzelm@12263
   245
    val elements' =
wenzelm@12263
   246
      if null rs then elements
wenzelm@12263
   247
      else map (rename_elem (param_names ~~ foldr replace (rs, param_names))) elements;
wenzelm@12014
   248
  in
wenzelm@12263
   249
    activate_elements_i elements' ctxt handle ProofContext.CONTEXT (msg, c) =>
wenzelm@12014
   250
      raise ProofContext.CONTEXT (msg ^ "\nThe error(s) above occurred in locale " ^
wenzelm@12014
   251
        quote (cond_extern (Theory.sign_of thy) name), c)
wenzelm@12070
   252
  end
wenzelm@12014
   253
wenzelm@12263
   254
and activate_loc rs name ctxt =
wenzelm@12263
   255
  activate_locale_elements rs (foldl (activate_locale_elements rs)
wenzelm@12063
   256
    (ctxt, #imports (the_locale (ProofContext.theory_of ctxt) name)), name);
wenzelm@12046
   257
wenzelm@12070
   258
wenzelm@12070
   259
fun activate_elements elems ctxt =
wenzelm@12070
   260
  foldl ((fn (ctxt, elem) => activate (ctxt, read_elem ctxt elem))) (ctxt, elems);
wenzelm@12070
   261
wenzelm@12263
   262
val activate_locale_i = activate_loc [];
wenzelm@12263
   263
wenzelm@12046
   264
fun activate_locale xname ctxt =
wenzelm@12046
   265
  activate_locale_i (intern (ProofContext.sign_of ctxt) xname) ctxt;
wenzelm@11896
   266
wenzelm@11896
   267
wenzelm@11896
   268
wenzelm@12070
   269
(** print locale **)
wenzelm@12070
   270
wenzelm@12070
   271
fun pretty_locale thy xname =
wenzelm@12070
   272
  let
wenzelm@12070
   273
    val sg = Theory.sign_of thy;
wenzelm@12070
   274
    val name = intern sg xname;
wenzelm@12263
   275
    val {imports, elements, params = _, text = _, closed = _} = the_locale thy name;
wenzelm@12070
   276
    val locale_ctxt = ProofContext.init thy |> activate_locale_i name;
wenzelm@12070
   277
wenzelm@12070
   278
    val prt_typ = Pretty.quote o ProofContext.pretty_typ locale_ctxt;
wenzelm@12070
   279
    val prt_term = Pretty.quote o ProofContext.pretty_term locale_ctxt;
wenzelm@12070
   280
    val prt_thm = Pretty.quote o ProofContext.pretty_thm locale_ctxt;
wenzelm@12070
   281
wenzelm@12070
   282
    fun prt_syn syn =
wenzelm@12070
   283
      let val s = (case syn of None => "(structure)" | Some mx => Syntax.string_of_mixfix mx)
wenzelm@12070
   284
      in if s = "" then [] else [Pretty.brk 4, Pretty.str s] end;
wenzelm@12070
   285
    fun prt_fix (x, Some T, syn) = Pretty.block (Pretty.str (x ^ " ::") :: Pretty.brk 1 ::
wenzelm@12070
   286
          prt_typ T :: Pretty.brk 1 :: prt_syn syn)
wenzelm@12070
   287
      | prt_fix (x, None, syn) = Pretty.block (Pretty.str x :: Pretty.brk 1 :: prt_syn syn);
wenzelm@12070
   288
wenzelm@12070
   289
    fun prt_asm (("", _), ts) = Pretty.block (Pretty.breaks (map (prt_term o fst) ts))
wenzelm@12070
   290
      | prt_asm ((a, _), ts) = Pretty.block
wenzelm@12070
   291
          (Pretty.breaks (Pretty.str (a ^ ":") :: map (prt_term o fst) ts));
wenzelm@12070
   292
    fun prt_asms asms = Pretty.block
wenzelm@12070
   293
      (flat (separate [Pretty.fbrk, Pretty.str "and"] (map (single o prt_asm) asms)));
wenzelm@12070
   294
wenzelm@12070
   295
    fun prt_def (("", _), (t, _)) = Pretty.block [Pretty.brk 1, prt_term t]
wenzelm@12070
   296
      | prt_def ((a, _), (t, _)) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term t];
wenzelm@12070
   297
wenzelm@12070
   298
    fun prt_fact (("", _), ths) = Pretty.block (Pretty.breaks (map prt_thm (flat (map fst ths))))
wenzelm@12070
   299
      | prt_fact ((a, _), ths) = Pretty.block
wenzelm@12070
   300
          (Pretty.breaks (Pretty.str (a ^ ":") :: map prt_thm (flat (map fst ths))));
wenzelm@12070
   301
wenzelm@12263
   302
    fun prt_expr (Locale name) = Pretty.str (cond_extern sg name)
wenzelm@12263
   303
      | prt_expr (Merge exprs) = Pretty.enclose "(" ")"
wenzelm@12263
   304
          (flat (separate [Pretty.str " +", Pretty.brk 1]
wenzelm@12263
   305
            (map (single o prt_expr) exprs)))
wenzelm@12263
   306
      | prt_expr (Rename (expr, xs)) = Pretty.enclose "(" ")"
wenzelm@12263
   307
          (Pretty.breaks (prt_expr expr :: map (fn x => Pretty.str (if_none x "_")) xs));
wenzelm@12263
   308
wenzelm@12070
   309
    fun prt_elem (Fixes fixes) = Pretty.big_list "fixes" (map prt_fix fixes)
wenzelm@12070
   310
      | prt_elem (Assumes asms) = Pretty.big_list "assumes" (map prt_asm asms)
wenzelm@12070
   311
      | prt_elem (Defines defs) = Pretty.big_list "defines" (map prt_def defs)
wenzelm@12070
   312
      | prt_elem (Notes facts) = Pretty.big_list "notes" (map prt_fact facts)
wenzelm@12263
   313
      | prt_elem (Uses expr) = Pretty.block [Pretty.str "uses", Pretty.brk 1, prt_expr expr];
wenzelm@12070
   314
wenzelm@12070
   315
    val prt_header = Pretty.block (Pretty.str ("locale " ^ cond_extern sg name ^ " =") ::
wenzelm@12070
   316
       (if null imports then [] else
wenzelm@12084
   317
       (Pretty.str " " :: flat (separate [Pretty.str " +", Pretty.brk 1]
wenzelm@12084
   318
           (map (single o Pretty.str o cond_extern sg) imports)) @ [Pretty.str " +"])));
wenzelm@12070
   319
  in Pretty.block (Pretty.fbreaks (prt_header :: map prt_elem elements)) end;
wenzelm@12070
   320
wenzelm@12070
   321
val print_locale = Pretty.writeln oo pretty_locale;
wenzelm@12070
   322
wenzelm@12070
   323
wenzelm@12070
   324
wenzelm@11896
   325
(** define locales **)
wenzelm@11896
   326
wenzelm@12063
   327
(* closeup dangling frees *)
wenzelm@12063
   328
wenzelm@12063
   329
fun close_frees_wrt ctxt t =
wenzelm@12063
   330
  let val frees = rev (filter_out (ProofContext.is_fixed ctxt o #1) (Drule.add_frees ([], t)))
wenzelm@12063
   331
  in curry Term.list_all_free frees end;
wenzelm@11896
   332
wenzelm@12063
   333
fun closeup ctxt (Assumes asms) = Assumes (asms |> map (fn (a, propps) =>
wenzelm@12063
   334
      (a, propps |> map (fn (t, (ps1, ps2)) =>
wenzelm@12063
   335
        let val close = close_frees_wrt ctxt t in (close t, (map close ps1, map close ps2)) end))))
wenzelm@12063
   336
  | closeup ctxt (Defines defs) = Defines (defs |> map (fn (a, (t, ps)) =>
wenzelm@12063
   337
      let
wenzelm@12084
   338
        val (_, t') = ProofContext.cert_def ctxt t;
wenzelm@12063
   339
        val close = close_frees_wrt ctxt t';
wenzelm@12063
   340
      in (a, (close t', map close ps)) end))
wenzelm@12063
   341
  | closeup ctxt elem = elem;
wenzelm@11896
   342
wenzelm@11896
   343
wenzelm@12063
   344
(* add_locale(_i) *)
wenzelm@11896
   345
wenzelm@12063
   346
fun gen_add_locale prep_locale prep_elem bname raw_imports raw_elems thy =
wenzelm@12063
   347
  let
wenzelm@12063
   348
    val sign = Theory.sign_of thy;
wenzelm@11896
   349
    val name = Sign.full_name sign bname;
wenzelm@12063
   350
    val _ =
wenzelm@12063
   351
      if is_none (get_locale thy name) then () else
wenzelm@12063
   352
      error ("Duplicate definition of locale " ^ quote name);
wenzelm@11896
   353
wenzelm@12063
   354
    val imports = map (prep_locale sign) raw_imports;
wenzelm@12263
   355
    val imports_ctxt = foldl (activate_locale_elements []) (ProofContext.init thy, imports);
wenzelm@12063
   356
    fun prep (ctxt, raw_elem) =
wenzelm@12063
   357
      let val elem = closeup ctxt (prep_elem ctxt raw_elem)
wenzelm@12063
   358
      in (activate (ctxt, elem), elem) end;
wenzelm@12063
   359
    val (locale_ctxt, elems) = foldl_map prep (imports_ctxt, raw_elems);
wenzelm@12263
   360
    val params = [];  (* FIXME *)
wenzelm@12263
   361
    val text = None;  (* FIXME *)
wenzelm@12063
   362
  in
wenzelm@12063
   363
    thy
wenzelm@12063
   364
    |> declare_locale name
wenzelm@12263
   365
    |> put_locale name (make_locale imports elems params text false)
wenzelm@12063
   366
  end;
wenzelm@12063
   367
wenzelm@12063
   368
val add_locale = gen_add_locale intern read_elem;
wenzelm@12063
   369
val add_locale_i = gen_add_locale (K I) (K I);
wenzelm@12063
   370
wenzelm@11896
   371
wenzelm@12063
   372
wenzelm@12070
   373
(** store results **)
wenzelm@11896
   374
wenzelm@12143
   375
fun add_thmss name args thy =
wenzelm@12063
   376
  let
wenzelm@12263
   377
    val {imports, elements, params, text, closed} = the_locale thy name;
wenzelm@12143
   378
    val _ = conditional closed (fn () =>
wenzelm@12143
   379
      error ("Cannot store results in closed locale: " ^ quote name));
wenzelm@12143
   380
    val note = Notes (map (fn ((a, ths), atts) =>
wenzelm@12143
   381
      ((a, atts), [(map (curry Thm.name_thm a) ths, [])])) args);
wenzelm@12063
   382
  in
wenzelm@12143
   383
    activate (thy |> ProofContext.init |> activate_locale_i name, note);    (*test attributes!*)
wenzelm@12263
   384
    thy |> put_locale name (make_locale imports (elements @ [note]) params text closed)
wenzelm@12063
   385
  end;
wenzelm@11896
   386
wenzelm@12063
   387
wenzelm@11896
   388
wenzelm@11896
   389
(** locale theory setup **)
wenzelm@12063
   390
wenzelm@11896
   391
val setup =
wenzelm@11896
   392
 [LocalesData.init];
wenzelm@11896
   393
wenzelm@11896
   394
end;
wenzelm@11896
   395
wenzelm@11896
   396
structure BasicLocale: BASIC_LOCALE = Locale;
wenzelm@11896
   397
open BasicLocale;