src/Pure/Isar/object_logic.ML
author wenzelm
Fri Apr 08 16:34:14 2011 +0200 (2011-04-08)
changeset 42290 b1f544c84040
parent 41581 72a02e3dec7e
child 42360 da8817d01e7c
permissions -rw-r--r--
discontinued special treatment of structure Lexicon;
     1 (*  Title:      Pure/Isar/object_logic.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Specifics about common object-logics.
     5 *)
     6 
     7 signature OBJECT_LOGIC =
     8 sig
     9   val get_base_sort: theory -> sort option
    10   val add_base_sort: sort -> theory -> theory
    11   val add_judgment: binding * typ * mixfix -> theory -> theory
    12   val add_judgment_cmd: binding * string * mixfix -> theory -> theory
    13   val judgment_name: theory -> string
    14   val is_judgment: theory -> term -> bool
    15   val drop_judgment: theory -> term -> term
    16   val fixed_judgment: theory -> string -> term
    17   val ensure_propT: theory -> term -> term
    18   val dest_judgment: cterm -> cterm
    19   val judgment_conv: conv -> conv
    20   val elim_concl: thm -> term option
    21   val declare_atomize: attribute
    22   val declare_rulify: attribute
    23   val atomize_term: theory -> term -> term
    24   val atomize: conv
    25   val atomize_prems: conv
    26   val atomize_prems_tac: int -> tactic
    27   val full_atomize_tac: int -> tactic
    28   val rulify_term: theory -> term -> term
    29   val rulify_tac: int -> tactic
    30   val rulify: thm -> thm
    31   val rulify_no_asm: thm -> thm
    32   val rule_format: attribute
    33   val rule_format_no_asm: attribute
    34 end;
    35 
    36 structure Object_Logic: OBJECT_LOGIC =
    37 struct
    38 
    39 (** theory data **)
    40 
    41 datatype data = Data of
    42  {base_sort: sort option,
    43   judgment: string option,
    44   atomize_rulify: thm list * thm list};
    45 
    46 fun make_data (base_sort, judgment, atomize_rulify) =
    47   Data {base_sort = base_sort, judgment = judgment, atomize_rulify = atomize_rulify};
    48 
    49 structure Data = Theory_Data
    50 (
    51   type T = data;
    52   val empty = make_data (NONE, NONE, ([], []));
    53   val extend = I;
    54 
    55   fun merge_opt eq (SOME x, SOME y) =
    56         if eq (x, y) then SOME x else error "Attempt to merge different object-logics"
    57     | merge_opt _ data = merge_options data;
    58 
    59   fun merge
    60      (Data {base_sort = base_sort1, judgment = judgment1, atomize_rulify = (atomize1, rulify1)},
    61       Data {base_sort = base_sort2, judgment = judgment2, atomize_rulify = (atomize2, rulify2)}) =
    62     make_data (merge_opt (op =) (base_sort1, base_sort2), merge_opt (op =) (judgment1, judgment2),
    63       (Thm.merge_thms (atomize1, atomize2), Thm.merge_thms (rulify1, rulify2)));
    64 );
    65 
    66 fun map_data f = Data.map (fn (Data {base_sort, judgment, atomize_rulify}) =>
    67   make_data (f (base_sort, judgment, atomize_rulify)));
    68 
    69 fun get_data thy = Data.get thy |> (fn Data args => args);
    70 
    71 
    72 
    73 (** generic treatment of judgments -- with a single argument only **)
    74 
    75 (* base_sort *)
    76 
    77 val get_base_sort = #base_sort o get_data;
    78 
    79 fun add_base_sort S = map_data (fn (base_sort, judgment, atomize_rulify) =>
    80   if is_some base_sort then error "Attempt to redeclare object-logic base sort"
    81   else (SOME S, judgment, atomize_rulify));
    82 
    83 
    84 (* add judgment *)
    85 
    86 local
    87 
    88 fun gen_add_judgment add_consts (b, T, mx) thy =
    89   let val c = Sign.full_name thy b in
    90     thy
    91     |> add_consts [(b, T, mx)]
    92     |> (fn thy' => Theory.add_deps c (c, Sign.the_const_type thy' c) [] thy')
    93     |> map_data (fn (base_sort, judgment, atomize_rulify) =>
    94         if is_some judgment then error "Attempt to redeclare object-logic judgment"
    95         else (base_sort, SOME c, atomize_rulify))
    96   end;
    97 
    98 in
    99 
   100 val add_judgment = gen_add_judgment Sign.add_consts_i;
   101 val add_judgment_cmd = gen_add_judgment Sign.add_consts;
   102 
   103 end;
   104 
   105 
   106 (* judgments *)
   107 
   108 fun judgment_name thy =
   109   (case #judgment (get_data thy) of
   110     SOME name => name
   111   | _ => raise TERM ("Unknown object-logic judgment", []));
   112 
   113 fun is_judgment thy (Const (c, _) $ _) = c = judgment_name thy
   114   | is_judgment _ _ = false;
   115 
   116 fun drop_judgment thy (Abs (x, T, t)) = Abs (x, T, drop_judgment thy t)
   117   | drop_judgment thy (tm as (Const (c, _) $ t)) =
   118       if (c = judgment_name thy handle TERM _ => false) then t else tm
   119   | drop_judgment _ tm = tm;
   120 
   121 fun fixed_judgment thy x =
   122   let  (*be robust wrt. low-level errors*)
   123     val c = judgment_name thy;
   124     val aT = TFree (Name.aT, []);
   125     val T =
   126       the_default (aT --> propT) (Sign.const_type thy c)
   127       |> Term.map_type_tvar (fn ((x, _), S) => TFree (x, S));
   128     val U = Term.domain_type T handle Match => aT;
   129   in Const (c, T) $ Free (x, U) end;
   130 
   131 fun ensure_propT thy t =
   132   let val T = Term.fastype_of t
   133   in if T = propT then t else Const (judgment_name thy, T --> propT) $ t end;
   134 
   135 fun dest_judgment ct =
   136   if is_judgment (Thm.theory_of_cterm ct) (Thm.term_of ct)
   137   then Thm.dest_arg ct
   138   else raise CTERM ("dest_judgment", [ct]);
   139 
   140 fun judgment_conv cv ct =
   141   if is_judgment (Thm.theory_of_cterm ct) (Thm.term_of ct)
   142   then Conv.arg_conv cv ct
   143   else raise CTERM ("judgment_conv", [ct]);
   144 
   145 
   146 (* elimination rules *)
   147 
   148 fun elim_concl rule =
   149   let
   150     val thy = Thm.theory_of_thm rule;
   151     val concl = Thm.concl_of rule;
   152     val C = drop_judgment thy concl;
   153   in
   154     if Term.is_Var C andalso
   155       exists (fn prem => concl aconv Logic.strip_assums_concl prem) (Thm.prems_of rule)
   156     then SOME C else NONE
   157   end;
   158 
   159 
   160 
   161 (** treatment of meta-level connectives **)
   162 
   163 (* maintain rules *)
   164 
   165 val get_atomize = #1 o #atomize_rulify o get_data;
   166 val get_rulify = #2 o #atomize_rulify o get_data;
   167 
   168 fun add_atomize th = map_data (fn (base_sort, judgment, (atomize, rulify)) =>
   169   (base_sort, judgment, (Thm.add_thm th atomize, rulify)));
   170 
   171 fun add_rulify th = map_data (fn (base_sort, judgment, (atomize, rulify)) =>
   172   (base_sort, judgment, (atomize, Thm.add_thm th rulify)));
   173 
   174 val declare_atomize = Thm.declaration_attribute (fn th => Context.mapping (add_atomize th) I);
   175 val declare_rulify = Thm.declaration_attribute (fn th => Context.mapping (add_rulify th) I);
   176 
   177 val _ = Context.>> (Context.map_theory (fold add_rulify Drule.norm_hhf_eqs));
   178 
   179 
   180 (* atomize *)
   181 
   182 fun atomize_term thy =
   183   drop_judgment thy o Raw_Simplifier.rewrite_term thy (get_atomize thy) [];
   184 
   185 fun atomize ct =
   186   Raw_Simplifier.rewrite true (get_atomize (Thm.theory_of_cterm ct)) ct;
   187 
   188 fun atomize_prems ct =
   189   if Logic.has_meta_prems (Thm.term_of ct) then
   190     Conv.params_conv ~1 (K (Conv.prems_conv ~1 atomize))
   191       (ProofContext.init_global (Thm.theory_of_cterm ct)) ct
   192   else Conv.all_conv ct;
   193 
   194 val atomize_prems_tac = CONVERSION atomize_prems;
   195 val full_atomize_tac = CONVERSION atomize;
   196 
   197 
   198 (* rulify *)
   199 
   200 fun rulify_term thy = Raw_Simplifier.rewrite_term thy (get_rulify thy) [];
   201 fun rulify_tac i st = rewrite_goal_tac (get_rulify (Thm.theory_of_thm st)) i st;
   202 
   203 fun gen_rulify full thm =
   204   Raw_Simplifier.simplify full (get_rulify (Thm.theory_of_thm thm)) thm
   205   |> Drule.gen_all |> Thm.strip_shyps |> Drule.zero_var_indexes;
   206 
   207 val rulify = gen_rulify true;
   208 val rulify_no_asm = gen_rulify false;
   209 
   210 fun rule_format x = Thm.rule_attribute (fn _ => rulify) x;
   211 fun rule_format_no_asm x = Thm.rule_attribute (fn _ => rulify_no_asm) x;
   212 
   213 end;