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