src/Pure/Isar/local_theory.ML
author wenzelm
Thu Aug 26 15:48:08 2010 +0200 (2010-08-26 ago)
changeset 38757 2b3e054ae6fc
parent 38756 d07959fabde6
child 42360 da8817d01e7c
permissions -rw-r--r--
renamed Local_Theory.theory(_result) to Local_Theory.background_theory(_result) to emphasize that this belongs to the infrastructure and is rarely appropriate in user-space tools;
     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 type generic_theory = Context.generic;
     9 
    10 signature LOCAL_THEORY =
    11 sig
    12   type operations
    13   val affirm: local_theory -> local_theory
    14   val naming_of: local_theory -> Name_Space.naming
    15   val full_name: local_theory -> binding -> string
    16   val map_naming: (Name_Space.naming -> Name_Space.naming) -> local_theory -> local_theory
    17   val conceal: local_theory -> local_theory
    18   val new_group: local_theory -> local_theory
    19   val reset_group: local_theory -> local_theory
    20   val restore_naming: local_theory -> local_theory -> local_theory
    21   val target_of: local_theory -> Proof.context
    22   val raw_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
    23   val raw_theory: (theory -> theory) -> local_theory -> local_theory
    24   val background_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
    25   val background_theory: (theory -> theory) -> local_theory -> local_theory
    26   val target_result: (Proof.context -> 'a * Proof.context) -> local_theory -> 'a * local_theory
    27   val target: (Proof.context -> Proof.context) -> local_theory -> local_theory
    28   val map_contexts: (Context.generic -> Context.generic) -> local_theory -> local_theory
    29   val propagate_ml_env: generic_theory -> generic_theory
    30   val standard_morphism: local_theory -> Proof.context -> morphism
    31   val target_morphism: local_theory -> morphism
    32   val global_morphism: local_theory -> morphism
    33   val define: (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
    34     (term * (string * thm)) * local_theory
    35   val note: Attrib.binding * thm list -> local_theory -> (string * thm list) * local_theory
    36   val notes: (Attrib.binding * (thm list * Attrib.src list) list) list ->
    37     local_theory -> (string * thm list) list * local_theory
    38   val notes_kind: string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
    39     local_theory -> (string * thm list) list * local_theory
    40   val abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
    41     (term * term) * local_theory
    42   val declaration: bool -> declaration -> local_theory -> local_theory
    43   val syntax_declaration: bool -> declaration -> local_theory -> local_theory
    44   val pretty: local_theory -> Pretty.T list
    45   val set_defsort: sort -> local_theory -> local_theory
    46   val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> local_theory -> local_theory
    47   val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
    48   val class_alias: binding -> class -> local_theory -> local_theory
    49   val type_alias: binding -> string -> local_theory -> local_theory
    50   val const_alias: binding -> string -> local_theory -> local_theory
    51   val init: serial option -> string -> operations -> Proof.context -> local_theory
    52   val restore: local_theory -> local_theory
    53   val exit: local_theory -> Proof.context
    54   val exit_global: local_theory -> theory
    55   val exit_result: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * Proof.context
    56   val exit_result_global: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * theory
    57 end;
    58 
    59 structure Local_Theory: LOCAL_THEORY =
    60 struct
    61 
    62 (** local theory data **)
    63 
    64 (* type lthy *)
    65 
    66 type operations =
    67  {define: (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
    68     (term * (string * thm)) * local_theory,
    69   notes: string ->
    70     (Attrib.binding * (thm list * Attrib.src list) list) list ->
    71     local_theory -> (string * thm list) list * local_theory,
    72   abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
    73     (term * term) * local_theory,
    74   declaration: bool -> declaration -> local_theory -> local_theory,
    75   syntax_declaration: bool -> declaration -> local_theory -> local_theory,
    76   pretty: local_theory -> Pretty.T list,
    77   exit: local_theory -> Proof.context};
    78 
    79 datatype lthy = LThy of
    80  {naming: Name_Space.naming,
    81   theory_prefix: string,
    82   operations: operations,
    83   target: Proof.context};
    84 
    85 fun make_lthy (naming, theory_prefix, operations, target) =
    86   LThy {naming = naming, theory_prefix = theory_prefix,
    87     operations = operations, target = target};
    88 
    89 
    90 (* context data *)
    91 
    92 structure Data = Proof_Data
    93 (
    94   type T = lthy option;
    95   fun init _ = NONE;
    96 );
    97 
    98 fun get_lthy lthy =
    99   (case Data.get lthy of
   100     NONE => error "No local theory context"
   101   | SOME (LThy data) => data);
   102 
   103 fun map_lthy f lthy =
   104   let val {naming, theory_prefix, operations, target} = get_lthy lthy
   105   in Data.put (SOME (make_lthy (f (naming, theory_prefix, operations, target)))) lthy end;
   106 
   107 val affirm = tap get_lthy;
   108 
   109 
   110 (* naming *)
   111 
   112 val naming_of = #naming o get_lthy;
   113 val full_name = Name_Space.full_name o naming_of;
   114 
   115 fun map_naming f = map_lthy (fn (naming, theory_prefix, operations, target) =>
   116   (f naming, theory_prefix, operations, target));
   117 
   118 val conceal = map_naming Name_Space.conceal;
   119 val new_group = map_naming Name_Space.new_group;
   120 val reset_group = map_naming Name_Space.reset_group;
   121 
   122 val restore_naming = map_naming o K o naming_of;
   123 
   124 
   125 (* target *)
   126 
   127 val target_of = #target o get_lthy;
   128 
   129 fun map_target f = map_lthy (fn (naming, theory_prefix, operations, target) =>
   130   (naming, theory_prefix, operations, f target));
   131 
   132 
   133 (* substructure mappings *)
   134 
   135 fun raw_theory_result f lthy =
   136   let
   137     val (res, thy') = f (ProofContext.theory_of lthy);
   138     val lthy' = lthy
   139       |> map_target (ProofContext.transfer thy')
   140       |> ProofContext.transfer thy';
   141   in (res, lthy') end;
   142 
   143 fun raw_theory f = #2 o raw_theory_result (f #> pair ());
   144 
   145 val checkpoint = raw_theory Theory.checkpoint;
   146 
   147 fun background_theory_result f lthy =
   148   lthy |> raw_theory_result (fn thy =>
   149     thy
   150     |> Sign.map_naming (K (naming_of lthy))
   151     |> f
   152     ||> Sign.restore_naming thy
   153     ||> Theory.checkpoint);
   154 
   155 fun background_theory f = #2 o background_theory_result (f #> pair ());
   156 
   157 fun target_result f lthy =
   158   let
   159     val (res, ctxt') = target_of lthy
   160       |> Context_Position.set_visible false
   161       |> f
   162       ||> Context_Position.restore_visible lthy;
   163     val thy' = ProofContext.theory_of ctxt';
   164     val lthy' = lthy
   165       |> map_target (K ctxt')
   166       |> ProofContext.transfer thy';
   167   in (res, lthy') end;
   168 
   169 fun target f = #2 o target_result (f #> pair ());
   170 
   171 fun map_contexts f =
   172   background_theory (Context.theory_map f) #>
   173   target (Context.proof_map f) #>
   174   Context.proof_map f;
   175 
   176 fun propagate_ml_env (context as Context.Proof lthy) =
   177       Context.Proof (map_contexts (ML_Env.inherit context) lthy)
   178   | propagate_ml_env context = context;
   179 
   180 
   181 (* morphisms *)
   182 
   183 fun standard_morphism lthy ctxt =
   184   ProofContext.norm_export_morphism lthy ctxt $>
   185   Morphism.binding_morphism (Name_Space.transform_binding (naming_of lthy));
   186 
   187 fun target_morphism lthy = standard_morphism lthy (target_of lthy);
   188 fun global_morphism lthy =
   189   standard_morphism lthy (ProofContext.init_global (ProofContext.theory_of lthy));
   190 
   191 
   192 (* primitive operations *)
   193 
   194 fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
   195 fun operation1 f x = operation (fn ops => f ops x);
   196 fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
   197 
   198 val pretty = operation #pretty;
   199 val abbrev = apsnd checkpoint ooo operation2 #abbrev;
   200 val define = apsnd checkpoint oo operation1 #define;
   201 val notes_kind = apsnd checkpoint ooo operation2 #notes;
   202 val declaration = checkpoint ooo operation2 #declaration;
   203 val syntax_declaration = checkpoint ooo operation2 #syntax_declaration;
   204 
   205 
   206 
   207 (** basic derived operations **)
   208 
   209 val notes = notes_kind "";
   210 fun note (a, ths) = notes [(a, [(ths, [])])] #>> the_single;
   211 
   212 fun set_defsort S =
   213   syntax_declaration false (K (Context.mapping (Sign.set_defsort S) (ProofContext.set_defsort S)));
   214 
   215 
   216 (* notation *)
   217 
   218 fun type_notation add mode raw_args lthy =
   219   let val args = map (apfst (Morphism.typ (target_morphism lthy))) raw_args
   220   in syntax_declaration false (ProofContext.target_type_notation add mode args) lthy end;
   221 
   222 fun notation add mode raw_args lthy =
   223   let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
   224   in syntax_declaration false (ProofContext.target_notation add mode args) lthy end;
   225 
   226 
   227 (* name space aliases *)
   228 
   229 fun alias global_alias local_alias b name =
   230   syntax_declaration false (fn phi =>
   231     let val b' = Morphism.binding phi b
   232     in Context.mapping (global_alias b' name) (local_alias b' name) end)
   233   #> local_alias b name;
   234 
   235 val class_alias = alias Sign.class_alias ProofContext.class_alias;
   236 val type_alias = alias Sign.type_alias ProofContext.type_alias;
   237 val const_alias = alias Sign.const_alias ProofContext.const_alias;
   238 
   239 
   240 
   241 (** init and exit **)
   242 
   243 (* init *)
   244 
   245 fun init group theory_prefix operations target =
   246   let val naming =
   247     Sign.naming_of (ProofContext.theory_of target)
   248     |> Name_Space.set_group group
   249     |> Name_Space.mandatory_path theory_prefix;
   250   in
   251     target |> Data.map
   252       (fn NONE => SOME (make_lthy (naming, theory_prefix, operations, target))
   253         | SOME _ => error "Local theory already initialized")
   254     |> checkpoint
   255   end;
   256 
   257 fun restore lthy =
   258   let val {naming, theory_prefix, operations, target} = get_lthy lthy
   259   in init (Name_Space.get_group naming) theory_prefix operations target end;
   260 
   261 
   262 (* exit *)
   263 
   264 val exit = ProofContext.background_theory Theory.checkpoint o operation #exit;
   265 val exit_global = ProofContext.theory_of o exit;
   266 
   267 fun exit_result f (x, lthy) =
   268   let
   269     val ctxt = exit lthy;
   270     val phi = standard_morphism lthy ctxt;
   271   in (f phi x, ctxt) end;
   272 
   273 fun exit_result_global f (x, lthy) =
   274   let
   275     val thy = exit_global lthy;
   276     val thy_ctxt = ProofContext.init_global thy;
   277     val phi = standard_morphism lthy thy_ctxt;
   278   in (f phi x, thy) end;
   279 
   280 end;