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