src/Pure/Isar/local_theory.ML
author wenzelm
Thu Nov 19 14:44:22 2009 +0100 (2009-11-19 ago)
changeset 33764 7bcefaab8d41
parent 33724 5ee13e0428d2
child 35412 b8dead547d9e
permissions -rw-r--r--
Local_Theory.define: eliminated slightly odd kind argument -- such low-level definitions should be hardly ever exposed to end-users anyway;
     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 notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
    46   val init: serial option -> string -> operations -> Proof.context -> local_theory
    47   val restore: local_theory -> local_theory
    48   val reinit: local_theory -> local_theory
    49   val exit: local_theory -> Proof.context
    50   val exit_global: local_theory -> theory
    51   val exit_result: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * Proof.context
    52   val exit_result_global: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * theory
    53 end;
    54 
    55 structure Local_Theory: LOCAL_THEORY =
    56 struct
    57 
    58 (** local theory data **)
    59 
    60 (* type lthy *)
    61 
    62 type operations =
    63  {pretty: local_theory -> Pretty.T list,
    64   abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
    65     (term * term) * local_theory,
    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   type_syntax: bool -> declaration -> local_theory -> local_theory,
    72   term_syntax: bool -> declaration -> local_theory -> local_theory,
    73   declaration: bool -> declaration -> local_theory -> local_theory,
    74   reinit: local_theory -> local_theory,
    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 (ProofContext.theory_of lthy);
   136     val lthy' = lthy
   137       |> map_target (ProofContext.transfer thy')
   138       |> ProofContext.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 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 
   152 fun theory f = #2 o theory_result (f #> pair ());
   153 
   154 fun target_result f lthy =
   155   let
   156     val (res, ctxt') = target_of lthy
   157       |> Context_Position.set_visible false
   158       |> f
   159       ||> Context_Position.restore_visible lthy;
   160     val thy' = ProofContext.theory_of ctxt';
   161     val lthy' = lthy
   162       |> map_target (K ctxt')
   163       |> ProofContext.transfer thy';
   164   in (res, lthy') end;
   165 
   166 fun target f = #2 o target_result (f #> pair ());
   167 
   168 fun map_contexts f =
   169   theory (Context.theory_map f) #>
   170   target (Context.proof_map f) #>
   171   Context.proof_map f;
   172 
   173 
   174 (* morphisms *)
   175 
   176 fun standard_morphism lthy ctxt =
   177   ProofContext.norm_export_morphism lthy ctxt $>
   178   Morphism.binding_morphism (Name_Space.transform_binding (naming_of lthy));
   179 
   180 fun target_morphism lthy = standard_morphism lthy (target_of lthy);
   181 fun global_morphism lthy = standard_morphism lthy (ProofContext.init (ProofContext.theory_of lthy));
   182 
   183 
   184 (* basic operations *)
   185 
   186 fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
   187 fun operation1 f x = operation (fn ops => f ops x);
   188 fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
   189 
   190 val pretty = operation #pretty;
   191 val abbrev = apsnd checkpoint ooo operation2 #abbrev;
   192 val define = apsnd checkpoint oo operation1 #define;
   193 val notes_kind = apsnd checkpoint ooo operation2 #notes;
   194 val type_syntax = checkpoint ooo operation2 #type_syntax;
   195 val term_syntax = checkpoint ooo operation2 #term_syntax;
   196 val declaration = checkpoint ooo operation2 #declaration;
   197 
   198 val notes = notes_kind "";
   199 fun note (a, ths) = notes [(a, [(ths, [])])] #>> the_single;
   200 
   201 fun notation add mode raw_args lthy =
   202   let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
   203   in term_syntax false (ProofContext.target_notation add mode args) lthy end;
   204 
   205 
   206 (* init *)
   207 
   208 fun init group theory_prefix operations target =
   209   let val naming =
   210     Sign.naming_of (ProofContext.theory_of target)
   211     |> Name_Space.set_group group
   212     |> Name_Space.mandatory_path theory_prefix;
   213   in
   214     target |> Data.map
   215       (fn NONE => SOME (make_lthy (naming, theory_prefix, operations, target))
   216         | SOME _ => error "Local theory already initialized")
   217     |> checkpoint
   218   end;
   219 
   220 fun restore lthy =
   221   let val {naming, theory_prefix, operations, target} = get_lthy lthy
   222   in init (Name_Space.get_group naming) theory_prefix operations target end;
   223 
   224 val reinit = checkpoint o operation #reinit;
   225 
   226 
   227 (* exit *)
   228 
   229 val exit = ProofContext.theory Theory.checkpoint o operation #exit;
   230 val exit_global = ProofContext.theory_of o exit;
   231 
   232 fun exit_result f (x, lthy) =
   233   let
   234     val ctxt = exit lthy;
   235     val phi = standard_morphism lthy ctxt;
   236   in (f phi x, ctxt) end;
   237 
   238 fun exit_result_global f (x, lthy) =
   239   let
   240     val thy = exit_global lthy;
   241     val thy_ctxt = ProofContext.init thy;
   242     val phi = standard_morphism lthy thy_ctxt;
   243   in (f phi x, thy) end;
   244 
   245 end;