src/Pure/Isar/local_theory.ML
author wenzelm
Tue Dec 05 22:14:42 2006 +0100 (2006-12-05)
changeset 21658 5e31241e1e3c
parent 21614 89105c15b436
child 21664 dd4e89123359
permissions -rw-r--r--
Attrib.internal: morphism;
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@21433
    27
  val defs: string -> ((bstring * mixfix) * ((bstring * Attrib.src list) * term)) list ->
wenzelm@20888
    28
    local_theory -> (term * (bstring * thm)) list * local_theory
wenzelm@21433
    29
  val notes: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@18951
    30
    local_theory -> (bstring * thm list) list * local_theory
wenzelm@21498
    31
  val type_syntax: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory
wenzelm@21498
    32
  val term_syntax: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory
wenzelm@21498
    33
  val declaration: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory
wenzelm@21433
    34
  val def: string -> (bstring * mixfix) * ((bstring * Attrib.src list) * term) ->
wenzelm@18951
    35
    local_theory -> (term * (bstring * thm)) * local_theory
wenzelm@21433
    36
  val note: string -> (bstring * Attrib.src list) * thm list ->
wenzelm@20888
    37
    local_theory -> (bstring * thm list) * Proof.context
wenzelm@21206
    38
  val notation: Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@21529
    39
  val abbrevs: Syntax.mode -> ((bstring * mixfix) * term) 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@21433
    61
  defs: string -> ((bstring * mixfix) * ((bstring * Attrib.src list) * term)) list ->
wenzelm@21433
    62
    local_theory -> (term * (bstring * thm)) list * local_theory,
wenzelm@21433
    63
  notes: string ->
wenzelm@21433
    64
    ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@21433
    65
    local_theory -> (bstring * thm list) list * local_theory,
wenzelm@21498
    66
  type_syntax: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory,
wenzelm@21498
    67
  term_syntax: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory,
wenzelm@21498
    68
  declaration: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory,
wenzelm@21529
    69
  target_morphism: local_theory -> morphism,
wenzelm@21529
    70
  target_name: local_theory -> bstring -> string,
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@18876
    87
  val name = "Pure/local_theory";
wenzelm@20888
    88
  type T = lthy option;
wenzelm@20888
    89
  fun init _ = NONE;
wenzelm@18744
    90
  fun print _ _ = ();
wenzelm@18744
    91
);
wenzelm@18744
    92
val _ = Context.add_setup Data.init;
wenzelm@18744
    93
wenzelm@20888
    94
fun get_lthy lthy =
wenzelm@20888
    95
  (case Data.get lthy of
wenzelm@20888
    96
    NONE => error "No local theory context"
wenzelm@20888
    97
  | SOME (LThy data) => data);
wenzelm@18951
    98
wenzelm@20888
    99
fun map_lthy f lthy =
wenzelm@20888
   100
  let val {theory_prefix, operations, target} = get_lthy lthy
wenzelm@20888
   101
  in Data.put (SOME (make_lthy (f (theory_prefix, operations, target)))) lthy end;
wenzelm@18767
   102
wenzelm@20888
   103
val target_of = #target o get_lthy;
wenzelm@21034
   104
val assert = tap target_of;
wenzelm@18767
   105
wenzelm@20888
   106
fun map_target f = map_lthy (fn (theory_prefix, operations, target) =>
wenzelm@20888
   107
  (theory_prefix, operations, f target));
wenzelm@18767
   108
wenzelm@18767
   109
wenzelm@20888
   110
(* substructure mappings *)
wenzelm@19661
   111
wenzelm@20960
   112
fun raw_theory_result f lthy =
wenzelm@20960
   113
  let
wenzelm@20960
   114
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   115
    val lthy' = lthy
wenzelm@20960
   116
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   117
      |> ProofContext.transfer thy';
