src/Pure/conv.ML
author wenzelm
Thu Oct 04 20:29:42 2007 +0200 (2007-10-04)
changeset 24850 0cfd722ab579
parent 24834 5684cbf8c895
child 26130 03a7cfed5e9e
permissions -rw-r--r--
Name.uu, Name.aT;
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
  val no_conv: conv
wenzelm@22905
    14
  val all_conv: conv
wenzelm@22937
    15
  val then_conv: conv * conv -> conv
wenzelm@22937
    16
  val else_conv: conv * conv -> conv
wenzelm@22926
    17
  val first_conv: conv list -> conv
wenzelm@22926
    18
  val every_conv: conv list -> conv
wenzelm@22937
    19
  val try_conv: conv -> conv
wenzelm@22937
    20
  val repeat_conv: conv -> conv
wenzelm@24834
    21
  val abs_conv: (Proof.context -> conv) -> Proof.context -> conv
wenzelm@22926
    22
  val combination_conv: conv -> conv -> conv
wenzelm@22926
    23
  val comb_conv: conv -> conv
wenzelm@22926
    24
  val arg_conv: conv -> conv
wenzelm@22926
    25
  val fun_conv: conv -> conv
wenzelm@22926
    26
  val arg1_conv: conv -> conv
wenzelm@22926
    27
  val fun2_conv: conv -> conv
chaieb@23034
    28
  val binop_conv: conv -> conv
wenzelm@24834
    29
  val forall_conv: int -> (Proof.context -> conv) -> Proof.context -> conv
wenzelm@22905
    30
  val concl_conv: int -> conv -> conv
wenzelm@23583
    31
  val prems_conv: int -> conv -> conv
wenzelm@22905
    32
  val fconv_rule: conv -> thm -> thm
wenzelm@23583
    33
  val gconv_rule: conv -> int -> thm -> thm
wenzelm@22905
    34
end;
wenzelm@22905
    35
wenzelm@22905
    36
structure Conv: CONV =
wenzelm@22905
    37
struct
wenzelm@22905
    38
wenzelm@22905
    39
(* conversionals *)
wenzelm@22905
    40
wenzelm@22905
    41
fun no_conv _ = raise CTERM ("no conversion", []);
wenzelm@22905
    42
val all_conv = Thm.reflexive;
wenzelm@22905
    43
wenzelm@22937
    44
fun (cv1 then_conv cv2) ct =
wenzelm@22905
    45
  let
wenzelm@22926
    46
    val eq1 = cv1 ct;
wenzelm@22926
    47
    val eq2 = cv2 (Thm.rhs_of eq1);
wenzelm@22905
    48
  in
wenzelm@23596
    49
    if Thm.is_reflexive eq1 then eq2
wenzelm@23596
    50
    else if Thm.is_reflexive eq2 then eq1
wenzelm@22905
    51
    else Thm.transitive eq1 eq2
wenzelm@22905
    52
  end;
wenzelm@22905
    53
wenzelm@22937
    54
fun (cv1 else_conv cv2) ct =
wenzelm@23583
    55
  (cv1 ct
wenzelm@23583
    56
    handle THM _ => cv2 ct
wenzelm@23583
    57
      | CTERM _ => cv2 ct
wenzelm@23583
    58
      | TERM _ => cv2 ct
wenzelm@23583
    59
      | TYPE _ => cv2 ct);
wenzelm@22926
    60
wenzelm@22937
    61
fun first_conv cvs = fold_rev (curry op else_conv) cvs no_conv;
wenzelm@22937
    62
fun every_conv cvs = fold_rev (curry op then_conv) cvs all_conv;
wenzelm@22926
    63
wenzelm@22937
    64
fun try_conv cv = cv else_conv all_conv;
wenzelm@22937
    65
fun repeat_conv cv ct = try_conv (cv then_conv repeat_conv cv) ct;
wenzelm@22926
    66
wenzelm@22905
    67
wenzelm@22905
    68
wenzelm@22926
    69
(** Pure conversions **)
wenzelm@22926
    70
wenzelm@22926
    71
(* lambda terms *)
wenzelm@22926
    72
wenzelm@24834
    73
