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