src/Pure/conv.ML
author wenzelm
Tue Jul 03 17:17:09 2007 +0200 (2007-07-03)
changeset 23534 3f82d1798976
parent 23490 1dfbfc92017a
child 23583 00751df1f98c
permissions -rw-r--r--
removed obsolete goals_conv (cf. prems_conv);
added efficient goal_conv_rule;
tuned comments;
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@23490
    11
type conv = cterm -> thm;
wenzelm@23490
    12
wenzelm@22905
    13
signature CONV =
wenzelm@22905
    14
sig
wenzelm@22905
    15
  val no_conv: conv
wenzelm@22905
    16
  val all_conv: conv
wenzelm@23490
    17
  val is_refl: thm -> bool
wenzelm@22937
    18
  val then_conv: conv * conv -> conv
wenzelm@22937
    19
  val else_conv: conv * conv -> conv
wenzelm@22926
    20
  val first_conv: conv list -> conv
wenzelm@22926
    21
  val every_conv: conv list -> conv
wenzelm@22937
    22
  val try_conv: conv -> conv
wenzelm@22937
    23
  val repeat_conv: conv -> conv
wenzelm@22926
    24
  val cache_conv: conv -> conv
wenzelm@22926
    25
  val abs_conv: conv -> conv
wenzelm@22926
    26
  val combination_conv: conv -> conv -> conv
wenzelm@22926
    27
  val comb_conv: conv -> conv
wenzelm@22926
    28
  val arg_conv: conv -> conv
wenzelm@22926
    29
  val fun_conv: conv -> conv
wenzelm@22926
    30
  val arg1_conv: conv -> conv
wenzelm@22926
    31
  val fun2_conv: conv -> conv
chaieb@23034
    32
  val binop_conv: conv -> conv
wenzelm@22905
    33
  val forall_conv: int -> conv -> conv
wenzelm@22905
    34
  val concl_conv: int -> conv -> conv
wenzelm@22905
    35
  val prems_conv: int -> (int -> conv) -> conv
wenzelm@22905
    36
  val fconv_rule: conv -> thm -> thm
wenzelm@23534
    37
  val goal_conv_rule: conv -> int -> thm -> thm
wenzelm@22905
    38
end;
wenzelm@22905
    39
wenzelm@22905
    40
structure Conv: CONV =
wenzelm@22905
    41
struct
wenzelm@22905
    42
wenzelm@22905
    43
(* conversionals *)
wenzelm@22905
    44
wenzelm@22926
    45
type conv = cterm -> thm;
wenzelm@22905
    46
wenzelm@22905
    47
fun no_conv _ = raise CTERM ("no conversion", []);
wenzelm@22905
    48
val all_conv = Thm.reflexive;
wenzelm@22905
    49
wenzelm@22905
    50
val is_refl = op aconv o Logic.dest_equals o Thm.prop_of;
wenzelm@22905
    51
wenzelm@22937
    52
fun (cv1 then_conv cv2) ct =
wenzelm@22905
    53
  let
wenzelm@22926
    54
    val eq1 = cv1 ct;
wenzelm@22926
    55
    val eq2 = cv2 (Thm.rhs_of eq1);
wenzelm@22905
    56
  in
wenzelm@22905
    57
    if is_refl eq1 then eq2
wenzelm@22905
    58
    else if is_refl eq2 then eq1
wenzelm@22905
    59
    else Thm.transitive eq1 eq2
wenzelm@22905
    60
  end;
wenzelm@22905
    61
wenzelm@22937
    62
fun (cv1 else_conv cv2) ct =
wenzelm@22926
    63
  (case try cv1 ct of SOME eq => eq | NONE => cv2 ct);
wenzelm@22926
    64
wenzelm@22937
    65
fun first_conv cvs = fold_rev (curry op else_conv) cvs no_conv;
wenzelm@22937
    66
fun every_conv cvs = fold_rev (curry op then_conv) cvs all_conv;
wenzelm@22926
    67
wenzelm@22937
    68
fun try_conv cv = cv else_conv all_conv;
wenzelm@22937
    69
fun repeat_conv cv ct = try_conv (cv then_conv repeat_conv cv) ct;
wenzelm@22926
    70
wenzelm@22926
    71
fun cache_conv cv =
wenzelm@22926
    72
  let
wenzelm@22926
    73
    val cache = ref Termtab.empty;
wenzelm@22926
    74
    fun conv ct =
wenzelm@22926
    75
      (case Termtab.lookup (! cache) (term_of ct) of
wenzelm@22926
    76
        SOME th => th
wenzelm@22926
    77
      | NONE =>
wenzelm@22926
    78
          let val th = cv ct
wenzelm@22926
    79
          in change cache (Termtab.update (term_of ct, th)); th end);
wenzelm@22926
    80
 in conv end;
wenzelm@22926
    81
wenzelm@22905
    82
wenzelm@22905
    83
wenzelm@22926
    84
(** Pure conversions **)
wenzelm@22926
    85
wenzelm@22926
    86
(* lambda terms *)
wenzelm@22926
    87
wenzelm@22926
    88
fun abs_conv cv ct =
wenzelm@22926
    89
  (case term_of ct of
wenzelm@22926
    90
    Abs (x, _, _) =>
wenzelm@22926
    91
      let val (v, ct') = Thm.dest_abs (SOME (gensym "abs_")) ct
wenzelm@22926
    92
      in Thm.abstract_rule x v (cv ct') end
wenzelm@22926
    93
  | _ => raise CTERM ("abs_conv", [ct]));
wenzelm@22926
    94
wenzelm@22926
    95
fun combination_conv cv1 cv2 ct =
wenzelm@22926
    96
  let val (ct1, ct2) = Thm.dest_comb ct
wenzelm@22926
    97
  in Thm.combination (cv1 ct1) (cv2 ct2) end;
wenzelm@22926
    98
wenzelm@22926
    99
fun comb_conv cv = combination_conv cv cv;
wenzelm@22926
   100
fun arg_conv cv = combination_conv all_conv cv;
wenzelm@22926
   101
fun fun_conv cv = combination_conv cv all_conv;
wenzelm@22926
   102
wenzelm@22926
   103
val arg1_conv = fun_conv o arg_conv;
wenzelm@22926
   104
val fun2_conv = fun_conv o fun_conv;
wenzelm@22926
   105
chaieb@23034
   106
fun binop_conv cv = combination_conv (arg_conv cv) cv;
wenzelm@22926
   107
wenzelm@23169
   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@23534
   144
fun fconv_rule cv th = Thm.equal_elim (cv (Thm.cprop_of th)) th;  (*FCONV_RULE in LCF*)
wenzelm@22905
   145
wenzelm@23534
   146
fun goal_conv_rule cv i = Drule.with_subgoal i (fn th =>
wenzelm@23534
   147
  (case try (Thm.dest_implies o Thm.cprop_of) th of
wenzelm@23534
   148
    NONE => raise THM ("goal_conv_rule", i, [th])
wenzelm@23534
   149
  | SOME (A, B) => Thm.equal_elim (Drule.imp_cong_rule (cv A) (all_conv B)) th));
chaieb@23411
   150
wenzelm@22905
   151
end;