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