src/Pure/Isar/local_theory.ML
author wenzelm
Sat Mar 13 14:40:36 2010 +0100 (2010-03-13 ago)
changeset 35738 98fd035c3fe3
parent 35412 b8dead547d9e
child 35798 fd1bb29f8170
permissions -rw-r--r--
added Local_Theory.alias operations (independent of target);
     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 checkpoint: 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 standard_morphism: local_theory -> Proof.context -> morphism
    30   val target_morphism: local_theory -> morphism
    31   val global_morphism: local_theory -> morphism
    32   val pretty: local_theory -> Pretty.T list
    33   val abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
    34     (term * term) * local_theory
    35   val define: (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
    36     (term * (string * thm)) * local_theory
    37   val note: Attrib.binding * thm list -> local_theory -> (string * thm list) * local_theory
    38   val notes: (Attrib.binding * (thm list * Attrib.src list) list) list ->
    39     local_theory -> (string * thm list) list * local_theory
    40   val notes_kind: string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
    41     local_theory -> (string * thm list) list * local_theory
    42   val type_syntax: bool -> declaration -> local_theory -> local_theory
    43   val term_syntax: bool -> declaration -> local_theory -> local_theory
    44   val declaration: bool -> declaration -> 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 reinit: 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  {pretty: local_theory -> Pretty.T list,
    68   abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
    69     (term * term) * local_theory,
    70   define: (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
    71     (term * (string * thm)) * local_theory,
    72   notes: string ->
    73     (Attrib.binding * (thm list * Attrib.src list) list) list ->
    74     local_theory -> (string * thm list) list * local_theory,
    75   type_syntax: bool -> declaration -> local_theory -> local_theory,
    76   term_syntax: bool -> declaration -> local_theory -> local_theory,
    77   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 
   156 fun theory f = #2 o theory_result (f #> pair ());
   157 
   158 fun target_result f lthy =
   159   let
   160     val (res, ctxt') = target_of lthy
   161       |> Context_Position.set_visible false
   162       |> f
   163       ||> Context_Position.restore_visible lthy;
   164     val thy' = ProofContext.theory_of ctxt';
   165     val lthy' = lthy
   166       |> map_target (K ctxt')
   167       |> ProofContext.transfer thy';
   168   in (res, lthy') end;
   169 
   170 fun target f = #2 o target_result (f #> pair ());
   171 
   172 fun map_contexts f =
   173   theory (Context.theory_map f) #>
   174   target (Context.proof_map f) #>
   175   Context.proof_map f;
   176 
   177 
   178 (* morphisms *)
   179 
   180 fun standard_morphism lthy ctxt =
   181   ProofContext.norm_export_morphism lthy ctxt $>
   182   Morphism.binding_morphism (Name_Space.transform_binding (naming_of lthy));
   183 
   184 fun target_morphism lthy = standard_morphism lthy (target_of lthy);
   185 fun global_morphism lthy = standard_morphism lthy (ProofContext.init (ProofContext.theory_of lthy));
   186 
   187 
   188 (* basic 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 type_syntax = checkpoint ooo operation2 #type_syntax;
   199 val term_syntax = checkpoint ooo operation2 #term_syntax;
   200 val declaration = checkpoint ooo operation2 #declaration;
   201 
   202 val notes = notes_kind "";
   203 fun note (a, ths) = notes [(a, [(ths, [])])] #>> the_single;
   204 
   205 
   206 (* notation *)
   207 
   208 fun type_notation add mode raw_args lthy =
   209   let val args = map (apfst (Morphism.typ (target_morphism lthy))) raw_args
   210   in type_syntax false (ProofContext.target_type_notation add mode args) lthy end;
   211 
   212 fun notation add mode raw_args lthy =
   213   let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
   214   in term_syntax false (ProofContext.target_notation add mode args) lthy end;
   215 
   216 
   217 (* name space aliases *)
   218 
   219 fun alias syntax_declaration global_alias local_alias b name =
   220   syntax_declaration false (fn phi =>
   221     let val b' = Morphism.binding phi b
   222     in Context.mapping (global_alias b' name) (local_alias b' name) end)
   223   #> local_alias b name;
   224 
   225 val class_alias = alias type_syntax Sign.class_alias ProofContext.class_alias;
   226 val type_alias = alias type_syntax Sign.type_alias ProofContext.type_alias;
   227 val const_alias = alias term_syntax Sign.const_alias ProofContext.const_alias;
   228 
   229 
   230 (* init *)
   231 
   232 fun init group theory_prefix operations target =
   233   let val naming =
   234     Sign.naming_of (ProofContext.theory_of target)
   235     |> Name_Space.set_group group
   236     |> Name_Space.mandatory_path theory_prefix;
   237   in
   238     target |> Data.map
   239       (fn NONE => SOME (make_lthy (naming, theory_prefix, operations, target))
   240         | SOME _ => error "Local theory already initialized")
   241     |> checkpoint
   242   end;
   243 
   244 fun restore lthy =
   245   let val {naming, theory_prefix, operations, target} = get_lthy lthy
   246   in init (Name_Space.get_group naming) theory_prefix operations target end;
   247 
   248 val reinit = checkpoint o operation #reinit;
   249 
   250 
   251 (* exit *)
   252 
   253 val exit = ProofContext.theory Theory.checkpoint o operation #exit;
   254 val exit_global = ProofContext.theory_of o exit;
   255 
   256 fun exit_result f (x, lthy) =
   257   let
   258     val ctxt = exit lthy;
   259     val phi = standard_morphism lthy ctxt;
   260   in (f phi x, ctxt) end;
   261 
   262 fun exit_result_global f (x, lthy) =
   263   let
   264     val thy = exit_global lthy;
   265     val thy_ctxt = ProofContext.init thy;
   266     val phi = standard_morphism lthy thy_ctxt;
   267   in (f phi x, thy) end;
   268 
   269 end;