src/Pure/conv.ML
author haftmann
Tue Oct 20 16:13:01 2009 +0200 (2009-10-20)
changeset 33037 b22e44496dc2
parent 32843 c8f5a7c8353f
child 36936 c52d1c130898
permissions -rw-r--r--
replaced old_style infixes eq_set, subset, union, inter and variants by generic versions
wenzelm@22905
     1
(*  Title:      Pure/conv.ML
wenzelm@32843
     2
    Author:     Amine Chaieb, TU Muenchen
wenzelm@32843
     3
    Author:     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
boehmes@30136
    11
signature BASIC_CONV =
boehmes@30136
    12
sig
boehmes@30136
    13
  val then_conv: conv * conv -> conv
boehmes@30136
    14
  val else_conv: conv * conv -> conv
boehmes@30136
    15
end;
boehmes@30136
    16
wenzelm@22905
    17
signature CONV =
wenzelm@22905
    18
sig
boehmes@30136
    19
  include BASIC_CONV
wenzelm@22905
    20
  val no_conv: conv
wenzelm@22905
    21
  val all_conv: conv
wenzelm@22926
    22
  val first_conv: conv list -> conv
wenzelm@22926
    23
  val every_conv: conv list -> conv
wenzelm@22937
    24
  val try_conv: conv -> conv
wenzelm@22937
    25
  val repeat_conv: conv -> conv
wenzelm@32843
    26
  val cache_conv: conv -> conv
wenzelm@26571
    27
  val abs_conv: (cterm * Proof.context -> conv) -> Proof.context -> conv
wenzelm@22926
    28
  val combination_conv: conv -> conv -> conv
wenzelm@22926
    29
  val comb_conv: conv -> conv
wenzelm@22926
    30
  val arg_conv: conv -> conv
wenzelm@22926
    31
  val fun_conv: conv -> conv
wenzelm@22926
    32
  val arg1_conv: conv -> conv
wenzelm@22926
    33
  val fun2_conv: conv -> conv
chaieb@23034
    34
  val binop_conv: conv -> conv
wenzelm@26571
    35
  val forall_conv: (cterm * Proof.context -> conv) -> Proof.context -> conv
wenzelm@26571
    36
  val implies_conv: conv -> conv -> conv
wenzelm@26571
    37
  val implies_concl_conv: conv -> conv
wenzelm@26571
    38
  val rewr_conv: thm -> conv
wenzelm@26571
    39
  val params_conv: int -> (Proof.context -> conv) -> Proof.context -> conv
wenzelm@26571
    40
  val prems_conv: int -> conv -> conv
wenzelm@22905
    41
  val concl_conv: int -> conv -> conv
wenzelm@22905
    42
  val fconv_rule: conv -> thm -> thm
wenzelm@23583
    43
  val gconv_rule: conv -> int -> thm -> thm
wenzelm@22905
    44
end;
wenzelm@22905
    45
wenzelm@22905
    46
structure Conv: CONV =
wenzelm@22905
    47
struct
wenzelm@22905
    48
wenzelm@32843
    49
(* basic conversionals *)
wenzelm@22905
    50
wenzelm@22905
    51
fun no_conv _ = raise CTERM ("no conversion", []);
wenzelm@22905
    52
val all_conv = Thm.reflexive;
wenzelm@22905
    53
wenzelm@22937
    54
fun (cv1 then_conv cv2) ct =
wenzelm@22905
    55
  let
wenzelm@22926
    56
    val eq1 = cv1 ct;
wenzelm@22926
    57
    val eq2 = cv2 (Thm.rhs_of eq1);
wenzelm@22905
    58
  in
wenzelm@23596
    59
    if Thm.is_reflexive eq1 then eq2
wenzelm@23596
    60
    else if Thm.is_reflexive eq2 then eq1
wenzelm@22905
    61
    else Thm.transitive eq1 eq2
wenzelm@22905
    62
  end;
wenzelm@22905
    63
wenzelm@22937
    64
fun (cv1 else_conv cv2) ct =
wenzelm@23583
    65
  (cv1 ct
wenzelm@23583
    66
    handle THM _ => cv2 ct
wenzelm@23583
    67
      | CTERM _ => cv2 ct
wenzelm@23583
    68
      | TERM _ => cv2 ct
wenzelm@23583
    69
      | TYPE _ => cv2 ct);
wenzelm@22926
    70
wenzelm@22937
    71
fun first_conv cvs = fold_rev (curry op else_conv) cvs no_conv;
wenzelm@22937
    72
fun every_conv cvs = fold_rev (curry op then_conv) cvs all_conv;
wenzelm@22926
    73
wenzelm@22937
    74
fun try_conv cv = cv else_conv all_conv;
wenzelm@22937
    75
fun repeat_conv cv ct = try_conv (cv then_conv repeat_conv cv) ct;
wenzelm@22926
    76
wenzelm@32843
    77
fun cache_conv (cv: conv) = Thm.cterm_cache cv;
wenzelm@32843
    78
wenzelm@22905
    79
wenzelm@22905
    80
wenzelm@22926
    81
(** Pure conversions **)
wenzelm@22926
    82
wenzelm@22926
    83
(* lambda terms *)
wenzelm@22926
    84
wenzelm@24834
    85
fun abs_conv cv ctxt ct =
wenzelm@23587
    86
  (case Thm.term_of ct of
wenzelm@22926
    87
    Abs (x, _, _) =>
wenzelm@23596
    88
      let
wenzelm@24834
    89
        val ([u], ctxt') = Variable.variant_fixes ["u"] ctxt;
wenzelm@24834
    90
        val (v, ct') = Thm.dest_abs (SOME u) ct;
wenzelm@26571
    91
        val eq = cv (v, ctxt') ct';
wenzelm@23596
    92
      in if Thm.is_reflexive eq then all_conv ct else Thm.abstract_rule x v eq 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@26571
   109
(* primitive logic *)
wenzelm@26571
   110
wenzelm@26571
   111
fun forall_conv cv ctxt ct =
wenzelm@26571
   112
  (case Thm.term_of ct of
wenzelm@26571
   113
    Const ("all", _) $ Abs _ => arg_conv (abs_conv cv ctxt) ct
wenzelm@26571
   114
  | _ => raise CTERM ("forall_conv", [ct]));
wenzelm@26571
   115
wenzelm@26571
   116
fun implies_conv cv1 cv2 ct =
wenzelm@26571
   117
  (case Thm.term_of ct of
wenzelm@26571
   118
    Const ("==>", _) $ _ $ _ => combination_conv (arg_conv cv1) cv2 ct
wenzelm@26571
   119
  | _ => raise CTERM ("implies_conv", [ct]));
wenzelm@26571
   120
wenzelm@26571
   121
fun implies_concl_conv cv ct =
wenzelm@26571
   122
  (case Thm.term_of ct of
wenzelm@26571
   123
    Const ("==>", _) $ _ $ _ => arg_conv cv ct
wenzelm@26571
   124
  | _ => raise CTERM ("implies_concl_conv", [ct]));
wenzelm@26571
   125
wenzelm@26571
   126
wenzelm@26571
   127
(* single rewrite step, cf. REWR_CONV in HOL *)
wenzelm@26571
   128
wenzelm@26571
   129
fun rewr_conv rule ct =
wenzelm@26571
   130
  let
wenzelm@26571
   131
    val rule1 = Thm.incr_indexes (#maxidx (Thm.rep_cterm ct) + 1) rule;
wenzelm@26571
   132
    val lhs = Thm.lhs_of rule1;
wenzelm@26571
   133
    val rule2 = Thm.rename_boundvars (Thm.term_of lhs) (Thm.term_of ct) rule1;
wenzelm@26571
   134
  in
wenzelm@26571
   135
    Drule.instantiate (Thm.match (lhs, ct)) rule2
wenzelm@26571
   136
      handle Pattern.MATCH => raise CTERM ("rewr_conv", [lhs, ct])
wenzelm@26571
   137
  end;
wenzelm@26571
   138
wenzelm@26571
   139
wenzelm@26571
   140
(* conversions on HHF rules *)
wenzelm@22905
   141
wenzelm@22905
   142
(*rewrite B in !!x1 ... xn. B*)
wenzelm@26571
   143
fun params_conv n cv ctxt ct =
wenzelm@27332
   144
  if n <> 0 andalso Logic.is_all (Thm.term_of ct)
wenzelm@26571
   145
  then arg_conv (abs_conv (params_conv (n - 1) cv o #2) ctxt) ct
wenzelm@24834
   146
  else cv ctxt ct;
wenzelm@22905
   147
wenzelm@26571
   148
(*rewrite the A's in A1 ==> ... ==> An ==> B*)
wenzelm@26571
   149
fun prems_conv 0 _ ct = all_conv ct
wenzelm@26571
   150
  | prems_conv n cv ct =
wenzelm@26571
   151
      (case try Thm.dest_implies ct of
wenzelm@26571
   152
        NONE => all_conv ct
wenzelm@26571
   153
      | SOME (A, B) => Drule.imp_cong_rule (cv A) (prems_conv (n - 1) cv B));
wenzelm@26571
   154
wenzelm@22905
   155
(*rewrite B in A1 ==> ... ==> An ==> B*)
wenzelm@22905
   156
fun concl_conv 0 cv ct = cv ct
wenzelm@22905
   157
  | concl_conv n cv ct =
wenzelm@22905
   158
      (case try Thm.dest_implies ct of
wenzelm@22905
   159
        NONE => cv ct
wenzelm@22926
   160
      | SOME (A, B) => Drule.imp_cong_rule (all_conv A) (concl_conv (n - 1) cv B));
wenzelm@22905
   161
wenzelm@23596
   162
wenzelm@26571
   163
(* conversions as inference rules *)
wenzelm@22905
   164
wenzelm@23596
   165
(*forward conversion, cf. FCONV_RULE in LCF*)
wenzelm@23596
   166
fun fconv_rule cv th =
wenzelm@23596
   167
  let val eq = cv (Thm.cprop_of th) in
wenzelm@23596
   168
    if Thm.is_reflexive eq then th
wenzelm@23596
   169
    else Thm.equal_elim eq th
wenzelm@23596
   170
  end;
wenzelm@22905
   171
wenzelm@23596
   172
(*goal conversion*)
wenzelm@23596
   173
fun gconv_rule cv i th =
wenzelm@23596
   174
  (case try (Thm.cprem_of th) i of
wenzelm@23596
   175
    SOME ct =>
wenzelm@23596
   176
      let val eq = cv ct in
wenzelm@23596
   177
        if Thm.is_reflexive eq then th
wenzelm@23596
   178
        else Drule.with_subgoal i (fconv_rule (arg1_conv (K eq))) th
wenzelm@23596
   179
      end
wenzelm@23596
   180
  | NONE => raise THM ("gconv_rule", i, [th]));
chaieb@23411
   181
wenzelm@22905
   182
end;
boehmes@30136
   183
wenzelm@32843
   184
structure Basic_Conv: BASIC_CONV = Conv;
wenzelm@32843
   185
open Basic_Conv;