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