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