src/Pure/Isar/local_theory.ML
author wenzelm
Sun Jul 08 19:51:58 2007 +0200 (2007-07-08)
changeset 23655 d2d1138e0ddc
parent 22846 fb79144af9a3
child 24020 ed4d7abffee7
permissions -rw-r--r--
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
     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 target_of: local_theory -> Proof.context
    14   val raw_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
    15   val raw_theory: (theory -> theory) -> local_theory -> local_theory
    16   val full_naming: local_theory -> NameSpace.naming
    17   val full_name: local_theory -> bstring -> string
    18   val theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
    19   val theory: (theory -> theory) -> local_theory -> local_theory
    20   val target_result: (Proof.context -> 'a * Proof.context) -> local_theory -> 'a * local_theory
    21   val target: (Proof.context -> Proof.context) -> local_theory -> local_theory
    22   val affirm: local_theory -> local_theory
    23   val pretty: local_theory -> Pretty.T list
    24   val consts: (string * typ -> bool) ->
    25     ((bstring * typ) * mixfix) list -> local_theory -> (term * thm) list * local_theory
    26   val axioms: string -> ((bstring * Attrib.src list) * term list) list -> local_theory ->
    27     (bstring * thm list) list * local_theory
    28   val abbrev: Syntax.mode -> (bstring * mixfix) * term -> local_theory ->
    29     (term * term) * local_theory
    30   val defs: string -> ((bstring * mixfix) * ((bstring * Attrib.src list) * term)) list ->
    31     local_theory -> (term * (bstring * thm)) list * local_theory
    32   val notes: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    33     local_theory -> (bstring * thm list) list * local_theory
    34   val type_syntax: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory
    35   val term_syntax: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory
    36   val declaration: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory
    37   val def: string -> (bstring * mixfix) * ((bstring * Attrib.src list) * term) ->
    38     local_theory -> (term * (bstring * thm)) * local_theory
    39   val note: string -> (bstring * Attrib.src list) * thm list ->
    40     local_theory -> (bstring * thm list) * Proof.context
    41   val notation: Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
    42   val target_morphism: local_theory -> morphism
    43   val target_naming: local_theory -> NameSpace.naming
    44   val target_name: local_theory -> bstring -> string
    45   val init: string -> operations -> Proof.context -> local_theory
    46   val restore: local_theory -> local_theory
    47   val reinit: local_theory -> theory -> local_theory
    48   val exit: local_theory -> Proof.context
    49 end;
    50 
    51 structure LocalTheory: LOCAL_THEORY =
    52 struct
    53 
    54 (** local theory data **)
    55 
    56 (* type lthy *)
    57 
    58 type operations =
    59  {pretty: local_theory -> Pretty.T list,
    60   consts: (string * typ -> bool) -> ((bstring * typ) * mixfix) list -> local_theory ->
    61     (term * thm) list * local_theory,
    62   axioms: string -> ((bstring * Attrib.src list) * term list) list -> local_theory ->
    63     (bstring * thm list) list * local_theory,
    64   abbrev: Syntax.mode -> (bstring * mixfix) * term -> local_theory -> (term * term) * local_theory,
    65   defs: string -> ((bstring * mixfix) * ((bstring * Attrib.src list) * term)) list ->
    66     local_theory -> (term * (bstring * thm)) list * local_theory,
    67   notes: string ->
    68     ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    69     local_theory -> (bstring * thm list) list * local_theory,
    70   type_syntax: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory,
    71   term_syntax: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory,
    72   declaration: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory,
    73   target_morphism: local_theory -> morphism,
    74   target_naming: local_theory -> NameSpace.naming,
    75   reinit: local_theory -> theory -> local_theory,
    76   exit: local_theory -> Proof.context};
    77 
    78 datatype lthy = LThy of
    79  {theory_prefix: string,
    80   operations: operations,
    81   target: Proof.context};
    82 
    83 fun make_lthy (theory_prefix, operations, target) =
    84   LThy {theory_prefix = theory_prefix, operations = operations, target = target};
    85 
    86 
    87 (* context data *)
    88 
    89 structure Data = ProofDataFun
    90 (
    91   type T = lthy option;
    92   fun init _ = NONE;
    93 );
    94 
    95 fun get_lthy lthy =
    96   (case Data.get lthy of
    97     NONE => error "No local theory context"
    98   | SOME (LThy data) => data);
    99 
   100 fun map_lthy f lthy =
   101   let val {theory_prefix, operations, target} = get_lthy lthy
   102   in Data.put (SOME (make_lthy (f (theory_prefix, operations, target)))) lthy end;
   103 
   104 val target_of = #target o get_lthy;
   105 val affirm = tap target_of;
   106 
   107 fun map_target f = map_lthy (fn (theory_prefix, operations, target) =>
   108   (theory_prefix, operations, f target));
   109 
   110 
   111 (* substructure mappings *)
   112 
   113 fun raw_theory_result f lthy =
   114   let
   115     val (res, thy') = f (ProofContext.theory_of lthy);
   116     val lthy' = lthy
   117       |> map_target (ProofContext.transfer thy')
   118       |> ProofContext.transfer thy';
   119   in (res, lthy') end;
   120 
   121 fun raw_theory f = #2 o raw_theory_result (f #> pair ());
   122 
   123 fun full_naming lthy =
   124   Sign.naming_of (ProofContext.theory_of lthy)
   125   |> NameSpace.sticky_prefix (#theory_prefix (get_lthy lthy))
   126   |> NameSpace.qualified_names;
   127 
   128 val full_name = NameSpace.full o full_naming;
   129 
   130 fun theory_result f lthy = lthy |> raw_theory_result (fn thy => thy
   131   |> Sign.sticky_prefix (#theory_prefix (get_lthy lthy))
   132   |> Sign.qualified_names
   133   |> f
   134   ||> Sign.restore_naming thy);
   135 
   136 fun theory f = #2 o theory_result (f #> pair ());
   137 
   138 fun target_result f lthy =
   139   let
   140     val (res, ctxt') = f (#target (get_lthy lthy));
   141     val thy' = ProofContext.theory_of ctxt';
   142     val lthy' = lthy
   143       |> map_target (K ctxt')
   144       |> ProofContext.transfer thy';
   145   in (res, lthy') end;
   146 
   147 fun target f = #2 o target_result (f #> pair ());
   148 
   149 
   150 (* basic operations *)
   151 
   152 fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
   153 fun operation1 f x = operation (fn ops => f ops x);
   154 fun operation2 f x y = operation (fn ops => f ops x y);
   155 
   156 val pretty = operation #pretty;
   157 val consts = operation2 #consts;
   158 val axioms = operation2 #axioms;
   159 val abbrev = operation2 #abbrev;
   160 val defs = operation2 #defs;
   161 val notes = operation2 #notes;
   162 val type_syntax = operation1 #type_syntax;
   163 val term_syntax = operation1 #term_syntax;
   164 val declaration = operation1 #declaration;
   165 val target_morphism = operation #target_morphism;
   166 val target_naming = operation #target_naming;
   167 
   168 fun def kind arg lthy = lthy |> defs kind [arg] |>> hd;
   169 fun note kind (a, ths) lthy = lthy |> notes kind [(a, [(ths, [])])] |>> hd;
   170 
   171 fun notation mode raw_args lthy =
   172   let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
   173   in term_syntax (ProofContext.target_notation mode args) lthy end;
   174 
   175 val target_name = NameSpace.full o target_naming;
   176 
   177 
   178 (* init -- exit *)
   179 
   180 fun init theory_prefix operations target = target |> Data.map
   181   (fn NONE => SOME (make_lthy (theory_prefix, operations, target))
   182     | SOME _ => error "Local theory already initialized");
   183 
   184 fun restore lthy =
   185   let val {theory_prefix, operations, target} = get_lthy lthy
   186   in init theory_prefix operations target end;
   187 
   188 val reinit = operation #reinit;
   189 val exit = operation #exit;
   190 
   191 end;