src/Pure/Isar/local_theory.ML
author haftmann
Fri Dec 05 08:04:53 2008 +0100 (2008-12-05 ago)
changeset 28991 694227dd3e8c
parent 28965 1de908189869
child 29134 9657871890c7
permissions -rw-r--r--
dropped NameSpace.declare_base
     1 (*  Title:      Pure/Isar/local_theory.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Local theory operations, with abstract target context.
     6 *)
     7 
     8 type local_theory = Proof.context;
     9 
    10 signature LOCAL_THEORY =
    11 sig
    12   type operations
    13   val group_of: local_theory -> string
    14   val group_properties_of: local_theory -> Properties.T
    15   val group_position_of: local_theory -> Properties.T
    16   val set_group: string -> local_theory -> local_theory
    17   val target_of: local_theory -> Proof.context
    18   val raw_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
    19   val raw_theory: (theory -> theory) -> local_theory -> local_theory
    20   val checkpoint: local_theory -> local_theory
    21   val full_naming: local_theory -> NameSpace.naming
    22   val full_name: local_theory -> Binding.T -> string
    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 affirm: local_theory -> local_theory
    28   val pretty: local_theory -> Pretty.T list
    29   val abbrev: Syntax.mode -> (Binding.T * mixfix) * term -> local_theory ->
    30     (term * term) * local_theory
    31   val define: string -> (Binding.T * mixfix) * (Attrib.binding * term) -> local_theory ->
    32     (term * (string * thm)) * local_theory
    33   val note: string -> Attrib.binding * thm list -> local_theory -> (string * thm list) * local_theory
    34   val notes: string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
    35     local_theory -> (string * thm list) list * local_theory
    36   val type_syntax: declaration -> local_theory -> local_theory
    37   val term_syntax: declaration -> local_theory -> local_theory
    38   val declaration: declaration -> local_theory -> local_theory
    39   val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
    40   val target_morphism: local_theory -> morphism
    41   val init: string -> operations -> Proof.context -> local_theory
    42   val restore: local_theory -> local_theory
    43   val reinit: local_theory -> local_theory
    44   val exit: local_theory -> Proof.context
    45   val exit_global: local_theory -> theory
    46   val exit_result: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * Proof.context
    47   val exit_result_global: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * theory
    48 end;
    49 
    50 structure LocalTheory: LOCAL_THEORY =
    51 struct
    52 
    53 (** local theory data **)
    54 
    55 (* type lthy *)
    56 
    57 type operations =
    58  {pretty: local_theory -> Pretty.T list,
    59   abbrev: Syntax.mode -> (Binding.T * mixfix) * term -> local_theory ->
    60     (term * term) * local_theory,
    61   define: string ->
    62     (Binding.T * mixfix) * (Attrib.binding * term) -> local_theory ->
    63     (term * (string * thm)) * local_theory,
    64   notes: string ->
    65     (Attrib.binding * (thm list * Attrib.src list) list) list ->
    66     local_theory -> (string * thm list) list * local_theory,
    67   type_syntax: declaration -> local_theory -> local_theory,
    68   term_syntax: declaration -> local_theory -> local_theory,
    69   declaration: declaration -> local_theory -> local_theory,
    70   reinit: local_theory -> local_theory,
    71   exit: local_theory -> Proof.context};
    72 
    73 datatype lthy = LThy of
    74  {group: string,
    75   theory_prefix: string,
    76   operations: operations,
    77   target: Proof.context};
    78 
    79 fun make_lthy (group, theory_prefix, operations, target) =
    80   LThy {group = group, theory_prefix = theory_prefix, operations = operations, target = target};
    81 
    82 
    83 (* context data *)
    84 
    85 structure Data = ProofDataFun
    86 (
    87   type T = lthy option;
    88   fun init _ = NONE;
    89 );
    90 
    91 fun get_lthy lthy =
    92   (case Data.get lthy of
    93     NONE => error "No local theory context"
    94   | SOME (LThy data) => data);
    95 
    96 fun map_lthy f lthy =
    97   let val {group, theory_prefix, operations, target} = get_lthy lthy
    98   in Data.put (SOME (make_lthy (f (group, theory_prefix, operations, target)))) lthy end;
    99 
   100 
   101 (* group *)
   102 
   103 val group_of = #group o get_lthy;
   104 
   105 fun group_properties_of lthy =
   106   (case group_of lthy of
   107     "" => []
   108   | group => [(Markup.groupN, group)]);
   109 
   110 fun group_position_of lthy =
   111   group_properties_of lthy @ Position.properties_of (Position.thread_data ());
   112 
   113 fun set_group group = map_lthy (fn (_, theory_prefix, operations, target) =>
   114   (group, theory_prefix, operations, target));
   115 
   116 
   117 (* target *)
   118 
   119 val target_of = #target o get_lthy;
   120 val affirm = tap target_of;
   121 
   122 fun map_target f = map_lthy (fn (group, theory_prefix, operations, target) =>
   123   (group, theory_prefix, operations, f target));
   124 
   125 
   126 (* substructure mappings *)
   127 
   128 fun raw_theory_result f lthy =
   129   let
   130     val (res, thy') = f (ProofContext.theory_of lthy);
   131     val lthy' = lthy
   132       |> map_target (ProofContext.transfer thy')
   133       |> ProofContext.transfer thy';
   134   in (res, lthy') end;
   135 
   136 fun raw_theory f = #2 o raw_theory_result (f #> pair ());
   137 
   138 val checkpoint = raw_theory Theory.checkpoint;
   139 
   140 fun full_naming lthy =
   141   Sign.naming_of (ProofContext.theory_of lthy)
   142   |> NameSpace.sticky_prefix (#theory_prefix (get_lthy lthy))
   143   |> NameSpace.qualified_names;
   144 
   145 fun full_name naming = NameSpace.full_name (full_naming naming);
   146 
   147 fun theory_result f lthy = lthy |> raw_theory_result (fn thy => thy
   148   |> Sign.sticky_prefix (#theory_prefix (get_lthy lthy))
   149   |> Sign.qualified_names
   150   |> f
   151   ||> Sign.restore_naming thy);
   152 
   153 fun theory f = #2 o theory_result (f #> pair ());
   154 
   155 fun target_result f lthy =
   156   let
   157     val (res, ctxt') = target_of lthy
   158       |> ContextPosition.set_visible false
   159       |> f
   160       ||> ContextPosition.restore_visible lthy;
   161     val thy' = ProofContext.theory_of ctxt';
   162     val lthy' = lthy
   163       |> map_target (K ctxt')
   164       |> ProofContext.transfer thy';
   165   in (res, lthy') end;
   166 
   167 fun target f = #2 o target_result (f #> pair ());
   168 
   169 
   170 (* basic operations *)
   171 
   172 fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
   173 fun operation1 f x = operation (fn ops => f ops x);
   174 fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
   175 
   176 val pretty = operation #pretty;
   177 val abbrev = apsnd checkpoint ooo operation2 #abbrev;
   178 val define = apsnd checkpoint ooo operation2 #define;
   179 val notes = apsnd checkpoint ooo operation2 #notes;
   180 val type_syntax = checkpoint oo operation1 #type_syntax;
   181 val term_syntax = checkpoint oo operation1 #term_syntax;
   182 val declaration = checkpoint oo operation1 #declaration;
   183 
   184 fun note kind (a, ths) = notes kind [(a, [(ths, [])])] #>> the_single;
   185 
   186 fun target_morphism lthy =
   187   ProofContext.norm_export_morphism lthy (target_of lthy);
   188 
   189 fun notation add mode raw_args lthy =
   190   let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
   191   in term_syntax (ProofContext.target_notation add mode args) lthy end;
   192 
   193 
   194 (* init *)
   195 
   196 fun init theory_prefix operations target = target |> Data.map
   197   (fn NONE => SOME (make_lthy ("", theory_prefix, operations, target))
   198     | SOME _ => error "Local theory already initialized")
   199   |> checkpoint;
   200 
   201 fun restore lthy =
   202   let val {group = _, theory_prefix, operations, target} = get_lthy lthy
   203   in init theory_prefix operations target end;
   204 
   205 val reinit = checkpoint o operation #reinit;
   206 
   207 
   208 (* exit *)
   209 
   210 val exit = ProofContext.theory Theory.checkpoint o operation #exit;
   211 val exit_global = ProofContext.theory_of o exit;
   212 
   213 fun exit_result f (x, lthy) =
   214   let
   215     val ctxt = exit lthy;
   216     val phi = ProofContext.norm_export_morphism lthy ctxt;
   217   in (f phi x, ctxt) end;
   218 
   219 fun exit_result_global f (x, lthy) =
   220   let
   221     val thy = exit_global lthy;
   222     val thy_ctxt = ProofContext.init thy;
   223     val phi = ProofContext.norm_export_morphism lthy thy_ctxt;
   224   in (f phi x, thy) end;
   225 
   226 end;