src/Pure/Isar/local_theory.ML
author wenzelm
Sun Jan 28 23:29:17 2007 +0100 (2007-01-28 ago)
changeset 22203 efc0cdc01307
parent 21860 c4492c6bf450
child 22240 36cc1875619f
permissions -rw-r--r--
added interface for target_naming;
     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_naming: local_theory -> NameSpace.naming
    43   val target_name: local_theory -> bstring -> string
    44   val init: string -> operations -> Proof.context -> local_theory
    45   val restore: local_theory -> local_theory
    46   val reinit: local_theory -> theory -> local_theory
    47   val exit: local_theory -> Proof.context
    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   consts: (string * typ -> bool) -> ((bstring * typ) * mixfix) list -> local_theory ->
    60     (term * thm) list * local_theory,
    61   axioms: string -> ((bstring * Attrib.src list) * term list) list -> local_theory ->
    62     (bstring * thm list) list * local_theory,
    63   abbrev: Syntax.mode -> (bstring * mixfix) * term -> local_theory -> (term * term) * local_theory,
    64   defs: string -> ((bstring * mixfix) * ((bstring * Attrib.src list) * term)) list ->
    65     local_theory -> (term * (bstring * thm)) list * local_theory,
    66   notes: string ->
    67     ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    68     local_theory -> (bstring * thm list) list * local_theory,
    69   type_syntax: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory,
    70   term_syntax: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory,
    71   declaration: (morphism -> Context.generic -> Context.generic) -> local_theory -> local_theory,
    72   target_morphism: local_theory -> morphism,
    73   target_naming: local_theory -> NameSpace.naming,
    74   reinit: local_theory -> theory -> local_theory,
    75   exit: local_theory -> Proof.context};
    76 
    77 datatype lthy = LThy of
    78  {theory_prefix: string,
    79   operations: operations,
    80   target: Proof.context};
    81 
    82 fun make_lthy (theory_prefix, operations, target) =
    83   LThy {theory_prefix = theory_prefix, operations = operations, target = target};
    84 
    85 
    86 (* context data *)
    87 
    88 structure Data = ProofDataFun
    89 (
    90   val name = "Pure/local_theory";
    91   type T = lthy option;
    92   fun init _ = NONE;
    93   fun print _ _ = ();
    94 );
    95 val _ = Context.add_setup Data.init;
    96 
    97 fun get_lthy lthy =
    98   (case Data.get lthy of
    99     NONE => error "No local theory context"
   100   | SOME (LThy data) => data);
   101 
   102 fun map_lthy f lthy =
   103   let val {theory_prefix, operations, target} = get_lthy lthy
   104   in Data.put (SOME (make_lthy (f (theory_prefix, operations, target)))) lthy end;
   105 
   106 val target_of = #target o get_lthy;
   107 val affirm = tap target_of;
   108 
   109 fun map_target f = map_lthy (fn (theory_prefix, operations, target) =>
   110   (theory_prefix, operations, f target));
   111 
   112 
   113 (* substructure mappings *)
   114 
   115 fun raw_theory_result f lthy =
   116   let
   117     val (res, thy') = f (ProofContext.theory_of lthy);
   118     val lthy' = lthy
   119       |> map_target (ProofContext.transfer thy')
   120       |> ProofContext.transfer thy';
   121   in (res, lthy') end;
   122 
   123 fun raw_theory f = #2 o raw_theory_result (f #> pair ());
   124 
   125 fun full_name lthy =
   126   let
   127     val naming = Sign.naming_of (ProofContext.theory_of lthy)
   128       |> NameSpace.sticky_prefix (#theory_prefix (get_lthy lthy))
   129       |> NameSpace.qualified_names;
   130   in NameSpace.full naming end;
   131 
   132 fun theory_result f lthy = lthy |> raw_theory_result (fn thy => thy
   133   |> Sign.sticky_prefix (#theory_prefix (get_lthy lthy))
   134   |> Sign.qualified_names
   135   |> f
   136   ||> Sign.restore_naming thy);
   137 
   138 fun theory f = #2 o theory_result (f #> pair ());
   139 
   140 fun target_result f lthy =
   141   let
   142     val (res, ctxt') = f (#target (get_lthy lthy));
   143     val thy' = ProofContext.theory_of ctxt';
   144     val lthy' = lthy
   145       |> map_target (K ctxt')
   146       |> ProofContext.transfer thy';
   147   in (res, lthy') end;
   148 
   149 fun target f = #2 o target_result (f #> pair ());
   150 
   151 
   152 (* basic operations *)
   153 
   154 fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
   155 fun operation1 f x = operation (fn ops => f ops x);
   156 fun operation2 f x y = operation (fn ops => f ops x y);
   157 
   158 val pretty = operation #pretty;
   159 val consts = operation2 #consts;
   160 val axioms = operation2 #axioms;
   161 val abbrev = operation2 #abbrev;
   162 val defs = operation2 #defs;
   163 val notes = operation2 #notes;
   164 val type_syntax = operation1 #type_syntax;
   165 val term_syntax = operation1 #term_syntax;
   166 val declaration = operation1 #declaration;
   167 val target_morphism = operation #target_morphism;
   168 val target_naming = operation #target_naming;
   169 
   170 fun def kind arg lthy = lthy |> defs kind [arg] |>> hd;
   171 fun note kind (a, ths) lthy = lthy |> notes kind [(a, [(ths, [])])] |>> hd;
   172 
   173 fun notation mode raw_args lthy =
   174   let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
   175   in term_syntax (ProofContext.target_notation mode args) lthy end;
   176 
   177 val target_name = NameSpace.full o target_naming;
   178 
   179 
   180 (* init -- exit *)
   181 
   182 fun init theory_prefix operations target = target |> Data.map
   183   (fn NONE => SOME (make_lthy (theory_prefix, operations, target))
   184     | SOME _ => error "Local theory already initialized");
   185 
   186 fun restore lthy =
   187   let val {theory_prefix, operations, target} = get_lthy lthy
   188   in init theory_prefix operations target end;
   189 
   190 val reinit = operation #reinit;
   191 val exit = operation #exit;
   192 
   193 end;