src/Pure/Isar/local_theory.ML
author wenzelm
Sat Jul 24 12:14:53 2010 +0200 (2010-07-24 ago)
changeset 37949 48a874444164
parent 36610 bafd82950e24
child 38308 0e4649095739
permissions -rw-r--r--
moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
theory loader: reduced warnings -- thy database can be bypassed in certain situations;
Proof/Local_Theory.propagate_ml_env;
ML use function: propagate ML environment as well;
pervasive type generic_theory;
wenzelm@18744
     1
(*  Title:      Pure/Isar/local_theory.ML
wenzelm@18744
     2
    Author:     Makarius
wenzelm@18744
     3
wenzelm@20888
     4
Local theory operations, with abstract target context.
wenzelm@18744
     5
*)
wenzelm@18744
     6
wenzelm@18951
     7
type local_theory = Proof.context;
wenzelm@37949
     8
type generic_theory = Context.generic;
wenzelm@18951
     9
wenzelm@18744
    10
signature LOCAL_THEORY =
wenzelm@18744
    11
sig
wenzelm@20888
    12
  type operations
wenzelm@33166
    13
  val affirm: local_theory -> local_theory
wenzelm@33166
    14
  val naming_of: local_theory -> Name_Space.naming
wenzelm@33166
    15
  val full_name: local_theory -> binding -> string
wenzelm@33166
    16
  val map_naming: (Name_Space.naming -> Name_Space.naming) -> local_theory -> local_theory
wenzelm@33166
    17
  val conceal: local_theory -> local_theory
wenzelm@33724
    18
  val new_group: local_theory -> local_theory
wenzelm@33724
    19
  val reset_group: local_theory -> local_theory
wenzelm@33276
    20
  val restore_naming: local_theory -> local_theory -> local_theory
wenzelm@20888
    21
  val target_of: local_theory -> Proof.context
