src/Pure/Isar/local_theory.ML
author wenzelm
Fri Nov 13 21:11:15 2009 +0100 (2009-11-13 ago)
changeset 33671 4b0f2599ed48
parent 33670 02b7738aef6a
child 33724 5ee13e0428d2
permissions -rw-r--r--
modernized structure Local_Theory;
     1 (*  Title:      Pure/Isar/local_theory.ML
     2     Author:     Makarius
     3 
     4 Local theory operations, with abstract target context.
     5 *)
     6 
     7 type local_theory = Proof.context;
     8 
     9 signature LOCAL_THEORY =
    10 sig
    11   type operations
    12   val affirm: local_theory -> local_theory
    13   val naming_of: local_theory -> Name_Space.naming
    14   val full_name: local_theory -> binding -> string
    15   val map_naming: (Name_Space.naming -> Name_Space.naming) -> local_theory -> local_theory
    16   val conceal: local_theory -> local_theory
    17   val set_group: serial -> local_theory -> local_theory
    18   val restore_naming: local_theory -> local_theory -> local_theory
    19   val target_of: local_theory -> Proof.context
    20   val raw_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
    21   val raw_theory: (theory -> theory) -> local_theory -> local_theory
    22   val checkpoint: local_theory -> local_theory
    23   val theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
    24   val theory: (theory -> theory) -> local_theory -> local_theory
    25   val target_result: (Proof.context -> 'a * Proof.context) -> local_theory -> 'a * local_theory
    26   val target: (Proof.context -> Proof.context) -> local_theory -> local_theory
    27   val map_contexts: (Context.generic -> Context.generic) -> local_theory -> local_theory
    28   val standard_morphism: local_theory -> Proof.context -> morphism
    29   val target_morphism: local_theory -> morphism
    30   val global_morphism: local_theory -> morphism
    31   val pretty: local_theory -> Pretty.T list
    32   val abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
    33     (term * term) * local_theory
    34   val define: string -> (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
    35     (term * (string * thm)) * local_theory
    36   val note: Attrib.binding * thm list -> local_theory -> (string * thm list) * local_theory
    37   val notes: (Attrib.binding * (thm list * Attrib.src list) list) list ->
    38     local_theory -> (string * thm list) list * local_theory
    39   val notes_kind: string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
    40     local_theory -> (string * thm list) list * local_theory
    41   val type_syntax: bool -> declaration -> local_theory -> local_theory
    42   val term_syntax: bool -> declaration -> local_theory -> local_theory
    43   val declaration: bool -> declaration -> local_theory -> local_theory
    44   val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
    45   val init: string -> operations -> Proof.context -> local_theory
    46   val restore: local_theory -> local_theory
    47   val reinit: local_theory -> local_theory
    48   val exit: local_theory -> Proof.context
    49   val exit_global: local_theory -> theory
    50   val exit_result: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * Proof.context
    51   val exit_result_global: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * theory
    52 end;
    53 
    54 structure Local_Theory: LOCAL_THEORY =
    55 struct
    56 
    57 (** local theory data **)
    58 
    59 (* type lthy *)
    60 
    61 type operations =
    62  {pretty: local_theory -> Pretty.T list,
    63   abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
    64     (term * term) * local_theory,
    65   define: string ->
    66     (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
    67     (term * (string * thm)) * local_theory,
    68   notes: string ->
    69     (Attrib.binding * (thm list * Attrib.src list) list) list ->
    70     local_theory -> (string * thm list) list * local_theory,
    71   type_syntax: bool -> declaration -> local_theory -> local_theory,
    72   term_syntax: bool -> declaration -> local_theory -> local_theory,
    73   declaration: bool -> declaration -> local_theory -> local_theory,
    74   reinit: local_theory -> local_theory,
    75   exit: local_theory -> Proof.context};
    76 
    77 datatype lthy = LThy of
    78  {naming: Name_Space.naming,
    79   theory_prefix: string,
    80   operations: operations,
    81   target: Proof.context};
    82 
    83 fun make_lthy (naming, theory_prefix, operations, target) =
    84   LThy {naming = naming, theory_prefix = theory_prefix,
    85     operations = operations, target = target};
    86 
    87 
    88 (* context data *)
    89 
    90 structure Data = Proof_Data
    91 (
    92   type T = lthy option;
    93   fun init _ = NONE;
    94 );
    95 
    96 fun get_lthy lthy =
    97   (case Data.get lthy of
    98     NONE => error "No local theory context"
    99   | SOME (LThy data) => data);
   100 
   101 fun map_lthy f lthy =
   102   let val {naming, theory_prefix, operations, target} = get_lthy lthy
   103   in Data.put (SOME (make_lthy (f (naming, theory_prefix, operations, target)))) lthy end;
   104 
   105 val affirm = tap get_lthy;
   106 
   107 
   108 (* naming *)
   109 
   110 val naming_of = #naming o get_lthy;
   111 val full_name = Name_Space.full_name o naming_of;
   112 
   113 fun map_naming f = map_lthy (fn (naming, theory_prefix, operations, target) =>
   114   (f naming, theory_prefix, operations, target));
   115 
   116 val conceal = map_naming Name_Space.conceal;
   117 val set_group = map_naming o Name_Space.set_group;
   118 
   119 val restore_naming = map_naming o K o naming_of;
   120 
   121 
   122 (* target *)
   123 
   124 val target_of = #target o get_lthy;
   125 
   126 fun map_target f = map_lthy (fn (naming, theory_prefix, operations, target) =>
   127   (naming, theory_prefix, operations, f target));
   128 
   129 
   130 (* substructure mappings *)
   131 
   132 fun raw_theory_result f lthy =
   133   let
   134     val (res, thy') = f (ProofContext.theory_of lthy);
   135     val lthy' = lthy
   136       |> map_target (ProofContext.transfer thy')
   137       |> ProofContext.transfer thy';
   138   in (res, lthy') end;
   139 
   140 fun raw_theory f = #2 o raw_theory_result (f #> pair ());
   141 
   142 val checkpoint = raw_theory Theory.checkpoint;
   143 
   144 fun theory_result f lthy =
   145   lthy |> raw_theory_result (fn thy =>
   146     thy
   147     |> Sign.map_naming (K (naming_of lthy))
   148     |> f
   149     ||> Sign.restore_naming thy);
   150 
   151 fun theory f = #2 o theory_result (f #> pair ());
   152 
   153 fun target_result f lthy =
   154   let
   155     val (res, ctxt') = target_of lthy
   156       |> Context_Position.set_visible false
   157       |> f
   158       ||> Context_Position.restore_visible lthy;
   159     val thy' = ProofContext.theory_of ctxt';
   160     val lthy' = lthy
   161       |> map_target (K ctxt')
   162       |> ProofContext.transfer thy';
   163   in (res, lthy') end;
   164 
   165 fun target f = #2 o target_result (f #> pair ());
   166 
   167 fun map_contexts f =
   168   theory (Context.theory_map f) #>
   169   target (Context.proof_map f) #>
   170   Context.proof_map f;
   171 
   172 
   173 (* morphisms *)
   174 
   175 fun standard_morphism lthy ctxt =
   176   ProofContext.norm_export_morphism lthy ctxt $>
   177   Morphism.binding_morphism (Name_Space.transform_binding (naming_of lthy));
   178 
   179 fun target_morphism lthy = standard_morphism lthy (target_of lthy);
   180 fun global_morphism lthy = standard_morphism lthy (ProofContext.init (ProofContext.theory_of lthy));
   181 
   182 
   183 (* basic operations *)
   184 
   185 fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
   186 fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
   187 
   188 val pretty = operation #pretty;
   189 val abbrev = apsnd checkpoint ooo operation2 #abbrev;
   190 val define = apsnd checkpoint ooo operation2 #define;
   191 val notes_kind = apsnd checkpoint ooo operation2 #notes;
   192 val type_syntax = checkpoint ooo operation2 #type_syntax;
   193 val term_syntax = checkpoint ooo operation2 #term_syntax;
   194 val declaration = checkpoint ooo operation2 #declaration;
   195 
   196 val notes = notes_kind "";
   197 fun note (a, ths) = notes [(a, [(ths, [])])] #>> the_single;
   198 
   199 fun notation add mode raw_args lthy =
   200   let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
   201   in term_syntax false (ProofContext.target_notation add mode args) lthy end;
   202 
   203 
   204 (* init *)
   205 
   206 fun init theory_prefix operations target =
   207   let val naming =
   208     Sign.naming_of (ProofContext.theory_of target)
   209     |> Name_Space.mandatory_path theory_prefix;
   210   in
   211     target |> Data.map
   212       (fn NONE => SOME (make_lthy (naming, theory_prefix, operations, target))
   213         | SOME _ => error "Local theory already initialized")
   214     |> checkpoint
   215   end;
   216 
   217 fun restore lthy =
   218   let val {theory_prefix, operations, target, ...} = get_lthy lthy
   219   in init theory_prefix operations target end;
   220 
   221 val reinit = checkpoint o operation #reinit;
   222 
   223 
   224 (* exit *)
   225 
   226 val exit = ProofContext.theory Theory.checkpoint o operation #exit;
   227 val exit_global = ProofContext.theory_of o exit;
   228 
   229 fun exit_result f (x, lthy) =
   230   let
   231     val ctxt = exit lthy;
   232     val phi = standard_morphism lthy ctxt;
   233   in (f phi x, ctxt) end;
   234 
   235 fun exit_result_global f (x, lthy) =
   236   let
   237     val thy = exit_global lthy;
   238     val thy_ctxt = ProofContext.init thy;
   239     val phi = standard_morphism lthy thy_ctxt;
   240   in (f phi x, thy) end;
   241 
   242 end;