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