src/Pure/Isar/local_theory.ML
author wenzelm
Sun Dec 10 15:30:42 2006 +0100 (2006-12-10 ago)
changeset 21743 fe94c6797c09
parent 21700 785c3d0242c5
child 21802 e024aa65f4ce
permissions -rw-r--r--
added notation/abbrev (from term_syntax.ML);
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@21614
    16
  val full_name: local_theory -> bstring -> string
wenzelm@20960
    17
  val theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@18951
    18
  val theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@20960
    19
  val target_result: (Proof.context -> 'a * Proof.context) -> local_theory -> 'a * local_theory
wenzelm@20888
    20
  val target: (Proof.context -> Proof.context) -> local_theory -> local_theory
wenzelm@21034
    21
  val assert: local_theory -> local_theory
wenzelm@20960
    22
  val pretty: local_theory -> Pretty.T list
wenzelm@20888
    23
  val consts: (string * typ -> bool) ->
wenzelm@20888
    24
    ((bstring * typ) * mixfix) list -> local_theory -> (term * thm) list * local_theory
wenzelm@21433
    25
  val axioms: string -> ((bstring * Attrib.src list) * term list) list -> local_theory ->
wenzelm@18951
    26
    (bstring * thm list) list * local_theory
wenzelm@21743
    27
  val abbrev: Syntax.mode -> (bstring * mixfix) * term -> local_theory -> local_theory
wenzelm@21433
    28
  val defs: string -> ((bstring * mixfix) * ((bstring * Attrib.src list) * term)) list ->
wenzelm@20888
    29
    local_theory -> (term * (bstring * thm)) list * local_theory
wenzelm@21433
    30
  val notes: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@18951
    31
    local_theory -> (bstring * thm list) list * local_theory
wenzelm@21498
    32
  val type_syntax: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory
wenzelm@21498
    33
  val term_syntax: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory
wenzelm@21498
    34
  val declaration: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory
wenzelm@21433
    35
  val def: string -> (bstring * mixfix) * ((bstring * Attrib.src list) * term) ->
wenzelm@18951
    36
    local_theory -> (term * (bstring * thm)) * local_theory
wenzelm@21433
    37
  val note: string -> (bstring * Attrib.src list) * thm list ->
wenzelm@20888
    38
    local_theory -> (bstring * thm list) * Proof.context
wenzelm@21743
    39
  val notation: Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@21529
    40
  val target_morphism: local_theory -> morphism
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@20888
    57
  consts: (string * typ -> bool) -> ((bstring * typ) * mixfix) list -> local_theory ->
wenzelm@20888
    58
    (term * thm) list * local_theory,
wenzelm@21433
    59
  axioms: string -> ((bstring * Attrib.src list) * term list) list -> local_theory ->
wenzelm@20888
    60
    (bstring * thm list) list * local_theory,
wenzelm@21743
    61
  abbrev: Syntax.mode -> (bstring * mixfix) * term -> local_theory -> local_theory,
wenzelm@21433
    62
  defs: string -> ((bstring * mixfix) * ((bstring * Attrib.src list) * term)) list ->
wenzelm@21433
    63
    local_theory -> (term * (bstring * thm)) list * 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@21498
    67
  type_syntax: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory,
wenzelm@21498
    68
  term_syntax: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory,
wenzelm@21498
    69
  declaration: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory,
wenzelm@21529
    70
  target_morphism: local_theory -> morphism,
wenzelm@21529
    71
  target_name: local_theory -> bstring -> string,
wenzelm@21292
    72
  reinit: local_theory -> theory -> local_theory,
wenzelm@21292
    73
  exit: local_theory -> Proof.context};
wenzelm@20888
    74
wenzelm@20888
    75
datatype lthy = LThy of
wenzelm@21529
    76
 {theory_prefix: string,
wenzelm@20888
    77
  operations: operations,
wenzelm@20888
    78
  target: Proof.context};
wenzelm@20888
    79
wenzelm@20888
    80
fun make_lthy (theory_prefix, operations, target) =
wenzelm@20888
    81
  LThy {theory_prefix = theory_prefix, operations = operations, target = target};
wenzelm@20888
    82
wenzelm@20888
    83
wenzelm@20888
    84
(* context data *)
wenzelm@18744
    85
wenzelm@18744
    86
structure Data = ProofDataFun
wenzelm@18744
    87
(
wenzelm@18876
    88
  val name = "Pure/local_theory";
wenzelm@20888
    89
  type T = lthy option;
wenzelm@20888
    90
  fun init _ = NONE;
wenzelm@18744
    91
  fun print _ _ = ();
wenzelm@18744
    92
);
wenzelm@18744
    93
val _ = Context.add_setup Data.init;
wenzelm@18744
    94
wenzelm@20888
    95
fun get_lthy lthy =
wenzelm@20888
    96
  (case Data.get lthy of
wenzelm@20888
    97
    NONE => error "No local theory context"
wenzelm@20888
    98
  | SOME (LThy data) => data);
wenzelm@18951
    99
wenzelm@20888
   100
fun map_lthy f lthy =
wenzelm@20888
   101
  let val {theory_prefix, operations, target} = get_lthy lthy
