src/Pure/Isar/locale.ML
author wenzelm
Fri Nov 09 00:16:07 2001 +0100 (2001-11-09)
changeset 12118 3d62ee5bec5e
parent 12084 2f794ad3c015
child 12143 dc42d17c5b53
permissions -rw-r--r--
proper close_locale;
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@12063
     8
Kammueller'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@12014
    22
  type expression
wenzelm@12046
    23
  datatype ('typ, 'term, 'fact, 'att) elem =
wenzelm@12058
    24
    Fixes of (string * 'typ option * mixfix option) list |
wenzelm@12046
    25
    Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list |
wenzelm@12046
    26
    Defines of ((string * 'att list) * ('term * 'term list)) list |
wenzelm@12046
    27
    Notes of ((string * 'att list) * ('fact * 'att list) list) list |
wenzelm@12046
    28
    Uses of expression
wenzelm@12046
    29
  type 'att element
wenzelm@12046
    30
  type 'att element_i
wenzelm@12046
    31
  type locale
wenzelm@12046
    32
  val intern: Sign.sg -> xstring -> string
wenzelm@12014
    33
  val cond_extern: Sign.sg -> string -> xstring
wenzelm@12063
    34
  val attribute: ('att -> context attribute) ->
wenzelm@12046
    35
    ('typ, 'term, 'thm, 'att) elem -> ('typ, 'term, 'thm, context attribute) elem
wenzelm@12046
    36
  val activate_elements: context attribute element list -> context -> context
wenzelm@12046
    37
  val activate_elements_i: context attribute element_i list -> context -> context
wenzelm@12046
    38
  val activate_locale: xstring -> context -> context
wenzelm@12046
    39
  val activate_locale_i: string -> context -> context
wenzelm@12063
    40
  val add_locale: bstring -> xstring list -> context attribute element list -> theory -> theory
wenzelm@12063
    41
  val add_locale_i: bstring -> xstring list -> context attribute element_i list -> theory -> theory
wenzelm@12063
    42
  val store_thm: string -> (string * thm) * context attribute list -> theory -> theory
wenzelm@11896
    43
  val setup: (theory -> theory) list
wenzelm@11896
    44
end;
wenzelm@11896
    45
wenzelm@11896
    46
structure Locale: LOCALE =
wenzelm@11896
    47
struct
wenzelm@11896
    48
wenzelm@11896
    49
wenzelm@12014
    50
(** locale elements and locales **)
wenzelm@11896
    51
wenzelm@12014
    52
type context = ProofContext.context;
wenzelm@11896
    53
wenzelm@12070
    54
type expression = string;
wenzelm@11896
    55
wenzelm@12046
    56
datatype ('typ, 'term, 'fact, 'att) elem =
wenzelm@12058
    57
  Fixes of (string * 'typ option * mixfix option) list |
wenzelm@12046
    58
  Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list |
wenzelm@12046
    59
  Defines of ((string * 'att list) * ('term * 'term list)) list |
wenzelm@12046
    60
  Notes of ((string * 'att list) * ('fact * 'att list) list) list |
wenzelm@12014
    61
  Uses of expression;
wenzelm@11896
    62
wenzelm@12046
    63
type 'att element = (string, string, string, 'att) elem;
wenzelm@12046
    64
type 'att element_i = (typ, term, thm list, 'att) elem;
wenzelm@12070
    65
wenzelm@12070
    66
type locale =
wenzelm@12070
    67
  {imports: expression list, elements: context attribute element_i list, closed: bool};
wenzelm@12063
    68
wenzelm@12063
    69
fun make_locale imports elements closed =
wenzelm@12063
    70
  {imports = imports, elements = elements, closed = closed}: locale;
wenzelm@12046
    71
wenzelm@12118
    72
fun close_locale {imports, elements, closed = _} = make_locale imports elements true;
wenzelm@12063
    73
wenzelm@11896
    74
wenzelm@11896
    75
wenzelm@11896
    76
(** theory data **)
wenzelm@11896
    77
wenzelm@11896
    78
(* data kind 'Pure/locales' *)
wenzelm@11896
    79
wenzelm@11896
    80
structure LocalesArgs =
wenzelm@11896
    81
struct
wenzelm@12014
    82
  val name = "Isar/locales";
wenzelm@12063
    83
  type T = NameSpace.T * locale Symtab.table;
wenzelm@11896
    84
wenzelm@12063
    85
  val empty = (NameSpace.empty, Symtab.empty);
wenzelm@12063
    86
  val copy = I;
wenzelm@12118
    87
  fun finish (space, locales) = (space, Symtab.map close_locale locales);
wenzelm@12118
    88
  val prep_ext = I;
wenzelm@12063
    89
  fun merge ((space1, locales1), (space2, locales2)) =
wenzelm@12063
    90
      (NameSpace.merge (space1, space2), Symtab.merge (K true) (locales1, locales2));
wenzelm@11896
    91
wenzelm@12063
    92
  fun print _ (space, locales) =
wenzelm@12014
    93
    Pretty.strs ("locales:" :: map (NameSpace.cond_extern space o #1) (Symtab.dest locales))
wenzelm@12014
    94
    |> Pretty.writeln;
wenzelm@11896
    95
end;
wenzelm@11896
    96
wenzelm@11896
    97
structure LocalesData = TheoryDataFun(LocalesArgs);
wenzelm@11896
    98
val print_locales = LocalesData.print;
wenzelm@11896
    99
wenzelm@12063
   100
val intern = NameSpace.intern o #1 o LocalesData.get_sg;
wenzelm@12063
   101
val cond_extern = NameSpace.cond_extern o #1 o LocalesData.get_sg;
wenzelm@12014
   102
wenzelm@11896
   103
wenzelm@11896
   104
(* access locales *)
wenzelm@11896
   105
wenzelm@12063
   106
fun declare_locale name =
wenzelm@12063
   107
  LocalesData.map (apfst (fn space => (NameSpace.extend (space, [name]))));
wenzelm@11896
   108
wenzelm@12063
   109
fun put_locale name locale =
wenzelm@12063
   110
  LocalesData.map (apsnd (fn locales => Symtab.update ((name, locale), locales)));
wenzelm@12063
   111
wenzelm@12063
   112
fun get_locale thy name = Symtab.lookup (#2 (LocalesData.get thy), name);
wenzelm@11896
   113
wenzelm@12014
   114
fun the_locale thy name =
wenzelm@12014
   115
  (case get_locale thy name of
wenzelm@12014
   116
    Some loc => loc
wenzelm@12014
   117
  | None => error ("Unknown locale " ^ quote name));
wenzelm@11896
   118
wenzelm@11896
   119
wenzelm@12046
   120
wenzelm@12063
   121
(** internalize elements **)
wenzelm@12063
   122
wenzelm@12063
   123
(* read_elem *)
wenzelm@12046
   124
wenzelm@12063
   125
fun read_elem ctxt =
wenzelm@12063
   126
 fn Fixes fixes =>
wenzelm@12063
   127
      let val vars =
wenzelm@12063
   128
        #2 (foldl_map ProofContext.read_vars (ctxt, map (fn (x, T, _) => ([x], T)) fixes))
wenzelm@12063
   129
      in Fixes (map2 (fn (([x'], T'), (_, _, mx)) => (x', T', mx)) (vars, fixes)) end
wenzelm@12063
   130
  | Assumes asms =>
wenzelm@12063
   131
      Assumes (map #1 asms ~~ #2 (ProofContext.read_propp (ctxt, map #2 asms)))
wenzelm@12063
   132
  | Defines defs =>
wenzelm@12063
   133
      let val propps =
wenzelm@12063
   134
        #2 (ProofContext.read_propp (ctxt, map (fn (_, (t, ps)) => [(t, (ps, []))]) defs))
wenzelm@12063
   135
      in Defines (map #1 defs ~~ map (fn [(t', (ps', []))] => (t', ps')) propps) end
wenzelm@12063
   136
  | Notes facts =>
wenzelm@12063
   137
      Notes (map (apsnd (map (apfst (ProofContext.get_thms ctxt)))) facts)
wenzelm@12070
   138
  | Uses xname => Uses (intern (ProofContext.sign_of ctxt) xname);
wenzelm@12063
   139
wenzelm@12063
   140
wenzelm@12063
   141
(* prepare attributes *)
wenzelm@12063
   142
wenzelm@12063
   143
local fun int_att attrib (x, srcs) = (x, map attrib srcs) in
wenzelm@12063
   144
wenzelm@12063
   145
fun attribute _ (Fixes fixes) = Fixes fixes
wenzelm@12063
   146
  | attribute attrib (Assumes asms) = Assumes (map (apfst (int_att attrib)) asms)
wenzelm@12063
   147
  | attribute attrib (Defines defs) = Defines (map (apfst (int_att attrib)) defs)
wenzelm@12063
   148
  | attribute attrib (Notes facts) =
wenzelm@12046
   149
      Notes (map (apfst (int_att attrib) o apsnd (map (int_att attrib))) facts)
wenzelm@12070
   150
  | attribute _ (Uses name) = Uses name;
wenzelm@12063
   151
wenzelm@12063
   152
end;
wenzelm@12046
   153
wenzelm@12046
   154
wenzelm@11896
   155
wenzelm@12014
   156
(** activate locales **)
wenzelm@11896
   157
wenzelm@12046
   158
fun activate (ctxt, Fixes fixes) =
wenzelm@12070
   159
      ctxt |> ProofContext.fix_direct (map (fn (x, T, mx) => ([x], T)) fixes)
wenzelm@12070
   160
      |> ProofContext.add_syntax fixes
wenzelm@12058
   161
  | activate (ctxt, Assumes asms) =
wenzelm@12058
   162
      ctxt |> ProofContext.fix_frees (flat (map (map #1 o #2) asms))
wenzelm@12058
   163
      |> ProofContext.assume_i ProofContext.export_assume asms |> #1
wenzelm@12046
   164
  | activate (ctxt, Defines defs) = #1 (ProofContext.assume_i ProofContext.export_def
wenzelm@12084
   165
      (map (fn ((name, atts), (t, ps)) =>
wenzelm@12084
   166
        let val (c, t') = ProofContext.cert_def ctxt t
wenzelm@12084
   167
        in ((if name = "" then Thm.def_name c else name, atts), [(t', (ps, []))]) end) defs) ctxt)
wenzelm@12046
   168
  | activate (ctxt, Notes facts) = #1 (ProofContext.have_thmss facts ctxt)
wenzelm@12070
   169
  | activate (ctxt, Uses name) = activate_locale_i name ctxt
wenzelm@11896
   170
wenzelm@12070
   171
and activate_elements_i elems ctxt = foldl activate (ctxt, elems)
wenzelm@12046
   172
wenzelm@12070
   173
and activate_locale_elements (ctxt, name) =
wenzelm@12014
   174
  let
wenzelm@12014
   175
    val thy = ProofContext.theory_of ctxt;
wenzelm@12063
   176
    val {elements, ...} = the_locale thy name;    (*exception ERROR*)
wenzelm@12014
   177
  in
wenzelm@12063
   178
    activate_elements_i elements ctxt handle ProofContext.CONTEXT (msg, c) =>
wenzelm@12014
   179
      raise ProofContext.CONTEXT (msg ^ "\nThe error(s) above occurred in locale " ^
wenzelm@12014
   180
        quote (cond_extern (Theory.sign_of thy) name), c)
wenzelm@12070
   181
  end
wenzelm@12014
   182
wenzelm@12070
   183
and activate_locale_i name ctxt =
wenzelm@12063
   184
  activate_locale_elements (foldl activate_locale_elements
wenzelm@12063
   185
    (ctxt, #imports (the_locale (ProofContext.theory_of ctxt) name)), name);
wenzelm@12046
   186
wenzelm@12070
   187
wenzelm@12070
   188
fun activate_elements elems ctxt =
wenzelm@12070
   189
  foldl ((fn (ctxt, elem) => activate (ctxt, read_elem ctxt elem))) (ctxt, elems);
wenzelm@12070
   190
wenzelm@12046
   191
fun activate_locale xname ctxt =
wenzelm@12046
   192
  activate_locale_i (intern (ProofContext.sign_of ctxt) xname) ctxt;
wenzelm@11896
   193
wenzelm@11896
   194
wenzelm@11896
   195
wenzelm@12070
   196
(** print locale **)
wenzelm@12070
   197
wenzelm@12070
   198
fun pretty_locale thy xname =
wenzelm@12070
   199
  let
wenzelm@12070
   200
    val sg = Theory.sign_of thy;
wenzelm@12070
   201
    val name = intern sg xname;
wenzelm@12070
   202
    val {imports, elements, closed = _} = the_locale thy name;
wenzelm@12070
   203
    val locale_ctxt = ProofContext.init thy |> activate_locale_i name;
wenzelm@12070
   204
wenzelm@12070
   205
    val prt_typ = Pretty.quote o ProofContext.pretty_typ locale_ctxt;
wenzelm@12070
   206
    val prt_term = Pretty.quote o ProofContext.pretty_term locale_ctxt;
wenzelm@12070
   207
    val prt_thm = Pretty.quote o ProofContext.pretty_thm locale_ctxt;
wenzelm@12070
   208
wenzelm@12070
   209
    fun prt_syn syn =
wenzelm@12070
   210
      let val s = (case syn of None => "(structure)" | Some mx => Syntax.string_of_mixfix mx)
wenzelm@12070
   211
      in if s = "" then [] else [Pretty.brk 4, Pretty.str s] end;
wenzelm@12070
   212
    fun prt_fix (x, Some T, syn) = Pretty.block (Pretty.str (x ^ " ::") :: Pretty.brk 1 ::
wenzelm@12070
   213
          prt_typ T :: Pretty.brk 1 :: prt_syn syn)
wenzelm@12070
   214
      | prt_fix (x, None, syn) = Pretty.block (Pretty.str x :: Pretty.brk 1 :: prt_syn syn);
wenzelm@12070
   215
wenzelm@12070
   216
    fun prt_asm (("", _), ts) = Pretty.block (Pretty.breaks (map (prt_term o fst) ts))
wenzelm@12070
   217
      | prt_asm ((a, _), ts) = Pretty.block
wenzelm@12070
   218
          (Pretty.breaks (Pretty.str (a ^ ":") :: map (prt_term o fst) ts));
wenzelm@12070
   219
    fun prt_asms asms = Pretty.block
wenzelm@12070
   220
      (flat (separate [Pretty.fbrk, Pretty.str "and"] (map (single o prt_asm) asms)));
wenzelm@12070
   221
wenzelm@12070
   222
    fun prt_def (("", _), (t, _)) = Pretty.block [Pretty.brk 1, prt_term t]
wenzelm@12070
   223
      | prt_def ((a, _), (t, _)) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term t];
wenzelm@12070
   224
wenzelm@12070
   225
    fun prt_fact (("", _), ths) = Pretty.block (Pretty.breaks (map prt_thm (flat (map fst ths))))
wenzelm@12070
   226
      | prt_fact ((a, _), ths) = Pretty.block
wenzelm@12070
   227
          (Pretty.breaks (Pretty.str (a ^ ":") :: map prt_thm (flat (map fst ths))));
wenzelm@12070
   228
wenzelm@12070
   229
    fun prt_elem (Fixes fixes) = Pretty.big_list "fixes" (map prt_fix fixes)
wenzelm@12070
   230
      | prt_elem (Assumes asms) = Pretty.big_list "assumes" (map prt_asm asms)
wenzelm@12070
   231
      | prt_elem (Defines defs) = Pretty.big_list "defines" (map prt_def defs)
wenzelm@12070
   232
      | prt_elem (Notes facts) = Pretty.big_list "notes" (map prt_fact facts)
wenzelm@12070
   233
      | prt_elem (Uses name) = Pretty.str ("uses " ^ cond_extern sg name);
wenzelm@12070
   234
wenzelm@12070
   235
    val prt_header = Pretty.block (Pretty.str ("locale " ^ cond_extern sg name ^ " =") ::
wenzelm@12070
   236
       (if null imports then [] else
wenzelm@12084
   237
       (Pretty.str " " :: flat (separate [Pretty.str " +", Pretty.brk 1]
wenzelm@12084
   238
           (map (single o Pretty.str o cond_extern sg) imports)) @ [Pretty.str " +"])));
wenzelm@12070
   239
  in Pretty.block (Pretty.fbreaks (prt_header :: map prt_elem elements)) end;
wenzelm@12070
   240
wenzelm@12070
   241
val print_locale = Pretty.writeln oo pretty_locale;
wenzelm@12070
   242
wenzelm@12070
   243
wenzelm@12070
   244
wenzelm@11896
   245
(** define locales **)
wenzelm@11896
   246
wenzelm@12063
   247
(* closeup dangling frees *)
wenzelm@12063
   248
wenzelm@12063
   249
fun close_frees_wrt ctxt t =
wenzelm@12063
   250
  let val frees = rev (filter_out (ProofContext.is_fixed ctxt o #1) (Drule.add_frees ([], t)))
wenzelm@12063
   251
  in curry Term.list_all_free frees end;
wenzelm@11896
   252
wenzelm@12063
   253
fun closeup ctxt (Assumes asms) = Assumes (asms |> map (fn (a, propps) =>
wenzelm@12063
   254
      (a, propps |> map (fn (t, (ps1, ps2)) =>
wenzelm@12063
   255
        let val close = close_frees_wrt ctxt t in (close t, (map close ps1, map close ps2)) end))))
wenzelm@12063
   256
  | closeup ctxt (Defines defs) = Defines (defs |> map (fn (a, (t, ps)) =>
wenzelm@12063
   257
      let
wenzelm@12084
   258
        val (_, t') = ProofContext.cert_def ctxt t;
wenzelm@12063
   259
        val close = close_frees_wrt ctxt t';
wenzelm@12063
   260
      in (a, (close t', map close ps)) end))
wenzelm@12063
   261
  | closeup ctxt elem = elem;
wenzelm@11896
   262
wenzelm@11896
   263
wenzelm@12063
   264
(* add_locale(_i) *)
wenzelm@11896
   265
wenzelm@12063
   266
fun gen_add_locale prep_locale prep_elem bname raw_imports raw_elems thy =
wenzelm@12063
   267
  let
wenzelm@12063
   268
    val sign = Theory.sign_of thy;
wenzelm@11896
   269
    val name = Sign.full_name sign bname;
wenzelm@12063
   270
    val _ =
wenzelm@12063
   271
      if is_none (get_locale thy name) then () else
wenzelm@12063
   272
      error ("Duplicate definition of locale " ^ quote name);
wenzelm@11896
   273
wenzelm@12063
   274
    val imports = map (prep_locale sign) raw_imports;
wenzelm@12063
   275
    val imports_ctxt = foldl activate_locale_elements (ProofContext.init thy, imports);
wenzelm@12063
   276
    fun prep (ctxt, raw_elem) =
wenzelm@12063
   277
      let val elem = closeup ctxt (prep_elem ctxt raw_elem)
wenzelm@12063
   278
      in (activate (ctxt, elem), elem) end;
wenzelm@12063
   279
    val (locale_ctxt, elems) = foldl_map prep (imports_ctxt, raw_elems);
wenzelm@12063
   280
  in
wenzelm@12063
   281
    thy
wenzelm@12063
   282
    |> declare_locale name
wenzelm@12063
   283
    |> put_locale name (make_locale imports elems false)
wenzelm@12063
   284
  end;
wenzelm@12063
   285
wenzelm@12063
   286
val add_locale = gen_add_locale intern read_elem;
wenzelm@12063
   287
val add_locale_i = gen_add_locale (K I) (K I);
wenzelm@12063
   288
wenzelm@11896
   289
wenzelm@12063
   290
wenzelm@12070
   291
(** store results **)
wenzelm@11896
   292
wenzelm@12063
   293
fun store_thm name ((a, th), atts) thy =
wenzelm@12063
   294
  let
wenzelm@12070
   295
    val {imports, elements, closed} = the_locale thy name;
wenzelm@12063
   296
    val note = Notes [((a, atts), [([Thm.name_thm (a, th)], [])])];
wenzelm@12063
   297
  in
wenzelm@12084
   298
    conditional closed (fn () => error ("Cannot store results in closed locale: " ^ quote name));
wenzelm@12070
   299
    activate (ProofContext.init thy |> activate_locale_i name, note);    (*test attribute*)
wenzelm@12070
   300
    thy |> put_locale name (make_locale imports (elements @ [note]) closed)
wenzelm@12063
   301
  end;
wenzelm@11896
   302
wenzelm@12063
   303
wenzelm@11896
   304
wenzelm@11896
   305
(** locale theory setup **)
wenzelm@12063
   306
wenzelm@11896
   307
val setup =
wenzelm@11896
   308
 [LocalesData.init];
wenzelm@11896
   309
wenzelm@11896
   310
end;
wenzelm@11896
   311
wenzelm@11896
   312
structure BasicLocale: BASIC_LOCALE = Locale;
wenzelm@11896
   313
open BasicLocale;