src/Pure/Isar/local_theory.ML
author wenzelm
Wed Jun 02 11:09:26 2010 +0200 (2010-06-02 ago)
changeset 37251 72c7e636067b
parent 36610 bafd82950e24
child 37949 48a874444164
permissions -rw-r--r--
normalize and postprocess proof body in a separate future, taking care of platforms without multithreading (greately improves parallelization in general without the overhead of promised proofs, cf. usedir -q 0);
     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 new_group: local_theory -> local_theory
    18   val reset_group: local_theory -> local_theory
    19   val restore_naming: local_theory -> local_theory -> local_theory
    20   val target_of: local_theory -> Proof.context
    21   val raw_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
    22   val raw_theory: (theory -> theory) -> 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: (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 declaration: bool -> declaration -> local_theory -> local_theory
    42   val syntax_declaration: bool -> declaration -> local_theory -> local_theory
    43   val set_defsort: sort -> local_theory -> local_theory
    44   val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> local_theory -> local_theory
    45   val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
    46   val class_alias: binding -> class -> local_theory -> local_theory
    47   val type_alias: binding -> string -> local_theory -> local_theory
    48   val const_alias: binding -> string -> local_theory -> local_theory
    49   val init: serial option -> string -> operations -> Proof.context -> local_theory
    50   val restore: local_theory -> local_theory
    51   val reinit: 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  {pretty: local_theory -> Pretty.T list,
    67   abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
    68     (term * term) * local_theory,
    69   define: (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
    70     (term * (string * thm)) * local_theory,
    71   notes: string ->
    72     (Attrib.binding * (thm list * Attrib.src list) list) list ->
    73     local_theory -> (string * thm list) list * local_theory,
    74   declaration: bool -> declaration -> local_theory -> local_theory,
    75   syntax_declaration: bool -> declaration -> local_theory -> local_theory,
    76   reinit: local_theory -> local_theory,
    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 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 theory f = #2 o 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   theory (Context.theory_map f) #>
   173   target (Context.proof_map f) #>
   174   Context.proof_map f;
   175 
   176 
   177 (* morphisms *)
   178 
   179 fun standard_morphism lthy ctxt =
   180   ProofContext.norm_export_morphism lthy ctxt $>
   181   Morphism.binding_morphism (Name_Space.transform_binding (naming_of lthy));
   182 
   183 fun target_morphism lthy = standard_morphism lthy (target_of lthy);
   184 fun global_morphism lthy =
   185   standard_morphism lthy (ProofContext.init_global (ProofContext.theory_of lthy));
   186 
   187 
   188 (* primitive operations *)
   189 
   190 fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
   191 fun operation1 f x = operation (fn ops => f ops x);
   192 fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
   193 
   194 val pretty = operation #pretty;
   195 val abbrev = apsnd checkpoint ooo operation2 #abbrev;
   196 val define = apsnd checkpoint oo operation1 #define;
   197 val notes_kind = apsnd checkpoint ooo operation2 #notes;
   198 val declaration = checkpoint ooo operation2 #declaration;
   199 val syntax_declaration = checkpoint ooo operation2 #syntax_declaration;
   200 
   201 
   202 
   203 (** basic derived operations **)
   204 
   205 val notes = notes_kind "";
   206 fun note (a, ths) = notes [(a, [(ths, [])])] #>> the_single;
   207 
   208 fun set_defsort S =
   209   syntax_declaration false (K (Context.mapping (Sign.set_defsort S) (ProofContext.set_defsort S)));
   210 
   211 
   212 (* notation *)
   213 
   214 fun type_notation add mode raw_args lthy =
   215   let val args = map (apfst (Morphism.typ (target_morphism lthy))) raw_args
   216   in syntax_declaration false (ProofContext.target_type_notation add mode args) lthy end;
   217 
   218 fun notation add mode raw_args lthy =
   219   let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
   220   in syntax_declaration false (ProofContext.target_notation add mode args) lthy end;
   221 
   222 
   223 (* name space aliases *)
   224 
   225 fun alias global_alias local_alias b name =
   226   syntax_declaration false (fn phi =>
   227     let val b' = Morphism.binding phi b
   228     in Context.mapping (global_alias b' name) (local_alias b' name) end)
   229   #> local_alias b name;
   230 
   231 val class_alias = alias Sign.class_alias ProofContext.class_alias;
   232 val type_alias = alias Sign.type_alias ProofContext.type_alias;
   233 val const_alias = alias Sign.const_alias ProofContext.const_alias;
   234 
   235 
   236 
   237 (** init and exit **)
   238 
   239 (* init *)
   240 
   241 fun init group theory_prefix operations target =
   242   let val naming =
   243     Sign.naming_of (ProofContext.theory_of target)
   244     |> Name_Space.set_group group
   245     |> Name_Space.mandatory_path theory_prefix;
   246   in
   247     target |> Data.map
   248       (fn NONE => SOME (make_lthy (naming, theory_prefix, operations, target))
   249         | SOME _ => error "Local theory already initialized")
   250     |> checkpoint
   251   end;
   252 
   253 fun restore lthy =
   254   let val {naming, theory_prefix, operations, target} = get_lthy lthy
   255   in init (Name_Space.get_group naming) theory_prefix operations target end;
   256 
   257 val reinit = checkpoint o operation #reinit;
   258 
   259 
   260 (* exit *)
   261 
   262 val exit = ProofContext.theory Theory.checkpoint o operation #exit;
   263 val exit_global = ProofContext.theory_of o exit;
   264 
   265 fun exit_result f (x, lthy) =
   266   let
   267     val ctxt = exit lthy;
   268     val phi = standard_morphism lthy ctxt;
   269   in (f phi x, ctxt) end;
   270 
   271 fun exit_result_global f (x, lthy) =
   272   let
   273     val thy = exit_global lthy;
   274     val thy_ctxt = ProofContext.init_global thy;
   275     val phi = standard_morphism lthy thy_ctxt;
   276   in (f phi x, thy) end;
   277 
   278 end;