wenzelm@20888
   102
  in Data.put (SOME (make_lthy (f (theory_prefix, operations, target)))) lthy end;
wenzelm@18767
   103
wenzelm@20888
   104
val target_of = #target o get_lthy;
wenzelm@21034
   105
val assert = tap target_of;
wenzelm@18767
   106
wenzelm@20888
   107
fun map_target f = map_lthy (fn (theory_prefix, operations, target) =>
wenzelm@20888
   108
  (theory_prefix, operations, f target));
wenzelm@18767
   109
wenzelm@18767
   110
wenzelm@20888
   111
(* substructure mappings *)
wenzelm@19661
   112
wenzelm@20960
   113
fun raw_theory_result f lthy =
wenzelm@20960
   114
  let
wenzelm@20960
   115
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   116
    val lthy' = lthy
wenzelm@20960
   117
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   118
      |> ProofContext.transfer thy';
wenzelm@20960
   119
  in (res, lthy') end;
wenzelm@20960
   120
wenzelm@20960
   121
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   122
wenzelm@21614
   123
fun full_name lthy =
wenzelm@21614
   124
  let
wenzelm@21614
   125
    val naming = Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@21614
   126
      |> NameSpace.sticky_prefix (#theory_prefix (get_lthy lthy))
wenzelm@21614
   127
      |> NameSpace.qualified_names;
wenzelm@21685
   128
  in NameSpace.full naming end;
wenzelm@21614
   129
wenzelm@21529
   130
fun theory_result f lthy = lthy |> raw_theory_result (fn thy => thy
wenzelm@21529
   131
  |> Sign.sticky_prefix (#theory_prefix (get_lthy lthy))
wenzelm@21529
   132
  |> Sign.qualified_names
wenzelm@21529
   133
  |> f
wenzelm@21529
   134
  ||> Sign.restore_naming thy);
wenzelm@19661
   135
wenzelm@20888
   136
fun theory f = #2 o theory_result (f #> pair ());
wenzelm@19661
   137
wenzelm@20888
   138
fun target_result f lthy =
wenzelm@18744
   139
  let
wenzelm@20888
   140
    val (res, ctxt') = f (#target (get_lthy lthy));
wenzelm@20888
   141
    val thy' = ProofContext.theory_of ctxt';
wenzelm@20888
   142
    val lthy' = lthy
wenzelm@20888
   143
      |> map_target (K ctxt')
wenzelm@20888
   144
      |> ProofContext.transfer thy';
wenzelm@20888
   145
  in (res, lthy') end;
wenzelm@18876
   146
wenzelm@20888
   147
fun target f = #2 o target_result (f #> pair ());
wenzelm@18767
   148
wenzelm@18767
   149
wenzelm@21664
   150
(* basic operations *)
wenzelm@18781
   151
wenzelm@20888
   152
fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
wenzelm@20888
   153
fun operation1 f x = operation (fn ops => f ops x);
wenzelm@20888
   154
fun operation2 f x y = operation (fn ops => f ops x y);
wenzelm@18781
   155
wenzelm@20888
   156
val pretty = operation #pretty;
wenzelm@20888
   157
val consts = operation2 #consts;
wenzelm@21433
   158
val axioms = operation2 #axioms;
wenzelm@21743
   159
val abbrev = operation2 #abbrev;
wenzelm@21433
   160
val defs = operation2 #defs;
wenzelm@21433
   161
val notes = operation2 #notes;
wenzelm@21498
   162
val type_syntax = operation1 #type_syntax;
wenzelm@20888
   163
val term_syntax = operation1 #term_syntax;
wenzelm@20888
   164
val declaration = operation1 #declaration;
wenzelm@21529
   165
val target_morphism = operation #target_morphism;
wenzelm@21529
   166
val target_name = operation #target_name;
wenzelm@18781
   167
wenzelm@21433
   168
fun def kind arg lthy = lthy |> defs kind [arg] |>> hd;
wenzelm@21433
   169
fun note kind (a, ths) lthy = lthy |> notes kind [(a, [(ths, [])])] |>> hd;
wenzelm@18781
   170
wenzelm@21743
   171
fun notation mode raw_args lthy =
wenzelm@21743
   172
  let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
wenzelm@21743
   173
  in term_syntax (ProofContext.target_notation mode args) lthy end;
wenzelm@21743
   174
wenzelm@21664
   175
wenzelm@20910
   176
(* init -- exit *)
wenzelm@20910
   177
wenzelm@20910
   178
fun init theory_prefix operations target = target |> Data.map
wenzelm@20910
   179
  (fn NONE => SOME (make_lthy (theory_prefix, operations, target))
wenzelm@20910
   180
    | SOME _ => error "Local theory already initialized");
wenzelm@20910
   181
wenzelm@21273
   182
fun restore lthy =
wenzelm@20910
   183
  let val {theory_prefix, operations, target} = get_lthy lthy
wenzelm@20910
   184
  in init theory_prefix operations target end;
wenzelm@20910
   185
wenzelm@21292
   186
val reinit = operation #reinit;
wenzelm@21292
   187
val exit = operation #exit;
wenzelm@20910
   188
wenzelm@18781
   189
end;