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