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