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