src/Pure/Isar/local_theory.ML
author wenzelm
Wed Sep 03 17:47:40 2008 +0200 (2008-09-03 ago)
changeset 28115 cd0d170d4dc6
parent 28084 a05ca48ef263
child 28379 0de8a35b866a
permissions -rw-r--r--
discontinued local axioms -- too difficult to implement, too easy to produce nonsense;
     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 full_naming: local_theory -> NameSpace.naming
    21   val full_name: local_theory -> bstring -> string
    22   val theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
    23   val theory: (theory -> theory) -> local_theory -> local_theory
    24   val target_result: (Proof.context -> 'a * Proof.context) -> local_theory -> 'a * local_theory
    25   val target: (Proof.context -> Proof.context) -> local_theory -> local_theory
    26   val affirm: local_theory -> local_theory
    27   val pretty: local_theory -> Pretty.T list
    28   val abbrev: Syntax.mode -> (Name.binding * mixfix) * term -> local_theory ->
    29     (term * term) * local_theory
    30   val define: string -> (Name.binding * mixfix) * (Attrib.binding * term) -> local_theory ->
    31     (term * (string * thm)) * local_theory
    32   val note: string -> Attrib.binding * thm list -> local_theory -> (string * thm list) * local_theory
    33   val notes: string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
    34     local_theory -> (string * thm list) list * local_theory
    35   val type_syntax: declaration -> local_theory -> local_theory
    36   val term_syntax: declaration -> local_theory -> local_theory
    37   val declaration: declaration -> local_theory -> local_theory
    38   val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
    39   val target_morphism: local_theory -> morphism
    40   val init: string -> operations -> Proof.context -> local_theory
    41   val restore: local_theory -> local_theory
    42   val reinit: local_theory -> local_theory
    43   val exit: local_theory -> Proof.context
    44 end;
    45 
    46 structure LocalTheory: LOCAL_THEORY =
    47 struct
    48 
    49 (** local theory data **)
    50 
    51 (* type lthy *)
    52 
    53 type operations =
    54  {pretty: local_theory -> Pretty.T list,
    55   abbrev: Syntax.mode -> (Name.binding * mixfix) * term -> local_theory ->
    56     (term * term) * local_theory,
    57   define: string ->
    58     (Name.binding * mixfix) * (Attrib.binding * term) -> local_theory ->
    59     (term * (string * thm)) * local_theory,
    60   notes: string ->
    61     (Attrib.binding * (thm list * Attrib.src list) list) list ->
    62     local_theory -> (string * thm list) list * local_theory,
    63   type_syntax: declaration -> local_theory -> local_theory,
    64   term_syntax: declaration -> local_theory -> local_theory,
    65   declaration: declaration -> local_theory -> local_theory,
    66   reinit: local_theory -> local_theory,
    67   exit: local_theory -> Proof.context};
    68 
    69 datatype lthy = LThy of
    70  {group: string,
    71   theory_prefix: string,
    72   operations: operations,
    73   target: Proof.context};
    74 
    75 fun make_lthy (group, theory_prefix, operations, target) =
    76   LThy {group = group, theory_prefix = theory_prefix, operations = operations, target = target};
    77 
    78 
    79 (* context data *)
    80 
    81 structure Data = ProofDataFun
    82 (
    83   type T = lthy option;
    84   fun init _ = NONE;
    85 );
    86 
    87 fun get_lthy lthy =
    88   (case Data.get lthy of
    89     NONE => error "No local theory context"
    90   | SOME (LThy data) => data);
    91 
    92 fun map_lthy f lthy =
    93   let val {group, theory_prefix, operations, target} = get_lthy lthy
    94   in Data.put (SOME (make_lthy (f (group, theory_prefix, operations, target)))) lthy end;
    95 
    96 
    97 (* group *)
    98 
    99 val group_of = #group o get_lthy;
   100 
   101 fun group_properties_of lthy =
   102   (case group_of lthy of
   103     "" => []
   104   | group => [(Markup.groupN, group)]);
   105 
   106 fun group_position_of lthy =
   107   group_properties_of lthy @ Position.properties_of (Position.thread_data ());
   108 
   109 fun set_group group = map_lthy (fn (_, theory_prefix, operations, target) =>
   110   (group, theory_prefix, operations, target));
   111 
   112 
   113 (* target *)
   114 
   115 val target_of = #target o get_lthy;
   116 val affirm = tap target_of;
   117 
   118 fun map_target f = map_lthy (fn (group, theory_prefix, operations, target) =>
   119   (group, theory_prefix, operations, f target));
   120 
   121 
   122 (* substructure mappings *)
   123 
   124 fun raw_theory_result f lthy =
   125   let
   126     val (res, thy') = f (ProofContext.theory_of lthy);
   127     val lthy' = lthy
   128       |> map_target (ProofContext.transfer thy')
   129       |> ProofContext.transfer thy';
   130   in (res, lthy') end;
   131 
   132 fun raw_theory f = #2 o raw_theory_result (f #> pair ());
   133 
   134 fun full_naming lthy =
   135   Sign.naming_of (ProofContext.theory_of lthy)
   136   |> NameSpace.sticky_prefix (#theory_prefix (get_lthy lthy))
   137   |> NameSpace.qualified_names;
   138 
   139 val full_name = NameSpace.full o full_naming;
   140 
   141 fun theory_result f lthy = lthy |> raw_theory_result (fn thy => thy
   142   |> Sign.sticky_prefix (#theory_prefix (get_lthy lthy))
   143   |> Sign.qualified_names
   144   |> f
   145   ||> Sign.restore_naming thy);
   146 
   147 fun theory f = #2 o theory_result (f #> pair ());
   148 
   149 fun target_result f lthy =
   150   let
   151     val (res, ctxt') = f (target_of lthy);
   152     val thy' = ProofContext.theory_of ctxt';
   153     val lthy' = lthy
   154       |> map_target (K ctxt')
   155       |> ProofContext.transfer thy';
   156   in (res, lthy') end;
   157 
   158 fun target f = #2 o target_result (f #> pair ());
   159 
   160 
   161 (* basic operations *)
   162 
   163 fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
   164 fun operation1 f x = operation (fn ops => f ops x);
   165 fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
   166 
   167 val pretty = operation #pretty;
   168 val abbrev = operation2 #abbrev;
   169 val define = operation2 #define;
   170 val notes = operation2 #notes;
   171 val type_syntax = operation1 #type_syntax;
   172 val term_syntax = operation1 #term_syntax;
   173 val declaration = operation1 #declaration;
   174 
   175 fun note kind (a, ths) = notes kind [(a, [(ths, [])])] #>> the_single;
   176 
   177 fun target_morphism lthy =
   178   ProofContext.export_morphism lthy (target_of lthy) $>
   179   Morphism.thm_morphism Goal.norm_result;
   180 
   181 fun notation add mode raw_args lthy =
   182   let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
   183   in term_syntax (ProofContext.target_notation add mode args) lthy end;
   184 
   185 
   186 (* init -- exit *)
   187 
   188 fun init theory_prefix operations target = target |> Data.map
   189   (fn NONE => SOME (make_lthy ("", theory_prefix, operations, target))
   190     | SOME _ => error "Local theory already initialized");
   191 
   192 fun restore lthy =
   193   let val {group = _, theory_prefix, operations, target} = get_lthy lthy
   194   in init theory_prefix operations target end;
   195 
   196 val reinit = operation #reinit;
   197 val exit = operation #exit;
   198 
   199 end;