src/Pure/Isar/local_theory.ML
author wenzelm
Sat Oct 13 17:16:44 2007 +0200 (2007-10-13 ago)
changeset 25021 8f00edb993bd
parent 24984 952045a8dcf2
child 25026 ecdc1733d3cc
permissions -rw-r--r--
renamed def to define;
abbrev: return hypothetical def;
wenzelm@18744
     1
(*  Title:      Pure/Isar/local_theory.ML
wenzelm@18744
     2
    ID:         $Id$
wenzelm@18744
     3
    Author:     Makarius
wenzelm@18744
     4
wenzelm@20888
     5
Local theory operations, with abstract target context.
wenzelm@18744
     6
*)
wenzelm@18744
     7
wenzelm@18951
     8
type local_theory = Proof.context;
wenzelm@18951
     9
wenzelm@18744
    10
signature LOCAL_THEORY =
wenzelm@18744
    11
sig
wenzelm@20888
    12
  type operations
wenzelm@20888
    13
  val target_of: local_theory -> Proof.context
wenzelm@20960
    14
  val raw_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@20960
    15
  val raw_theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@22240
    16
  val full_naming: local_theory -> NameSpace.naming
wenzelm@21614
    17
  val full_name: local_theory -> bstring -> string
wenzelm@20960
    18
  val theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@18951
    19
  val theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@20960
    20
  val target_result: (Proof.context -> 'a * Proof.context) -> local_theory -> 'a * local_theory
wenzelm@20888
    21
  val target: (Proof.context -> Proof.context) -> local_theory -> local_theory
wenzelm@21860
    22
  val affirm: local_theory -> local_theory
wenzelm@20960
    23
  val pretty: local_theory -> Pretty.T list
wenzelm@24984
    24
  val axioms: string ->
wenzelm@24984
    25
    ((bstring * typ) * mixfix) list * ((bstring * Attrib.src list) * term list) list -> local_theory
wenzelm@24984
    26
    -> (term list * (bstring * thm list) list) * local_theory
wenzelm@21802
    27
  val abbrev: Syntax.mode -> (bstring * mixfix) * term -> local_theory ->
wenzelm@25021
    28
    (term * (bstring * thm)) * local_theory
wenzelm@25021
    29
  val define: string -> (bstring * mixfix) * ((bstring * Attrib.src list) * term) -> local_theory ->
wenzelm@25021
    30
    (term * (bstring * thm)) * local_theory
wenzelm@21433
    31
  val notes: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@18951
    32
    local_theory -> (bstring * thm list) list * local_theory
wenzelm@24020
    33
  val type_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    34
  val term_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    35
  val declaration: declaration -> local_theory -> local_theory
wenzelm@21433
    36
  val note: string -> (bstring * Attrib.src list) * thm list ->
wenzelm@24554
    37
    local_theory -> (bstring * thm list) * local_theory
wenzelm@24949
    38
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@21529
    39
  val target_morphism: local_theory -> morphism
wenzelm@22203
    40
  val target_naming: local_theory -> NameSpace.naming
wenzelm@21529
    41
  val target_name: local_theory -> bstring -> string
wenzelm@21529
    42
  val init: string -> operations -> Proof.context -> local_theory
wenzelm@21273
    43
  val restore: local_theory -> local_theory
wenzelm@21292
    44
  val reinit: local_theory -> theory -> local_theory
wenzelm@21292
    45
  val exit: local_theory -> Proof.context
wenzelm@18744
    46
end;
wenzelm@18744
    47
wenzelm@18744
    48
structure LocalTheory: LOCAL_THEORY =
wenzelm@18744
    49
struct
wenzelm@18744
    50
wenzelm@20888
    51
(** local theory data **)
wenzelm@19059
    52
wenzelm@20888
    53
(* type lthy *)
wenzelm@20888
    54
wenzelm@20888
    55
type operations =
wenzelm@20960
    56
 {pretty: local_theory -> Pretty.T list,
wenzelm@24984
    57
  axioms: string ->
wenzelm@24984
    58
    ((bstring * typ) * mixfix) list * ((bstring * Attrib.src list) * term list) list -> local_theory
wenzelm@24984
    59
    -> (term list * (bstring * thm list) list) * local_theory,
wenzelm@25021
    60
  abbrev: Syntax.mode -> (bstring * mixfix) * term -> local_theory ->
wenzelm@25021
    61
    (term * (bstring * thm)) * local_theory,
wenzelm@25021
    62
  define: string -> (bstring * mixfix) * ((bstring * Attrib.src list) * term) -> local_theory ->
wenzelm@25021
    63
    (term * (bstring * thm)) * local_theory,
wenzelm@21433
    64
  notes: string ->
wenzelm@21433
    65
    ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@21433
    66
    local_theory -> (bstring * thm list) list * local_theory,
wenzelm@24020
    67
  type_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    68
  term_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    69
  declaration: declaration -> local_theory -> local_theory,
wenzelm@22203
    70
  target_naming: local_theory -> NameSpace.naming,
wenzelm@21292
    71
  reinit: local_theory -> theory -> local_theory,
wenzelm@21292
    72
  exit: local_theory -> Proof.context};
wenzelm@20888
    73
wenzelm@20888
    74
datatype lthy = LThy of
wenzelm@21529
    75
 {theory_prefix: string,
wenzelm@20888
    76
  operations: operations,
wenzelm@20888
    77
  target: Proof.context};
wenzelm@20888
    78
wenzelm@20888
    79
fun make_lthy (theory_prefix, operations, target) =
wenzelm@20888
    80
  LThy {theory_prefix = theory_prefix, operations = operations, target = target};
wenzelm@20888
    81
wenzelm@20888
    82
wenzelm@20888
    83
(* context data *)
wenzelm@18744
    84
wenzelm@18744
    85
structure Data = ProofDataFun
wenzelm@18744
    86
(
wenzelm@20888
    87
  type T = lthy option;
wenzelm@20888
    88
  fun init _ = NONE;
wenzelm@18744
    89
);
wenzelm@18744
    90
wenzelm@20888
    91
fun get_lthy lthy =
wenzelm@20888
    92
  (case Data.get lthy of
wenzelm@20888
    93
    NONE => error "No local theory context"
wenzelm@20888
    94
  | SOME (LThy data) => data);
wenzelm@18951
    95
wenzelm@20888
    96
fun map_lthy f lthy =
wenzelm@20888
    97
  let val {theory_prefix, operations, target} = get_lthy lthy
wenzelm@20888
    98
  in Data.put (SOME (make_lthy (f (theory_prefix, operations, target)))) lthy end;
wenzelm@18767
    99
wenzelm@20888
   100
val target_of = #target o get_lthy;
wenzelm@21860
   101
val affirm = tap target_of;
wenzelm@18767
   102
wenzelm@20888
   103
fun map_target f = map_lthy (fn (theory_prefix, operations, target) =>
wenzelm@20888
   104
  (theory_prefix, operations, f target));
wenzelm@18767
   105
wenzelm@18767
   106
wenzelm@20888
   107
(* substructure mappings *)
wenzelm@19661
   108
wenzelm@20960
   109
fun raw_theory_result f lthy =
wenzelm@20960
   110
  let
wenzelm@20960
   111
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   112
    val lthy' = lthy
wenzelm@20960
   113
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   114
      |> ProofContext.transfer thy';
wenzelm@20960
   115
  in (res, lthy') end;
wenzelm@20960
   116
wenzelm@20960
   117
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   118
wenzelm@22240
   119
fun full_naming lthy =
wenzelm@22240
   120
  Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@22240
   121
  |> NameSpace.sticky_prefix (#theory_prefix (get_lthy lthy))
wenzelm@22240
   122
  |> NameSpace.qualified_names;
wenzelm@22240
   123
wenzelm@22240
   124
val full_name = NameSpace.full o full_naming;
wenzelm@21614
   125
wenzelm@21529
   126
fun theory_result f lthy = lthy |> raw_theory_result (fn thy => thy
wenzelm@21529
   127
  |> Sign.sticky_prefix (#theory_prefix (get_lthy lthy))
wenzelm@21529
   128
  |> Sign.qualified_names
wenzelm@21529
   129
  |> f
wenzelm@21529
   130
  ||> Sign.restore_naming thy);
wenzelm@19661
   131
wenzelm@20888
   132
fun theory f = #2 o theory_result (f #> pair ());
wenzelm@19661
   133
wenzelm@20888
   134
fun target_result f lthy =
wenzelm@18744
   135
  let
wenzelm@20888
   136
    val (res, ctxt') = f (#target (get_lthy lthy));
wenzelm@20888
   137
    val thy' = ProofContext.theory_of ctxt';
wenzelm@20888
   138
    val lthy' = lthy
wenzelm@20888
   139
      |> map_target (K ctxt')
wenzelm@20888
   140
      |> ProofContext.transfer thy';
wenzelm@20888
   141
  in (res, lthy') end;
wenzelm@18876
   142
wenzelm@20888
   143
fun target f = #2 o target_result (f #> pair ());
wenzelm@18767
   144
wenzelm@18767
   145
wenzelm@21664
   146
(* basic operations *)
wenzelm@18781
   147
wenzelm@20888
   148
fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
wenzelm@20888
   149
fun operation1 f x = operation (fn ops => f ops x);
wenzelm@20888
   150
fun operation2 f x y = operation (fn ops => f ops x y);
wenzelm@18781
   151
wenzelm@20888
   152
val pretty = operation #pretty;
wenzelm@21433
   153
val axioms = operation2 #axioms;
wenzelm@21743
   154
val abbrev = operation2 #abbrev;
wenzelm@25021
   155
val define = operation2 #define;
wenzelm@21433
   156
val notes = operation2 #notes;
wenzelm@21498
   157
val type_syntax = operation1 #type_syntax;
wenzelm@20888
   158
val term_syntax = operation1 #term_syntax;
wenzelm@20888
   159
val declaration = operation1 #declaration;
wenzelm@22203
   160
val target_naming = operation #target_naming;
wenzelm@18781
   161
wenzelm@21433
   162
fun note kind (a, ths) lthy = lthy |> notes kind [(a, [(ths, [])])] |>> hd;
wenzelm@18781
   163
wenzelm@24933
   164
fun target_morphism lthy =
wenzelm@24933
   165
  ProofContext.export_morphism lthy (target_of lthy) $>
wenzelm@24933
   166
  Morphism.thm_morphism Goal.norm_result;
wenzelm@24933
   167
wenzelm@24949
   168
fun notation add mode raw_args lthy =
wenzelm@21743
   169
  let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
wenzelm@24949
   170
  in term_syntax (ProofContext.target_notation add mode args) lthy end;
wenzelm@21743
   171
wenzelm@22203
   172
val target_name = NameSpace.full o target_naming;
wenzelm@22203
   173
wenzelm@21664
   174
wenzelm@20910
   175
(* init -- exit *)
wenzelm@20910
   176
wenzelm@20910
   177
fun init theory_prefix operations target = target |> Data.map
wenzelm@20910
   178
  (fn NONE => SOME (make_lthy (theory_prefix, operations, target))
wenzelm@20910
   179
    | SOME _ => error "Local theory already initialized");
wenzelm@20910
   180
wenzelm@21273
   181
fun restore lthy =
wenzelm@20910
   182
  let val {theory_prefix, operations, target} = get_lthy lthy
wenzelm@20910
   183
  in init theory_prefix operations target end;
wenzelm@20910
   184
wenzelm@21292
   185
val reinit = operation #reinit;
wenzelm@21292
   186
val exit = operation #exit;
wenzelm@20910
   187
wenzelm@18781
   188
end;