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