src/Pure/Isar/locale.ML
author wenzelm
Sun, 04 Nov 2001 20:58:26 +0100
changeset 12046 a404358fd965
parent 12014 035ab884b9e0
child 12058 cc182b43dd55
permissions -rw-r--r--
locale elements;
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:     Florian Kammueller, University of Cambridge
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
     4
    Author:     Markus Wenzel, TU Muenchen
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
     5
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
     6
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
     7
Locales. The theory section 'locale' declarings constants, assumptions
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
     8
and definitions that have local scope.
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
     9
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    10
TODO:
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    11
  - reset scope context on qed of legacy goal (!??);
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    12
  - implicit closure of ``loose'' free vars;
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    13
  - avoid dynamic scoping of facts/atts
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    14
    (use thms_closure for globals, even within att expressions);
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    15
  - scope of implicit fixed in elementents vs. locales (!??);
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    16
  - Fixes: optional type (!?);
11896
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
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    19
signature BASIC_LOCALE =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    20
sig
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    21
  val print_locales: theory -> unit
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    22
end;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    23
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    24
signature LOCALE =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    25
sig
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    26
  include BASIC_LOCALE
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    27
  type context
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    28
  type expression
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    29
  datatype ('typ, 'term, 'fact, 'att) elem =
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    30
    Fixes of (string * 'typ * mixfix option) list |
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    31
    Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list |
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    32
    Defines of ((string * 'att list) * ('term * 'term list)) list |
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    33
    Notes of ((string * 'att list) * ('fact * 'att list) list) list |
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    34
    Uses of expression
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    35
  type 'att element
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    36
  type 'att element_i
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    37
  type locale
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    38
  val intern: Sign.sg -> xstring -> string
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    39
  val cond_extern: Sign.sg -> string -> xstring
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    40
  val intern_att: ('att -> context attribute) ->
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    41
    ('typ, 'term, 'thm, 'att) elem -> ('typ, 'term, 'thm, context attribute) elem
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    42
  val activate_elements: context attribute element list -> context -> context
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    43
  val activate_elements_i: context attribute element_i list -> context -> context
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    44
  val activate_locale: xstring -> context -> context
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    45
  val activate_locale_i: string -> context -> context
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    46
(*
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    47
  val add_locale: bstring -> xstring option -> (string * string * mixfix) list ->
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    48
    ((string * context attribute list) * string) list ->
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    49
    ((string * context attribute list) * string) list -> theory -> theory
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    50
  val add_locale_i: bstring -> xstring option -> (string * typ * mixfix) list ->
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    51
    ((string * context attribute list) * term) list ->
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    52
    ((string * context attribute list) * term) list -> theory -> theory
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    53
  val read_prop_schematic: Sign.sg -> string -> cterm
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    54
*)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    55
  val setup: (theory -> theory) list
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    56
end;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    57
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    58
structure Locale: LOCALE =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    59
struct
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    60
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    61
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    62
(** locale elements and locales **)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    63
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    64
type context = ProofContext.context;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    65
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    66
type expression = unit;  (* FIXME *)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    67
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    68
datatype ('typ, 'term, 'fact, 'att) elem =
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    69
  Fixes of (string * 'typ * mixfix option) list |
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    70
  Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list |
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    71
  Defines of ((string * 'att list) * ('term * 'term list)) list |
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    72
  Notes of ((string * 'att list) * ('fact * 'att list) list) list |
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    73
  Uses of expression;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    74
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    75
type 'att element = (string, string, string, 'att) elem;
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    76
type 'att element_i = (typ, term, thm list, 'att) elem;
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    77
type locale = (thm -> string) * string list * context attribute element_i list;
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    78
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    79
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    80
fun fixes_of_elem (Fixes fixes) = map #1 fixes
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    81
  | fixes_of_elem _ = [];
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    82
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    83
fun frees_of_elem _ = [];  (* FIXME *)
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
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    87
(** theory data **)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    88
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    89
(* data kind 'Pure/locales' *)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    90
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    91
type locale_data =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    92
  {space: NameSpace.T,
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    93
    locales: locale Symtab.table,
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    94
    scope: ((string * locale) list * context option) ref};
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    95
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    96
fun make_locale_data space locales scope =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    97
  {space = space, locales = locales, scope = scope}: locale_data;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    98
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    99
val empty_scope = ([], None);
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   100
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   101
structure LocalesArgs =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   102
struct
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   103
  val name = "Isar/locales";
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   104
  type T = locale_data;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   105
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   106
  val empty = make_locale_data NameSpace.empty Symtab.empty (ref empty_scope);
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   107
  fun copy {space, locales, scope = ref r} = make_locale_data space locales (ref r);
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   108
  fun prep_ext {space, locales, scope = _} = make_locale_data space locales (ref empty_scope);
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   109
  fun merge ({space = space1, locales = locales1, scope = _},
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   110
    {space = space2, locales = locales2, scope = _}) =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   111
      make_locale_data (NameSpace.merge (space1, space2))
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   112
        (Symtab.merge (K true) (locales1, locales2)) (ref empty_scope);
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   113
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   114
  fun print _ {space, locales, scope = _} =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   115
    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
   116
    |> Pretty.writeln;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   117
end;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   118
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   119
structure LocalesData = TheoryDataFun(LocalesArgs);
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   120
val print_locales = LocalesData.print;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   121
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   122
val intern = NameSpace.intern o #space o LocalesData.get_sg;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   123
val cond_extern = NameSpace.cond_extern o #space o LocalesData.get_sg;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   124
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   125
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   126
(* access locales *)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   127
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   128
fun get_locale_sg sg name = Symtab.lookup (#locales (LocalesData.get_sg sg), name);
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   129
val get_locale = get_locale_sg o Theory.sign_of;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   130
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   131
fun put_locale (name, locale) = LocalesData.map (fn {space, locales, scope} =>
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   132
  make_locale_data (NameSpace.extend (space, [name]))
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   133
    (Symtab.update ((name, locale), locales)) scope);
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   134
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   135
fun the_locale thy name =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   136
  (case get_locale thy name of
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   137
    Some loc => loc
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   138
  | None => error ("Unknown locale " ^ quote name));
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   139
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   140
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   141
(* access scope *)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   142
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   143
fun get_scope_sg sg =
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   144
  if Sign.eq_sg (sg, Theory.sign_of ProtoPure.thy) then empty_scope
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   145
  else ! (#scope (LocalesData.get_sg sg));
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   146
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   147
val get_scope = get_scope_sg o Theory.sign_of;
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   148
val nonempty_scope_sg = not o null o #1 o get_scope_sg;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   149
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   150
fun change_scope f thy =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   151
  let val {scope, ...} = LocalesData.get thy
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   152
  in scope := f (! scope); thy end;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   153
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   154
fun print_scope thy =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   155
  Pretty.writeln (Pretty.strs ("current scope:" ::
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   156
    rev (map (cond_extern (Theory.sign_of thy) o #1) (#1 (get_scope thy)))));
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   157
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   158
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   159
(* print locales *)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   160
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   161
fun pretty_locale thy xname =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   162
  let
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   163
    val sg = Theory.sign_of thy;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   164
    val name = intern sg xname;
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   165
    val (_, ancestors, elements) = the_locale thy name;
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   166
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   167
    val prt_typ = Pretty.quote o Sign.pretty_typ sg;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   168
    val prt_term = Pretty.quote o Sign.pretty_term sg;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   169
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   170
    fun prt_syn syn =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   171
      let val s = (case syn of None => "(structure)" | Some mx => Syntax.string_of_mixfix mx)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   172
      in if s = "" then [] else [Pretty.brk 4, Pretty.str s] end;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   173
    fun prt_fix (x, T, syn) = Pretty.block (Pretty.str (x ^ " ::") :: Pretty.brk 1 ::
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   174
      prt_typ T :: Pretty.brk 1 :: prt_syn syn);
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   175
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   176
    fun prt_asm ((a, _), ts) = Pretty.block
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   177
      (Pretty.breaks (Pretty.str (a ^ ":") :: map (prt_term o fst) ts));
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   178
    fun prt_asms asms = Pretty.block
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   179
      (flat (separate [Pretty.fbrk, Pretty.str "and"] (map (single o prt_asm) asms)));
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   180
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   181
    fun prt_def ((a, _), (t, _)) = Pretty.block
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   182
      [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term t];
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   183
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   184
    fun prt_fact ((a, _), ths) = Pretty.block
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   185
      (Pretty.breaks (Pretty.str (a ^ ":") :: map Display.pretty_thm (flat (map fst ths))));
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   186
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   187
    fun prt_elem (Fixes fixes) = Pretty.big_list "fixes" (map prt_fix fixes)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   188
      | prt_elem (Assumes asms) = Pretty.big_list "assumes" (map prt_asm asms)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   189
      | prt_elem (Defines defs) = Pretty.big_list "defines" (map prt_def defs)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   190
      | prt_elem (Notes facts) = Pretty.big_list "notes" (map prt_fact facts)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   191
      | prt_elem (Uses _) = Pretty.str "FIXME";
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   192
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   193
    val prt_header = Pretty.block (Pretty.str ("locale " ^ cond_extern sg name ^ " =") ::
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   194
       (if null ancestors then [] else
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   195
       (flat (separate [Pretty.str "+", Pretty.brk 1] (map (single o Pretty.str) ancestors)) @
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   196
         [Pretty.str "+"])));
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   197
  in Pretty.block (Pretty.fbreaks (prt_header :: map prt_elem elements)) end;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   198
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   199
val print_locale = Pretty.writeln oo pretty_locale;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   200
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   201
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   202
(** internalization of theorems and attributes **)
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   203
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   204
fun int_att attrib (x, srcs) = (x, map attrib srcs);
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   205
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   206
fun intern_att _ (Fixes fixes) = Fixes fixes
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   207
  | intern_att attrib (Assumes asms) = Assumes (map (apfst (int_att attrib)) asms)
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   208
  | intern_att attrib (Defines defs) = Defines (map (apfst (int_att attrib)) defs)
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   209
  | intern_att attrib (Notes facts) =
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   210
      Notes (map (apfst (int_att attrib) o apsnd (map (int_att attrib))) facts)
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   211
  | intern_att _ (Uses FIXME) = Uses FIXME;
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   212
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   213
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   214
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   215
(** activate locales **)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   216
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   217
(* FIXME old
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   218
fun pack_def eq =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   219
  let
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   220
    val (lhs, rhs) = Logic.dest_equals eq;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   221
    val (f, xs) = Term.strip_comb lhs;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   222
  in (xs, Logic.mk_equals (f, foldr (uncurry lambda) (xs, rhs))) end;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   223
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   224
fun unpack_def xs thm =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   225
  let
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   226
    val cxs = map (Thm.cterm_of (Thm.sign_of_thm thm)) xs;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   227
    fun unpack (th, cx) =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   228
      Thm.combination th (Thm.reflexive cx)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   229
      |> MetaSimplifier.fconv_rule (Thm.beta_conversion true);
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   230
  in foldl unpack (thm, cxs) end;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   231
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   232
fun prep_def ((name, atts), eq) =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   233
  let val (xs, eq') = pack_def eq
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   234
  in ((name, Drule.rule_attribute (K (unpack_def xs)) :: atts), [(eq', ([], []))]) end;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   235
*)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   236
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   237
fun read_elem closure ctxt =
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   238
 fn (Fixes fixes) =>
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   239
      let val vars =
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   240
        #2 (foldl_map ProofContext.read_vars (ctxt, map (fn (x, T, _) => ([x], Some T)) fixes))
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   241
      in Fixes (map2 (fn (([x'], Some T'), (_, _, mx)) => (x', T', mx)) (vars, fixes)) end
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   242
  | (Assumes asms) =>
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   243
      Assumes (map #1 asms ~~ #2 (ProofContext.read_propp (ctxt, map #2 asms)))
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   244
  | (Defines defs) =>
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   245
      let val propps =
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   246
        #2 (ProofContext.read_propp (ctxt, map (fn (_, (t, ps)) => [(t, (ps, []))]) defs))
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   247
      in Defines (map #1 defs ~~ map (fn [(t', (ps', []))] => (t', ps')) propps) end
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   248
  | (Notes facts) =>
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   249
      Notes (map (apsnd (map (apfst (ProofContext.get_thms ctxt)))) facts)
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   250
  | (Uses FIXME) => Uses FIXME;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   251
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   252
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   253
fun activate (ctxt, Fixes fixes) =
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   254
      ProofContext.fix_direct (map (fn (x, T, FIXME) => ([x], Some T)) fixes) ctxt
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   255
  | activate (ctxt, Assumes asms) = #1 (ProofContext.assume_i ProofContext.export_assume asms ctxt)
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   256
  | activate (ctxt, Defines defs) = #1 (ProofContext.assume_i ProofContext.export_def
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   257
      (map (fn (a, (t, ps)) => (a, [(t, (ps, []))])) defs) ctxt)
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   258
  | activate (ctxt, Notes facts) = #1 (ProofContext.have_thmss facts ctxt)
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   259
  | activate (ctxt, Uses FIXME) = ctxt;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   260
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   261
(* FIXME closure? *)
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   262
fun read_activate (ctxt, elem) =
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   263
  let val elem' = read_elem (PureThy.get_thms (ProofContext.theory_of ctxt)) ctxt elem
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   264
  in (activate (ctxt, elem'), elem') end;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   265
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   266
fun activate_elements_i elems ctxt = foldl activate (ctxt, elems);
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   267
fun activate_elements elems ctxt = foldl (#1 o read_activate) (ctxt, elems);
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   268
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   269
fun with_locale f name ctxt =
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   270
  let
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   271
    val thy = ProofContext.theory_of ctxt;
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   272
    val locale = the_locale thy name;
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   273
  in
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   274
    f locale ctxt handle ProofContext.CONTEXT (msg, c) =>
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   275
      raise ProofContext.CONTEXT (msg ^ "\nThe error(s) above occurred in locale " ^
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   276
        quote (cond_extern (Theory.sign_of thy) name), c)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   277
  end;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   278
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   279
val activate_locale_elements = with_locale (activate_elements_i o #3);
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   280
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   281
fun activate_locale_ancestors name ctxt =
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   282
  foldl (fn (c, es) => activate_locale_elements es c)
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   283
    (ctxt, #2 (the_locale (ProofContext.theory_of ctxt) name));
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   284
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   285
fun activate_locale_i name ctxt =
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   286
  ctxt |> activate_locale_ancestors name |> activate_locale_elements name;
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   287
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   288
fun activate_locale xname ctxt =
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   289
  activate_locale_i (intern (ProofContext.sign_of ctxt) xname) ctxt;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   290
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   291
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   292
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   293
(* FIXME
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   294
(** define locales **)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   295
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   296
(* prepare types *)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   297
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   298
fun read_typ sg (envT, s) =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   299
  let
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   300
    fun def_sort (x, ~1) = assoc (envT, x)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   301
      | def_sort _ = None;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   302
    val T = Type.no_tvars (Sign.read_typ (sg, def_sort) s) handle TYPE (msg, _, _) => error msg;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   303
  in (Term.add_typ_tfrees (T, envT), T) end;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   304
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   305
fun cert_typ sg (envT, raw_T) =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   306
  let val T = Type.no_tvars (Sign.certify_typ sg raw_T) handle TYPE (msg, _, _) => error msg
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   307
  in (Term.add_typ_tfrees (T, envT), T) end;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   308
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   309
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   310
(* prepare props *)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   311
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   312
(* Bind a term with !! over a list of "free" Free's.
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   313
   To enable definitions like x + y == .... (without quantifier).
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   314
   Complications, because x and y have to be removed from defaults *)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   315
fun abs_over_free clist ((defaults: (string * sort) list * (string * typ) list * string list), (s, term)) =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   316
    let val diffl = rev(difflist term clist);
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   317
        fun abs_o (t, (x as Free(v,T))) = all(T) $ Abs(v, T, abstract_over (x,t))
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   318
          | abs_o (_ , _) = error ("Can't be: abs_over_free");
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   319
        val diffl' = map (fn (Free (s, T)) => s) diffl;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   320
        val defaults' = (#1 defaults, filter (fn x => not((fst x) mem diffl')) (#2 defaults), #3 defaults)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   321
    in (defaults', (s, foldl abs_o (term, diffl))) end;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   322
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   323
(* assume a definition, i.e assume the cterm of a definiton term and then eliminate
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   324
   the binding !!, so that the def can be applied as rewrite. The meta hyp will still contain !! *)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   325
fun prep_hyps clist sg = forall_elim_vars(0) o Thm.assume o (Thm.cterm_of sg);
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   326
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   327
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   328
(* concrete syntax *)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   329
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   330
fun mark_syn c = "\\<^locale>" ^ c;
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
fun mk_loc_tr c ts = list_comb (Free (c, dummyT), ts);
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   333
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   334
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   335
(* add_locale *)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   336
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   337
fun gen_add_locale prep_typ prep_term bname bpar raw_fixes raw_assumes raw_defs thy =
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   338
  let val sign = Theory.sign_of thy;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   339
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   340
    val name = Sign.full_name sign bname;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   341
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   342
    val (envSb, old_loc_fixes, _) =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   343
                    case bpar of
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   344
                       Some loc => (get_defaults thy loc)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   345
                     | None      => ([],[],[]);
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   346
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   347
    val old_nosyn = case bpar of
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   348
                       Some loc => #nosyn(#2(the_locale thy loc))
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   349
                     | None      => [];
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   350
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   351
    (* Get the full name of the parent *)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   352
    val parent = case bparent of
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   353
                       Some loc => Some(#1(the_locale thy loc))
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   354
                     | None      => None;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   355
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   356
     (* prepare locale fixes *)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   357
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   358
    fun prep_const (envS, (raw_c, raw_T, raw_mx)) =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   359
      let
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   360
        val c = Syntax.const_name raw_c raw_mx;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   361
        val c_syn = mark_syn c;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   362
        val mx = Syntax.fix_mixfix raw_c raw_mx;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   363
        val (envS', T) = prep_typ sign (envS, raw_T) handle ERROR =>
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   364
          error ("The error(s) above occured in locale constant " ^ quote c);
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   365
        val trfun = if mx = Syntax.NoSyn then None else Some (c_syn, mk_loc_tr c);
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   366
      in (envS', ((c, T), (c_syn, T, mx), trfun)) end;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   367
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   368
    val (envS0, loc_fixes_syn) = foldl_map prep_const (envSb, raw_fixes);
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   369
    val loc_fixes = map #1 loc_fixes_syn;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   370
    val loc_fixes = old_loc_fixes @ loc_fixes;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   371
    val loc_syn = map #2 loc_fixes_syn;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   372
    val nosyn = old_nosyn @ (map (#1 o #1) (filter (fn x => (#3(#2 x)) = NoSyn) loc_fixes_syn));
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   373
    val loc_trfuns = mapfilter #3 loc_fixes_syn;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   374
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   375
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   376
    (* 1st stage: syntax_thy *)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   377
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   378
    val syntax_thy =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   379
      thy
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   380
      |> Theory.add_modesyntax_i ("", true) loc_syn
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   381
      |> Theory.add_trfuns ([], loc_trfuns, [], []);
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   382
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   383
    val syntax_sign = Theory.sign_of syntax_thy;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   384
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   385
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   386
    (* prepare assumes and defs *)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   387
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   388
    fun prep_axiom (env, (a, raw_t)) =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   389
      let
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   390
        val (env', t) = prep_term syntax_sign (env, (a, raw_t)) handle ERROR =>
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   391
          error ("The error(s) above occured in locale rule / definition " ^ quote a);
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   392
      in (env', (a, t)) end;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   393
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   394
    val ((envS1, envT1, used1), loc_assumes) =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   395
      foldl_map prep_axiom ((envS0, loc_fixes, map fst envS0), raw_assumes);
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   396
    val (defaults, loc_defs) =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   397
        foldl_map prep_axiom ((envS1, envT1, used1), raw_defs);
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   398
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   399
    val old_loc_fixes = collect_fixes syntax_sign;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   400
    val new_loc_fixes = (map #1 loc_fixes);
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   401
    val all_loc_fixes = old_loc_fixes @ new_loc_fixes;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   402
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   403
    val (defaults, loc_defs_terms) =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   404
        foldl_map (abs_over_free all_loc_fixes) (defaults, loc_defs);
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   405
    val loc_defs_thms =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   406
        map (apsnd (prep_hyps (map #1 loc_fixes) syntax_sign)) loc_defs_terms;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   407
    val (defaults, loc_thms_terms) =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   408
        foldl_map (abs_over_free all_loc_fixes) (defaults, loc_assumes);
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   409
    val loc_thms = map (apsnd (prep_hyps (map #1 loc_fixes) syntax_sign))
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   410
                       (loc_thms_terms)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   411
                   @ loc_defs_thms;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   412
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   413
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   414
    (* error messages *)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   415
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   416
    fun locale_error msg = error (msg ^ "\nFor locale " ^ quote name);
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   417
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   418
    val err_dup_locale =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   419
      if is_none (get_locale thy name) then []
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   420
      else ["Duplicate definition of locale " ^ quote name];
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   421
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   422
    (* check if definientes are locale constants
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   423
       (in the same locale, so no redefining!) *)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   424
    val err_def_head =
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   425
      let fun peal_appl t =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   426
            case t of
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   427
                 t1 $ t2 => peal_appl t1
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   428
               | Free(t) => t
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   429
               | _ => locale_error ("Bad form of LHS in locale definition");
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   430
          fun lhs (_, Const ("==" , _) $  d1 $ d2) = peal_appl d1
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   431
            | lhs _ = locale_error ("Definitions must use the == relation");
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   432
          val defs = map lhs loc_defs;
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   433
          val check = defs subset loc_fixes
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   434
      in if check then []
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   435
         else ["defined item not declared fixed in locale " ^ quote name]
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   436
      end;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   437
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   438
    (* check that variables on rhs of definitions are either fixed or on lhs *)
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   439
    val err_var_rhs =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   440
      let fun compare_var_sides (t, (_, Const ("==", _) $ d1 $ d2)) =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   441
                let val varl1 = difflist d1 all_loc_fixes;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   442
                    val varl2 = difflist d2 all_loc_fixes
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   443
                in t andalso (varl2 subset varl1)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   444
                end
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   445
            | compare_var_sides (_,_) =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   446
                locale_error ("Definitions must use the == relation")
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   447
          val check = foldl compare_var_sides (true, loc_defs)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   448
      in if check then []
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   449
         else ["nonfixed variable on right hand side of a locale definition in locale " ^ quote name]
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   450
      end;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   451
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   452
    val errs = err_dup_locale @ err_def_head @ err_var_rhs;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   453
  in
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   454
    if null errs then ()
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   455
    else error (cat_lines errs);
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   456
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   457
    syntax_thy
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   458
    |> put_locale (name,
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   459
                   make_locale parent loc_fixes nosyn loc_thms_terms
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   460
                                        loc_defs_terms   loc_thms defaults)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   461
  end;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   462
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   463
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   464
val add_locale = gen_add_locale read_typ read_axm;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   465
val add_locale_i = gen_add_locale cert_typ cert_axm;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   466
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   467
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   468
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   469
(*
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   470
(** support for legacy proof scripts (cf. goals.ML) **)     (* FIXME move to goals.ML (!?) *)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   471
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   472
(* hyps_in_scope *)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   473
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   474
fun hyps_in_scope sg hyps =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   475
  let val locs = map #2 (#1 (get_scope_sg sg))
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   476
  in gen_subset Term.aconv (hyps, map #2 (flat (map #assumes locs @ map #defines locs))) end;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   477
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   478
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   479
(* get theorems *)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   480
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   481
fun thmx get_local get_global name =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   482
  let val thy = Context.the_context () in
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   483
    (case #2 (get_scope thy) of
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   484
      None => get_global thy name
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   485
    | Some ctxt => get_local ctxt name)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   486
  end;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   487
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   488
val thm = thmx ProofContext.get_thm PureThy.get_thm;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   489
val thms = thmx ProofContext.get_thms PureThy.get_thms;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   490
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   491
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   492
(** scope operations -- for old-style goals **)  (* FIXME move to goals.ML (!?) *)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   493
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   494
(* open *)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   495
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   496
local
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   497
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   498
fun is_open thy name = exists (equal name o #1) (#1 (get_scope thy));
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   499
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   500
fun open_loc thy name =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   501
  let
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   502
    val (ancestors, elements) = the_locale thy name;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   503
  in
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   504
    (case #parent locale of None => thy
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   505
    | Some par =>
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   506
        if is_open thy par then thy
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   507
        else (writeln ("Opening locale " ^ quote par ^ "(required by " ^ quote name ^ ")");
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   508
          open_loc name thy))
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   509
    |> change_scope (fn (locs, _) => ((name, locale) :: locs, None))
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   510
  end;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   511
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   512
in
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   513
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   514
fun open_locale xname thy =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   515
  let val name = intern (Theory.sign_of thy) xname in
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   516
    if is_open thy name then (warning ("Locale " ^ quote name ^ " already open"); thy)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   517
    else open_loc name thy
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   518
  end;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   519
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   520
end;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   521
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   522
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   523
(* close *)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   524
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   525
fun close_locale xname thy =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   526
  let val name = intern_locale (Theory.sign_of thy) xname in
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   527
    thy |> change_scope (fn ([], _) => error "Currently no open locales"
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   528
    | ((name', _) :: locs, _) =>
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   529
        if name <> name' then error ("Locale " ^ quote name ^ " not at top of scope")
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   530
        else (locs, None))
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   531
  end;
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   532
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   533
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   534
(* implicit context versions *)
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   535
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   536
fun Open_locale xname = (open_locale xname (Context.the_context ()); ());
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   537
fun Close_locale xname = (close_locale xname (Context.the_context ()); ());
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   538
fun Print_scope () = (print_scope (Context.the_context ()); ());
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   539
*)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   540
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   541
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   542
(** locale theory setup **)
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   543
*)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   544
val setup =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   545
 [LocalesData.init];
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   546
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   547
end;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   548
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   549
structure BasicLocale: BASIC_LOCALE = Locale;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   550
open BasicLocale;