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