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