src/Pure/conv.ML
author wenzelm
Mon Apr 07 21:25:22 2008 +0200 (2008-04-07)
changeset 26571 114da911bc41
parent 26130 03a7cfed5e9e
child 27332 94790a9620c3
permissions -rw-r--r--
abs_conv: extra argument for bound variable;
renamed iterated forall_conv to params_conv;
added forall_conv, implies_conv, implies_concl_conv, rewr_conv;
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@26571
    21
  val abs_conv: (cterm * 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@26571
    29
  val forall_conv: (cterm * Proof.context -> conv) -> Proof.context -> conv
wenzelm@26571
    30
  val implies_conv: conv -> conv -> conv
wenzelm@26571
    31
  val implies_concl_conv: conv -> conv
wenzelm@26571
    32
  val rewr_conv: thm -> conv
wenzelm@26571
    33
  val params_conv: int -> (Proof.context -> conv) -> Proof.context -> conv
wenzelm@26571
    34
  val prems_conv: int -> conv -> conv
wenzelm@22905
    35
  val concl_conv: int -> conv -> conv
wenzelm@22905
    36
  val fconv_rule: conv -> thm -> thm
wenzelm@23583
    37
  val gconv_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@22905
    45
fun no_conv _ = raise CTERM ("no conversion", []);
wenzelm@22905
    46
val all_conv = Thm.reflexive;
wenzelm@22905
    47
wenzelm@22937
    48
fun (cv1 then_conv cv2) ct =
wenzelm@22905
    49
  let
wenzelm@22926
    50
    val eq1 = cv1 ct;
wenzelm@22926
    51
    val eq2 = cv2 (Thm.rhs_of eq1);
wenzelm@22905
    52
  in
wenzelm@23596
    53
    if Thm.is_reflexive eq1 then eq2
wenzelm@23596
    54
    else if Thm.is_reflexive eq2 then eq1
wenzelm@22905
    55
    else Thm.transitive eq1 eq2
wenzelm@22905
    56
  end;
wenzelm@22905
    57
wenzelm@22937
    58
fun (cv1 else_conv cv2) ct =
wenzelm@23583
    59
  (cv1 ct
wenzelm@23583
    60
    handle THM _ => cv2 ct
wenzelm@23583
    61
      | CTERM _ => cv2 ct
wenzelm@23583
    62
      | TERM _ => cv2 ct
wenzelm@23583
    63
      | TYPE _ => 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@22905
    71
wenzelm@22905
    72
wenzelm@22926
    73
(** Pure conversions **)
wenzelm@22926
    74
wenzelm@22926
    75
(* lambda terms *)
wenzelm@22926
    76
wenzelm@24834
    77
fun abs_conv cv ctxt ct =
wenzelm@23587
    78
  (case Thm.term_of ct of
wenzelm@22926
    79
    Abs (x, _, _) =>
wenzelm@23596
    80
      let
wenzelm@24834
    81
        val ([u], ctxt') = Variable.variant_fixes ["u"] ctxt;
wenzelm@24834
    82
        val (v, ct') = Thm.dest_abs (SOME u) ct;
wenzelm@26571
    83
        val eq = cv (v, ctxt') ct';
wenzelm@23596
    84
      in if Thm.is_reflexive eq then all_conv ct else Thm.abstract_rule x v eq end
wenzelm@22926
    85
  | _ => raise CTERM ("abs_conv", [ct]));
wenzelm@22926
    86
wenzelm@22926
    87
fun combination_conv cv1 cv2 ct =
wenzelm@22926
    88
  let val (ct1, ct2) = Thm.dest_comb ct
wenzelm@22926
    89
  in Thm.combination (cv1 ct1) (cv2 ct2) end;
wenzelm@22926
    90
wenzelm@22926
    91
fun comb_conv cv = combination_conv cv cv;
wenzelm@22926
    92
fun arg_conv cv = combination_conv all_conv cv;
wenzelm@22926
    93
fun fun_conv cv = combination_conv cv all_conv;
wenzelm@22926
    94
wenzelm@22926
    95
val arg1_conv = fun_conv o arg_conv;
wenzelm@22926
    96
val fun2_conv = fun_conv o fun_conv;
wenzelm@22926
    97
chaieb@23034
    98
fun binop_conv cv = combination_conv (arg_conv cv) cv;
wenzelm@22926
    99
wenzelm@23169
   100
wenzelm@26571
   101
(* primitive logic *)
wenzelm@26571
   102
wenzelm@26571
   103
fun forall_conv cv ctxt ct =
wenzelm@26571
   104
  (case Thm.term_of ct of
wenzelm@26571
   105
    Const ("all", _) $ Abs _ => arg_conv (abs_conv cv ctxt) ct
wenzelm@26571
   106
  | _ => raise CTERM ("forall_conv", [ct]));
wenzelm@26571
   107
wenzelm@26571
   108
fun implies_conv cv1 cv2 ct =
wenzelm@26571
   109
  (case Thm.term_of ct of
wenzelm@26571
   110
    Const ("==>", _) $ _ $ _ => combination_conv (arg_conv cv1) cv2 ct
wenzelm@26571
   111
  | _ => raise CTERM ("implies_conv", [ct]));
wenzelm@26571
   112
wenzelm@26571
   113
fun implies_concl_conv cv ct =
wenzelm@26571
   114
  (case Thm.term_of ct of
wenzelm@26571
   115
    Const ("==>", _) $ _ $ _ => arg_conv cv ct
wenzelm@26571
   116
  | _ => raise CTERM ("implies_concl_conv", [ct]));
wenzelm@26571
   117
wenzelm@26571
   118
wenzelm@26571
   119
(* single rewrite step, cf. REWR_CONV in HOL *)
wenzelm@26571
   120
wenzelm@26571
   121
fun rewr_conv rule ct =
wenzelm@26571
   122
  let
wenzelm@26571
   123
    val rule1 = Thm.incr_indexes (#maxidx (Thm.rep_cterm ct) + 1) rule;
wenzelm@26571
   124
    val lhs = Thm.lhs_of rule1;
wenzelm@26571
   125
    val rule2 = Thm.rename_boundvars (Thm.term_of lhs) (Thm.term_of ct) rule1;
wenzelm@26571
   126
  in
wenzelm@26571
   127
    Drule.instantiate (Thm.match (lhs, ct)) rule2
wenzelm@26571
   128
      handle Pattern.MATCH => raise CTERM ("rewr_conv", [lhs, ct])
wenzelm@26571
   129
  end;
wenzelm@26571
   130
wenzelm@26571
   131
wenzelm@26571
   132
(* conversions on HHF rules *)
wenzelm@22905
   133
wenzelm@22905
   134
(*rewrite B in !!x1 ... xn. B*)
wenzelm@26571
   135
fun params_conv n cv ctxt ct =
wenzelm@23596
   136
  if n <> 0 andalso can Logic.dest_all (Thm.term_of ct)
wenzelm@26571
   137
  then arg_conv (abs_conv (params_conv (n - 1) cv o #2) ctxt) ct
wenzelm@24834
   138
  else cv ctxt ct;
wenzelm@22905
   139
wenzelm@26571
   140
(*rewrite the A's in A1 ==> ... ==> An ==> B*)
wenzelm@26571
   141
fun prems_conv 0 _ ct = all_conv ct
wenzelm@26571
   142
  | prems_conv n cv ct =
wenzelm@26571
   143
      (case try Thm.dest_implies ct of
wenzelm@26571
   144
        NONE => all_conv ct
wenzelm@26571
   145
      | SOME (A, B) => Drule.imp_cong_rule (cv A) (prems_conv (n - 1) cv B));
wenzelm@26571
   146
wenzelm@22905
   147
(*rewrite B in A1 ==> ... ==> An ==> B*)
wenzelm@22905
   148
fun concl_conv 0 cv ct = cv ct
wenzelm@22905
   149
  | concl_conv n cv ct =
wenzelm@22905
   150
      (case try Thm.dest_implies ct of
wenzelm@22905
   151
        NONE => cv ct
wenzelm@22926
   152
      | SOME (A, B) => Drule.imp_cong_rule (all_conv A) (concl_conv (n - 1) cv B));
wenzelm@22905
   153
wenzelm@23596
   154
wenzelm@26571
   155
(* conversions as inference rules *)
wenzelm@22905
   156
wenzelm@23596
   157
(*forward conversion, cf. FCONV_RULE in LCF*)
wenzelm@23596
   158
fun fconv_rule cv th =
wenzelm@23596
   159
  let val eq = cv (Thm.cprop_of th) in
wenzelm@23596
   160
    if Thm.is_reflexive eq then th
wenzelm@23596
   161
    else Thm.equal_elim eq th
wenzelm@23596
   162
  end;
wenzelm@22905
   163
wenzelm@23596
   164
(*goal conversion*)
wenzelm@23596
   165
fun gconv_rule cv i th =
wenzelm@23596
   166
  (case try (Thm.cprem_of th) i of
wenzelm@23596
   167
    SOME ct =>
wenzelm@23596
   168
      let val eq = cv ct in
wenzelm@23596
   169
        if Thm.is_reflexive eq then th
wenzelm@23596
   170
        else Drule.with_subgoal i (fconv_rule (arg1_conv (K eq))) th
wenzelm@23596
   171
      end
wenzelm@23596
   172
  | NONE => raise THM ("gconv_rule", i, [th]));
chaieb@23411
   173
wenzelm@22905
   174
end;