src/Pure/Isar/object_logic.ML
author paulson
Fri May 14 16:48:37 2004 +0200 (2004-05-14)
changeset 14743 81001d6cb8c0
parent 14226 7afe0e5bcc83
child 14854 61bdf2ae4dc5
permissions -rw-r--r--
conversion of theorems to atomic form
     1 (*  Title:      Pure/Isar/object_logic.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Specifics about common object-logics.
     7 *)
     8 
     9 signature OBJECT_LOGIC =
    10 sig
    11   val add_judgment: bstring * string * mixfix -> theory -> theory
    12   val add_judgment_i: bstring * typ * mixfix -> theory -> theory
    13   val judgment_name: Sign.sg -> string
    14   val is_judgment: Sign.sg -> term -> bool
    15   val drop_judgment: Sign.sg -> term -> term
    16   val fixed_judgment: Sign.sg -> string -> term
    17   val assert_propT: Sign.sg -> term -> term
    18   val declare_atomize: theory attribute
    19   val declare_rulify: theory attribute
    20   val atomize_term: Sign.sg -> term -> term
    21   val atomize_thm: thm -> thm
    22   val atomize_rule: Sign.sg -> cterm -> thm
    23   val atomize_tac: int -> tactic
    24   val full_atomize_tac: int -> tactic
    25   val atomize_goal: int -> thm -> thm
    26   val rulify: thm -> thm
    27   val rulify_no_asm: thm -> thm
    28   val rule_format: 'a attribute
    29   val rule_format_no_asm: 'a attribute
    30   val setup: (theory -> theory) list
    31 end;
    32 
    33 structure ObjectLogic: OBJECT_LOGIC =
    34 struct
    35 
    36 
    37 (** object-logic theory data **)
    38 
    39 (* data kind 'Pure/object-logic' *)
    40 
    41 structure ObjectLogicDataArgs =
    42 struct
    43   val name = "Pure/object-logic";
    44   type T = string option * (thm list * thm list);
    45 
    46   val empty = (None, ([], [Drule.norm_hhf_eq]));
    47   val copy = I;
    48   val prep_ext = I;
    49 
    50   fun merge_judgment (Some x, Some y) =
    51         if x = y then Some x else error "Attempt to merge different object-logics"
    52     | merge_judgment (j1, j2) = if is_some j1 then j1 else j2;
    53 
    54   fun merge ((judgment1, (atomize1, rulify1)), (judgment2, (atomize2, rulify2))) =
    55     (merge_judgment (judgment1, judgment2),
    56       (Drule.merge_rules (atomize1, atomize2), Drule.merge_rules (rulify1, rulify2)));
    57 
    58   fun print _ _ = ();
    59 end;
    60 
    61 structure ObjectLogicData = TheoryDataFun(ObjectLogicDataArgs);
    62 
    63 
    64 (** generic treatment of judgments -- with a single argument only **)
    65 
    66 (* add_judgment(_i) *)
    67 
    68 local
    69 
    70 fun new_judgment name (None, rules) = (Some name, rules)
    71   | new_judgment _ (Some _, _) = error "Attempt to redeclare object-logic judgment";
    72 
    73 fun add_final name thy =
    74   let
    75     val typ = case Sign.const_type (sign_of thy) name of
    76 		Some T => T
    77 	      | None => error "Internal error in ObjectLogic.gen_add_judgment";
    78   in
    79     Theory.add_finals_i false [Const(name,typ)] thy
    80   end;
    81 
    82 fun gen_add_judgment add_consts (name, T, syn) thy =
    83   let
    84     val fullname = Sign.full_name (Theory.sign_of thy) name;
    85   in
    86     thy
    87     |> add_consts [(name, T, syn)]
    88     |> add_final fullname
    89     |> ObjectLogicData.map (new_judgment fullname)
    90   end;
    91 
    92 in
    93 
    94 val add_judgment = gen_add_judgment Theory.add_consts;
    95 val add_judgment_i = gen_add_judgment Theory.add_consts_i;
    96 
    97 end;
    98 
    99 
   100 (* term operations *)
   101 
   102 fun judgment_name sg =
   103   (case ObjectLogicData.get_sg sg of
   104     (Some name, _) => name
   105   | _ => raise TERM ("Unknown object-logic judgment", []));
   106 
   107 fun is_judgment sg (Const (c, _) $ _) = c = judgment_name sg
   108   | is_judgment _ _ = false;
   109 
   110 fun drop_judgment sg (Abs (x, T, t)) = Abs (x, T, drop_judgment sg t)
   111   | drop_judgment sg (tm as (Const (c, _) $ t)) =
   112       if (c = judgment_name sg handle TERM _ => false) then t else tm
   113   | drop_judgment _ tm = tm;
   114 
   115 fun fixed_judgment sg x =
   116   let  (*be robust wrt. low-level errors*)
   117     val c = judgment_name sg;
   118     val aT = TFree ("'a", logicS);
   119     val T =
   120       if_none (Sign.const_type sg c) (aT --> propT)
   121       |> Term.map_type_tvar (fn ((x, _), S) => TFree (x, S));
   122     val U = Term.domain_type T handle Match => aT;
   123   in Const (c, T) $ Free (x, U) end;
   124 
   125 fun assert_propT sg t =
   126   let val T = Term.fastype_of t
   127   in if T = propT then t else Const (judgment_name sg, T --> propT) $ t end;
   128 
   129 
   130 
   131 (** treatment of meta-level connectives **)
   132 
   133 (* maintain rules *)
   134 
   135 val get_atomize = #1 o #2 o ObjectLogicData.get_sg;
   136 val get_rulify = #2 o #2 o ObjectLogicData.get_sg;
   137 
   138 val add_atomize = ObjectLogicData.map o Library.apsnd o Library.apfst o Drule.add_rule;
   139 val add_rulify = ObjectLogicData.map o Library.apsnd o Library.apsnd o Drule.add_rule;
   140 
   141 fun declare_atomize (thy, th) = (add_atomize th thy, th);
   142 fun declare_rulify (thy, th) = (add_rulify th thy, th);
   143 
   144 
   145 (* atomize *)
   146 
   147 fun rewrite_prems_tac rews i = PRIMITIVE (MetaSimplifier.fconv_rule
   148   (MetaSimplifier.goals_conv (Library.equal i)
   149     (MetaSimplifier.forall_conv
   150       (MetaSimplifier.goals_conv (K true) (Tactic.rewrite true rews)))));
   151 
   152 fun atomize_term sg =
   153   drop_judgment sg o MetaSimplifier.rewrite_term sg (get_atomize sg) [];
   154 
   155 fun atomize_rule sg = Tactic.rewrite true (get_atomize sg);
   156 
   157 (*Convert a natural-deduction rule into a formula (probably in FOL)*)
   158 fun atomize_thm th =
   159   rewrite_rule  (get_atomize (Thm.sign_of_thm th)) th;
   160 
   161 fun atomize_tac i st =
   162   if Logic.has_meta_prems (Thm.prop_of st) i then
   163     (rewrite_prems_tac (get_atomize (Thm.sign_of_thm st)) i) st
   164   else all_tac st;
   165 
   166 fun full_atomize_tac i st =
   167   rewrite_goal_tac (get_atomize (Thm.sign_of_thm st)) i st;
   168 
   169 fun atomize_goal i st =
   170   (case Seq.pull (atomize_tac i st) of None => st | Some (st', _) => st');
   171 
   172 
   173 (* rulify *)
   174 
   175 fun gen_rulify full thm =
   176   Tactic.simplify full (get_rulify (Thm.sign_of_thm thm)) thm
   177   |> Drule.gen_all |> Drule.strip_shyps_warning |> Drule.zero_var_indexes;
   178 
   179 val rulify = gen_rulify true;
   180 val rulify_no_asm = gen_rulify false;
   181 
   182 fun rule_format x = Drule.rule_attribute (fn _ => rulify) x;
   183 fun rule_format_no_asm x = Drule.rule_attribute (fn _ => rulify_no_asm) x;
   184 
   185 
   186 
   187 (** theory setup **)
   188 
   189 val setup = [ObjectLogicData.init];
   190 
   191 
   192 end;