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