src/Pure/Isar/new_locale.ML
author ballarin
Mon, 24 Nov 2008 18:02:52 +0100
changeset 28878 141ed00656ae
parent 28872 686963dbf6cd
child 28894 ff724071b902
permissions -rw-r--r--
Generalised activation code.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     1
(*  Title:      Pure/Isar/expression.ML
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     2
    ID:         $Id$
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     3
    Author:     Clemens Ballarin, TU Muenchen
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     4
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     5
New locale development --- experimental.
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     6
*)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     7
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     8
signature NEW_LOCALE =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     9
sig
28872
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
    10
  type identifiers
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
    11
  val empty: identifiers
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
    12
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    13
  type locale
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    14
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    15
  val test_locale: theory -> string -> bool
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    16
  val register_locale: string ->
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    17
    (string * sort) list * (Name.binding * typ option * mixfix) list ->
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    18
    term option * term list ->
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    19
    (declaration * stamp) list * (declaration * stamp) list ->
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    20
    ((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * stamp) list ->
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    21
    ((string * Morphism.morphism) * stamp) list -> theory -> theory
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    22
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    23
  (* Locale name space *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    24
  val intern: theory -> xstring -> string
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    25
  val extern: theory -> string -> xstring
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    26
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    27
  (* Specification *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    28
  val params_of: theory -> string -> (Name.binding * typ option * mixfix) list
28872
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
    29
  val specification_of: theory -> string -> term option * term list
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
    30
  val declarations_of: theory -> string -> declaration list * declaration list
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    31
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
    32
  (* Storing results *)
28833
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
    33
  val add_thmss: string -> string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
    34
    Proof.context -> Proof.context
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
    35
  val add_type_syntax: string -> declaration -> Proof.context -> Proof.context
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
    36
  val add_term_syntax: string -> declaration -> Proof.context -> Proof.context
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
    37
  val add_declaration: string -> declaration -> Proof.context -> Proof.context
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
    38
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
    39
  (* Activate locales *)
28878
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
    40
  val activate_declarations: theory -> string * Morphism.morphism ->
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
    41
    identifiers * Proof.context -> identifiers * Proof.context
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
    42
  val activate_facts: theory -> string * Morphism.morphism ->
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
    43
    identifiers * Proof.context -> identifiers * Proof.context
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
    44
(*  val activate: string -> theory -> (Element.context_i -> 'a -> 'a) -> 'a -> 'a *)
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    45
  val init: string -> theory -> Proof.context
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    46
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    47
  (* Diagnostic *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    48
  val print_locales: theory -> unit
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    49
  val print_locale: theory -> bool -> bstring -> unit
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    50
end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    51
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    52
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    53
structure NewLocale: NEW_LOCALE =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    54
struct
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    55
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    56
datatype ctxt = datatype Element.ctxt;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    57
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    58
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    59
(*** Basics ***)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    60
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    61
datatype locale = Loc of {
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    62
  (* extensible lists are in reverse order: decls, notes, dependencies *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    63
  parameters: (string * sort) list * (Name.binding * typ option * mixfix) list,
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    64
    (* type and term parameters *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    65
  spec: term option * term list,
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    66
    (* assumptions (as a single predicate expression) and defines *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    67
  decls: (declaration * stamp) list * (declaration * stamp) list,
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    68
    (* type and term syntax declarations *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    69
  notes: ((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * stamp) list,
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    70
    (* theorem declarations *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    71
  dependencies: ((string * Morphism.morphism) * stamp) list
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    72
    (* locale dependencies (sublocale relation) *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    73
}
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    74
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    75
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    76
(*** Theory data ***)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    77
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    78
structure LocalesData = TheoryDataFun
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    79
(
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    80
  type T = NameSpace.T * locale Symtab.table;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    81
    (* locale namespace and locales of the theory *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    82
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    83
  val empty = (NameSpace.empty, Symtab.empty);
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    84
  val copy = I;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    85
  val extend = I;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    86
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    87
  fun join_locales _
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    88
    (Loc {parameters, spec, decls = (decls1, decls2), notes, dependencies},
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    89
      Loc {decls = (decls1', decls2'), notes = notes',
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    90
        dependencies = dependencies', ...}) =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    91
        let fun s_merge x = merge (eq_snd (op =)) x in
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    92
          Loc {parameters = parameters,
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    93
            spec = spec,
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    94
            decls = (s_merge (decls1, decls1'), s_merge (decls2, decls2')),
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    95
            notes = s_merge (notes, notes'),
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    96
            dependencies = s_merge (dependencies, dependencies')
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    97
          }          
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    98
        end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    99
  fun merge _ ((space1, locs1), (space2, locs2)) =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   100
    (NameSpace.merge (space1, space2), Symtab.join join_locales (locs1, locs2));
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   101
);
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   102
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   103
val intern = NameSpace.intern o #1 o LocalesData.get;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   104
val extern = NameSpace.extern o #1 o LocalesData.get;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   105
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   106
fun get_locale thy name = Symtab.lookup (#2 (LocalesData.get thy)) name;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   107
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   108
fun the_locale thy name = case get_locale thy name
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   109
 of SOME loc => loc
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   110
  | NONE => error ("Unknown locale " ^ quote name);
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   111
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   112
fun test_locale thy name = case get_locale thy name
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   113
 of SOME _ => true | NONE => false;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   114
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   115
fun register_locale name parameters spec decls notes dependencies thy =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   116
  thy |> LocalesData.map (fn (space, locs) =>
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   117
    (Sign.declare_name thy name space, Symtab.update (name,
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   118
      Loc {parameters = parameters, spec = spec, decls = decls, notes = notes,
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   119
        dependencies = dependencies}) locs));
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   120
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   121
fun change_locale name f thy =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   122
  let
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   123
    val Loc {parameters, spec, decls, notes, dependencies} =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   124
        the_locale thy name;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   125
    val (parameters', spec', decls', notes', dependencies') =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   126
      f (parameters, spec, decls, notes, dependencies);
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   127
  in
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   128
    thy
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   129
    |> (LocalesData.map o apsnd) (Symtab.update (name, Loc {parameters = parameters',
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   130
      spec = spec', decls = decls', notes = notes', dependencies = dependencies'}))
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   131
  end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   132
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   133
fun print_locales thy =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   134
  let val (space, locs) = LocalesData.get thy in
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   135
    Pretty.strs ("locales:" :: map #1 (NameSpace.extern_table (space, locs)))
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   136
    |> Pretty.writeln
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   137
  end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   138
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   139
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   140
(*** Primitive operations ***)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   141
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   142
fun params_of thy name =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   143
  let
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   144
    val Loc {parameters = (_, params), ...} = the_locale thy name
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   145
  in params end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   146
28872
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   147
fun specification_of thy name =
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   148
  let
28872
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   149
    val Loc {spec, ...} = the_locale thy name
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   150
  in spec end;
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   151
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   152
fun declarations_of thy name =
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   153
  let
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   154
    val Loc {decls, ...} = the_locale thy name
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   155
  in
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   156
    decls |> apfst (map fst) |> apsnd (map fst)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   157
  end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   158
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   159
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   160
(*** Activate context elements of locale ***)
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   161
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   162
(* Resolve locale dependencies in a depth-first fashion *)
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   163
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   164
local
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   165
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   166
structure Idtab = TableFun(type key = string * term list
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   167
  val ord = prod_ord string_ord (list_ord Term.fast_term_ord));
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   168
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   169
in
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   170
28872
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   171
type identifiers = unit Idtab.table
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   172
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   173
val empty = (Idtab.empty : identifiers);
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   174
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   175
fun roundup thy (deps, marked) =
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   176
(* FIXME simplify code: should probably only get one dep as argument *)
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   177
  let
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   178
    fun add (name, morph) (deps, marked) =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   179
      let
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   180
        val Loc {parameters = (_, params), dependencies, ...} = the_locale thy name;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   181
        val instance = params |>
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   182
          map ((fn (b, T, _) => Free (Name.name_of b, the T)) #> Morphism.term morph);
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   183
      in
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   184
        if Idtab.defined marked (name, instance)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   185
        then (deps, marked)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   186
        else
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   187
          let
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   188
            val dependencies' =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   189
              map (fn ((name, morph'), _) => (name, morph' $>  morph)) dependencies;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   190
            val marked' = Idtab.insert (op =) ((name, instance), ()) marked;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   191
            val (deps', marked'') = fold_rev add dependencies' ([], marked');
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   192
          in
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   193
            (cons (name, morph) deps' @ deps, marked'')
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   194
          end
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   195
      end
28872
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   196
  in fold_rev add deps ([], marked) end;
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   197
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   198
end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   199
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   200
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   201
fun activate_decls thy (name, morph) ctxt =
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   202
  let
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   203
    val Loc {decls = (typ_decls, term_decls), ...} = the_locale thy name;
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   204
  in
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   205
    ctxt |> fold_rev (fn (decl, _) => Context.proof_map (decl morph)) typ_decls |>
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   206
      fold_rev (fn (decl, _) => Context.proof_map (decl morph)) term_decls
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   207
  end;
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   208
28878
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   209
fun activate_declarations thy (name, morph) (marked, ctxt) =
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   210
  let
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   211
    val name' = intern thy name;
28872
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   212
    val Loc {dependencies, ...} = the_locale thy name';
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   213
    val (dependencies', marked) =
28878
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   214
      roundup thy ((name', morph) ::
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   215
        map (fn ((n, morph'), _) => (n, morph' $> morph)) dependencies, marked);
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   216
  in
28872
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   217
    (marked, ctxt |> fold_rev (activate_decls thy) dependencies')
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   218
  end;
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   219
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   220
fun activate_notes activ_elem thy (name, morph) input =
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   221
  let
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   222
    val Loc {notes, ...} = the_locale thy name;
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   223
    fun activate ((kind, facts), _) input =
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   224
      let
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   225
        val facts' = facts |> Element.facts_map (Element.morph_ctxt morph)
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   226
      in activ_elem (Notes (kind, facts')) input end;
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   227
  in
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   228
    fold_rev activate notes input
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   229
  end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   230
28878
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   231
fun activate_facts_intern thy activ_elem (name, morph) (marked, ctxt) =
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   232
  let
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   233
    val name' = intern thy name;
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   234
    val Loc {dependencies, ...} = the_locale thy name';
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   235
    val (dependencies', marked) =
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   236
      roundup thy ((name', morph) ::
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   237
        map (fn ((n, morph'), _) => (n, morph' $> morph)) dependencies, marked);
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   238
  in
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   239
    (marked, ctxt |> fold_rev (activate_notes activ_elem thy) dependencies')
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   240
  end;
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   241
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   242
fun activate name thy activ_elem input =
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   243
  let
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   244
    val name' = intern thy name;
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   245
    val Loc {parameters = (_, params), spec = (asm, defs), dependencies, ...} =
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   246
      the_locale thy name';
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   247
    val dependencies' =
28872
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   248
      (name', Morphism.identity) :: fst (roundup thy (map fst dependencies, empty));
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   249
  in
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   250
    input |>
28851
368aca388dd9 Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents: 28833
diff changeset
   251
      (if not (null params) then activ_elem (Fixes params) else I) |>
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   252
      (* FIXME type parameters *)
28851
368aca388dd9 Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents: 28833
diff changeset
   253
      (if is_some asm then activ_elem (Assumes [(Attrib.no_binding, [(the asm, [])])]) else I) |>
368aca388dd9 Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents: 28833
diff changeset
   254
      (if not (null defs)
368aca388dd9 Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents: 28833
diff changeset
   255
        then activ_elem (Defines (map (fn def => (Attrib.no_binding, (def, []))) defs))
368aca388dd9 Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents: 28833
diff changeset
   256
        else I) |>
28878
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   257
      pair empty |> activate_facts_intern thy activ_elem (name', Morphism.identity) |> snd
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   258
  end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   259
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   260
local
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   261
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   262
fun init_elem (Fixes fixes) ctxt = ctxt |>
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   263
      ProofContext.add_fixes_i fixes |> snd
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   264
  | init_elem (Assumes assms) ctxt =
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   265
      let
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   266
        val assms' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) assms
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   267
      in
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   268
        ctxt |> fold Variable.auto_fixes (maps (map fst o snd) assms') |>
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   269
          ProofContext.add_assms_i Assumption.assume_export assms' |> snd
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   270
     end 
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   271
  | init_elem (Defines defs) ctxt =
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   272
      let
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   273
        val defs' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) defs
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   274
      in
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   275
        ctxt |> fold Variable.auto_fixes (map (fst o snd) defs') |>
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   276
          ProofContext.add_assms_i LocalDefs.def_export (map (fn (attn, t) => (attn, [t])) defs') |>
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   277
          snd
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   278
      end
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   279
  | init_elem (Notes (kind, facts)) ctxt =
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   280
      let
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   281
        val facts' = Attrib.map_facts (Attrib.attribute_i (ProofContext.theory_of ctxt)) facts
28865
194e8f3439fe Locale.local_note_qualified
haftmann
parents: 28851
diff changeset
   282
      in fold (fn args => Locale.local_note_qualified kind args #> snd) facts' ctxt end
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   283
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   284
fun cons_elem false (Notes notes) elems = elems
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   285
  | cons_elem _ elem elems = elem :: elems
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   286
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   287
in
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   288
28878
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   289
fun activate_facts thy dep = activate_facts_intern thy init_elem dep;
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
   290
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   291
fun init name thy = activate name thy init_elem (ProofContext.init thy);
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   292
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   293
fun print_locale thy show_facts name =
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   294
  let val ctxt = init name thy
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   295
  in
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   296
    Pretty.big_list "locale elements:"
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   297
      (activate name thy (cons_elem show_facts) [] |> rev |> 
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   298
        map (Element.pretty_ctxt ctxt) |> map Pretty.chunks) |> Pretty.writeln
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   299
  end
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   300
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   301
end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   302
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   303
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   304
(*** Storing results ***)
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   305
28833
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   306
(* Theorems *)
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   307
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   308
fun add_thmss loc kind args ctxt =
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   309
  let
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   310
    val (([Notes args'], _), ctxt') = Element.activate_i [Notes (kind, args)] ctxt;
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   311
    val ctxt'' = ctxt' |> ProofContext.theory
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   312
      (change_locale loc
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   313
        (fn (parameters, spec, decls, notes, dependencies) =>
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   314
          (parameters, spec, decls, (args', stamp ()) :: notes, dependencies)))
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   315
      (* FIXME registrations *)
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   316
  in ctxt'' end;
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   317
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   318
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   319
(* Declarations *)
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   320
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   321
local
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   322
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   323
fun decl_attrib decl phi = Thm.declaration_attribute (K (decl phi));
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   324
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   325
fun add_decls add loc decl =
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   326
  ProofContext.theory (change_locale loc
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   327
    (fn (parameters, spec, decls, notes, dependencies) =>
28833
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   328
      (parameters, spec, add (decl, stamp ()) decls, notes, dependencies))) #>
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   329
  add_thmss loc Thm.internalK
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   330
    [((Name.no_binding, [Attrib.internal (decl_attrib decl)]), [([Drule.dummy_thm], [])])];
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   331
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   332
in
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   333
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   334
val add_type_syntax = add_decls (apfst o cons);
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   335
val add_term_syntax = add_decls (apsnd o cons);
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   336
val add_declaration = add_decls (K I);
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   337
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   338
end;
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   339
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   340
end;
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   341