src/Pure/Isar/new_locale.ML
author ballarin
Mon, 08 Dec 2008 14:18:29 +0100
changeset 29019 8e7d6f959bd7
parent 29018 17538bdef546
child 29020 3e95d28114a1
permissions -rw-r--r--
Explicitly close up defines.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
     1
(*  Title:      Pure/Isar/new_locale.ML
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     2
    Author:     Clemens Ballarin, TU Muenchen
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     3
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     4
New locale development --- experimental.
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     5
*)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     6
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     7
signature NEW_LOCALE =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     8
sig
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
     9
  type locale
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    10
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    11
  val test_locale: theory -> string -> bool
28991
694227dd3e8c dropped NameSpace.declare_base
haftmann
parents: 28965
diff changeset
    12
  val register_locale: bstring ->
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28951
diff changeset
    13
    (string * sort) list * (Binding.T * typ option * mixfix) list ->
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    14
    term option * term list ->
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    15
    (declaration * stamp) list * (declaration * stamp) list ->
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    16
    ((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * stamp) list ->
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    17
    ((string * Morphism.morphism) * stamp) list -> theory -> theory
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    18
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    19
  (* Locale name space *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    20
  val intern: theory -> xstring -> string
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    21
  val extern: theory -> string -> xstring
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    22
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    23
  (* Specification *)
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28951
diff changeset
    24
  val params_of: theory -> string -> (Binding.T * typ option * mixfix) list
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
    25
  val instance_of: theory -> string -> Morphism.morphism -> term list
28872
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
    26
  val specification_of: theory -> string -> term option * term list
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
    27
  val declarations_of: theory -> string -> declaration list * declaration list
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    28
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
    29
  (* Storing results *)
28833
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
    30
  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
    31
    Proof.context -> Proof.context
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
    32
  val add_type_syntax: string -> declaration -> Proof.context -> Proof.context
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
    33
  val add_term_syntax: string -> declaration -> Proof.context -> Proof.context
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
    34
  val add_declaration: string -> declaration -> Proof.context -> Proof.context
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
    35
  val add_dependency: string -> (string * Morphism.morphism) -> theory -> theory
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
    36
28903
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    37
  (* Activation *)
28878
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
    38
  val activate_declarations: theory -> string * Morphism.morphism ->
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
    39
    Proof.context -> Proof.context
29018
17538bdef546 Interpretation in proof contexts.
ballarin
parents: 28994
diff changeset
    40
  val activate_global_facts: string * Morphism.morphism -> theory -> theory
17538bdef546 Interpretation in proof contexts.
ballarin
parents: 28994
diff changeset
    41
  val activate_local_facts: string * Morphism.morphism -> Proof.context -> Proof.context
28878
141ed00656ae Generalised activation code.
ballarin
parents: 28872
diff changeset
    42
(*  val activate: string -> theory -> (Element.context_i -> 'a -> 'a) -> 'a -> 'a *)
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    43
  val init: string -> theory -> Proof.context
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    44
28903
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    45
  (* Reasoning about locales *)
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    46
  val witness_attrib: attribute
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    47
  val intro_attrib: attribute
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    48
  val unfold_attrib: attribute
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    49
  val intro_locales_tac: bool -> Proof.context -> thm list -> tactic
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    50
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
    51
  (* Identifiers and registrations *)
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
    52
  val clear_local_idents: Proof.context -> Proof.context
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
    53
  val add_global_registration: (string * Morphism.morphism) -> theory -> theory
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
    54
  val get_global_registrations: theory -> (string * Morphism.morphism) list
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
    55
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    56
  (* Diagnostic *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    57
  val print_locales: theory -> unit
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    58
  val print_locale: theory -> bool -> bstring -> unit
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    59
end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    60
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    61
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
    62
(*** Theorem list extensible via attribute --- to control intro_locales_tac ***)
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
    63
28903
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    64
(* FIXME: consider moving named_thms.ML up in the build hierarchy and using that *)
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    65
functor ThmsFun() =
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    66
struct
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    67
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    68
structure Data = GenericDataFun
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    69
(
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    70
  type T = thm list;
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    71
  val empty = [];
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    72
  val extend = I;
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    73
  fun merge _ = Thm.merge_thms;
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    74
);
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    75
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    76
val get = Data.get o Context.Proof;
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    77
val add = Thm.declaration_attribute (Data.map o Thm.add_thm);
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    78
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    79
end;
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    80
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
    81
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    82
structure NewLocale: NEW_LOCALE =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    83
struct
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    84
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    85
datatype ctxt = datatype Element.ctxt;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    86
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    87
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    88
(*** Basics ***)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    89
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    90
datatype locale = Loc of {
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    91
  (* extensible lists are in reverse order: decls, notes, dependencies *)
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28951
diff changeset
    92
  parameters: (string * sort) list * (Binding.T * typ option * mixfix) list,
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    93
    (* type and term parameters *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    94
  spec: term option * term list,
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    95
    (* assumptions (as a single predicate expression) and defines *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    96
  decls: (declaration * stamp) list * (declaration * stamp) list,
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    97
    (* type and term syntax declarations *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    98
  notes: ((string * (Attrib.binding * (thm list * Attrib.src list) list) list) * stamp) list,
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
    99
    (* theorem declarations *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   100
  dependencies: ((string * Morphism.morphism) * stamp) list
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   101
    (* locale dependencies (sublocale relation) *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   102
}
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   103
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   104
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   105
(*** Theory data ***)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   106
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   107
structure LocalesData = TheoryDataFun
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   108
(
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   109
  type T = NameSpace.T * locale Symtab.table;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   110
    (* locale namespace and locales of the theory *)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   111
28991
694227dd3e8c dropped NameSpace.declare_base
haftmann
parents: 28965
diff changeset
   112
  val empty = NameSpace.empty_table;
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   113
  val copy = I;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   114
  val extend = I;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   115
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   116
  fun join_locales _
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   117
    (Loc {parameters, spec, decls = (decls1, decls2), notes, dependencies},
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   118
      Loc {decls = (decls1', decls2'), notes = notes',
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   119
        dependencies = dependencies', ...}) =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   120
        let fun s_merge x = merge (eq_snd (op =)) x in
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   121
          Loc {parameters = parameters,
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   122
            spec = spec,
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   123
            decls = (s_merge (decls1, decls1'), s_merge (decls2, decls2')),
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   124
            notes = s_merge (notes, notes'),
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   125
            dependencies = s_merge (dependencies, dependencies')
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   126
          }          
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   127
        end;
28991
694227dd3e8c dropped NameSpace.declare_base
haftmann
parents: 28965
diff changeset
   128
  fun merge _ = NameSpace.join_tables join_locales;
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   129
);
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   130
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   131
val intern = NameSpace.intern o #1 o LocalesData.get;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   132
val extern = NameSpace.extern o #1 o LocalesData.get;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   133
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   134
fun get_locale thy name = Symtab.lookup (#2 (LocalesData.get thy)) name;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   135
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   136
fun the_locale thy name = case get_locale thy name
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   137
 of SOME loc => loc
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   138
  | NONE => error ("Unknown locale " ^ quote name);
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   139
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   140
fun test_locale thy name = case get_locale thy name
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   141
 of SOME _ => true | NONE => false;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   142
28991
694227dd3e8c dropped NameSpace.declare_base
haftmann
parents: 28965
diff changeset
   143
fun register_locale bname parameters spec decls notes dependencies thy =
694227dd3e8c dropped NameSpace.declare_base
haftmann
parents: 28965
diff changeset
   144
  thy |> LocalesData.map (NameSpace.bind (Sign.naming_of thy) (Binding.name bname,
694227dd3e8c dropped NameSpace.declare_base
haftmann
parents: 28965
diff changeset
   145
    Loc {parameters = parameters, spec = spec, decls = decls, notes = notes,
694227dd3e8c dropped NameSpace.declare_base
haftmann
parents: 28965
diff changeset
   146
      dependencies = dependencies}) #> snd);
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   147
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   148
fun change_locale name f thy =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   149
  let
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   150
    val Loc {parameters, spec, decls, notes, dependencies} =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   151
        the_locale thy name;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   152
    val (parameters', spec', decls', notes', dependencies') =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   153
      f (parameters, spec, decls, notes, dependencies);
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   154
  in
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   155
    thy
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   156
    |> (LocalesData.map o apsnd) (Symtab.update (name, Loc {parameters = parameters',
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   157
      spec = spec', decls = decls', notes = notes', dependencies = dependencies'}))
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   158
  end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   159
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   160
fun print_locales thy =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   161
  let val (space, locs) = LocalesData.get thy in
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   162
    Pretty.strs ("locales:" :: map #1 (NameSpace.extern_table (space, locs)))
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   163
    |> Pretty.writeln
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   164
  end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   165
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   166
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   167
(*** Primitive operations ***)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   168
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   169
fun params_of thy name =
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   170
  let
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   171
    val Loc {parameters = (_, params), ...} = the_locale thy name
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   172
  in params end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   173
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   174
fun instance_of thy name morph =
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   175
  params_of thy name |>
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   176
    map ((fn (b, T, _) => Free (Name.name_of b, the T)) #> Morphism.term morph);
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   177
28872
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   178
fun specification_of thy name =
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   179
  let
28872
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   180
    val Loc {spec, ...} = the_locale thy name
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   181
  in spec end;
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   182
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   183
fun declarations_of thy name =
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   184
  let
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   185
    val Loc {decls, ...} = the_locale thy name
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   186
  in
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   187
    decls |> apfst (map fst) |> apsnd (map fst)
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   188
  end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   189
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   190
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   191
(*** Activate context elements of locale ***)
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   192
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   193
(** Identifiers: activated locales in theory or proof context **)
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   194
28899
7bf5d7f154b8 Perform higher-order pattern matching during round-up.
ballarin
parents: 28897
diff changeset
   195
type identifiers = (string * term list) list;
7bf5d7f154b8 Perform higher-order pattern matching during round-up.
ballarin
parents: 28897
diff changeset
   196
7bf5d7f154b8 Perform higher-order pattern matching during round-up.
ballarin
parents: 28897
diff changeset
   197
val empty = ([] : identifiers);
7bf5d7f154b8 Perform higher-order pattern matching during round-up.
ballarin
parents: 28897
diff changeset
   198
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   199
fun ident_eq thy ((n, ts), (m, ss)) = (m = n) andalso Pattern.matchess thy (ss, ts);
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   200
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   201
local
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   202
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   203
datatype 'a delayed = Ready of 'a | ToDo of ('a delayed * 'a delayed);
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   204
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   205
structure IdentifiersData = GenericDataFun
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   206
(
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   207
  type T = identifiers delayed;
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   208
  val empty = Ready empty;
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   209
  val extend = I;
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   210
  fun merge _ = ToDo;
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   211
);
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   212
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   213
in
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   214
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   215
fun finish thy (ToDo (i1, i2)) = merge (ident_eq thy) (finish thy i1, finish thy i2)
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   216
  | finish _ (Ready ids) = ids;
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   217
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   218
val _ = Context.>> (Context.map_theory (Theory.at_begin (fn thy =>
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   219
  (case IdentifiersData.get (Context.Theory thy) of
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   220
    Ready _ => NONE |
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   221
    ids => SOME (Context.theory_map (IdentifiersData.put (Ready (finish thy ids))) thy))
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   222
  )));
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   223
  
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   224
fun get_global_idents thy =
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   225
  let val (Ready ids) = (IdentifiersData.get o Context.Theory) thy in ids end;
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   226
val put_global_idents = Context.theory_map o IdentifiersData.put o Ready;
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   227
val clear_global_idents = put_global_idents empty;
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   228
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   229
fun get_local_idents ctxt =
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   230
  let val (Ready ids) = (IdentifiersData.get o Context.Proof) ctxt in ids end;
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   231
val put_local_idents = Context.proof_map o IdentifiersData.put o Ready;
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   232
val clear_local_idents = put_local_idents empty;
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   233
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   234
end;
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   235
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   236
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   237
(** Resolve locale dependencies in a depth-first fashion **)
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   238
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   239
local
28872
686963dbf6cd add_locale functional.
ballarin
parents: 28865
diff changeset
   240
28897
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   241
val roundup_bound = 120;
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   242
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   243
fun add thy depth (name, morph) (deps, marked) =
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   244
  if depth > roundup_bound
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   245
  then error "Roundup bound exceeded (sublocale relation probably not terminating)."
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   246
  else
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   247
    let
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   248
      val Loc {parameters = (_, params), dependencies, ...} = the_locale thy name;
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   249
      val instance = instance_of thy name morph;
28897
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   250
    in
28899
7bf5d7f154b8 Perform higher-order pattern matching during round-up.
ballarin
parents: 28897
diff changeset
   251
      if member (ident_eq thy) marked (name, instance)
28897
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   252
      then (deps, marked)
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   253
      else
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   254
        let
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   255
          val dependencies' =
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   256
            map (fn ((name, morph'), _) => (name, morph' $>  morph)) dependencies;
28899
7bf5d7f154b8 Perform higher-order pattern matching during round-up.
ballarin
parents: 28897
diff changeset
   257
          val marked' = (name, instance) :: marked;
28897
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   258
          val (deps', marked'') = fold_rev (add thy (depth + 1)) dependencies' ([], marked');
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   259
        in
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   260
          ((name, morph) :: deps' @ deps, marked'')
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   261
        end
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   262
    end;
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   263
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   264
in
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   265
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   266
fun roundup thy activate_dep (name, morph) (marked, input) =
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   267
  let
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   268
    (* Find all dependencies incuding new ones (which are dependencies enriching
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   269
      existing registrations). *)
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   270
    val (dependencies, marked') = add thy 0 (name, morph) ([], empty);
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   271
    (* Filter out exisiting fragments. *)
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   272
    val dependencies' = filter_out (fn (name, morph) =>
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   273
      member (ident_eq thy) marked (name, instance_of thy name morph)) dependencies;
28894
ff724071b902 Command to add dependencies, fixed processing of dependencies.
ballarin
parents: 28878
diff changeset
   274
  in
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   275
    (merge (ident_eq thy) (marked, marked'), input |> fold_rev (activate_dep thy) dependencies')
28894
ff724071b902 Command to add dependencies, fixed processing of dependencies.
ballarin
parents: 28878
diff changeset
   276
  end;
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   277
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   278
end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   279
28897
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   280
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   281
(* Declarations, facts and entire locale content *)
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   282
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   283
fun activate_decls thy (name, morph) ctxt =
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   284
  let
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   285
    val Loc {decls = (typ_decls, term_decls), ...} = the_locale thy name;
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   286
  in
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   287
    ctxt |> fold_rev (fn (decl, _) => Context.proof_map (decl morph)) typ_decls |>
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   288
      fold_rev (fn (decl, _) => Context.proof_map (decl morph)) term_decls
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   289
  end;
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   290
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   291
fun activate_notes activ_elem thy (name, morph) input =
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   292
  let
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   293
    val Loc {notes, ...} = the_locale thy name;
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   294
    fun activate ((kind, facts), _) input =
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   295
      let
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   296
        val facts' = facts |> Element.facts_map (Element.morph_ctxt morph)
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   297
      in activ_elem (Notes (kind, facts')) input end;
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   298
  in
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   299
    fold_rev activate notes input
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   300
  end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   301
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   302
fun activate_all name thy activ_elem (marked, input) =
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   303
  let
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   304
    val Loc {parameters = (_, params), spec = (asm, defs), ...} =
28902
2019bcc9d8bf Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents: 28899
diff changeset
   305
      the_locale thy name;
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   306
  in
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   307
    input |>
28851
368aca388dd9 Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents: 28833
diff changeset
   308
      (if not (null params) then activ_elem (Fixes params) else I) |>
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   309
      (* FIXME type parameters *)
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28951
diff changeset
   310
      (if is_some asm then activ_elem (Assumes [(Attrib.empty_binding, [(the asm, [])])]) else I) |>
28851
368aca388dd9 Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents: 28833
diff changeset
   311
      (if not (null defs)
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28951
diff changeset
   312
        then activ_elem (Defines (map (fn def => (Attrib.empty_binding, (def, []))) defs))
28851
368aca388dd9 Use 'if' in connection with 'is_some' and 'the'.
ballarin
parents: 28833
diff changeset
   313
        else I) |>
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   314
      pair marked |> roundup thy (activate_notes activ_elem) (name, Morphism.identity)
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   315
  end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   316
28897
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   317
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   318
(** Public activation functions **)
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   319
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   320
local
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   321
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   322
fun init_global_elem (Notes (kind, facts)) thy =
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   323
      let
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   324
        val facts' = Attrib.map_facts (Attrib.attribute_i thy) facts
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   325
      in Locale.global_note_qualified kind facts' thy |> snd end
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   326
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   327
fun init_local_elem (Fixes fixes) ctxt = ctxt |>
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   328
      ProofContext.add_fixes_i fixes |> snd
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   329
  | init_local_elem (Assumes assms) ctxt =
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   330
      let
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   331
        val assms' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) assms
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   332
      in
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   333
        ctxt |> fold Variable.auto_fixes (maps (map fst o snd) assms') |>
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   334
          ProofContext.add_assms_i Assumption.assume_export assms' |> snd
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   335
     end 
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   336
  | init_local_elem (Defines defs) ctxt =
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   337
      let
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   338
        val defs' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) defs
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   339
      in
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   340
        ctxt |> fold Variable.auto_fixes (map (fst o snd) defs') |>
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   341
          ProofContext.add_assms_i LocalDefs.def_export (map (fn (attn, t) => (attn, [t])) defs') |>
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   342
          snd
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   343
      end
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   344
  | init_local_elem (Notes (kind, facts)) ctxt =
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   345
      let
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   346
        val facts' = Attrib.map_facts (Attrib.attribute_i (ProofContext.theory_of ctxt)) facts
28940
df0cb410be35 Locale.*note* with conventional argument type
haftmann
parents: 28927
diff changeset
   347
      in Locale.local_note_qualified kind facts' ctxt |> snd end
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   348
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   349
fun cons_elem false (Notes notes) elems = elems
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   350
  | cons_elem _ elem elems = elem :: elems
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   351
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   352
in
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   353
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   354
fun activate_declarations thy dep ctxt =
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   355
  roundup thy activate_decls dep (get_local_idents ctxt, ctxt) |> uncurry put_local_idents;
29019
8e7d6f959bd7 Explicitly close up defines.
ballarin
parents: 29018
diff changeset
   356
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   357
fun activate_global_facts dep thy =
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   358
  roundup thy (activate_notes init_global_elem) dep (get_global_idents thy, thy) |>
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   359
  uncurry put_global_idents;
28897
3d166f1bd733 Roundup bound.
ballarin
parents: 28894
diff changeset
   360
29018
17538bdef546 Interpretation in proof contexts.
ballarin
parents: 28994
diff changeset
   361
fun activate_local_facts dep ctxt =
17538bdef546 Interpretation in proof contexts.
ballarin
parents: 28994
diff changeset
   362
  roundup (ProofContext.theory_of ctxt) (activate_notes init_local_elem) dep
17538bdef546 Interpretation in proof contexts.
ballarin
parents: 28994
diff changeset
   363
    (get_local_idents ctxt, ctxt) |>
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   364
  uncurry put_local_idents;
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   365
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   366
fun init name thy = activate_all name thy init_local_elem (empty, ProofContext.init thy) |>
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   367
  uncurry put_local_idents;
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   368
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   369
fun print_locale thy show_facts name =
28902
2019bcc9d8bf Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents: 28899
diff changeset
   370
  let
2019bcc9d8bf Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents: 28899
diff changeset
   371
    val name' = intern thy name;
2019bcc9d8bf Ahere to modern naming conventions; proper treatment of internal vs external names.
ballarin
parents: 28899
diff changeset
   372
    val ctxt = init name' thy
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   373
  in
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   374
    Pretty.big_list "locale elements:"
28951
e89dde5f365c Sublocale: removed public after_qed; identifiers private to NewLocale.
ballarin
parents: 28940
diff changeset
   375
      (activate_all name' thy (cons_elem show_facts) (empty, []) |> snd |> rev |> 
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   376
        map (Element.pretty_ctxt ctxt) |> map Pretty.chunks) |> Pretty.writeln
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   377
  end
28795
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   378
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   379
end;
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   380
6891e273c33b Initial part of locale reimplementation.
ballarin
parents:
diff changeset
   381
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   382
(*** Storing results ***)
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   383
28833
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   384
(* Theorems *)
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   385
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   386
fun add_thmss loc kind args ctxt =
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   387
  let
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   388
    val (([Notes args'], _), ctxt') = Element.activate_i [Notes (kind, args)] ctxt;
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   389
    val ctxt'' = ctxt' |> ProofContext.theory
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   390
      (change_locale loc
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   391
        (fn (parameters, spec, decls, notes, dependencies) =>
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   392
          (parameters, spec, decls, (args', stamp ()) :: notes, dependencies)))
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   393
      (* FIXME registrations *)
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   394
  in ctxt'' end;
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   395
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   396
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   397
(* Declarations *)
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   398
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   399
local
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   400
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   401
fun decl_attrib decl phi = Thm.declaration_attribute (K (decl phi));
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   402
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   403
fun add_decls add loc decl =
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   404
  ProofContext.theory (change_locale loc
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   405
    (fn (parameters, spec, decls, notes, dependencies) =>
28833
f356687a7659 add_thmss
ballarin
parents: 28818
diff changeset
   406
      (parameters, spec, add (decl, stamp ()) decls, notes, dependencies))) #>
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   407
  add_thmss loc Thm.internalK
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28951
diff changeset
   408
    [((Binding.empty, [Attrib.internal (decl_attrib decl)]), [([Drule.dummy_thm], [])])];
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   409
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   410
in
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   411
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   412
val add_type_syntax = add_decls (apfst o cons);
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   413
val add_term_syntax = add_decls (apsnd o cons);
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   414
val add_declaration = add_decls (K I);
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   415
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   416
end;
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   417
28894
ff724071b902 Command to add dependencies, fixed processing of dependencies.
ballarin
parents: 28878
diff changeset
   418
(* Dependencies *)
ff724071b902 Command to add dependencies, fixed processing of dependencies.
ballarin
parents: 28878
diff changeset
   419
ff724071b902 Command to add dependencies, fixed processing of dependencies.
ballarin
parents: 28878
diff changeset
   420
fun add_dependency loc dep =
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   421
  change_locale loc
28894
ff724071b902 Command to add dependencies, fixed processing of dependencies.
ballarin
parents: 28878
diff changeset
   422
    (fn (parameters, spec, decls, notes, dependencies) =>
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   423
      (parameters, spec, decls, notes, (dep, stamp ()) :: dependencies));
28894
ff724071b902 Command to add dependencies, fixed processing of dependencies.
ballarin
parents: 28878
diff changeset
   424
28903
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   425
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   426
(*** Reasoning about locales ***)
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   427
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   428
(** Storage for witnesses, intro and unfold rules **)
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   429
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   430
structure Witnesses = ThmsFun();
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   431
structure Intros = ThmsFun();
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   432
structure Unfolds = ThmsFun();
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   433
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   434
val witness_attrib = Witnesses.add;
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   435
val intro_attrib = Intros.add;
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   436
val unfold_attrib = Unfolds.add;
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   437
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   438
(** Tactic **)
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   439
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   440
fun intro_locales_tac eager ctxt facts st =
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   441
  Method.intros_tac
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   442
    (Witnesses.get ctxt @ Intros.get ctxt @ (if eager then Unfolds.get ctxt else [])) facts st;
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   443
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   444
val _ = Context.>> (Context.map_theory
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   445
  (Method.add_methods
28927
7e631979922f Methods intro_locales and unfold_locales apply to both old and new locales.
ballarin
parents: 28903
diff changeset
   446
    [("intro_locales",
7e631979922f Methods intro_locales and unfold_locales apply to both old and new locales.
ballarin
parents: 28903
diff changeset
   447
      Method.ctxt_args (fn ctxt => Method.METHOD (intro_locales_tac false ctxt ORELSE'
7e631979922f Methods intro_locales and unfold_locales apply to both old and new locales.
ballarin
parents: 28903
diff changeset
   448
        Locale.intro_locales_tac false ctxt)),
28903
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   449
      "back-chain introduction rules of locales without unfolding predicates"),
28927
7e631979922f Methods intro_locales and unfold_locales apply to both old and new locales.
ballarin
parents: 28903
diff changeset
   450
     ("unfold_locales",
7e631979922f Methods intro_locales and unfold_locales apply to both old and new locales.
ballarin
parents: 28903
diff changeset
   451
      Method.ctxt_args (fn ctxt => Method.METHOD (intro_locales_tac true ctxt ORELSE'
7e631979922f Methods intro_locales and unfold_locales apply to both old and new locales.
ballarin
parents: 28903
diff changeset
   452
        Locale.intro_locales_tac true ctxt)),
28903
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   453
      "back-chain all introduction rules of locales")]));
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   454
b3fc3a62247a Intro_locales_tac to simplify goals involving locale predicates.
ballarin
parents: 28902
diff changeset
   455
29018
17538bdef546 Interpretation in proof contexts.
ballarin
parents: 28994
diff changeset
   456
(*** Registrations: interpretations in theories ***)
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   457
29018
17538bdef546 Interpretation in proof contexts.
ballarin
parents: 28994
diff changeset
   458
(* FIXME only global variant needed *)
28993
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   459
structure RegistrationsData = GenericDataFun
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   460
(
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   461
  type T = ((string * Morphism.morphism) * stamp) list;
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   462
    (* registrations, in reverse order of declaration *)
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   463
  val empty = [];
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   464
  val extend = I;
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   465
  fun merge _ = Library.merge (eq_snd (op =));
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   466
    (* FIXME consolidate with dependencies, consider one data slot only *)
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   467
);
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   468
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   469
val get_global_registrations = map fst o RegistrationsData.get o Context.Theory;
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   470
fun add_global_registration reg =
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   471
  (Context.theory_map o RegistrationsData.map) (cons (reg, stamp ()));
829e684b02ef Interpretation in theories including interaction with subclass relation.
ballarin
parents: 28951
diff changeset
   472
28818
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   473
end;
249e394e5b8e Generic activation of locales.
ballarin
parents: 28795
diff changeset
   474