src/Pure/Isar/locale.ML
author wenzelm
Wed Nov 28 00:42:04 2001 +0100 (2001-11-28)
changeset 12307 459aa05af6be
parent 12289 ec2dafd0a6a9
child 12323 e151ee6e820f
permissions -rw-r--r--
qualify imported facts;
clarified read/cert_element;
theory data: removed obsolete finish;
tuned;
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@12289
     7
syntax and implicit structures.  Draws some basic ideas from Florian
wenzelm@12289
     8
Kammüller's original version of locales, but uses the richer
wenzelm@12063
     9
infrastructure of Isar instead of the raw meta-logic.
wenzelm@11896
    10
*)
wenzelm@11896
    11
wenzelm@11896
    12
signature LOCALE =
wenzelm@11896
    13
sig
wenzelm@12046
    14
  type context
wenzelm@12046
    15
  datatype ('typ, 'term, 'fact, 'att) elem =
wenzelm@12058
    16
    Fixes of (string * 'typ option * mixfix option) list |
wenzelm@12046
    17
    Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list |
wenzelm@12046
    18
    Defines of ((string * 'att list) * ('term * 'term list)) list |
wenzelm@12273
    19
    Notes of ((string * 'att list) * ('fact * 'att list) list) list
wenzelm@12273
    20
  datatype expr =
wenzelm@12273
    21
    Locale of string |
wenzelm@12273
    22
    Rename of expr * string option list |
wenzelm@12273
    23
    Merge of expr list
wenzelm@12273
    24
  val empty: expr
wenzelm@12273
    25
  datatype ('typ, 'term, 'fact, 'att) elem_expr =
wenzelm@12273
    26
    Elem of ('typ, 'term, 'fact, 'att) elem | Expr of expr
wenzelm@12046
    27
  type 'att element
wenzelm@12046
    28
  type 'att element_i
wenzelm@12046
    29
  type locale
wenzelm@12046
    30
  val intern: Sign.sg -> xstring -> string
wenzelm@12014
    31
  val cond_extern: Sign.sg -> string -> xstring
wenzelm@12273
    32
  val attribute: ('att -> context attribute) -> ('typ, 'term, 'thm, 'att) elem_expr
wenzelm@12273
    33
    -> ('typ, 'term, 'thm, context attribute) elem_expr
wenzelm@12046
    34
  val activate_elements: context attribute element list -> context -> context
wenzelm@12046
    35
  val activate_elements_i: context attribute element_i list -> context -> context
wenzelm@12046
    36
  val activate_locale: xstring -> context -> context
wenzelm@12046
    37
  val activate_locale_i: string -> context -> context
wenzelm@12273
    38
  val add_locale: bstring -> expr -> context attribute element list -> theory -> theory
wenzelm@12273
    39
  val add_locale_i: bstring -> expr -> context attribute element_i list -> theory -> theory
wenzelm@12289
    40
  val print_locales: theory -> unit
wenzelm@12289
    41
  val print_locale: theory -> expr -> unit
wenzelm@12273
    42
  val add_thmss: string -> ((string * thm list) * context attribute list) list -> theory -> theory
wenzelm@11896
    43
  val setup: (theory -> theory) list
wenzelm@11896
    44
end;
wenzelm@11896
    45
wenzelm@12289
    46
structure Locale: LOCALE =
wenzelm@11896
    47
struct
wenzelm@11896
    48
wenzelm@11896
    49
wenzelm@12273
    50
(** locale elements and expressions **)
wenzelm@11896
    51
wenzelm@12014
    52
type context = ProofContext.context;
wenzelm@11896
    53
wenzelm@12046
    54
datatype ('typ, 'term, 'fact, 'att) elem =
wenzelm@12058
    55
  Fixes of (string * 'typ option * mixfix option) list |
wenzelm@12046
    56
  Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list |
wenzelm@12046
    57
  Defines of ((string * 'att list) * ('term * 'term list)) list |
wenzelm@12273
    58
  Notes of ((string * 'att list) * ('fact * 'att list) list) list;
wenzelm@12273
    59
wenzelm@12273
    60
datatype expr =
wenzelm@12273
    61
  Locale of string |
wenzelm@12273
    62
  Rename of expr * string option list |
wenzelm@12273
    63
  Merge of expr list;
wenzelm@11896
    64
wenzelm@12273
    65
val empty = Merge [];
wenzelm@12273
    66
wenzelm@12273
    67
datatype ('typ, 'term, 'fact, 'att) elem_expr =
wenzelm@12273
    68
  Elem of ('typ, 'term, 'fact, 'att) elem | Expr of expr;
wenzelm@12273
    69
wenzelm@12273
    70
type 'att element = (string, string, string, 'att) elem_expr;
wenzelm@12273
    71
type 'att element_i = (typ, term, thm list, 'att) elem_expr;
wenzelm@12070
    72
wenzelm@12070
    73
type locale =
wenzelm@12289
    74
 {import: expr,                                                         (*dynamic import*)
wenzelm@12289
    75
  elems: ((typ, term, thm list, context attribute) elem * stamp) list,  (*static content*)
wenzelm@12289
    76
  params: (string * typ option) list * (string * typ option) list,      (*all vs. local params*)
wenzelm@12289
    77
  text: (string * typ) list * term list}                                (*logical representation*)
wenzelm@12063
    78
wenzelm@12289
    79
fun make_locale import elems params text =
wenzelm@12289
    80
 {import = import, elems = elems, params = params, text = text}: locale;
wenzelm@12063
    81
wenzelm@11896
    82
wenzelm@11896
    83
wenzelm@11896
    84
(** theory data **)
wenzelm@11896
    85
wenzelm@11896
    86
structure LocalesArgs =
wenzelm@11896
    87
struct
wenzelm@12014
    88
  val name = "Isar/locales";
wenzelm@12063
    89
  type T = NameSpace.T * locale Symtab.table;
wenzelm@11896
    90
wenzelm@12063
    91
  val empty = (NameSpace.empty, Symtab.empty);
wenzelm@12063
    92
  val copy = I;
wenzelm@12118
    93
  val prep_ext = I;
wenzelm@12289
    94
wenzelm@12289
    95
  (*joining of locale elements: only facts may be added later!*)
wenzelm@12289
    96
  fun join ({import, elems, params, text}: locale, {elems = elems', ...}: locale) =
wenzelm@12289
    97
    Some (make_locale import (gen_merge_lists eq_snd elems elems') params text);
wenzelm@12273
    98
  fun merge ((space1, locs1), (space2, locs2)) =
wenzelm@12289
    99
    (NameSpace.merge (space1, space2), Symtab.join join (locs1, locs2));
wenzelm@12289
   100
wenzelm@12273
   101
  fun print _ (space, locs) =
wenzelm@12273
   102
    Pretty.strs ("locales:" :: map (NameSpace.cond_extern space o #1) (Symtab.dest locs))
wenzelm@12014
   103
    |> Pretty.writeln;
wenzelm@11896
   104
end;
wenzelm@11896
   105
wenzelm@11896
   106
structure LocalesData = TheoryDataFun(LocalesArgs);
wenzelm@11896
   107
val print_locales = LocalesData.print;
wenzelm@11896
   108
wenzelm@12289
   109
val intern = NameSpace.intern o #1 o LocalesData.get_sg;
wenzelm@12289
   110
val cond_extern = NameSpace.cond_extern o #1 o LocalesData.get_sg;
wenzelm@12289
   111
wenzelm@12277
   112
wenzelm@12277
   113
(* access locales *)
wenzelm@12277
   114
wenzelm@12063
   115
fun declare_locale name =
wenzelm@12063
   116
  LocalesData.map (apfst (fn space => (NameSpace.extend (space, [name]))));
wenzelm@11896
   117
wenzelm@12273
   118
fun put_locale name loc = LocalesData.map (apsnd (fn locs => Symtab.update ((name, loc), locs)));
wenzelm@12063
   119
fun get_locale thy name = Symtab.lookup (#2 (LocalesData.get thy), name);
wenzelm@11896
   120
wenzelm@12014
   121
fun the_locale thy name =
wenzelm@12014
   122
  (case get_locale thy name of
wenzelm@12014
   123
    Some loc => loc
wenzelm@12014
   124
  | None => error ("Unknown locale " ^ quote name));
wenzelm@11896
   125
wenzelm@12046
   126
wenzelm@12277
   127
(* diagnostics *)
wenzelm@12273
   128
wenzelm@12277
   129
fun err_in_locale ctxt msg ids =
wenzelm@12277
   130
  let
wenzelm@12289
   131
    fun prt_id (name, parms) = [Pretty.block (Pretty.breaks (map Pretty.str (name :: parms)))];
wenzelm@12289
   132
    val prt_ids = flat (separate [Pretty.str " +", Pretty.brk 1] (map prt_id ids));
wenzelm@12277
   133
  in
wenzelm@12277
   134
    if null ids then raise ProofContext.CONTEXT (msg, ctxt)
wenzelm@12277
   135
    else raise ProofContext.CONTEXT (msg ^ "\n" ^ Pretty.string_of (Pretty.block
wenzelm@12277
   136
      (Pretty.str "The error(s) above occurred in locale:" :: Pretty.brk 1 :: prt_ids)), ctxt)
wenzelm@12277
   137
  end;
wenzelm@12063
   138
wenzelm@12277
   139
wenzelm@12277
   140
wenzelm@12277
   141
(** operations on locale elements **)
wenzelm@12277
   142
wenzelm@12289
   143
(* prepare elements *)
wenzelm@12263
   144
wenzelm@12307
   145
local
wenzelm@12307
   146
wenzelm@12307
   147
fun prep_name ctxt (name, atts) =
wenzelm@12307
   148
  if NameSpace.is_qualified name then
wenzelm@12307
   149
    raise ProofContext.CONTEXT ("Illegal qualified name: " ^ quote name, ctxt)
wenzelm@12307
   150
  else (name, atts);
wenzelm@12307
   151
wenzelm@12307
   152
fun prep_elem prep_vars prep_propp prep_thms ctxt =
wenzelm@12063
   153
 fn Fixes fixes =>
wenzelm@12307
   154
      let val vars = snd (foldl_map prep_vars (ctxt, map (fn (x, T, _) => ([x], T)) fixes))
wenzelm@12307
   155
      in Fixes (map (fn (([x'], T'), (_, _, mx)) => (x', T', mx)) (vars ~~ fixes)) end
wenzelm@12063
   156
  | Assumes asms =>
wenzelm@12307
   157
      Assumes (map (prep_name ctxt o #1) asms ~~ snd (prep_propp (ctxt, map #2 asms)))
wenzelm@12063
   158
  | Defines defs =>
wenzelm@12307
   159
      let val propps = snd (prep_propp (ctxt, map (fn (_, (t, ps)) => [(t, (ps, []))]) defs)) in
wenzelm@12307
   160
        Defines (map (prep_name ctxt o #1) defs ~~ map (fn [(t', (ps', []))] => (t', ps')) propps)
wenzelm@12307
   161
      end
wenzelm@12063
   162
  | Notes facts =>
wenzelm@12307
   163
      Notes (map (fn (a, bs) => (prep_name ctxt a, map (apfst (prep_thms ctxt)) bs)) facts);
wenzelm@12307
   164
wenzelm@12307
   165
fun read_elem x = prep_elem ProofContext.read_vars ProofContext.read_propp ProofContext.get_thms x;
wenzelm@12307
   166
fun cert_elem x = prep_elem ProofContext.cert_vars ProofContext.cert_propp (K I) x;
wenzelm@12307
   167
wenzelm@12307
   168
fun read_att attrib (x, srcs) = (x, map attrib srcs);
wenzelm@12307
   169
wenzelm@12307
   170
in
wenzelm@12273
   171
wenzelm@12273
   172
fun read_expr ctxt (Locale xname) = Locale (intern (ProofContext.sign_of ctxt) xname)
wenzelm@12273
   173
  | read_expr ctxt (Merge exprs) = Merge (map (read_expr ctxt) exprs)
wenzelm@12289
   174
  | read_expr ctxt (Rename (expr, xs)) = Rename (read_expr ctxt expr, xs);
wenzelm@12273
   175
wenzelm@12273
   176
fun read_element ctxt (Elem e) = Elem (read_elem ctxt e)
wenzelm@12273
   177
  | read_element ctxt (Expr e) = Expr (read_expr ctxt e);
wenzelm@12063
   178
wenzelm@12307
   179
fun cert_element ctxt (Elem e) = Elem (cert_elem ctxt e)
wenzelm@12307
   180
  | cert_element ctxt (Expr e) = Expr e;
wenzelm@12063
   181
wenzelm@12273
   182
fun attribute _ (Elem (Fixes fixes)) = Elem (Fixes fixes)
wenzelm@12277
   183
  | attribute attrib (Elem (Assumes asms)) = Elem (Assumes (map (apfst (read_att attrib)) asms))
wenzelm@12277
   184
  | attribute attrib (Elem (Defines defs)) = Elem (Defines (map (apfst (read_att attrib)) defs))
wenzelm@12273
   185
  | attribute attrib (Elem (Notes facts)) =
wenzelm@12277
   186
      Elem (Notes (map (apfst (read_att attrib) o apsnd (map (read_att attrib))) facts))
wenzelm@12273
   187
  | attribute _ (Expr expr) = Expr expr;
wenzelm@12063
   188
wenzelm@12063
   189
end;
wenzelm@12046
   190
wenzelm@12046
   191
wenzelm@12277
   192
(* renaming *)
wenzelm@12263
   193
wenzelm@12263
   194
fun rename ren x = if_none (assoc_string (ren, x)) x;
wenzelm@12263
   195
wenzelm@12263
   196
fun rename_term ren (Free (x, T)) = Free (rename ren x, T)
wenzelm@12263
   197
  | rename_term ren (t $ u) = rename_term ren t $ rename_term ren u
wenzelm@12263
   198
  | rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t)
wenzelm@12263
   199
  | rename_term _ a = a;
wenzelm@12263
   200
wenzelm@12263
   201
fun rename_thm ren th =
wenzelm@12263
   202
  let
wenzelm@12263
   203
    val {sign, hyps, prop, maxidx, ...} = Thm.rep_thm th;
wenzelm@12263
   204
    val cert = Thm.cterm_of sign;
wenzelm@12263
   205
    val (xs, Ts) = Library.split_list (foldl Drule.add_frees ([], prop :: hyps));
wenzelm@12263
   206
    val xs' = map (rename ren) xs;
wenzelm@12263
   207
    fun cert_frees names = map (cert o Free) (names ~~ Ts);
wenzelm@12263
   208
    fun cert_vars names = map (cert o Var o apfst (rpair (maxidx + 1))) (names ~~ Ts);
wenzelm@12263
   209
  in
wenzelm@12263
   210
    if xs = xs' then th
wenzelm@12263
   211
    else
wenzelm@12263
   212
      th
wenzelm@12263
   213
      |> Drule.implies_intr_list (map cert hyps)
wenzelm@12263
   214
      |> Drule.forall_intr_list (cert_frees xs)
wenzelm@12263
   215
      |> Drule.forall_elim_list (cert_vars xs)
wenzelm@12263
   216
      |> Thm.instantiate ([], cert_vars xs ~~ cert_frees xs')
wenzelm@12263
   217
      |> (fn th' => Drule.implies_elim_list th' (map (Thm.assume o cert o rename_term ren) hyps))
wenzelm@12263
   218
  end;
wenzelm@12263
   219
wenzelm@12263
   220
fun rename_elem ren (Fixes fixes) = Fixes (map (fn (x, T, mx) =>
wenzelm@12273
   221
      (rename ren x, T, if mx = None then mx else Some Syntax.NoSyn)) fixes)    (*drops syntax!*)
wenzelm@12263
   222
  | rename_elem ren (Assumes asms) = Assumes (map (apsnd (map (fn (t, (ps, qs)) =>
wenzelm@12263
   223
      (rename_term ren t, (map (rename_term ren) ps, map (rename_term ren) qs))))) asms)
wenzelm@12263
   224
  | rename_elem ren (Defines defs) = Defines (map (apsnd (fn (t, ps) =>
wenzelm@12263
   225
      (rename_term ren t, map (rename_term ren) ps))) defs)
wenzelm@12273
   226
  | rename_elem ren (Notes facts) = Notes (map (apsnd (map (apfst (map (rename_thm ren))))) facts);
wenzelm@12263
   227
wenzelm@12307
   228
fun qualify_elem prfx elem =
wenzelm@12307
   229
  let
wenzelm@12307
   230
    fun qualify ((name, atts), x) =
wenzelm@12307
   231
      ((NameSpace.pack (filter_out (equal "") (prfx @ [name])), atts), x);
wenzelm@12307
   232
  in
wenzelm@12307
   233
    (case elem of
wenzelm@12307
   234
      Fixes fixes => Fixes fixes
wenzelm@12307
   235
    | Assumes asms => Assumes (map qualify asms)
wenzelm@12307
   236
    | Defines defs => Defines (map qualify defs)
wenzelm@12307
   237
    | Notes facts => Notes (map qualify facts))
wenzelm@12307
   238
  end;
wenzelm@12307
   239
wenzelm@12263
   240
wenzelm@12277
   241
(* evaluation *)
wenzelm@11896
   242
wenzelm@12273
   243
fun eval_expr ctxt expr =
wenzelm@12014
   244
  let
wenzelm@12014
   245
    val thy = ProofContext.theory_of ctxt;
wenzelm@12263
   246
wenzelm@12289
   247
    fun renaming (Some x :: xs) (y :: ys) = (y, x) :: renaming xs ys
wenzelm@12289
   248
      | renaming (None :: xs) (y :: ys) = renaming xs ys
wenzelm@12273
   249
      | renaming [] _ = []
wenzelm@12289
   250
      | renaming xs [] = raise ERROR_MESSAGE ("Too many arguments in renaming: " ^
wenzelm@12289
   251
          commas (map (fn None => "_" | Some x => quote x) xs));
wenzelm@12289
   252
wenzelm@12289
   253
    fun rename_parms ren (name, ps) =
wenzelm@12289
   254
      let val ps' = map (rename ren) ps in
wenzelm@12289
   255
        (case duplicates ps' of [] => (name, ps')
wenzelm@12289
   256
        | dups => err_in_locale ctxt ("Duplicate parameters: " ^ commas_quote dups) [(name, ps')])
wenzelm@12289
   257
      end;
wenzelm@12263
   258
wenzelm@12273
   259
    fun identify ((ids, parms), Locale name) =
wenzelm@12289
   260
          let
wenzelm@12289
   261
            val {import, params, ...} = the_locale thy name;
wenzelm@12289
   262
            val ps = map #1 (#1 params);
wenzelm@12289
   263
          in
wenzelm@12273
   264
            if (name, ps) mem ids then (ids, parms)
wenzelm@12277
   265
            else
wenzelm@12289
   266
              let val (ids', parms') = identify ((ids, parms), import);  (*acyclic dependencies!*)
wenzelm@12289
   267
              in (ids' @ [(name, ps)], merge_lists parms' ps) end
wenzelm@12273
   268
          end
wenzelm@12273
   269
      | identify ((ids, parms), Rename (e, xs)) =
wenzelm@12273
   270
          let
wenzelm@12273
   271
            val (ids', parms') = identify (([], []), e);
wenzelm@12289
   272
            val ren = renaming xs parms' handle ERROR_MESSAGE msg => err_in_locale ctxt msg ids';
wenzelm@12289
   273
            val ids'' = distinct (map (rename_parms ren) ids');
wenzelm@12289
   274
            val parms'' = distinct (flat (map #2 ids''));
wenzelm@12289
   275
          in (merge_lists ids ids'', merge_lists parms parms'') end
wenzelm@12273
   276
      | identify (arg, Merge es) = foldl identify (arg, es);
wenzelm@12014
   277
wenzelm@12307
   278
    fun eval (name, xs) =
wenzelm@12273
   279
      let
wenzelm@12273
   280
        val {params = (ps, _), elems, ...} = the_locale thy name;
wenzelm@12307
   281
        val ren = filter_out (op =) (map #1 ps ~~ xs);
wenzelm@12307
   282
        val (ps', elems') =
wenzelm@12307
   283
          if null ren then (ps, map #1 elems)
wenzelm@12307
   284
          else (map (apfst (rename ren)) ps, map (rename_elem ren o #1) elems);
wenzelm@12307
   285
      in ((name, ps'), map (qualify_elem [NameSpace.base name, space_implode "_" xs]) elems') end;
wenzelm@12307
   286
wenzelm@12307
   287
    (* FIXME unify types *)
wenzelm@12273
   288
wenzelm@12289
   289
    val (idents, parms) = identify (([], []), expr);
wenzelm@12277
   290
  in (map eval idents, parms) end;
wenzelm@12273
   291
wenzelm@12307
   292
fun eval_element _ (Elem e) = [(("", []), [e])]
wenzelm@12307
   293
  | eval_element ctxt (Expr e) = #1 (eval_expr ctxt e);
wenzelm@12046
   294
wenzelm@12070
   295
wenzelm@12273
   296
wenzelm@12273
   297
(** activation **)
wenzelm@12070
   298
wenzelm@12307
   299
(* internal elems *)
wenzelm@12307
   300
wenzelm@12273
   301
fun activate_elem (Fixes fixes) = ProofContext.add_syntax fixes o
wenzelm@12273
   302
      ProofContext.fix_direct (map (fn (x, T, mx) => ([x], T)) fixes)
wenzelm@12273
   303
  | activate_elem (Assumes asms) =
wenzelm@12273
   304
      #1 o ProofContext.assume_i ProofContext.export_assume asms o
wenzelm@12273
   305
      ProofContext.fix_frees (flat (map (map #1 o #2) asms))
wenzelm@12273
   306
  | activate_elem (Defines defs) = (fn ctxt => #1 (ProofContext.assume_i ProofContext.export_def
wenzelm@12273
   307
      (map (fn ((name, atts), (t, ps)) =>
wenzelm@12273
   308
        let val (c, t') = ProofContext.cert_def ctxt t
wenzelm@12273
   309
        in ((if name = "" then Thm.def_name c else name, atts), [(t', (ps, []))]) end) defs) ctxt))
wenzelm@12273
   310
  | activate_elem (Notes facts) = #1 o ProofContext.have_thmss facts;
wenzelm@12263
   311
wenzelm@12273
   312
fun activate_elems es ctxt = foldl (fn (c, e) => activate_elem e c) (ctxt, es);
wenzelm@12273
   313
wenzelm@12307
   314
fun activate_locale_elems named_elems = ProofContext.qualified (fn context =>
wenzelm@12289
   315
  foldl (fn (ctxt, ((name, ps), es)) =>    (* FIXME type inst *)
wenzelm@12277
   316
    activate_elems es ctxt handle ProofContext.CONTEXT (msg, ctxt) =>
wenzelm@12307
   317
      err_in_locale ctxt msg [(name, map fst ps)]) (context, named_elems));
wenzelm@12307
   318
wenzelm@12307
   319
wenzelm@12307
   320
(* external elements and locales *)
wenzelm@12277
   321
wenzelm@12307
   322
fun gen_activate_elements prep_element raw_elements context =
wenzelm@12307
   323
  foldl (fn (ctxt, e) => activate_locale_elems (eval_element ctxt (prep_element ctxt e)) ctxt)
wenzelm@12307
   324
    (context, raw_elements);
wenzelm@12273
   325
wenzelm@12307
   326
val activate_elements = gen_activate_elements read_element;
wenzelm@12307
   327
val activate_elements_i = gen_activate_elements cert_element;
wenzelm@12277
   328
val activate_locale_i = activate_elements_i o single o Expr o Locale;
wenzelm@12277
   329
val activate_locale = activate_elements o single o Expr o Locale;
wenzelm@11896
   330
wenzelm@11896
   331
wenzelm@11896
   332
wenzelm@12070
   333
(** print locale **)
wenzelm@12070
   334
wenzelm@12307
   335
fun print_locale thy raw_expr =
wenzelm@12070
   336
  let
wenzelm@12070
   337
    val sg = Theory.sign_of thy;
wenzelm@12289
   338
    val thy_ctxt = ProofContext.init thy;
wenzelm@12277
   339
wenzelm@12289
   340
    val expr = read_expr thy_ctxt raw_expr;
wenzelm@12307
   341
    val elems = #1 (eval_expr thy_ctxt expr);
wenzelm@12307
   342
    val ctxt = activate_locale_elems elems thy_ctxt;
wenzelm@12070
   343
wenzelm@12307
   344
    val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt;
wenzelm@12307
   345
    val prt_term = Pretty.quote o ProofContext.pretty_term ctxt;
wenzelm@12307
   346
    val prt_thm = Pretty.quote o ProofContext.pretty_thm ctxt;
wenzelm@12070
   347
wenzelm@12070
   348
    fun prt_syn syn =
wenzelm@12070
   349
      let val s = (case syn of None => "(structure)" | Some mx => Syntax.string_of_mixfix mx)
wenzelm@12070
   350
      in if s = "" then [] else [Pretty.brk 4, Pretty.str s] end;
wenzelm@12070
   351
    fun prt_fix (x, Some T, syn) = Pretty.block (Pretty.str (x ^ " ::") :: Pretty.brk 1 ::
wenzelm@12070
   352
          prt_typ T :: Pretty.brk 1 :: prt_syn syn)
wenzelm@12070
   353
      | prt_fix (x, None, syn) = Pretty.block (Pretty.str x :: Pretty.brk 1 :: prt_syn syn);
wenzelm@12070
   354
wenzelm@12307
   355
    fun prt_name "" = [Pretty.brk 1]
wenzelm@12307
   356
      | prt_name name = [Pretty.str (ProofContext.cond_extern ctxt name ^ ":"), Pretty.brk 1];
wenzelm@12307
   357
    fun prt_asm ((a, _), ts) = Pretty.block (prt_name a @ Pretty.breaks (map (prt_term o fst) ts));
wenzelm@12307
   358
    fun prt_def ((a, _), (t, _)) = Pretty.block (prt_name a @ [prt_term t]);
wenzelm@12307
   359
    fun prt_fact ((a, _), ths) = Pretty.block
wenzelm@12307
   360
      (prt_name a @ Pretty.breaks (map prt_thm (flat (map fst ths))));
wenzelm@12070
   361
wenzelm@12289
   362
    fun items _ [] = []
wenzelm@12289
   363
      | items prfx (x :: xs) = Pretty.block [Pretty.str prfx, Pretty.brk 1, x] :: items "  and" xs;
wenzelm@12289
   364
    fun prt_elem (Fixes fixes) = items "fixes" (map prt_fix fixes)
wenzelm@12289
   365
      | prt_elem (Assumes asms) = items "assumes" (map prt_asm asms)
wenzelm@12289
   366
      | prt_elem (Defines defs) = items "defines" (map prt_def defs)
wenzelm@12289
   367
      | prt_elem (Notes facts) = items "notes" (map prt_fact facts);
wenzelm@12277
   368
  in
wenzelm@12307
   369
    Pretty.big_list "locale elements:" (map (Pretty.chunks o prt_elem) (flat (map #2 elems)))
wenzelm@12307
   370
    |> Pretty.writeln
wenzelm@12277
   371
  end;
wenzelm@12070
   372
wenzelm@12070
   373
wenzelm@12070
   374
wenzelm@11896
   375
(** define locales **)
wenzelm@11896
   376
wenzelm@12277
   377
(* closeup -- quantify dangling frees *)
wenzelm@12063
   378
wenzelm@12063
   379
fun close_frees_wrt ctxt t =
wenzelm@12063
   380
  let val frees = rev (filter_out (ProofContext.is_fixed ctxt o #1) (Drule.add_frees ([], t)))
wenzelm@12063
   381
  in curry Term.list_all_free frees end;
wenzelm@11896
   382
wenzelm@12063
   383
fun closeup ctxt (Assumes asms) = Assumes (asms |> map (fn (a, propps) =>
wenzelm@12063
   384
      (a, propps |> map (fn (t, (ps1, ps2)) =>
wenzelm@12063
   385
        let val close = close_frees_wrt ctxt t in (close t, (map close ps1, map close ps2)) end))))
wenzelm@12063
   386
  | closeup ctxt (Defines defs) = Defines (defs |> map (fn (a, (t, ps)) =>
wenzelm@12063
   387
      let
wenzelm@12084
   388
        val (_, t') = ProofContext.cert_def ctxt t;
wenzelm@12063
   389
        val close = close_frees_wrt ctxt t';
wenzelm@12063
   390
      in (a, (close t', map close ps)) end))
wenzelm@12063
   391
  | closeup ctxt elem = elem;
wenzelm@11896
   392
wenzelm@11896
   393
wenzelm@12063
   394
(* add_locale(_i) *)
wenzelm@11896
   395
wenzelm@12273
   396
fun gen_add_locale prep_expr prep_element bname raw_import raw_elements thy =
wenzelm@12063
   397
  let
wenzelm@12063
   398
    val sign = Theory.sign_of thy;
wenzelm@11896
   399
    val name = Sign.full_name sign bname;
wenzelm@12063
   400
    val _ =
wenzelm@12063
   401
      if is_none (get_locale thy name) then () else
wenzelm@12063
   402
      error ("Duplicate definition of locale " ^ quote name);
wenzelm@11896
   403
wenzelm@12273
   404
    val thy_ctxt = ProofContext.init thy;
wenzelm@12273
   405
wenzelm@12273
   406
    val import = prep_expr thy_ctxt raw_import;
wenzelm@12277
   407
    val (import_elems, import_params) = eval_expr thy_ctxt import;
wenzelm@12277
   408
    val import_ctxt = activate_locale_elems import_elems thy_ctxt;
wenzelm@12273
   409
wenzelm@12273
   410
    fun prep (ctxt, raw_element) =
wenzelm@12277
   411
      let val elems = map (apsnd (map (closeup ctxt)))
wenzelm@12307
   412
        (eval_element ctxt (prep_element ctxt raw_element))
wenzelm@12277
   413
      in (activate_locale_elems elems ctxt, flat (map #2 elems)) end;
wenzelm@12273
   414
    val (locale_ctxt, elemss) = foldl_map prep (import_ctxt, raw_elements);
wenzelm@12273
   415
wenzelm@12273
   416
    val elems = flat elemss;
wenzelm@12277
   417
    val local_params =  (* FIXME lookup final types *)
wenzelm@12273
   418
      flat (map (fn Fixes fixes => map (fn (x, T, _) => (x, T)) fixes | _ => []) elems);
wenzelm@12289
   419
    val params = map (rpair None) import_params @ local_params;  (* FIXME *)
wenzelm@12273
   420
    val text = ([], []);  (* FIXME *)
wenzelm@12063
   421
  in
wenzelm@12063
   422
    thy
wenzelm@12063
   423
    |> declare_locale name
wenzelm@12289
   424
    |> put_locale name (make_locale import (map (fn e => (e, stamp ())) elems)
wenzelm@12289
   425
      (params, local_params) text)
wenzelm@12063
   426
  end;
wenzelm@12063
   427
wenzelm@12273
   428
val add_locale = gen_add_locale read_expr read_element;
wenzelm@12063
   429
val add_locale_i = gen_add_locale (K I) (K I);
wenzelm@12063
   430
wenzelm@11896
   431
wenzelm@12063
   432
wenzelm@12070
   433
(** store results **)
wenzelm@11896
   434
wenzelm@12143
   435
fun add_thmss name args thy =
wenzelm@12063
   436
  let
wenzelm@12289
   437
    val {import, params, elems, text} = the_locale thy name;
wenzelm@12143
   438
    val note = Notes (map (fn ((a, ths), atts) =>
wenzelm@12143
   439
      ((a, atts), [(map (curry Thm.name_thm a) ths, [])])) args);
wenzelm@12063
   440
  in
wenzelm@12273
   441
    thy |> ProofContext.init |> activate_locale_i name |> activate_elem note;  (*test attributes!*)
wenzelm@12289
   442
    thy |> put_locale name (make_locale import (elems @ [(note, stamp ())]) params text)
wenzelm@12063
   443
  end;
wenzelm@11896
   444
wenzelm@12063
   445
wenzelm@11896
   446
wenzelm@11896
   447
(** locale theory setup **)
wenzelm@12063
   448
wenzelm@11896
   449
val setup =
wenzelm@11896
   450
 [LocalesData.init];
wenzelm@11896
   451
wenzelm@11896
   452
end;