src/Pure/Isar/locale.ML
author wenzelm
Thu Nov 22 23:45:57 2001 +0100 (2001-11-22)
changeset 12273 7fb9840d358d
parent 12263 6f2acf10e2a2
child 12277 2b28d7dd91f5
permissions -rw-r--r--
beginnings of actual 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@12046
    22
  datatype ('typ, 'term, 'fact, 'att) elem =
wenzelm@12058
    23
    Fixes of (string * 'typ option * mixfix option) list |
wenzelm@12046
    24
    Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list |
wenzelm@12046
    25
    Defines of ((string * 'att list) * ('term * 'term list)) list |
wenzelm@12273
    26
    Notes of ((string * 'att list) * ('fact * 'att list) list) list
wenzelm@12273
    27
  datatype expr =
wenzelm@12273
    28
    Locale of string |
wenzelm@12273
    29
    Rename of expr * string option list |
wenzelm@12273
    30
    Merge of expr list
wenzelm@12273
    31
  val empty: expr
wenzelm@12273
    32
  datatype ('typ, 'term, 'fact, 'att) elem_expr =
wenzelm@12273
    33
    Elem of ('typ, 'term, 'fact, 'att) elem | Expr of expr
wenzelm@12046
    34
  type 'att element
wenzelm@12046
    35
  type 'att element_i
wenzelm@12046
    36
  type locale
wenzelm@12273
    37
  val the_locale: theory -> string -> locale  (* FIXME *)
wenzelm@12046
    38
  val intern: Sign.sg -> xstring -> string
wenzelm@12014
    39
  val cond_extern: Sign.sg -> string -> xstring
wenzelm@12273
    40
  val attribute: ('att -> context attribute) -> ('typ, 'term, 'thm, 'att) elem_expr
wenzelm@12273
    41
    -> ('typ, 'term, 'thm, context attribute) elem_expr
wenzelm@12046
    42
  val activate_elements: context attribute element list -> context -> context
wenzelm@12046
    43
  val activate_elements_i: context attribute element_i list -> context -> context
wenzelm@12046
    44
  val activate_locale: xstring -> context -> context
wenzelm@12046
    45
  val activate_locale_i: string -> context -> context
wenzelm@12273
    46
  val add_locale: bstring -> expr -> context attribute element list -> theory -> theory
wenzelm@12273
    47
  val add_locale_i: bstring -> expr -> context attribute element_i list -> theory -> theory
wenzelm@12273
    48
  val add_thmss: string -> ((string * thm list) * context attribute list) list -> theory -> theory
wenzelm@11896
    49
  val setup: (theory -> theory) list
wenzelm@11896
    50
end;
wenzelm@11896
    51
wenzelm@11896
    52
structure Locale: LOCALE =
wenzelm@11896
    53
struct
wenzelm@11896
    54
wenzelm@11896
    55
wenzelm@12273
    56
(** locale elements and expressions **)
wenzelm@11896
    57
wenzelm@12014
    58
type context = ProofContext.context;
wenzelm@11896
    59
wenzelm@12046
    60
datatype ('typ, 'term, 'fact, 'att) elem =
wenzelm@12058
    61
  Fixes of (string * 'typ option * mixfix option) list |
wenzelm@12046
    62
  Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list |
wenzelm@12046
    63
  Defines of ((string * 'att list) * ('term * 'term list)) list |
wenzelm@12273
    64
  Notes of ((string * 'att list) * ('fact * 'att list) list) list;
wenzelm@12273
    65
wenzelm@12273
    66
datatype expr =
wenzelm@12273
    67
  Locale of string |
wenzelm@12273
    68
  Rename of expr * string option list |
wenzelm@12273
    69
  Merge of expr list;
wenzelm@11896
    70
wenzelm@12273
    71
val empty = Merge [];
wenzelm@12273
    72
wenzelm@12273
    73
datatype ('typ, 'term, 'fact, 'att) elem_expr =
wenzelm@12273
    74
  Elem of ('typ, 'term, 'fact, 'att) elem | Expr of expr;
wenzelm@12273
    75
wenzelm@12273
    76
type 'att element = (string, string, string, 'att) elem_expr;
wenzelm@12273
    77
type 'att element_i = (typ, term, thm list, 'att) elem_expr;
wenzelm@12070
    78
wenzelm@12070
    79
type locale =
wenzelm@12273
    80
 {import: expr,                                                     (*dynamic import*)
wenzelm@12273
    81
  elems: (typ, term, thm list, context attribute) elem list,        (*static content*)
wenzelm@12273
    82
  params: (string * typ option) list * (string * typ option) list,  (*all vs. local params*)
wenzelm@12273
    83
  text: (string * typ) list * term list,                            (*logical representation*)
wenzelm@12273
    84
  closed: bool};                                                    (*disallow further notes*)
wenzelm@12063
    85
wenzelm@12273
    86
fun make_locale import elems params text closed =
wenzelm@12273
    87
 {import = import, elems = elems, params = params, text = text, closed = closed}: locale;
wenzelm@12063
    88
wenzelm@11896
    89
wenzelm@11896
    90
wenzelm@11896
    91
(** theory data **)
wenzelm@11896
    92
wenzelm@11896
    93
structure LocalesArgs =
wenzelm@11896
    94
struct
wenzelm@12014
    95
  val name = "Isar/locales";
wenzelm@12063
    96
  type T = NameSpace.T * locale Symtab.table;
wenzelm@11896
    97
wenzelm@12063
    98
  val empty = (NameSpace.empty, Symtab.empty);
wenzelm@12063
    99
  val copy = I;
wenzelm@12118
   100
  val prep_ext = I;
wenzelm@12273
   101
  fun merge ((space1, locs1), (space2, locs2)) =
wenzelm@12273
   102
    (NameSpace.merge (space1, space2), Symtab.merge (K true) (locs1, locs2));
wenzelm@12273
   103
  fun finish (space, locs) = (space, Symtab.map (fn {import, elems, params, text, closed = _} =>
wenzelm@12273
   104
    make_locale import elems params text true) locs);
wenzelm@12273
   105
  fun print _ (space, locs) =
wenzelm@12273
   106
    Pretty.strs ("locales:" :: map (NameSpace.cond_extern space o #1) (Symtab.dest locs))
wenzelm@12014
   107
    |> Pretty.writeln;
wenzelm@11896
   108
end;
wenzelm@11896
   109
wenzelm@11896
   110
structure LocalesData = TheoryDataFun(LocalesArgs);
wenzelm@11896
   111
val print_locales = LocalesData.print;
wenzelm@11896
   112
wenzelm@12063
   113
fun declare_locale name =
wenzelm@12063
   114
  LocalesData.map (apfst (fn space => (NameSpace.extend (space, [name]))));
wenzelm@11896
   115
wenzelm@12273
   116
fun put_locale name loc = LocalesData.map (apsnd (fn locs => Symtab.update ((name, loc), locs)));
wenzelm@12063
   117
fun get_locale thy name = Symtab.lookup (#2 (LocalesData.get thy), name);
wenzelm@11896
   118
wenzelm@12014
   119
fun the_locale thy name =
wenzelm@12014
   120
  (case get_locale thy name of
wenzelm@12014
   121
    Some loc => loc
wenzelm@12014
   122
  | None => error ("Unknown locale " ^ quote name));
wenzelm@11896
   123
wenzelm@11896
   124
wenzelm@12046
   125
wenzelm@12273
   126
(** internalization **)
wenzelm@12273
   127
wenzelm@12273
   128
(* names *)
wenzelm@12273
   129
wenzelm@12273
   130
val intern = NameSpace.intern o #1 o LocalesData.get_sg;
wenzelm@12273
   131
val cond_extern = NameSpace.cond_extern o #1 o LocalesData.get_sg;
wenzelm@12273
   132
wenzelm@12063
   133
wenzelm@12263
   134
(* read *)
wenzelm@12263
   135
wenzelm@12063
   136
fun read_elem ctxt =
wenzelm@12063
   137
 fn Fixes fixes =>
wenzelm@12063
   138
      let val vars =
wenzelm@12063
   139
        #2 (foldl_map ProofContext.read_vars (ctxt, map (fn (x, T, _) => ([x], T)) fixes))
wenzelm@12063
   140
      in Fixes (map2 (fn (([x'], T'), (_, _, mx)) => (x', T', mx)) (vars, fixes)) end
wenzelm@12063
   141
  | Assumes asms =>
wenzelm@12063
   142
      Assumes (map #1 asms ~~ #2 (ProofContext.read_propp (ctxt, map #2 asms)))
wenzelm@12063
   143
  | Defines defs =>
wenzelm@12063
   144
      let val propps =
wenzelm@12063
   145
        #2 (ProofContext.read_propp (ctxt, map (fn (_, (t, ps)) => [(t, (ps, []))]) defs))
wenzelm@12063
   146
      in Defines (map #1 defs ~~ map (fn [(t', (ps', []))] => (t', ps')) propps) end
wenzelm@12063
   147
  | Notes facts =>
wenzelm@12273
   148
      Notes (map (apsnd (map (apfst (ProofContext.get_thms ctxt)))) facts);
wenzelm@12273
   149
wenzelm@12273
   150
fun read_expr ctxt (Locale xname) = Locale (intern (ProofContext.sign_of ctxt) xname)
wenzelm@12273
   151
  | read_expr ctxt (Merge exprs) = Merge (map (read_expr ctxt) exprs)
wenzelm@12273
   152
  | read_expr ctxt (Rename (expr, xs)) =
wenzelm@12273
   153
      (case duplicates (mapfilter I xs) of [] => Rename (read_expr ctxt expr, xs)
wenzelm@12273
   154
      | ds => raise ProofContext.CONTEXT ("Duplicates in renaming: " ^ commas_quote ds, ctxt));;
wenzelm@12273
   155
wenzelm@12273
   156
fun read_element ctxt (Elem e) = Elem (read_elem ctxt e)
wenzelm@12273
   157
  | read_element ctxt (Expr e) = Expr (read_expr ctxt e);
wenzelm@12063
   158
wenzelm@12063
   159
wenzelm@12273
   160
(* attribute *)
wenzelm@12063
   161
wenzelm@12063
   162
local fun int_att attrib (x, srcs) = (x, map attrib srcs) in
wenzelm@12063
   163
wenzelm@12273
   164
fun attribute _ (Elem (Fixes fixes)) = Elem (Fixes fixes)
wenzelm@12273
   165
  | attribute attrib (Elem (Assumes asms)) = Elem (Assumes (map (apfst (int_att attrib)) asms))
wenzelm@12273
   166
  | attribute attrib (Elem (Defines defs)) = Elem (Defines (map (apfst (int_att attrib)) defs))
wenzelm@12273
   167
  | attribute attrib (Elem (Notes facts)) =
wenzelm@12273
   168
      Elem (Notes (map (apfst (int_att attrib) o apsnd (map (int_att attrib))) facts))
wenzelm@12273
   169
  | attribute _ (Expr expr) = Expr expr;
wenzelm@12063
   170
wenzelm@12063
   171
end;
wenzelm@12046
   172
wenzelm@12046
   173
wenzelm@12273
   174
wenzelm@12263
   175
(** renaming **)
wenzelm@12263
   176
wenzelm@12263
   177
fun rename ren x = if_none (assoc_string (ren, x)) x;
wenzelm@12263
   178
wenzelm@12263
   179
fun rename_term ren (Free (x, T)) = Free (rename ren x, T)
wenzelm@12263
   180
  | rename_term ren (t $ u) = rename_term ren t $ rename_term ren u
wenzelm@12263
   181
  | rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t)
wenzelm@12263
   182
  | rename_term _ a = a;
wenzelm@12263
   183
wenzelm@12263
   184
fun rename_thm ren th =
wenzelm@12263
   185
  let
wenzelm@12263
   186
    val {sign, hyps, prop, maxidx, ...} = Thm.rep_thm th;
wenzelm@12263
   187
    val cert = Thm.cterm_of sign;
wenzelm@12263
   188
    val (xs, Ts) = Library.split_list (foldl Drule.add_frees ([], prop :: hyps));
wenzelm@12263
   189
    val xs' = map (rename ren) xs;
wenzelm@12263
   190
    fun cert_frees names = map (cert o Free) (names ~~ Ts);
wenzelm@12263
   191
    fun cert_vars names = map (cert o Var o apfst (rpair (maxidx + 1))) (names ~~ Ts);
wenzelm@12263
   192
  in
wenzelm@12263
   193
    if xs = xs' then th
wenzelm@12263
   194
    else
wenzelm@12263
   195
      th
wenzelm@12263
   196
      |> Drule.implies_intr_list (map cert hyps)
wenzelm@12263
   197
      |> Drule.forall_intr_list (cert_frees xs)
wenzelm@12263
   198
      |> Drule.forall_elim_list (cert_vars xs)
wenzelm@12263
   199
      |> Thm.instantiate ([], cert_vars xs ~~ cert_frees xs')
wenzelm@12263
   200
      |> (fn th' => Drule.implies_elim_list th' (map (Thm.assume o cert o rename_term ren) hyps))
wenzelm@12263
   201
  end;
wenzelm@12263
   202
wenzelm@12263
   203
fun rename_elem ren (Fixes fixes) = Fixes (map (fn (x, T, mx) =>
wenzelm@12273
   204
      (rename ren x, T, if mx = None then mx else Some Syntax.NoSyn)) fixes)    (*drops syntax!*)
wenzelm@12263
   205
  | rename_elem ren (Assumes asms) = Assumes (map (apsnd (map (fn (t, (ps, qs)) =>
wenzelm@12263
   206
      (rename_term ren t, (map (rename_term ren) ps, map (rename_term ren) qs))))) asms)
wenzelm@12263
   207
  | rename_elem ren (Defines defs) = Defines (map (apsnd (fn (t, ps) =>
wenzelm@12263
   208
      (rename_term ren t, map (rename_term ren) ps))) defs)
wenzelm@12273
   209
  | rename_elem ren (Notes facts) = Notes (map (apsnd (map (apfst (map (rename_thm ren))))) facts);
wenzelm@12263
   210
wenzelm@12263
   211
wenzelm@11896
   212
wenzelm@12273
   213
(** evaluation **)
wenzelm@11896
   214
wenzelm@12273
   215
fun eval_expr ctxt expr =
wenzelm@12014
   216
  let
wenzelm@12014
   217
    val thy = ProofContext.theory_of ctxt;
wenzelm@12263
   218
wenzelm@12273
   219
    fun renaming (Some x :: xs) (y :: ys) = (y, x) :: renaming xs ys
wenzelm@12273
   220
      | renaming (None :: xs) (y :: ys) = renaming xs ys
wenzelm@12273
   221
      | renaming [] _ = []
wenzelm@12273
   222
      | renaming xs [] = raise ProofContext.CONTEXT ("Extraneous arguments in renaming: " ^
wenzelm@12273
   223
          commas (map (fn None => "_" | Some x => quote x) xs), ctxt);
wenzelm@12263
   224
wenzelm@12273
   225
    fun identify ((ids, parms), Locale name) =
wenzelm@12273
   226
          let
wenzelm@12273
   227
            val {import, params, ...} = the_locale thy name;
wenzelm@12273
   228
            val ps = map #1 (#1 params);
wenzelm@12273
   229
          in
wenzelm@12273
   230
            if (name, ps) mem ids then (ids, parms)
wenzelm@12273
   231
            else identify (((name, ps) :: ids, merge_lists parms ps), import)
wenzelm@12273
   232
          end
wenzelm@12273
   233
      | identify ((ids, parms), Rename (e, xs)) =
wenzelm@12273
   234
          let
wenzelm@12273
   235
            val (ids', parms') = identify (([], []), e);
wenzelm@12273
   236
            val ren = renaming xs parms';
wenzelm@12273
   237
            val ids'' = map (apsnd (map (rename ren))) ids' \\ ids;
wenzelm@12273
   238
          in (ids'' @ ids, merge_lists parms (map (rename ren) parms')) end
wenzelm@12273
   239
      | identify (arg, Merge es) = foldl identify (arg, es);
wenzelm@12014
   240
wenzelm@12273
   241
    val idents = rev (#1 (identify (([], []), expr)));
wenzelm@12273
   242
    val FIXME = PolyML.print idents;
wenzelm@12273
   243
wenzelm@12273
   244
    fun eval (name, ps') =
wenzelm@12273
   245
      let
wenzelm@12273
   246
        val {params = (ps, _), elems, ...} = the_locale thy name;
wenzelm@12273
   247
        val ren = filter_out (op =) (map #1 ps ~~ ps');
wenzelm@12273
   248
      in
wenzelm@12273
   249
        if null ren then ((name, ps), elems)
wenzelm@12273
   250
        else ((name, map (apfst (rename ren)) ps), map (rename_elem ren) elems)
wenzelm@12273
   251
      end;
wenzelm@12273
   252
    (* FIXME unify types; specific errors (name) *)
wenzelm@12273
   253
wenzelm@12273
   254
  in map eval idents end;
wenzelm@12273
   255
wenzelm@12273
   256
fun eval_elements ctxt =
wenzelm@12273
   257
  flat o map (fn Elem e => [e] | Expr e => flat (map #2 (eval_expr ctxt e)));
wenzelm@12046
   258
wenzelm@12070
   259
wenzelm@12273
   260
wenzelm@12273
   261
(** activation **)
wenzelm@12070
   262
wenzelm@12273
   263
fun activate_elem (Fixes fixes) = ProofContext.add_syntax fixes o
wenzelm@12273
   264
      ProofContext.fix_direct (map (fn (x, T, mx) => ([x], T)) fixes)
wenzelm@12273
   265
  | activate_elem (Assumes asms) =
wenzelm@12273
   266
      #1 o ProofContext.assume_i ProofContext.export_assume asms o
wenzelm@12273
   267
      ProofContext.fix_frees (flat (map (map #1 o #2) asms))
wenzelm@12273
   268
  | activate_elem (Defines defs) = (fn ctxt => #1 (ProofContext.assume_i ProofContext.export_def
wenzelm@12273
   269
      (map (fn ((name, atts), (t, ps)) =>
wenzelm@12273
   270
        let val (c, t') = ProofContext.cert_def ctxt t
wenzelm@12273
   271
        in ((if name = "" then Thm.def_name c else name, atts), [(t', (ps, []))]) end) defs) ctxt))
wenzelm@12273
   272
  | activate_elem (Notes facts) = #1 o ProofContext.have_thmss facts;
wenzelm@12263
   273
wenzelm@12273
   274
fun activate_elems es ctxt = foldl (fn (c, e) => activate_elem e c) (ctxt, es);
wenzelm@12273
   275
wenzelm@12273
   276
fun activate_elements_i elements ctxt = activate_elems (eval_elements ctxt elements) ctxt;
wenzelm@12273
   277
fun activate_elements elements ctxt = activate_elements_i (map (read_element ctxt) elements) ctxt;
wenzelm@12273
   278
wenzelm@12273
   279
fun activate_locale_i name = activate_elements_i [Expr (Locale name)];
wenzelm@12273
   280
fun activate_locale xname = activate_elements [Expr (Locale xname)];
wenzelm@11896
   281
wenzelm@11896
   282
wenzelm@11896
   283
wenzelm@12070
   284
(** print locale **)
wenzelm@12070
   285
wenzelm@12070
   286
fun pretty_locale thy xname =
wenzelm@12070
   287
  let
wenzelm@12070
   288
    val sg = Theory.sign_of thy;
wenzelm@12070
   289
    val name = intern sg xname;
wenzelm@12273
   290
    val {import, elems, params = _, text = _, closed = _} = the_locale thy name;
wenzelm@12070
   291
    val locale_ctxt = ProofContext.init thy |> activate_locale_i name;
wenzelm@12070
   292
wenzelm@12070
   293
    val prt_typ = Pretty.quote o ProofContext.pretty_typ locale_ctxt;
wenzelm@12070
   294
    val prt_term = Pretty.quote o ProofContext.pretty_term locale_ctxt;
wenzelm@12070
   295
    val prt_thm = Pretty.quote o ProofContext.pretty_thm locale_ctxt;
wenzelm@12070
   296
wenzelm@12070
   297
    fun prt_syn syn =
wenzelm@12070
   298
      let val s = (case syn of None => "(structure)" | Some mx => Syntax.string_of_mixfix mx)
wenzelm@12070
   299
      in if s = "" then [] else [Pretty.brk 4, Pretty.str s] end;
wenzelm@12070
   300
    fun prt_fix (x, Some T, syn) = Pretty.block (Pretty.str (x ^ " ::") :: Pretty.brk 1 ::
wenzelm@12070
   301
          prt_typ T :: Pretty.brk 1 :: prt_syn syn)
wenzelm@12070
   302
      | prt_fix (x, None, syn) = Pretty.block (Pretty.str x :: Pretty.brk 1 :: prt_syn syn);
wenzelm@12070
   303
wenzelm@12070
   304
    fun prt_asm (("", _), ts) = Pretty.block (Pretty.breaks (map (prt_term o fst) ts))
wenzelm@12070
   305
      | prt_asm ((a, _), ts) = Pretty.block
wenzelm@12070
   306
          (Pretty.breaks (Pretty.str (a ^ ":") :: map (prt_term o fst) ts));
wenzelm@12070
   307
    fun prt_asms asms = Pretty.block
wenzelm@12070
   308
      (flat (separate [Pretty.fbrk, Pretty.str "and"] (map (single o prt_asm) asms)));
wenzelm@12070
   309
wenzelm@12070
   310
    fun prt_def (("", _), (t, _)) = Pretty.block [Pretty.brk 1, prt_term t]
wenzelm@12070
   311
      | prt_def ((a, _), (t, _)) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term t];
wenzelm@12070
   312
wenzelm@12070
   313
    fun prt_fact (("", _), ths) = Pretty.block (Pretty.breaks (map prt_thm (flat (map fst ths))))
wenzelm@12070
   314
      | prt_fact ((a, _), ths) = Pretty.block
wenzelm@12070
   315
          (Pretty.breaks (Pretty.str (a ^ ":") :: map prt_thm (flat (map fst ths))));
wenzelm@12070
   316
wenzelm@12263
   317
    fun prt_expr (Locale name) = Pretty.str (cond_extern sg name)
wenzelm@12263
   318
      | prt_expr (Merge exprs) = Pretty.enclose "(" ")"
wenzelm@12263
   319
          (flat (separate [Pretty.str " +", Pretty.brk 1]
wenzelm@12263
   320
            (map (single o prt_expr) exprs)))
wenzelm@12263
   321
      | prt_expr (Rename (expr, xs)) = Pretty.enclose "(" ")"
wenzelm@12263
   322
          (Pretty.breaks (prt_expr expr :: map (fn x => Pretty.str (if_none x "_")) xs));
wenzelm@12263
   323
wenzelm@12070
   324
    fun prt_elem (Fixes fixes) = Pretty.big_list "fixes" (map prt_fix fixes)
wenzelm@12070
   325
      | prt_elem (Assumes asms) = Pretty.big_list "assumes" (map prt_asm asms)
wenzelm@12070
   326
      | prt_elem (Defines defs) = Pretty.big_list "defines" (map prt_def defs)
wenzelm@12273
   327
      | prt_elem (Notes facts) = Pretty.big_list "notes" (map prt_fact facts);
wenzelm@12070
   328
wenzelm@12070
   329
    val prt_header = Pretty.block (Pretty.str ("locale " ^ cond_extern sg name ^ " =") ::
wenzelm@12273
   330
       (if import = empty then [] else [Pretty.str " ", prt_expr import] @
wenzelm@12273
   331
         (if null elems then [] else [Pretty.str " +"]) @ [Pretty.brk 1]));
wenzelm@12273
   332
  in Pretty.block (Pretty.fbreaks (prt_header :: map prt_elem elems)) end;
wenzelm@12070
   333
wenzelm@12070
   334
val print_locale = Pretty.writeln oo pretty_locale;
wenzelm@12070
   335
wenzelm@12070
   336
wenzelm@12070
   337
wenzelm@11896
   338
(** define locales **)
wenzelm@11896
   339
wenzelm@12063
   340
(* closeup dangling frees *)
wenzelm@12063
   341
wenzelm@12063
   342
fun close_frees_wrt ctxt t =
wenzelm@12063
   343
  let val frees = rev (filter_out (ProofContext.is_fixed ctxt o #1) (Drule.add_frees ([], t)))
wenzelm@12063
   344
  in curry Term.list_all_free frees end;
wenzelm@11896
   345
wenzelm@12063
   346
fun closeup ctxt (Assumes asms) = Assumes (asms |> map (fn (a, propps) =>
wenzelm@12063
   347
      (a, propps |> map (fn (t, (ps1, ps2)) =>
wenzelm@12063
   348
        let val close = close_frees_wrt ctxt t in (close t, (map close ps1, map close ps2)) end))))
wenzelm@12063
   349
  | closeup ctxt (Defines defs) = Defines (defs |> map (fn (a, (t, ps)) =>
wenzelm@12063
   350
      let
wenzelm@12084
   351
        val (_, t') = ProofContext.cert_def ctxt t;
wenzelm@12063
   352
        val close = close_frees_wrt ctxt t';
wenzelm@12063
   353
      in (a, (close t', map close ps)) end))
wenzelm@12063
   354
  | closeup ctxt elem = elem;
wenzelm@11896
   355
wenzelm@11896
   356
wenzelm@12063
   357
(* add_locale(_i) *)
wenzelm@11896
   358
wenzelm@12273
   359
fun gen_add_locale prep_expr prep_element bname raw_import raw_elements thy =
wenzelm@12063
   360
  let
wenzelm@12063
   361
    val sign = Theory.sign_of thy;
wenzelm@11896
   362
    val name = Sign.full_name sign bname;
wenzelm@12063
   363
    val _ =
wenzelm@12063
   364
      if is_none (get_locale thy name) then () else
wenzelm@12063
   365
      error ("Duplicate definition of locale " ^ quote name);
wenzelm@11896
   366
wenzelm@12273
   367
    val thy_ctxt = ProofContext.init thy;
wenzelm@12273
   368
wenzelm@12273
   369
    val import = prep_expr thy_ctxt raw_import;
wenzelm@12273
   370
    val (import_ids, import_elemss) = split_list (eval_expr thy_ctxt import);
wenzelm@12273
   371
    val import_ctxt = activate_elems (flat import_elemss) thy_ctxt;
wenzelm@12273
   372
wenzelm@12273
   373
    fun prep (ctxt, raw_element) =
wenzelm@12273
   374
      let val elems = map (closeup ctxt) (eval_elements ctxt [prep_element ctxt raw_element]);
wenzelm@12273
   375
      in (activate_elems elems ctxt, elems) end;
wenzelm@12273
   376
    val (locale_ctxt, elemss) = foldl_map prep (import_ctxt, raw_elements);
wenzelm@12273
   377
wenzelm@12273
   378
    val elems = flat elemss;
wenzelm@12273
   379
    val local_ps =  (* FIXME proper types *)
wenzelm@12273
   380
      flat (map (fn Fixes fixes => map (fn (x, T, _) => (x, T)) fixes | _ => []) elems);
wenzelm@12273
   381
    val all_ps = distinct (flat (map #2 import_ids)) @ local_ps;
wenzelm@12273
   382
    val text = ([], []);  (* FIXME *)
wenzelm@12063
   383
  in
wenzelm@12063
   384
    thy
wenzelm@12063
   385
    |> declare_locale name
wenzelm@12273
   386
    |> put_locale name (make_locale import elems (all_ps, local_ps) text false)
wenzelm@12063
   387
  end;
wenzelm@12063
   388
wenzelm@12273
   389
val add_locale = gen_add_locale read_expr read_element;
wenzelm@12063
   390
val add_locale_i = gen_add_locale (K I) (K I);
wenzelm@12063
   391
wenzelm@11896
   392
wenzelm@12063
   393
wenzelm@12070
   394
(** store results **)
wenzelm@11896
   395
wenzelm@12143
   396
fun add_thmss name args thy =
wenzelm@12063
   397
  let
wenzelm@12273
   398
    val {import, params, elems, text, closed} = the_locale thy name;
wenzelm@12143
   399
    val _ = conditional closed (fn () =>
wenzelm@12143
   400
      error ("Cannot store results in closed locale: " ^ quote name));
wenzelm@12143
   401
    val note = Notes (map (fn ((a, ths), atts) =>
wenzelm@12143
   402
      ((a, atts), [(map (curry Thm.name_thm a) ths, [])])) args);
wenzelm@12063
   403
  in
wenzelm@12273
   404
    thy |> ProofContext.init |> activate_locale_i name |> activate_elem note;  (*test attributes!*)
wenzelm@12273
   405
    thy |> put_locale name (make_locale import (elems @ [note]) params text closed)
wenzelm@12063
   406
  end;
wenzelm@11896
   407
wenzelm@12063
   408
wenzelm@11896
   409
wenzelm@11896
   410
(** locale theory setup **)
wenzelm@12063
   411
wenzelm@11896
   412
val setup =
wenzelm@11896
   413
 [LocalesData.init];
wenzelm@11896
   414
wenzelm@11896
   415
end;
wenzelm@11896
   416
wenzelm@11896
   417
structure BasicLocale: BASIC_LOCALE = Locale;
wenzelm@11896
   418
open BasicLocale;