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