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