fun abs_conv cv ctxt ct =
wenzelm@23587
    74
  (case Thm.term_of ct of
wenzelm@22926
    75
    Abs (x, _, _) =>
wenzelm@23596
    76
      let
wenzelm@24834
    77
        val ([u], ctxt') = Variable.variant_fixes ["u"] ctxt;
wenzelm@24834
    78
        val (v, ct') = Thm.dest_abs (SOME u) ct;
wenzelm@24834
    79
        val eq = (cv ctxt') ct';
wenzelm@23596
    80
      in if Thm.is_reflexive eq then all_conv ct else Thm.abstract_rule x v eq end
wenzelm@22926
    81
  | _ => raise CTERM ("abs_conv", [ct]));
wenzelm@22926
    82
wenzelm@22926
    83
fun combination_conv cv1 cv2 ct =
wenzelm@22926
    84
  let val (ct1, ct2) = Thm.dest_comb ct
wenzelm@22926
    85
  in Thm.combination (cv1 ct1) (cv2 ct2) end;
wenzelm@22926
    86
wenzelm@22926
    87
fun comb_conv cv = combination_conv cv cv;
wenzelm@22926
    88
fun arg_conv cv = combination_conv all_conv cv;
wenzelm@22926
    89
fun fun_conv cv = combination_conv cv all_conv;
wenzelm@22926
    90
wenzelm@22926
    91
val arg1_conv = fun_conv o arg_conv;
wenzelm@22926
    92
val fun2_conv = fun_conv o fun_conv;
wenzelm@22926
    93
chaieb@23034
    94
fun binop_conv cv = combination_conv (arg_conv cv) cv;
wenzelm@22926
    95
wenzelm@23169
    96
wenzelm@22926
    97
(* logic *)
wenzelm@22905
    98
wenzelm@22905
    99
(*rewrite B in !!x1 ... xn. B*)
wenzelm@24834
   100
fun forall_conv n cv ctxt ct =
wenzelm@23596
   101
  if n <> 0 andalso can Logic.dest_all (Thm.term_of ct)
wenzelm@24834
   102
  then arg_conv (abs_conv (forall_conv (n - 1) cv) ctxt) ct
wenzelm@24834
   103
  else cv ctxt ct;
wenzelm@22905
   104
wenzelm@22905
   105
(*rewrite B in A1 ==> ... ==> An ==> B*)
wenzelm@22905
   106
fun concl_conv 0 cv ct = cv ct
wenzelm@22905
   107
  | concl_conv n cv ct =
wenzelm@22905
   108
      (case try Thm.dest_implies ct of
wenzelm@22905
   109
        NONE => cv ct
wenzelm@22926
   110
      | SOME (A, B) => Drule.imp_cong_rule (all_conv A) (concl_conv (n - 1) cv B));
wenzelm@22905
   111
wenzelm@22905
   112
(*rewrite the A's in A1 ==> ... ==> An ==> B*)
wenzelm@23596
   113
fun prems_conv 0 _ ct = all_conv ct
wenzelm@23596
   114
  | prems_conv n cv ct =
wenzelm@23596
   115
      (case try Thm.dest_implies ct of
wenzelm@23596
   116
        NONE => all_conv ct
wenzelm@23596
   117
      | SOME (A, B) => Drule.imp_cong_rule (cv A) (prems_conv (n - 1) cv B));
wenzelm@23596
   118
wenzelm@23596
   119
wenzelm@23596
   120
(* conversions as rules *)
wenzelm@22905
   121
wenzelm@23596
   122
(*forward conversion, cf. FCONV_RULE in LCF*)
wenzelm@23596
   123
fun fconv_rule cv th =
wenzelm@23596
   124
  let val eq = cv (Thm.cprop_of th) in
wenzelm@23596
   125
    if Thm.is_reflexive eq then th
wenzelm@23596
   126
    else Thm.equal_elim eq th
wenzelm@23596
   127
  end;
wenzelm@22905
   128
wenzelm@23596
   129
(*goal conversion*)
wenzelm@23596
   130
fun gconv_rule cv i th =
wenzelm@23596
   131
  (case try (Thm.cprem_of th) i of
wenzelm@23596
   132
    SOME ct =>
wenzelm@23596
   133
      let val eq = cv ct in
wenzelm@23596
   134
        if Thm.is_reflexive eq then th
wenzelm@23596
   135
        else Drule.with_subgoal i (fconv_rule (arg1_conv (K eq))) th
wenzelm@23596
   136
      end
wenzelm@23596
   137
  | NONE => raise THM ("gconv_rule", i, [th]));
chaieb@23411
   138
wenzelm@22905
   139
end;