wenzelm@20960
    22
  val raw_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@20960
    23
  val raw_theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@20960
    24
  val theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@18951
    25
  val theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@20960
    26
  val target_result: (Proof.context -> 'a * Proof.context) -> local_theory -> 'a * local_theory
wenzelm@20888
    27
  val target: (Proof.context -> Proof.context) -> local_theory -> local_theory
wenzelm@30578
    28
  val map_contexts: (Context.generic -> Context.generic) -> local_theory -> local_theory
wenzelm@37949
    29
  val propagate_ml_env: generic_theory -> generic_theory
wenzelm@33281
    30
  val standard_morphism: local_theory -> Proof.context -> morphism
wenzelm@33281
    31
  val target_morphism: local_theory -> morphism
wenzelm@33456
    32
  val global_morphism: local_theory -> morphism
wenzelm@20960
    33
  val pretty: local_theory -> Pretty.T list
haftmann@29581
    34
  val abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
wenzelm@25120
    35
    (term * term) * local_theory
wenzelm@33764
    36
  val define: (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@28083
    37
    (term * (string * thm)) * local_theory
wenzelm@33670
    38
  val note: Attrib.binding * thm list -> local_theory -> (string * thm list) * local_theory
wenzelm@33670
    39
  val notes: (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@33670
    40
    local_theory -> (string * thm list) list * local_theory
wenzelm@33670
    41
  val notes_kind: string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28083
    42
    local_theory -> (string * thm list) list * local_theory
wenzelm@33456
    43
  val declaration: bool -> declaration -> local_theory -> local_theory
wenzelm@35798
    44
  val syntax_declaration: bool -> declaration -> local_theory -> local_theory
wenzelm@36451
    45
  val set_defsort: sort -> local_theory -> local_theory
wenzelm@35412
    46
  val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> local_theory -> local_theory
wenzelm@24949
    47
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@35738
    48
  val class_alias: binding -> class -> local_theory -> local_theory
wenzelm@35738
    49
  val type_alias: binding -> string -> local_theory -> local_theory
wenzelm@35738
    50
  val const_alias: binding -> string -> local_theory -> local_theory
wenzelm@33724
    51
  val init: serial option -> string -> operations -> Proof.context -> local_theory
wenzelm@21273
    52
  val restore: local_theory -> local_theory
wenzelm@25289
    53
  val reinit: local_theory -> local_theory
wenzelm@21292
    54
  val exit: local_theory -> Proof.context
wenzelm@28395
    55
  val exit_global: local_theory -> theory
wenzelm@28395
    56
  val exit_result: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * Proof.context
wenzelm@28395
    57
  val exit_result_global: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * theory
wenzelm@18744
    58
end;
wenzelm@18744
    59
wenzelm@33671
    60
structure Local_Theory: LOCAL_THEORY =
wenzelm@18744
    61
struct
wenzelm@18744
    62
wenzelm@20888
    63
(** local theory data **)
wenzelm@19059
    64
wenzelm@20888
    65
(* type lthy *)
wenzelm@20888
    66
wenzelm@20888
    67
type operations =
wenzelm@20960
    68
 {pretty: local_theory -> Pretty.T list,
haftmann@29581
    69
  abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
wenzelm@28084
    70
    (term * term) * local_theory,
wenzelm@33764
    71
  define: (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@28083
    72
    (term * (string * thm)) * local_theory,
wenzelm@26131
    73
  notes: string ->
wenzelm@28084
    74
    (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28083
    75
    local_theory -> (string * thm list) list * local_theory,
wenzelm@33456
    76
  declaration: bool -> declaration -> local_theory -> local_theory,
wenzelm@35798
    77
  syntax_declaration: bool -> declaration -> local_theory -> local_theory,
wenzelm@25289
    78
  reinit: local_theory -> local_theory,
wenzelm@21292
    79
  exit: local_theory -> Proof.context};
wenzelm@20888
    80
wenzelm@20888
    81
datatype lthy = LThy of
wenzelm@33166
    82
 {naming: Name_Space.naming,
wenzelm@26131
    83
  theory_prefix: string,
wenzelm@20888
    84
  operations: operations,
wenzelm@20888
    85
  target: Proof.context};
wenzelm@20888
    86
wenzelm@33166
    87
fun make_lthy (naming, theory_prefix, operations, target) =
wenzelm@33166
    88
  LThy {naming = naming, theory_prefix = theory_prefix,
wenzelm@33157
    89
    operations = operations, target = target};
wenzelm@20888
    90
wenzelm@20888
    91
wenzelm@20888
    92
(* context data *)
wenzelm@18744
    93
wenzelm@33519
    94
structure Data = Proof_Data
wenzelm@18744
    95
(
wenzelm@20888
    96
  type T = lthy option;
wenzelm@20888
    97
  fun init _ = NONE;
wenzelm@18744
    98
);
wenzelm@18744
    99
wenzelm@20888
   100
fun get_lthy lthy =
wenzelm@20888
   101
  (case Data.get lthy of
wenzelm@20888
   102
    NONE => error "No local theory context"
wenzelm@20888
   103
  | SOME (LThy data) => data);
wenzelm@18951
   104
wenzelm@20888
   105
fun map_lthy f lthy =
wenzelm@33166
   106
  let val {naming, theory_prefix, operations, target} = get_lthy lthy
wenzelm@33166
   107
  in Data.put (SOME (make_lthy (f (naming, theory_prefix, operations, target)))) lthy end;
wenzelm@33166
   108
wenzelm@33166
   109
val affirm = tap get_lthy;
wenzelm@26131
   110
wenzelm@26131
   111
wenzelm@33166
   112
(* naming *)
wenzelm@26131
   113
wenzelm@33166
   114
val naming_of = #naming o get_lthy;
wenzelm@33166
   115
val full_name = Name_Space.full_name o naming_of;
wenzelm@26131
   116
wenzelm@33166
   117
fun map_naming f = map_lthy (fn (naming, theory_prefix, operations, target) =>
wenzelm@33166
   118
  (f naming, theory_prefix, operations, target));
wenzelm@26131
   119
wenzelm@33166
   120
val conceal = map_naming Name_Space.conceal;
wenzelm@33724
   121
val new_group = map_naming Name_Space.new_group;
wenzelm@33724
   122
val reset_group = map_naming Name_Space.reset_group;
wenzelm@26131
   123
wenzelm@33276
   124
val restore_naming = map_naming o K o naming_of;
wenzelm@33276
   125
wenzelm@26131
   126
wenzelm@26131
   127
(* target *)
wenzelm@18767
   128
wenzelm@20888
   129
val target_of = #target o get_lthy;
wenzelm@18767
   130
wenzelm@33166
   131
fun map_target f = map_lthy (fn (naming, theory_prefix, operations, target) =>
wenzelm@33166
   132
  (naming, theory_prefix, operations, f target));
wenzelm@18767
   133
wenzelm@18767
   134
wenzelm@20888
   135
(* substructure mappings *)
wenzelm@19661
   136
wenzelm@20960
   137
fun raw_theory_result f lthy =
wenzelm@20960
   138
  let
wenzelm@20960
   139
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   140
    val lthy' = lthy
wenzelm@20960
   141
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   142
      |> ProofContext.transfer thy';
wenzelm@20960
   143
  in (res, lthy') end;
wenzelm@20960
   144
wenzelm@20960
   145
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   146
wenzelm@28379
   147
val checkpoint = raw_theory Theory.checkpoint;
wenzelm@28379
   148
wenzelm@33166
   149
fun theory_result f lthy =
wenzelm@33166
   150
  lthy |> raw_theory_result (fn thy =>
wenzelm@33157
   151
    thy
wenzelm@33166
   152
    |> Sign.map_naming (K (naming_of lthy))
wenzelm@33157
   153
    |> f
wenzelm@36004
   154
    ||> Sign.restore_naming thy
wenzelm@36004
   155
    ||> Theory.checkpoint);
wenzelm@19661
   156
wenzelm@20888
   157
fun theory f = #2 o theory_result (f #> pair ());
wenzelm@19661
   158
wenzelm@20888
   159
fun target_result f lthy =
wenzelm@18744
   160
  let
wenzelm@28406
   161
    val (res, ctxt') = target_of lthy
wenzelm@33383
   162
      |> Context_Position.set_visible false
wenzelm@28406
   163
      |> f
wenzelm@33383
   164
      ||> Context_Position.restore_visible lthy;
wenzelm@20888
   165
    val thy' = ProofContext.theory_of ctxt';
wenzelm@20888
   166
    val lthy' = lthy
wenzelm@20888
   167
      |> map_target (K ctxt')
wenzelm@20888
   168
      |> ProofContext.transfer thy';
wenzelm@20888
   169
  in (res, lthy') end;
wenzelm@18876
   170
wenzelm@20888
   171
fun target f = #2 o target_result (f #> pair ());
wenzelm@18767
   172
wenzelm@30578
   173
fun map_contexts f =
wenzelm@30578
   174
  theory (Context.theory_map f) #>
wenzelm@30578
   175
  target (Context.proof_map f) #>
wenzelm@30578
   176
  Context.proof_map f;
wenzelm@30578
   177
wenzelm@37949
   178
fun propagate_ml_env (context as Context.Proof lthy) =
wenzelm@37949
   179
      Context.Proof (map_contexts (ML_Env.inherit context) lthy)
wenzelm@37949
   180
  | propagate_ml_env context = context;
wenzelm@37949
   181
wenzelm@18767
   182
wenzelm@33281
   183
(* morphisms *)
wenzelm@33281
   184
wenzelm@33281
   185
fun standard_morphism lthy ctxt =
wenzelm@33281
   186
  ProofContext.norm_export_morphism lthy ctxt $>
wenzelm@33281
   187
  Morphism.binding_morphism (Name_Space.transform_binding (naming_of lthy));
wenzelm@33281
   188
wenzelm@33281
   189
fun target_morphism lthy = standard_morphism lthy (target_of lthy);
wenzelm@36610
   190
fun global_morphism lthy =
wenzelm@36610
   191
  standard_morphism lthy (ProofContext.init_global (ProofContext.theory_of lthy));
wenzelm@33281
   192
wenzelm@33281
   193
wenzelm@36451
   194
(* primitive operations *)
wenzelm@18781
   195
wenzelm@20888
   196
fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
wenzelm@33764
   197
fun operation1 f x = operation (fn ops => f ops x);
wenzelm@26131
   198
fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
wenzelm@18781
   199
wenzelm@20888
   200
val pretty = operation #pretty;
wenzelm@28379
   201
val abbrev = apsnd checkpoint ooo operation2 #abbrev;
wenzelm@33764
   202
val define = apsnd checkpoint oo operation1 #define;
wenzelm@33670
   203
val notes_kind = apsnd checkpoint ooo operation2 #notes;
wenzelm@33456
   204
val declaration = checkpoint ooo operation2 #declaration;
wenzelm@35798
   205
val syntax_declaration = checkpoint ooo operation2 #syntax_declaration;
wenzelm@18781
   206
wenzelm@36451
   207
wenzelm@36451
   208
wenzelm@36451
   209
(** basic derived operations **)
wenzelm@36451
   210
wenzelm@33670
   211
val notes = notes_kind "";
wenzelm@33670
   212
fun note (a, ths) = notes [(a, [(ths, [])])] #>> the_single;
wenzelm@18781
   213
wenzelm@36451
   214
fun set_defsort S =
wenzelm@36451
   215
  syntax_declaration false (K (Context.mapping (Sign.set_defsort S) (ProofContext.set_defsort S)));
wenzelm@36451
   216
wenzelm@35738
   217
wenzelm@35738
   218
(* notation *)
wenzelm@35738
   219
wenzelm@35412
   220
fun type_notation add mode raw_args lthy =
wenzelm@35412
   221
  let val args = map (apfst (Morphism.typ (target_morphism lthy))) raw_args
wenzelm@35798
   222
  in syntax_declaration false (ProofContext.target_type_notation add mode args) lthy end;
wenzelm@35412
   223
wenzelm@24949
   224
fun notation add mode raw_args lthy =
wenzelm@21743
   225
  let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
wenzelm@35798
   226
  in syntax_declaration false (ProofContext.target_notation add mode args) lthy end;
wenzelm@21743
   227
wenzelm@21664
   228
wenzelm@35738
   229
(* name space aliases *)
wenzelm@35738
   230
wenzelm@35798
   231
fun alias global_alias local_alias b name =
wenzelm@35738
   232
  syntax_declaration false (fn phi =>
wenzelm@35738
   233
    let val b' = Morphism.binding phi b
wenzelm@35738
   234
    in Context.mapping (global_alias b' name) (local_alias b' name) end)
wenzelm@35738
   235
  #> local_alias b name;
wenzelm@35738
   236
wenzelm@35798
   237
val class_alias = alias Sign.class_alias ProofContext.class_alias;
wenzelm@35798
   238
val type_alias = alias Sign.type_alias ProofContext.type_alias;
wenzelm@35798
   239
val const_alias = alias Sign.const_alias ProofContext.const_alias;
wenzelm@35738
   240
wenzelm@35738
   241
wenzelm@36451
   242
wenzelm@36451
   243
(** init and exit **)
wenzelm@36451
   244
wenzelm@28395
   245
(* init *)
wenzelm@20910
   246
wenzelm@33724
   247
fun init group theory_prefix operations target =
wenzelm@33166
   248
  let val naming =
wenzelm@33166
   249
    Sign.naming_of (ProofContext.theory_of target)
wenzelm@33724
   250
    |> Name_Space.set_group group
wenzelm@33166
   251
    |> Name_Space.mandatory_path theory_prefix;
wenzelm@33166
   252
  in
wenzelm@33166
   253
    target |> Data.map
wenzelm@33166
   254
      (fn NONE => SOME (make_lthy (naming, theory_prefix, operations, target))
wenzelm@33166
   255
        | SOME _ => error "Local theory already initialized")
wenzelm@33166
   256
    |> checkpoint
wenzelm@33166
   257
  end;
wenzelm@20910
   258
wenzelm@21273
   259
fun restore lthy =
wenzelm@33724
   260
  let val {naming, theory_prefix, operations, target} = get_lthy lthy
wenzelm@33724
   261
  in init (Name_Space.get_group naming) theory_prefix operations target end;
wenzelm@20910
   262
wenzelm@28379
   263
val reinit = checkpoint o operation #reinit;
wenzelm@28395
   264
wenzelm@28395
   265
wenzelm@28395
   266
(* exit *)
wenzelm@28395
   267
wenzelm@28379
   268
val exit = ProofContext.theory Theory.checkpoint o operation #exit;
wenzelm@28395
   269
val exit_global = ProofContext.theory_of o exit;
wenzelm@28395
   270
wenzelm@28395
   271
fun exit_result f (x, lthy) =
wenzelm@28395
   272
  let
wenzelm@28395
   273
    val ctxt = exit lthy;
wenzelm@33281
   274
    val phi = standard_morphism lthy ctxt;
wenzelm@28395
   275
  in (f phi x, ctxt) end;
wenzelm@28395
   276
wenzelm@28395
   277
fun exit_result_global f (x, lthy) =
wenzelm@28395
   278
  let
wenzelm@28395
   279
    val thy = exit_global lthy;
wenzelm@36610
   280
    val thy_ctxt = ProofContext.init_global thy;
wenzelm@33281
   281
    val phi = standard_morphism lthy thy_ctxt;
wenzelm@28395
   282
  in (f phi x, thy) end;
wenzelm@20910
   283
wenzelm@18781
   284
end;