src/Pure/Isar/local_theory.ML
author haftmann
Fri Nov 10 07:44:47 2006 +0100 (2006-11-10)
changeset 21286 b5e7b80caa6a
parent 21273 3b01db9504a8
child 21292 11143b6ad87f
permissions -rw-r--r--
introduces canonical AList functions for loop_tacs
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@20960
    16
  val theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@18951
    17
  val theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@20960
    18
  val target_result: (Proof.context -> 'a * Proof.context) -> local_theory -> 'a * local_theory
wenzelm@20888
    19
  val target: (Proof.context -> Proof.context) -> local_theory -> local_theory
wenzelm@21034
    20
  val assert: local_theory -> local_theory
wenzelm@20960
    21
  val pretty: local_theory -> Pretty.T list
wenzelm@20888
    22
  val consts: (string * typ -> bool) ->
wenzelm@20888
    23
    ((bstring * typ) * mixfix) list -> local_theory -> (term * thm) list * local_theory
wenzelm@18951
    24
  val axioms: ((bstring * Attrib.src list) * term list) list -> local_theory ->
wenzelm@18951
    25
    (bstring * thm list) list * local_theory
wenzelm@20888
    26
  val defs: ((bstring * mixfix) * ((bstring * Attrib.src list) * term)) list ->
wenzelm@20888
    27
    local_theory -> (term * (bstring * thm)) list * local_theory
wenzelm@20888
    28
  val notes: ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@18951
    29
    local_theory -> (bstring * thm list) list * local_theory
wenzelm@20888
    30
  val term_syntax: (Context.generic -> Context.generic) -> local_theory -> local_theory
wenzelm@20888
    31
  val declaration: (Context.generic -> Context.generic) -> local_theory -> local_theory
wenzelm@18823
    32
  val def: (bstring * mixfix) * ((bstring * Attrib.src list) * term) ->
wenzelm@18951
    33
    local_theory -> (term * (bstring * thm)) * local_theory
wenzelm@20888
    34
  val note: (bstring * Attrib.src list) * thm list ->
wenzelm@20888
    35
    local_theory -> (bstring * thm list) * Proof.context
wenzelm@21206
    36
  val notation: Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@21273
    37
  val abbrevs: Syntax.mode -> ((bstring * mixfix) * term) list -> local_theory ->
wenzelm@21273
    38
    (term * term) list * local_theory
wenzelm@20910
    39
  val init: string option -> operations -> Proof.context -> local_theory
wenzelm@21273
    40
  val restore: local_theory -> local_theory
wenzelm@20981
    41
  val exit: bool -> local_theory -> Proof.context * theory
wenzelm@18744
    42
end;
wenzelm@18744
    43
wenzelm@18744
    44
structure LocalTheory: LOCAL_THEORY =
wenzelm@18744
    45
struct
wenzelm@18744
    46
wenzelm@20888
    47
(** local theory data **)
wenzelm@19059
    48
wenzelm@20888
    49
(* type lthy *)
wenzelm@20888
    50
wenzelm@20888
    51
type operations =
wenzelm@20960
    52
 {pretty: local_theory -> Pretty.T list,
wenzelm@20888
    53
  consts: (string * typ -> bool) -> ((bstring * typ) * mixfix) list -> local_theory ->
wenzelm@20888
    54
    (term * thm) list * local_theory,
wenzelm@20888
    55
  axioms: ((bstring * Attrib.src list) * term list) list -> local_theory ->
wenzelm@20888
    56
    (bstring * thm list) list * local_theory,
wenzelm@20888
    57
  defs: ((bstring * mixfix) * ((bstring * Attrib.src list) * term)) list -> local_theory ->
wenzelm@20888
    58
    (term * (bstring * thm)) list * local_theory,
wenzelm@20888
    59
  notes: ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list -> local_theory ->
wenzelm@20888
    60
    (bstring * thm list) list * local_theory,
wenzelm@20888
    61
  term_syntax: (Context.generic -> Context.generic) -> local_theory -> local_theory,
wenzelm@20910
    62
  declaration: (Context.generic -> Context.generic) -> local_theory -> local_theory,
wenzelm@21273
    63
  reinit: Proof.context -> local_theory,
wenzelm@20981
    64
  exit: bool -> local_theory -> local_theory};
wenzelm@20888
    65
wenzelm@20888
    66
datatype lthy = LThy of
wenzelm@20888
    67
 {theory_prefix: string option,
wenzelm@20888
    68
  operations: operations,
wenzelm@20888
    69
  target: Proof.context};
wenzelm@20888
    70
wenzelm@20888
    71
fun make_lthy (theory_prefix, operations, target) =
wenzelm@20888
    72
  LThy {theory_prefix = theory_prefix, operations = operations, target = target};
wenzelm@20888
    73
wenzelm@20888
    74
wenzelm@20888
    75
(* context data *)
wenzelm@18744
    76
wenzelm@18744
    77
structure Data = ProofDataFun
wenzelm@18744
    78
(
wenzelm@18876
    79
  val name = "Pure/local_theory";
wenzelm@20888
    80
  type T = lthy option;
wenzelm@20888
    81
  fun init _ = NONE;
wenzelm@18744
    82
  fun print _ _ = ();
wenzelm@18744
    83
);
wenzelm@18744
    84
val _ = Context.add_setup Data.init;
wenzelm@18744
    85
wenzelm@20888
    86
fun get_lthy lthy =
wenzelm@20888
    87
  (case Data.get lthy of
wenzelm@20888
    88
    NONE => error "No local theory context"
wenzelm@20888
    89
  | SOME (LThy data) => data);
wenzelm@18951
    90
wenzelm@20888
    91
fun map_lthy f lthy =
wenzelm@20888
    92
  let val {theory_prefix, operations, target} = get_lthy lthy
wenzelm@20888
    93
  in Data.put (SOME (make_lthy (f (theory_prefix, operations, target)))) lthy end;
wenzelm@18767
    94
wenzelm@20888
    95
val target_of = #target o get_lthy;
wenzelm@21034
    96
val assert = tap target_of;
wenzelm@18767
    97
wenzelm@20888
    98
fun map_target f = map_lthy (fn (theory_prefix, operations, target) =>
wenzelm@20888
    99
  (theory_prefix, operations, f target));
wenzelm@18767
   100
wenzelm@18767
   101
wenzelm@20888
   102
(* substructure mappings *)
wenzelm@19661
   103
wenzelm@20960
   104
fun raw_theory_result f lthy =
wenzelm@20960
   105
  let
wenzelm@20960
   106
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   107
    val lthy' = lthy
wenzelm@20960
   108
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   109
      |> ProofContext.transfer thy';
wenzelm@20960
   110
  in (res, lthy') end;
wenzelm@20960
   111
wenzelm@20960
   112
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   113
wenzelm@20888
   114
fun theory_result f lthy =
wenzelm@20888
   115
  (case #theory_prefix (get_lthy lthy) of
wenzelm@20888
   116
    NONE => error "Cannot change background theory"
wenzelm@20960
   117
  | SOME prefix => lthy |> raw_theory_result (fn thy => thy
wenzelm@20960
   118
      |> Sign.sticky_prefix prefix
wenzelm@20960
   119
      |> Sign.qualified_names
wenzelm@20960
   120
      |> f
wenzelm@20960
   121
      ||> Sign.restore_naming thy));
wenzelm@19661
   122
wenzelm@20888
   123
fun theory f = #2 o theory_result (f #> pair ());
wenzelm@19661
   124
wenzelm@20888
   125
fun target_result f lthy =
wenzelm@18744
   126
  let
wenzelm@20888
   127
    val (res, ctxt') = f (#target (get_lthy lthy));
wenzelm@20888
   128
    val thy' = ProofContext.theory_of ctxt';
wenzelm@20888
   129
    val lthy' = lthy
wenzelm@20888
   130
      |> map_target (K ctxt')
wenzelm@20888
   131
      |> ProofContext.transfer thy';
wenzelm@20888
   132
  in (res, lthy') end;
wenzelm@18876
   133
wenzelm@20888
   134
fun target f = #2 o target_result (f #> pair ());
wenzelm@18767
   135
wenzelm@18767
   136
wenzelm@20888
   137
(* primitive operations *)
wenzelm@18781
   138
wenzelm@20888
   139
fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
wenzelm@20888
   140
fun operation1 f x = operation (fn ops => f ops x);
wenzelm@20888
   141
fun operation2 f x y = operation (fn ops => f ops x y);
wenzelm@18781
   142
wenzelm@20888
   143
val pretty = operation #pretty;
wenzelm@20888
   144
val consts = operation2 #consts;
wenzelm@20888
   145
val axioms = operation1 #axioms;
wenzelm@20888
   146
val defs = operation1 #defs;
wenzelm@20888
   147
val notes = operation1 #notes;
wenzelm@20888
   148
val term_syntax = operation1 #term_syntax;
wenzelm@20888
   149
val declaration = operation1 #declaration;
wenzelm@21273
   150
val reinit = operation #reinit;
wenzelm@18781
   151
wenzelm@18781
   152
wenzelm@20888
   153
(* derived operations *)
wenzelm@18781
   154
wenzelm@20888
   155
fun def arg lthy = lthy |> defs [arg] |>> hd;
wenzelm@18781
   156
wenzelm@20888
   157
fun note (a, ths) lthy = lthy |> notes [(a, [(ths, [])])] |>> hd;
wenzelm@18781
   158
wenzelm@21206
   159
fun notation mode args = term_syntax
wenzelm@21206
   160
  (Context.mapping (Sign.add_notation mode args) (ProofContext.add_notation mode args));
wenzelm@18781
   161
wenzelm@21206
   162
fun abbrevs mode args = term_syntax
wenzelm@21273
   163
  (Context.mapping (snd o Sign.add_abbrevs mode args) (snd o ProofContext.add_abbrevs mode args))
wenzelm@21273
   164
  #> ProofContext.add_abbrevs mode args;  (* FIXME *)
wenzelm@18805
   165
wenzelm@20910
   166
wenzelm@20910
   167
(* init -- exit *)
wenzelm@20910
   168
wenzelm@20910
   169
fun init theory_prefix operations target = target |> Data.map
wenzelm@20910
   170
  (fn NONE => SOME (make_lthy (theory_prefix, operations, target))
wenzelm@20910
   171
    | SOME _ => error "Local theory already initialized");
wenzelm@20910
   172
wenzelm@21273
   173
fun restore lthy =
wenzelm@20910
   174
  let val {theory_prefix, operations, target} = get_lthy lthy
wenzelm@20910
   175
  in init theory_prefix operations target end;
wenzelm@20910
   176
wenzelm@20981
   177
fun exit int lthy = lthy
wenzelm@20981
   178
  |> operation1 #exit int |> target_of
wenzelm@20981
   179
  |> (fn ctxt => (ctxt, ProofContext.theory_of ctxt));
wenzelm@20910
   180
wenzelm@18781
   181
end;