src/Pure/conv.ML
author wenzelm
Thu May 31 18:31:36 2007 +0200 (2007-05-31)
changeset 23169 37091da05d8e
parent 23034 b3a6815754d6
child 23411 c524900454f3
permissions -rw-r--r--
moved aconvc to more_thm.ML;
     1 (*  Title:      Pure/conv.ML
     2     ID:         $Id$
     3     Author:     Amine Chaieb and Makarius
     4 
     5 Conversions: primitive equality reasoning.
     6 *)
     7 
     8 infix 1 then_conv;
     9 infix 0 else_conv;
    10 
    11 signature CONV =
    12 sig
    13   type conv = cterm -> thm
    14   val no_conv: conv
    15   val all_conv: conv
    16   val then_conv: conv * conv -> conv
    17   val else_conv: conv * conv -> conv
    18   val first_conv: conv list -> conv
    19   val every_conv: conv list -> conv
    20   val try_conv: conv -> conv
    21   val repeat_conv: conv -> conv
    22   val cache_conv: conv -> conv
    23   val abs_conv: conv -> conv
    24   val combination_conv: conv -> conv -> conv
    25   val comb_conv: conv -> conv
    26   val arg_conv: conv -> conv
    27   val fun_conv: conv -> conv
    28   val arg1_conv: conv -> conv
    29   val fun2_conv: conv -> conv
    30   val binop_conv: conv -> conv
    31   val forall_conv: int -> conv -> conv
    32   val concl_conv: int -> conv -> conv
    33   val prems_conv: int -> (int -> conv) -> conv
    34   val goals_conv: (int -> bool) -> conv -> conv
    35   val fconv_rule: conv -> thm -> thm
    36 end;
    37 
    38 structure Conv: CONV =
    39 struct
    40 
    41 (* conversionals *)
    42 
    43 type conv = cterm -> thm;
    44 
    45 fun no_conv _ = raise CTERM ("no conversion", []);
    46 val all_conv = Thm.reflexive;
    47 
    48 val is_refl = op aconv o Logic.dest_equals o Thm.prop_of;
    49 
    50 fun (cv1 then_conv cv2) ct =
    51   let
    52     val eq1 = cv1 ct;
    53     val eq2 = cv2 (Thm.rhs_of eq1);
    54   in
    55     if is_refl eq1 then eq2
    56     else if is_refl eq2 then eq1
    57     else Thm.transitive eq1 eq2
    58   end;
    59 
    60 fun (cv1 else_conv cv2) ct =
    61   (case try cv1 ct of SOME eq => eq | NONE => cv2 ct);
    62 
    63 fun first_conv cvs = fold_rev (curry op else_conv) cvs no_conv;
    64 fun every_conv cvs = fold_rev (curry op then_conv) cvs all_conv;
    65 
    66 fun try_conv cv = cv else_conv all_conv;
    67 fun repeat_conv cv ct = try_conv (cv then_conv repeat_conv cv) ct;
    68 
    69 fun cache_conv cv =
    70   let
    71     val cache = ref Termtab.empty;
    72     fun conv ct =
    73       (case Termtab.lookup (! cache) (term_of ct) of
    74         SOME th => th
    75       | NONE =>
    76           let val th = cv ct
    77           in change cache (Termtab.update (term_of ct, th)); th end);
    78  in conv end;
    79 
    80 
    81 
    82 (** Pure conversions **)
    83 
    84 (* lambda terms *)
    85 
    86 fun abs_conv cv ct =
    87   (case term_of ct of
    88     Abs (x, _, _) =>
    89       let val (v, ct') = Thm.dest_abs (SOME (gensym "abs_")) ct
    90       in Thm.abstract_rule x v (cv ct') end
    91   | _ => raise CTERM ("abs_conv", [ct]));
    92 
    93 fun combination_conv cv1 cv2 ct =
    94   let val (ct1, ct2) = Thm.dest_comb ct
    95   in Thm.combination (cv1 ct1) (cv2 ct2) end;
    96 
    97 fun comb_conv cv = combination_conv cv cv;
    98 fun arg_conv cv = combination_conv all_conv cv;
    99 fun fun_conv cv = combination_conv cv all_conv;
   100 
   101 val arg1_conv = fun_conv o arg_conv;
   102 val fun2_conv = fun_conv o fun_conv;
   103 
   104 fun binop_conv cv = combination_conv (arg_conv cv) cv;
   105 
   106 
   107 (* logic *)
   108 
   109 (*rewrite B in !!x1 ... xn. B*)
   110 fun forall_conv 0 cv ct = cv ct
   111   | forall_conv n cv ct =
   112       (case try Thm.dest_comb ct of
   113         NONE => cv ct
   114       | SOME (A, B) =>
   115           (case (term_of A, term_of B) of
   116             (Const ("all", _), Abs (x, _, _)) =>
   117               let val (v, B') = Thm.dest_abs (SOME (gensym "all_")) B in
   118                 Thm.combination (all_conv A)
   119                   (Thm.abstract_rule x v (forall_conv (n - 1) cv B'))
   120               end
   121           | _ => cv ct));
   122 
   123 (*rewrite B in A1 ==> ... ==> An ==> B*)
   124 fun concl_conv 0 cv ct = cv ct
   125   | concl_conv n cv ct =
   126       (case try Thm.dest_implies ct of
   127         NONE => cv ct
   128       | SOME (A, B) => Drule.imp_cong_rule (all_conv A) (concl_conv (n - 1) cv B));
   129 
   130 (*rewrite the A's in A1 ==> ... ==> An ==> B*)
   131 fun prems_conv 0 _ = all_conv
   132   | prems_conv n cv =
   133       let
   134         fun conv i ct =
   135           if i = n + 1 then all_conv ct
   136           else
   137             (case try Thm.dest_implies ct of
   138               NONE => all_conv ct
   139             | SOME (A, B) => Drule.imp_cong_rule (cv i A) (conv (i + 1) B));
   140   in conv 1 end;
   141 
   142 fun goals_conv pred cv = prems_conv ~1 (fn i => if pred i then cv else all_conv);
   143 fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th;
   144 
   145 end;