src/Pure/conv.ML
author wenzelm
Fri May 11 18:46:50 2007 +0200 (2007-05-11)
changeset 22937 08cf9aaf3aa1
parent 22926 fb6917e426da
child 23034 b3a6815754d6
permissions -rw-r--r--
unified names: foo_conv;
     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 forall_conv: int -> conv -> conv
    31   val concl_conv: int -> conv -> conv
    32   val prems_conv: int -> (int -> conv) -> conv
    33   val goals_conv: (int -> bool) -> conv -> conv
    34   val fconv_rule: conv -> thm -> thm
    35 end;
    36 
    37 structure Conv: CONV =
    38 struct
    39 
    40 (* conversionals *)
    41 
    42 type conv = cterm -> thm;
    43 
    44 fun no_conv _ = raise CTERM ("no conversion", []);
    45 val all_conv = Thm.reflexive;
    46 
    47 val is_refl = op aconv o Logic.dest_equals o Thm.prop_of;
    48 
    49 fun (cv1 then_conv cv2) ct =
    50   let
    51     val eq1 = cv1 ct;
    52     val eq2 = cv2 (Thm.rhs_of eq1);
    53   in
    54     if is_refl eq1 then eq2
    55     else if is_refl eq2 then eq1
    56     else Thm.transitive eq1 eq2
    57   end;
    58 
    59 fun (cv1 else_conv cv2) ct =
    60   (case try cv1 ct of SOME eq => eq | NONE => cv2 ct);
    61 
    62 fun first_conv cvs = fold_rev (curry op else_conv) cvs no_conv;
    63 fun every_conv cvs = fold_rev (curry op then_conv) cvs all_conv;
    64 
    65 fun try_conv cv = cv else_conv all_conv;
    66 fun repeat_conv cv ct = try_conv (cv then_conv repeat_conv cv) ct;
    67 
    68 fun cache_conv cv =
    69   let
    70     val cache = ref Termtab.empty;
    71     fun conv ct =
    72       (case Termtab.lookup (! cache) (term_of ct) of
    73         SOME th => th
    74       | NONE =>
    75           let val th = cv ct
    76           in change cache (Termtab.update (term_of ct, th)); th end);
    77  in conv end;
    78 
    79 
    80 
    81 (** Pure conversions **)
    82 
    83 (* lambda terms *)
    84 
    85 fun abs_conv cv ct =
    86   (case term_of ct of
    87     Abs (x, _, _) =>
    88       let val (v, ct') = Thm.dest_abs (SOME (gensym "abs_")) ct
    89       in Thm.abstract_rule x v (cv ct') end
    90   | _ => raise CTERM ("abs_conv", [ct]));
    91 
    92 fun combination_conv cv1 cv2 ct =
    93   let val (ct1, ct2) = Thm.dest_comb ct
    94   in Thm.combination (cv1 ct1) (cv2 ct2) end;
    95 
    96 fun comb_conv cv = combination_conv cv cv;
    97 fun arg_conv cv = combination_conv all_conv cv;
    98 fun fun_conv cv = combination_conv cv all_conv;
    99 
   100 val arg1_conv = fun_conv o arg_conv;
   101 val fun2_conv = fun_conv o fun_conv;
   102 
   103 
   104 (* logic *)
   105 
   106 (*rewrite B in !!x1 ... xn. B*)
   107 fun forall_conv 0 cv ct = cv ct
   108   | forall_conv n cv ct =
   109       (case try Thm.dest_comb ct of
   110         NONE => cv ct
   111       | SOME (A, B) =>
   112           (case (term_of A, term_of B) of
   113             (Const ("all", _), Abs (x, _, _)) =>
   114               let val (v, B') = Thm.dest_abs (SOME (gensym "all_")) B in
   115                 Thm.combination (all_conv A)
   116                   (Thm.abstract_rule x v (forall_conv (n - 1) cv B'))
   117               end
   118           | _ => cv ct));
   119 
   120 (*rewrite B in A1 ==> ... ==> An ==> B*)
   121 fun concl_conv 0 cv ct = cv ct
   122   | concl_conv n cv ct =
   123       (case try Thm.dest_implies ct of
   124         NONE => cv ct
   125       | SOME (A, B) => Drule.imp_cong_rule (all_conv A) (concl_conv (n - 1) cv B));
   126 
   127 (*rewrite the A's in A1 ==> ... ==> An ==> B*)
   128 fun prems_conv 0 _ = all_conv
   129   | prems_conv n cv =
   130       let
   131         fun conv i ct =
   132           if i = n + 1 then all_conv ct
   133           else
   134             (case try Thm.dest_implies ct of
   135               NONE => all_conv ct
   136             | SOME (A, B) => Drule.imp_cong_rule (cv i A) (conv (i + 1) B));
   137   in conv 1 end;
   138 
   139 fun goals_conv pred cv = prems_conv ~1 (fn i => if pred i then cv else all_conv);
   140 fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th;
   141 
   142 end;