wenzelm@20960
   118
  in (res, lthy') end;
wenzelm@20960
   119
wenzelm@20960
   120
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   121
wenzelm@21614
   122
fun full_name lthy =
wenzelm@21614
   123
  let
wenzelm@21614
   124
    val naming = Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@21614
   125
      |> NameSpace.sticky_prefix (#theory_prefix (get_lthy lthy))
wenzelm@21614
   126
      |> NameSpace.qualified_names;
wenzelm@21614
   127
  in NameSpace.full naming end;  
wenzelm@21614
   128
wenzelm@21529
   129
fun theory_result f lthy = lthy |> raw_theory_result (fn thy => thy
wenzelm@21529
   130
  |> Sign.sticky_prefix (#theory_prefix (get_lthy lthy))
wenzelm@21529
   131
  |> Sign.qualified_names
wenzelm@21529
   132
  |> f
wenzelm@21529
   133
  ||> Sign.restore_naming thy);
wenzelm@19661
   134
wenzelm@20888
   135
fun theory f = #2 o theory_result (f #> pair ());
wenzelm@19661
   136
wenzelm@20888
   137
fun target_result f lthy =
wenzelm@18744
   138
  let
wenzelm@20888
   139
    val (res, ctxt') = f (#target (get_lthy lthy));
wenzelm@20888
   140
    val thy' = ProofContext.theory_of ctxt';
wenzelm@20888
   141
    val lthy' = lthy
wenzelm@20888
   142
      |> map_target (K ctxt')
wenzelm@20888
   143
      |> ProofContext.transfer thy';
wenzelm@20888
   144
  in (res, lthy') end;
wenzelm@18876
   145
wenzelm@20888
   146
fun target f = #2 o target_result (f #> pair ());
wenzelm@18767
   147
wenzelm@18767
   148
wenzelm@20888
   149
(* primitive operations *)
wenzelm@18781
   150
wenzelm@20888
   151
fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
wenzelm@20888
   152
fun operation1 f x = operation (fn ops => f ops x);
wenzelm@20888
   153
fun operation2 f x y = operation (fn ops => f ops x y);
wenzelm@18781
   154
wenzelm@20888
   155
val pretty = operation #pretty;
wenzelm@20888
   156
val consts = operation2 #consts;
wenzelm@21433
   157
val axioms = operation2 #axioms;
wenzelm@21433
   158
val defs = operation2 #defs;
wenzelm@21433
   159
val notes = operation2 #notes;
wenzelm@21498
   160
val type_syntax = operation1 #type_syntax;
wenzelm@20888
   161
val term_syntax = operation1 #term_syntax;
wenzelm@20888
   162
val declaration = operation1 #declaration;
wenzelm@21529
   163
val target_morphism = operation #target_morphism;
wenzelm@21529
   164
val target_name = operation #target_name;
wenzelm@18781
   165
wenzelm@18781
   166
wenzelm@20888
   167
(* derived operations *)
wenzelm@18781
   168
wenzelm@21433
   169
fun def kind arg lthy = lthy |> defs kind [arg] |>> hd;
wenzelm@18781
   170
wenzelm@21433
   171
fun note kind (a, ths) lthy = lthy |> notes kind [(a, [(ths, [])])] |>> hd;
wenzelm@18781
   172
wenzelm@21529
   173
fun notation mode args = term_syntax (fn phi =>
wenzelm@21529
   174
  let val args' = args |> map (fn (t, mx) => (Morphism.term phi t, mx))
wenzelm@21529
   175
  in Context.mapping (Sign.add_notation mode args') (ProofContext.add_notation mode args') end);
wenzelm@18781
   176
wenzelm@21529
   177
fun abbrevs mode args = term_syntax (fn phi =>
wenzelm@21529
   178
  let val args' = args |> map (fn ((c, mx), t) => ((Morphism.name phi c, mx), Morphism.term phi t))
wenzelm@21529
   179
  in
wenzelm@21529
   180
    Context.mapping
wenzelm@21529
   181
      (snd o Sign.add_abbrevs mode args') (snd o ProofContext.add_abbrevs mode args')
wenzelm@21529
   182
  end);
wenzelm@18805
   183
wenzelm@20910
   184
wenzelm@20910
   185
(* init -- exit *)
wenzelm@20910
   186
wenzelm@20910
   187
fun init theory_prefix operations target = target |> Data.map
wenzelm@20910
   188
  (fn NONE => SOME (make_lthy (theory_prefix, operations, target))
wenzelm@20910
   189
    | SOME _ => error "Local theory already initialized");
wenzelm@20910
   190
wenzelm@21273
   191
fun restore lthy =
wenzelm@20910
   192
  let val {theory_prefix, operations, target} = get_lthy lthy
wenzelm@20910
   193
  in init theory_prefix operations target end;
wenzelm@20910
   194
wenzelm@21292
   195
val reinit = operation #reinit;
wenzelm@21292
   196
val exit = operation #exit;
wenzelm@20910
   197
wenzelm@18781
   198
end;