src/Pure/Isar/locale.ML
author wenzelm
Fri, 14 Dec 2001 11:55:34 +0100
changeset 12502 9e7f72e25022
parent 12323 e151ee6e820f
child 12510 172d18ec3b54
permissions -rw-r--r--
beginning support for type instantiation; tuned internal arrangements;
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
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
    32
  val the_locale: theory -> string -> locale
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    33
  val attribute: ('att -> context attribute) -> ('typ, 'term, 'thm, 'att) elem_expr
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    34
    -> ('typ, 'term, 'thm, context attribute) elem_expr
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
    35
  val activate_context: context -> expr * context attribute element list ->
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
    36
   (context -> context) * (context -> context)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
    37
  val activate_context_i: context -> expr * context attribute element_i list ->
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
    38
   (context -> context) * (context -> context)
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    39
  val add_locale: bstring -> expr -> context attribute element list -> theory -> theory
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    40
  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
    41
  val print_locales: theory -> unit
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
    42
  val print_locale: theory -> expr -> unit
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    43
  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
    44
  val setup: (theory -> theory) list
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    45
end;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    46
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
    47
structure Locale: LOCALE =
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    48
struct
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    49
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    50
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
    51
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    52
(** locale elements and expressions **)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    53
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    54
type context = ProofContext.context;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    55
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    56
datatype ('typ, 'term, 'fact, 'att) elem =
12058
cc182b43dd55 fixes: optional typ;
wenzelm
parents: 12046
diff changeset
    57
  Fixes of (string * 'typ option * mixfix option) list |
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    58
  Assumes of ((string * 'att list) * ('term * ('term list * 'term list)) list) list |
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
    59
  Defines of ((string * 'att list) * ('term * 'term list)) list |
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    60
  Notes of ((string * 'att list) * ('fact * 'att list) list) list;
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    61
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    62
datatype expr =
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    63
  Locale of string |
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    64
  Rename of expr * string option list |
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    65
  Merge of expr list;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    66
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    67
val empty = Merge [];
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    68
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    69
datatype ('typ, 'term, 'fact, 'att) elem_expr =
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    70
  Elem of ('typ, 'term, 'fact, 'att) elem | Expr of expr;
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    71
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    72
type 'att element = (string, string, string, 'att) elem_expr;
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
    73
type 'att element_i = (typ, term, thm list, 'att) elem_expr;
12070
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
    74
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
    75
type locale =
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
    76
 {import: expr,                                                         (*dynamic import*)
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
    77
  elems: ((typ, term, thm list, context attribute) elem * stamp) list,  (*static content*)
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
    78
  params: (string * typ option) list * string list,                     (*all vs. local params*)
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
    79
  text: (string * typ) list * term list}                                (*logical representation*)
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
    80
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
    81
fun make_locale import elems params text =
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
    82
 {import = import, elems = elems, params = params, text = text}: locale;
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
    83
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    84
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    85
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    86
(** theory data **)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    87
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    88
structure LocalesArgs =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    89
struct
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
    90
  val name = "Isar/locales";
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
    91
  type T = NameSpace.T * locale Symtab.table;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
    92
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
    93
  val empty = (NameSpace.empty, Symtab.empty);
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
    94
  val copy = I;
12118
3d62ee5bec5e proper close_locale;
wenzelm
parents: 12084
diff changeset
    95
  val prep_ext = I;
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
    96
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
    97
  (*joining of locale elements: only facts may be added later!*)
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
    98
  fun join ({import, elems, params, text}: locale, {elems = elems', ...}: locale) =
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
    99
    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
   100
  fun merge ((space1, locs1), (space2, locs2)) =
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   101
    (NameSpace.merge (space1, space2), Symtab.join join (locs1, locs2));
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   102
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   103
  fun print _ (space, locs) =
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   104
    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
   105
    |> Pretty.writeln;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   106
end;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   107
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   108
structure LocalesData = TheoryDataFun(LocalesArgs);
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   109
val print_locales = LocalesData.print;
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   110
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   111
val intern = NameSpace.intern o #1 o LocalesData.get_sg;
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   112
val cond_extern = NameSpace.cond_extern o #1 o LocalesData.get_sg;
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   113
12277
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   114
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   115
(* access locales *)
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   116
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   117
fun declare_locale name =
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   118
  LocalesData.map (apfst (fn space => (NameSpace.extend (space, [name]))));
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   119
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   120
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
   121
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
   122
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   123
fun the_locale thy name =
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   124
  (case get_locale thy name of
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   125
    Some loc => loc
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   126
  | None => error ("Unknown locale " ^ quote name));
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   127
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   128
12277
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   129
(* diagnostics *)
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   130
12277
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   131
fun err_in_locale ctxt msg ids =
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   132
  let
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   133
    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
   134
    val prt_ids = flat (separate [Pretty.str " +", Pretty.brk 1] (map prt_id ids));
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   135
    val err_msg =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   136
      if null ids then msg
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   137
      else msg ^ "\n" ^ Pretty.string_of (Pretty.block
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   138
        (Pretty.str "The error(s) above occurred in locale:" :: Pretty.brk 1 :: prt_ids));
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   139
  in raise ProofContext.CONTEXT (err_msg, ctxt) end;
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   140
12277
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   141
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   142
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   143
(** operations on locale elements **)
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   144
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   145
(* prepare elements *)
12263
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   146
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   147
local
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   148
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   149
fun prep_name ctxt (name, atts) =
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   150
  if NameSpace.is_qualified name then
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   151
    raise ProofContext.CONTEXT ("Illegal qualified name: " ^ quote name, ctxt)
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   152
  else (name, atts);
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   153
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   154
fun prep_elem prep_vars prep_propp prep_thms ctxt =
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   155
 fn Fixes fixes =>
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   156
      let val vars = snd (foldl_map prep_vars (ctxt, map (fn (x, T, _) => ([x], T)) fixes))
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   157
      in Fixes (map (fn (([x'], T'), (_, _, mx)) => (x', T', mx)) (vars ~~ fixes)) end
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   158
  | Assumes asms =>
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   159
      Assumes (map (prep_name ctxt o #1) asms ~~ snd (prep_propp (ctxt, map #2 asms)))
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   160
  | Defines defs =>
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   161
      let val propps = snd (prep_propp (ctxt, map (fn (_, (t, ps)) => [(t, (ps, []))]) defs)) in
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   162
        Defines (map (prep_name ctxt o #1) defs ~~ map (fn [(t', (ps', []))] => (t', ps')) propps)
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   163
      end
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   164
  | Notes facts =>
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   165
      Notes (map (fn (a, bs) => (prep_name ctxt a, map (apfst (prep_thms ctxt)) bs)) facts);
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   166
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   167
in
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   168
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   169
fun read_elem x = prep_elem ProofContext.read_vars ProofContext.read_propp ProofContext.get_thms x;
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   170
fun cert_elem x = prep_elem ProofContext.cert_vars ProofContext.cert_propp (K I) x;
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   171
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   172
fun read_expr ctxt (Locale xname) = Locale (intern (ProofContext.sign_of ctxt) xname)
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   173
  | read_expr ctxt (Merge exprs) = Merge (map (read_expr ctxt) exprs)
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   174
  | read_expr ctxt (Rename (expr, xs)) = Rename (read_expr ctxt expr, xs);
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   175
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   176
fun read_element ctxt (Elem e) = Elem (read_elem ctxt e)
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   177
  | read_element ctxt (Expr e) = Expr (read_expr ctxt e);
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   178
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   179
fun cert_element ctxt (Elem e) = Elem (cert_elem ctxt e)
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   180
  | cert_element ctxt (Expr e) = Expr e;
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   181
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   182
end;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   183
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   184
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   185
(* internalize attributes *)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   186
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   187
local fun read_att attrib (x, srcs) = (x, map attrib srcs) in
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   188
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   189
fun attribute _ (Elem (Fixes fixes)) = Elem (Fixes fixes)
12277
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   190
  | attribute attrib (Elem (Assumes asms)) = Elem (Assumes (map (apfst (read_att attrib)) asms))
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   191
  | 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
   192
  | attribute attrib (Elem (Notes facts)) =
12277
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   193
      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
   194
  | attribute _ (Expr expr) = Expr expr;
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   195
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   196
end;
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   197
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   198
12277
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   199
(* renaming *)
12263
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   200
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   201
fun rename ren x = if_none (assoc_string (ren, x)) x;
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   202
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   203
fun rename_term ren (Free (x, T)) = Free (rename ren x, T)
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   204
  | rename_term ren (t $ u) = rename_term ren t $ rename_term ren u
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   205
  | rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t)
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   206
  | rename_term _ a = a;
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   207
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   208
fun rename_thm ren th =
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   209
  let
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   210
    val {sign, hyps, prop, maxidx, ...} = Thm.rep_thm th;
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   211
    val cert = Thm.cterm_of sign;
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   212
    val (xs, Ts) = Library.split_list (foldl Term.add_frees ([], prop :: hyps));
12263
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   213
    val xs' = map (rename ren) xs;
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   214
    fun cert_frees names = map (cert o Free) (names ~~ Ts);
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   215
    fun cert_vars names = map (cert o Var o apfst (rpair (maxidx + 1))) (names ~~ Ts);
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   216
  in
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   217
    if xs = xs' then th
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   218
    else
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   219
      th
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   220
      |> Drule.implies_intr_list (map cert hyps)
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   221
      |> Drule.forall_intr_list (cert_frees xs)
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   222
      |> Drule.forall_elim_list (cert_vars xs)
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   223
      |> Thm.instantiate ([], cert_vars xs ~~ cert_frees xs')
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   224
      |> (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
   225
  end;
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   226
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   227
fun rename_elem ren (Fixes fixes) = Fixes (map (fn (x, T, mx) =>
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   228
      (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
   229
  | rename_elem ren (Assumes asms) = Assumes (map (apsnd (map (fn (t, (ps, qs)) =>
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   230
      (rename_term ren t, (map (rename_term ren) ps, map (rename_term ren) qs))))) asms)
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   231
  | rename_elem ren (Defines defs) = Defines (map (apsnd (fn (t, ps) =>
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   232
      (rename_term ren t, map (rename_term ren) ps))) defs)
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   233
  | rename_elem ren (Notes facts) = Notes (map (apsnd (map (apfst (map (rename_thm ren))))) facts);
12263
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   234
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   235
fun qualify_elem prfx elem =
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   236
  let
12323
e151ee6e820f qualify_elem: do not qualify empty names ("");
wenzelm
parents: 12307
diff changeset
   237
    fun qualify (arg as ((name, atts), x)) =
e151ee6e820f qualify_elem: do not qualify empty names ("");
wenzelm
parents: 12307
diff changeset
   238
      if name = "" then arg
e151ee6e820f qualify_elem: do not qualify empty names ("");
wenzelm
parents: 12307
diff changeset
   239
      else ((NameSpace.pack (filter_out (equal "") (prfx @ [name])), atts), x);
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   240
  in
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   241
    (case elem of
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   242
      Fixes fixes => Fixes fixes
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   243
    | Assumes asms => Assumes (map qualify asms)
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   244
    | Defines defs => Defines (map qualify defs)
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   245
    | Notes facts => Notes (map qualify facts))
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   246
  end;
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   247
12263
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   248
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   249
(* type instantiation *)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   250
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   251
fun inst_type [] T = T
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   252
  | inst_type env T = Term.map_type_tfree (fn v => if_none (assoc (env, v)) (TFree v)) T;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   253
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   254
fun inst_term [] t = t
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   255
  | inst_term env t = Term.map_term_types (inst_type env) t;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   256
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   257
fun inst_thm [] th = th
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   258
  | inst_thm env th =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   259
      let
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   260
        val {sign, hyps, prop, maxidx, ...} = Thm.rep_thm th;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   261
        val cert = Thm.cterm_of sign and certT = Thm.ctyp_of sign;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   262
        val names = foldr Term.add_term_tfree_names (prop :: hyps, []);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   263
        val env' = filter (fn ((a, _), _) => a mem_string names) env;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   264
      in
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   265
        if null env' then th
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   266
        else
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   267
          th
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   268
          |> Drule.implies_intr_list (map cert hyps)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   269
          |> Drule.tvars_intr_list names
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   270
          |> (fn (th', al) => th' |>
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   271
            Thm.instantiate ((map (fn ((a, _), T) => (the (assoc (al, a)), certT T)) env'), []))
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   272
          |> (fn th'' => Drule.implies_elim_list th''
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   273
              (map (Thm.assume o cert o inst_term env') hyps))
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   274
      end;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   275
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   276
fun inst_elem env (Fixes fixes) =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   277
      Fixes (map (fn (x, T, mx) => (x, apsome (inst_type env) T, mx)) fixes)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   278
  | inst_elem env (Assumes asms) = Assumes (map (apsnd (map (fn (t, (ps, qs)) =>
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   279
      (inst_term env t, (map (inst_term env) ps, map (inst_term env) qs))))) asms)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   280
  | inst_elem env (Defines defs) = Defines (map (apsnd (fn (t, ps) =>
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   281
      (inst_term env t, map (inst_term env) ps))) defs)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   282
  | inst_elem env (Notes facts) = Notes (map (apsnd (map (apfst (map (inst_thm env))))) facts);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   283
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   284
12277
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   285
(* evaluation *)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   286
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   287
fun frozen_tvars ctxt Ts =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   288
  let
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   289
    val tvars = rev (foldl Term.add_tvarsT ([], Ts));
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   290
    val tfrees = map TFree
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   291
      (Term.invent_type_names (ProofContext.used_types ctxt) (length tvars) ~~ map #2 tvars);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   292
  in map #1 tvars ~~ tfrees end;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   293
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   294
fun unify_parms ctxt raw_parmss =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   295
  let
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   296
    val tsig = Sign.tsig_of (ProofContext.sign_of ctxt);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   297
    val maxidx = length raw_parmss;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   298
    val idx_parmss = (0 upto maxidx - 1) ~~ raw_parmss;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   299
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   300
    fun varify i = Term.map_type_tfree (fn (a, S) => TVar ((a, i), S));
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   301
    fun varify_parms (i, ps) =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   302
      mapfilter (fn (_, None) => None | (x, Some T) => Some (x, varify i T)) ps;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   303
    val parms = flat (map varify_parms idx_parmss);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   304
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   305
    fun unify T ((env, maxidx), U) = Type.unify tsig maxidx env (U, T);  (*should never fail*)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   306
    fun unify_list (envir, T :: Us) = foldl (unify T) (envir, Us)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   307
      | unify_list (envir, []) = envir;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   308
    val (unifier, _) = foldl unify_list
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   309
      ((Vartab.empty, maxidx), map #2 (Symtab.dest (Symtab.make_multi parms)));
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   310
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   311
    val parms' = map (apsnd (Envir.norm_type unifier)) (gen_distinct eq_fst parms);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   312
    val unifier' = Vartab.extend (unifier, frozen_tvars ctxt (map #2 parms'));
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   313
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   314
    fun inst_parms (i, ps) =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   315
      foldr Term.add_typ_tfrees (mapfilter snd ps, [])
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   316
      |> mapfilter (fn (a, S) =>
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   317
          let val T = Envir.norm_type unifier' (TVar ((a, i), S))
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   318
          in if T = TFree (a, S) then None else Some ((a, S), T) end);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   319
  in map inst_parms idx_parmss end;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   320
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   321
fun unique_parms ctxt elemss =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   322
  let
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   323
    val param_decls =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   324
      flat (map (fn ((name, (ps, qs)), _) => map (rpair (name, ps)) qs) elemss)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   325
      |> Symtab.make_multi |> Symtab.dest;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   326
  in
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   327
    (case find_first (fn (_, ids) => length ids > 1) param_decls of
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   328
      Some (q, ids) => err_in_locale ctxt ("Multiple declaration of parameter " ^ quote q)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   329
          (map (apsnd (map fst)) ids)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   330
    | None => map (apfst (apsnd #1)) elemss)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   331
  end;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   332
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   333
fun inst_types _ [elems] = [elems]
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   334
  | inst_types ctxt elemss =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   335
      let
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   336
        val envs = unify_parms ctxt (map (#2 o #1) elemss);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   337
        fun inst (((name, ps), elems), env) =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   338
          ((name, map (apsnd (apsome (inst_type env))) ps), map (inst_elem env) elems);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   339
      in map inst (elemss ~~ envs) end;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   340
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   341
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   342
fun eval_expr ctxt expr =
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   343
  let
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   344
    val thy = ProofContext.theory_of ctxt;
12263
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   345
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   346
    fun renaming (Some x :: xs) (y :: ys) = (y, x) :: renaming xs ys
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   347
      | renaming (None :: xs) (y :: ys) = renaming xs ys
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   348
      | renaming [] _ = []
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   349
      | renaming xs [] = raise ERROR_MESSAGE ("Too many arguments in renaming: " ^
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   350
          commas (map (fn None => "_" | Some x => quote x) xs));
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   351
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   352
    fun rename_parms ren (name, ps) =
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   353
      let val ps' = map (rename ren) ps in
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   354
        (case duplicates ps' of [] => (name, ps')
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   355
        | dups => err_in_locale ctxt ("Duplicate parameters: " ^ commas_quote dups) [(name, ps')])
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   356
      end;
12263
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   357
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   358
    fun identify ((ids, parms), Locale name) =
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   359
          let
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   360
            val {import, params, ...} = the_locale thy name;
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   361
            val ps = map #1 (#1 params);
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   362
          in
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   363
            if (name, ps) mem ids then (ids, parms)
12277
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   364
            else
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   365
              let val (ids', parms') = identify ((ids, parms), import);  (*acyclic dependencies!*)
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   366
              in (ids' @ [(name, ps)], merge_lists parms' ps) end
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   367
          end
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   368
      | identify ((ids, parms), Rename (e, xs)) =
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   369
          let
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   370
            val (ids', parms') = identify (([], []), e);
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   371
            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
   372
            val ids'' = distinct (map (rename_parms ren) ids');
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   373
            val parms'' = distinct (flat (map #2 ids''));
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   374
          in (merge_lists ids ids'', merge_lists parms parms'') end
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   375
      | identify (arg, Merge es) = foldl identify (arg, es);
12014
035ab884b9e0 beginnings of new locales (not yet functional);
wenzelm
parents: 11896
diff changeset
   376
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   377
    fun eval (name, xs) =
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   378
      let
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   379
        val {params = (ps, qs), elems, ...} = the_locale thy name;
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   380
        val ren = filter_out (op =) (map #1 ps ~~ xs);
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   381
        val (params', elems') =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   382
          if null ren then ((ps, qs), map #1 elems)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   383
          else ((map (apfst (rename ren)) ps, map (rename ren) qs),
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   384
            map (rename_elem ren o #1) elems);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   385
        val elems'' = map (qualify_elem [NameSpace.base name, space_implode "_" xs]) elems';
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   386
      in ((name, params'), elems'') end;
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   387
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   388
    val raw_elemss = unique_parms ctxt (map eval (#1 (identify (([], []), expr))));
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   389
    val elemss = inst_types ctxt raw_elemss;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   390
  in elemss end;
12046
a404358fd965 locale elements;
wenzelm
parents: 12014
diff changeset
   391
12070
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   392
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   393
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   394
(** activation **)
12070
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   395
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   396
(* internalize elems *)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   397
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   398
fun declare_elem gen =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   399
  let
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   400
    val gen_typ = if gen then Term.map_type_tfree (Type.param []) else I;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   401
    val gen_term = if gen then Term.map_term_types gen_typ else I;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   402
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   403
    fun declare (Fixes fixes) = ProofContext.add_syntax fixes o
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   404
          ProofContext.fix_direct (map (fn (x, T, _) => ([x], apsome gen_typ T)) fixes)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   405
      | declare (Assumes asms) = (fn ctxt => #1 (ProofContext.bind_propp_i
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   406
          (ctxt, map (map (fn (t, (ps, ps')) =>
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   407
            (gen_term t, (map gen_term ps, map gen_term ps'))) o #2) asms)))
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   408
      | declare (Defines defs) = (fn ctxt => #1 (ProofContext.bind_propp_i
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   409
          (ctxt, map (fn (_, (t, ps)) => [(gen_term t, (map gen_term ps, []))]) defs)))
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   410
      | declare (Notes _) = I;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   411
  in declare end;
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   412
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   413
fun activate_elem (Fixes fixes) = ProofContext.add_syntax fixes o
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   414
      ProofContext.fix_direct (map (fn (x, T, _) => ([x], T)) fixes)
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   415
  | activate_elem (Assumes asms) =
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   416
      #1 o ProofContext.assume_i ProofContext.export_assume asms o
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   417
      ProofContext.fix_frees (flat (map (map #1 o #2) asms))
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   418
  | activate_elem (Defines defs) = (fn ctxt => #1 (ProofContext.assume_i ProofContext.export_def
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   419
      (map (fn ((name, atts), (t, ps)) =>
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   420
        let val (c, t') = ProofContext.cert_def ctxt t
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   421
        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
   422
  | activate_elem (Notes facts) = #1 o ProofContext.have_thmss facts;
12263
6f2acf10e2a2 locale expressions;
wenzelm
parents: 12143
diff changeset
   423
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   424
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   425
fun perform_elems f named_elems = ProofContext.qualified (fn context =>
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   426
  foldl (fn (ctxt, ((name, ps), es)) =>
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   427
    foldl (fn (c, e) => f e c) (ctxt, es) handle ProofContext.CONTEXT (msg, ctxt) =>
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   428
      err_in_locale ctxt msg [(name, map fst ps)]) (context, named_elems));
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   429
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   430
fun declare_elemss gen = perform_elems (declare_elem gen);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   431
fun activate_elemss x = perform_elems activate_elem x;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   432
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   433
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   434
(* context specifications: import expression + external elements *)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   435
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   436
local
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   437
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   438
fun close_frees ctxt t =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   439
  let val frees = rev (filter_out (ProofContext.is_fixed ctxt o #1) (Term.add_frees ([], t)))
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   440
  in Term.list_all_free (frees, t) end;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   441
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   442
(*quantify dangling frees, strip term bindings*)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   443
fun closeup ctxt (Assumes asms) = Assumes (asms |> map (fn (a, propps) =>
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   444
      (a, map (fn (t, _) => (close_frees ctxt t, ([], []))) propps)))
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   445
  | closeup ctxt (Defines defs) = Defines (defs |> map (fn (a, (t, _)) =>
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   446
      (a, (close_frees ctxt (#2 (ProofContext.cert_def ctxt t)), []))))
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   447
  | closeup ctxt elem = elem;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   448
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   449
fun prepare_context prep_elem prep_expr close context (import, elements) =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   450
  let
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   451
    fun prep_element (ctxt, Elem raw_elem) =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   452
          let val elem = (if close then closeup ctxt else I) (prep_elem ctxt raw_elem)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   453
          in (ctxt |> declare_elem false elem, [(("", []), [elem])]) end
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   454
      | prep_element (ctxt, Expr raw_expr) =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   455
          let
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   456
            val expr = prep_expr ctxt raw_expr;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   457
            val named_elemss = eval_expr ctxt expr;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   458
          in (ctxt |> declare_elemss true named_elemss, named_elemss) end;
12277
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   459
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   460
    val (import_ctxt, import_elemss) = prep_element (context, Expr import);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   461
    val (elements_ctxt, elements_elemss) =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   462
      apsnd flat (foldl_map prep_element (import_ctxt, elements));
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   463
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   464
    val xs = flat (map (map #1 o (#2 o #1)) (import_elemss @ elements_elemss));
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   465
    val env = frozen_tvars elements_ctxt (mapfilter (ProofContext.default_type elements_ctxt) xs);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   466
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   467
    fun inst_elems ((name, ps), elems) = ((name, ps), elems);  (* FIXME *)
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   468
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   469
  in (map inst_elems import_elemss, map inst_elems elements_elemss) end;
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   470
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   471
fun gen_activate_context prep_elem prep_expr ctxt args =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   472
  pairself activate_elemss (prepare_context prep_elem prep_expr false ctxt args);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   473
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   474
in
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   475
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   476
val read_context = prepare_context read_elem read_expr true;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   477
val cert_context = prepare_context cert_elem (K I) true;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   478
val activate_context = gen_activate_context read_elem read_expr;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   479
val activate_context_i = gen_activate_context cert_elem (K I);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   480
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   481
fun activate_locale name ctxt =
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   482
  #1 (activate_context_i ctxt (Locale name, [])) ctxt;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   483
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   484
end;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   485
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   486
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   487
12070
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   488
(** print locale **)
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   489
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   490
fun print_locale thy raw_expr =
12070
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   491
  let
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   492
    val sg = Theory.sign_of thy;
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   493
    val thy_ctxt = ProofContext.init thy;
12277
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   494
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   495
    val elemss = #1 (read_context thy_ctxt (raw_expr, []));
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   496
    val ctxt = activate_elemss elemss thy_ctxt;
12070
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   497
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   498
    val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt;
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   499
    val prt_term = Pretty.quote o ProofContext.pretty_term ctxt;
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   500
    val prt_thm = Pretty.quote o ProofContext.pretty_thm ctxt;
12070
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   501
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   502
    fun prt_syn syn =
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   503
      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
   504
      in if s = "" then [] else [Pretty.brk 4, Pretty.str s] end;
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   505
    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
   506
          prt_typ T :: Pretty.brk 1 :: prt_syn syn)
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   507
      | 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
   508
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   509
    fun prt_name "" = [Pretty.brk 1]
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   510
      | prt_name name = [Pretty.str (ProofContext.cond_extern ctxt name ^ ":"), Pretty.brk 1];
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   511
    fun prt_asm ((a, _), ts) = Pretty.block (prt_name a @ Pretty.breaks (map (prt_term o fst) ts));
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   512
    fun prt_def ((a, _), (t, _)) = Pretty.block (prt_name a @ [prt_term t]);
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   513
    fun prt_fact ((a, _), ths) = Pretty.block
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   514
      (prt_name a @ Pretty.breaks (map prt_thm (flat (map fst ths))));
12070
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   515
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   516
    fun items _ [] = []
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   517
      | 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
   518
    fun prt_elem (Fixes fixes) = items "fixes" (map prt_fix fixes)
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   519
      | prt_elem (Assumes asms) = items "assumes" (map prt_asm asms)
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   520
      | prt_elem (Defines defs) = items "defines" (map prt_def defs)
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   521
      | prt_elem (Notes facts) = items "notes" (map prt_fact facts);
12277
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   522
  in
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   523
    Pretty.big_list "locale elements:" (map (Pretty.chunks o prt_elem) (flat (map #2 elemss)))
12307
459aa05af6be qualify imported facts;
wenzelm
parents: 12289
diff changeset
   524
    |> Pretty.writeln
12277
2b28d7dd91f5 improved ordering of evaluated elements;
wenzelm
parents: 12273
diff changeset
   525
  end;
12070
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   526
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   527
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   528
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   529
(** define locales **)
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   530
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   531
(* add_locale(_i) *)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   532
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   533
local
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   534
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   535
fun gen_add_locale prep_context prep_expr bname raw_import raw_body thy =
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   536
  let
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   537
    val sign = Theory.sign_of thy;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   538
    val name = Sign.full_name sign bname;
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   539
    val _ = conditional (is_some (get_locale thy name)) (fn () =>
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   540
      error ("Duplicate definition of locale " ^ quote name));
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   541
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   542
    val thy_ctxt = ProofContext.init thy;
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   543
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   544
    val (import_elemss, body_elemss) = prep_context thy_ctxt (raw_import, raw_body);
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   545
    val import = prep_expr thy_ctxt raw_import;
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   546
    val import_elemss = eval_expr thy_ctxt import;
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   547
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   548
    val import_ctxt = thy_ctxt |> activate_elemss import_elemss;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   549
    val body_ctxt = import_ctxt |> activate_elemss body_elemss;
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   550
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   551
    val elems = flat (map #2 body_elemss);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   552
    val (import_parms, body_parms) = pairself (flat o map (#2 o #1)) (import_elemss, body_elemss);
12273
7fb9840d358d beginnings of actual locale expressions;
wenzelm
parents: 12263
diff changeset
   553
    val text = ([], []);  (* FIXME *)
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   554
  in
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   555
    thy
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   556
    |> declare_locale name
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   557
    |> put_locale name (make_locale import (map (fn e => (e, stamp ())) elems)
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   558
      (import_parms @ body_parms, map #1 body_parms) text)
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   559
  end;
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   560
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   561
in
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   562
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   563
val add_locale = gen_add_locale read_context read_expr;
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   564
val add_locale_i = gen_add_locale cert_context (K I);
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   565
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   566
end;
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   567
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   568
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   569
12070
c72fe1edc9e7 proper treatment of local syntax;
wenzelm
parents: 12063
diff changeset
   570
(** store results **)
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   571
12143
dc42d17c5b53 added add_thmss;
wenzelm
parents: 12118
diff changeset
   572
fun add_thmss name args thy =
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   573
  let
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   574
    val {import, params, elems, text} = the_locale thy name;
12143
dc42d17c5b53 added add_thmss;
wenzelm
parents: 12118
diff changeset
   575
    val note = Notes (map (fn ((a, ths), atts) =>
dc42d17c5b53 added add_thmss;
wenzelm
parents: 12118
diff changeset
   576
      ((a, atts), [(map (curry Thm.name_thm a) ths, [])])) args);
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   577
  in
12502
9e7f72e25022 beginning support for type instantiation;
wenzelm
parents: 12323
diff changeset
   578
    thy |> ProofContext.init |> activate_locale name |> activate_elem note;  (*test attributes!*)
12289
ec2dafd0a6a9 clarified locale operations (rename, merge);
wenzelm
parents: 12277
diff changeset
   579
    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
   580
  end;
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   581
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   582
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   583
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   584
(** locale theory setup **)
12063
4c16bdee47d4 added add_locale(_i) and store_thm;
wenzelm
parents: 12058
diff changeset
   585
11896
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   586
val setup =
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   587
 [LocalesData.init];
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   588
1ff33f896720 moved locale.ML to Isar/locale.ML;
wenzelm
parents:
diff changeset
   589
end;