src/Pure/Isar/local_theory.ML
author wenzelm
Mon Nov 14 16:52:19 2011 +0100 (2011-11-14 ago)
changeset 45488 6d71d9e52369
parent 45298 aa35859c8741
child 46992 eeea81b86b70
permissions -rw-r--r--
pass positions for named targets, for formal links in the document model;
     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: {syntax: bool, pervasive: bool} -> declaration -> local_theory -> local_theory
    43   val pretty: local_theory -> Pretty.T list
    44   val set_defsort: sort -> local_theory -> local_theory
    45   val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> local_theory -> local_theory
    46   val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
    47   val class_alias: binding -> class -> local_theory -> local_theory
    48   val type_alias: binding -> string -> local_theory -> local_theory
    49   val const_alias: binding -> string -> local_theory -> local_theory
    50   val init: serial option -> string -> operations -> Proof.context -> local_theory
    51   val restore: local_theory -> local_theory
    52   val exit: local_theory -> Proof.context
    53   val exit_global: local_theory -> theory
    54   val exit_result: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * Proof.context
    55   val exit_result_global: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * theory
    56 end;
    57 
    58 structure Local_Theory: LOCAL_THEORY =
    59 struct
    60 
    61 (** local theory data **)
    62 
    63 (* type lthy *)
    64 
    65 type operations =
    66  {define: (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   abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
    72     (term * term) * local_theory,
    73   declaration: {syntax: bool, pervasive: bool} -> declaration -> local_theory -> local_theory,
    74   pretty: local_theory -> Pretty.T list,
    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 new_group = map_naming Name_Space.new_group;
   118 val reset_group = map_naming Name_Space.reset_group;
   119 
   120 val restore_naming = map_naming o K o naming_of;
   121 
   122 
   123 (* target *)
   124 
   125 val target_of = #target o get_lthy;
   126 
   127 fun map_target f = map_lthy (fn (naming, theory_prefix, operations, target) =>
   128   (naming, theory_prefix, operations, f target));
   129 
   130 
   131 (* substructure mappings *)
   132 
   133 fun raw_theory_result f lthy =
   134   let
   135     val (res, thy') = f (Proof_Context.theory_of lthy);
   136     val lthy' = lthy
   137       |> map_target (Proof_Context.transfer thy')
   138       |> Proof_Context.transfer thy';
   139   in (res, lthy') end;
   140 
   141 fun raw_theory f = #2 o raw_theory_result (f #> pair ());
   142 
   143 val checkpoint = raw_theory Theory.checkpoint;
   144 
   145 fun background_theory_result f lthy =
   146   lthy |> raw_theory_result (fn thy =>
   147     thy
   148     |> Sign.map_naming (K (naming_of lthy))
   149     |> f
   150     ||> Sign.restore_naming thy
   151     ||> Theory.checkpoint);
   152 
   153 fun background_theory f = #2 o background_theory_result (f #> pair ());
   154 
   155 fun target_result f lthy =
   156   let
   157     val (res, ctxt') = target_of lthy
   158       |> Context_Position.set_visible false
   159       |> f
   160       ||> Context_Position.restore_visible lthy;
   161     val thy' = Proof_Context.theory_of ctxt';
   162     val lthy' = lthy
   163       |> map_target (K ctxt')
   164       |> Proof_Context.transfer thy';
   165   in (res, lthy') end;
   166 
   167 fun target f = #2 o target_result (f #> pair ());
   168 
   169 fun map_contexts f =
   170   background_theory (Context.theory_map f) #>
   171   target (Context.proof_map f) #>
   172   Context.proof_map f;
   173 
   174 fun propagate_ml_env (context as Context.Proof lthy) =
   175       Context.Proof (map_contexts (ML_Env.inherit context) lthy)
   176   | propagate_ml_env context = context;
   177 
   178 
   179 (* morphisms *)
   180 
   181 fun standard_morphism lthy ctxt =
   182   Proof_Context.norm_export_morphism lthy ctxt $>
   183   Morphism.binding_morphism (Name_Space.transform_binding (naming_of lthy));
   184 
   185 fun target_morphism lthy = standard_morphism lthy (target_of lthy);
   186 fun global_morphism lthy =
   187   standard_morphism lthy (Proof_Context.init_global (Proof_Context.theory_of lthy));
   188 
   189 
   190 (* primitive operations *)
   191 
   192 fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
   193 fun operation1 f x = operation (fn ops => f ops x);
   194 fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
   195 
   196 val pretty = operation #pretty;
   197 val abbrev = apsnd checkpoint ooo operation2 #abbrev;
   198 val define = apsnd checkpoint oo operation1 #define;
   199 val notes_kind = apsnd checkpoint ooo operation2 #notes;
   200 val declaration = checkpoint ooo operation2 #declaration;
   201 
   202 
   203 
   204 (** basic derived operations **)
   205 
   206 val notes = notes_kind "";
   207 fun note (a, ths) = notes [(a, [(ths, [])])] #>> the_single;
   208 
   209 fun set_defsort S =
   210   declaration {syntax = true, pervasive = false}
   211     (K (Context.mapping (Sign.set_defsort S) (Proof_Context.set_defsort S)));
   212 
   213 
   214 (* notation *)
   215 
   216 fun type_notation add mode raw_args lthy =
   217   let val args = map (apfst (Morphism.typ (target_morphism lthy))) raw_args in
   218     declaration {syntax = true, pervasive = false}
   219       (Proof_Context.target_type_notation add mode args) lthy
   220   end;
   221 
   222 fun notation add mode raw_args lthy =
   223   let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args in
   224     declaration {syntax = true, pervasive = false}
   225       (Proof_Context.target_notation add mode args) lthy
   226   end;
   227 
   228 
   229 (* name space aliases *)
   230 
   231 fun alias global_alias local_alias b name =
   232   declaration {syntax = true, pervasive = 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 
   236 val class_alias = alias Sign.class_alias Proof_Context.class_alias;
   237 val type_alias = alias Sign.type_alias Proof_Context.type_alias;
   238 val const_alias = alias Sign.const_alias Proof_Context.const_alias;
   239 
   240 
   241 
   242 (** init and exit **)
   243 
   244 (* init *)
   245 
   246 fun init group theory_prefix operations target =
   247   let val naming =
   248     Sign.naming_of (Proof_Context.theory_of target)
   249     |> Name_Space.set_group group
   250     |> Name_Space.mandatory_path theory_prefix;
   251   in
   252     target |> Data.map
   253       (fn NONE => SOME (make_lthy (naming, theory_prefix, operations, target))
   254         | SOME _ => error "Local theory already initialized")
   255     |> checkpoint
   256   end;
   257 
   258 fun restore lthy =
   259   let val {naming, theory_prefix, operations, target} = get_lthy lthy
   260   in init (Name_Space.get_group naming) theory_prefix operations target end;
   261 
   262 
   263 (* exit *)
   264 
   265 val exit = Proof_Context.background_theory Theory.checkpoint o operation #exit;
   266 val exit_global = Proof_Context.theory_of o exit;
   267 
   268 fun exit_result f (x, lthy) =
   269   let
   270     val ctxt = exit lthy;
   271     val phi = standard_morphism lthy ctxt;
   272   in (f phi x, ctxt) end;
   273 
   274 fun exit_result_global f (x, lthy) =
   275   let
   276     val thy = exit_global lthy;
   277     val thy_ctxt = Proof_Context.init_global thy;
   278     val phi = standard_morphism lthy thy_ctxt;
   279   in (f phi x, thy) end;
   280 
   281 end;