src/Pure/conv.ML
author bulwahn
Tue Aug 31 08:00:53 2010 +0200 (2010-08-31)
changeset 38950 62578950e748
parent 38668 e8236c4aff16
child 41248 bb28bf635202
permissions -rw-r--r--
storing options for prolog code generation in the theory
wenzelm@22905
     1
(*  Title:      Pure/conv.ML
wenzelm@32843
     2
    Author:     Amine Chaieb, TU Muenchen
wenzelm@36936
     3
    Author:     Sascha Boehme, TU Muenchen
wenzelm@32843
     4
    Author:     Makarius
wenzelm@22905
     5
wenzelm@22905
     6
Conversions: primitive equality reasoning.
wenzelm@22905
     7
*)
wenzelm@22905
     8
wenzelm@22937
     9
infix 1 then_conv;
wenzelm@22937
    10
infix 0 else_conv;
wenzelm@23169
    11
boehmes@30136
    12
signature BASIC_CONV =
boehmes@30136
    13
sig
boehmes@30136
    14
  val then_conv: conv * conv -> conv
boehmes@30136
    15
  val else_conv: conv * conv -> conv
boehmes@30136
    16
end;
boehmes@30136
    17
wenzelm@22905
    18
signature CONV =
wenzelm@22905
    19
sig
boehmes@30136
    20
  include BASIC_CONV
wenzelm@22905
    21
  val no_conv: conv
wenzelm@22905
    22
  val all_conv: conv
wenzelm@22926
    23
  val first_conv: conv list -> conv
wenzelm@22926
    24
  val every_conv: conv list -> conv
wenzelm@22937
    25
  val try_conv: conv -> conv
wenzelm@22937
    26
  val repeat_conv: conv -> conv
wenzelm@32843
    27
  val cache_conv: conv -> conv
wenzelm@26571
    28
  val abs_conv: (cterm * Proof.context -> conv) -> Proof.context -> conv
wenzelm@22926
    29
  val combination_conv: conv -> conv -> conv
wenzelm@22926
    30
  val comb_conv: conv -> conv
wenzelm@22926
    31
  val arg_conv: conv -> conv
wenzelm@22926
    32
  val fun_conv: conv -> conv
wenzelm@22926
    33
  val arg1_conv: conv -> conv
wenzelm@22926
    34
  val fun2_conv: conv -> conv
chaieb@23034
    35
  val binop_conv: conv -> conv
wenzelm@36936
    36
  val binder_conv: (cterm * Proof.context -> conv) -> Proof.context -> conv
wenzelm@26571
    37
  val forall_conv: (cterm * Proof.context -> conv) -> Proof.context -> conv
wenzelm@26571
    38
  val implies_conv: conv -> conv -> conv
wenzelm@26571
    39
  val implies_concl_conv: conv -> conv
wenzelm@26571
    40
  val rewr_conv: thm -> conv
wenzelm@36936
    41
  val rewrs_conv: thm list -> conv
wenzelm@36936
    42
  val sub_conv: (Proof.context -> conv) -> Proof.context -> conv
wenzelm@36936
    43
  val bottom_conv: (Proof.context -> conv) -> Proof.context -> conv
wenzelm@36936
    44
  val top_conv: (Proof.context -> conv) -> Proof.context -> conv
wenzelm@36936
    45
  val top_sweep_conv: (Proof.context -> conv) -> Proof.context -> conv
wenzelm@26571
    46
  val params_conv: int -> (Proof.context -> conv) -> Proof.context -> conv
wenzelm@26571
    47
  val prems_conv: int -> conv -> conv
wenzelm@22905
    48
  val concl_conv: int -> conv -> conv
wenzelm@22905
    49
  val fconv_rule: conv -> thm -> thm
wenzelm@23583
    50
  val gconv_rule: conv -> int -> thm -> thm
haftmann@38668
    51
  val tap_thy: (theory -> conv) -> conv
wenzelm@22905
    52
end;
wenzelm@22905
    53
wenzelm@22905
    54
structure Conv: CONV =
wenzelm@22905
    55
struct
wenzelm@22905
    56
wenzelm@32843
    57
(* basic conversionals *)
wenzelm@22905
    58
wenzelm@22905
    59
fun no_conv _ = raise CTERM ("no conversion", []);
wenzelm@22905
    60
val all_conv = Thm.reflexive;
wenzelm@22905
    61
wenzelm@22937
    62
fun (cv1 then_conv cv2) ct =
wenzelm@22905
    63
  let
wenzelm@22926
    64
    val eq1 = cv1 ct;
wenzelm@22926
    65
    val eq2 = cv2 (Thm.rhs_of eq1);
wenzelm@22905
    66
  in
wenzelm@23596
    67
    if Thm.is_reflexive eq1 then eq2
wenzelm@23596
    68
    else if Thm.is_reflexive eq2 then eq1
wenzelm@22905
    69
    else Thm.transitive eq1 eq2
wenzelm@22905
    70
  end;
wenzelm@22905
    71
wenzelm@22937
    72
fun (cv1 else_conv cv2) ct =
wenzelm@23583
    73
  (cv1 ct
wenzelm@23583
    74
    handle THM _ => cv2 ct
wenzelm@23583
    75
      | CTERM _ => cv2 ct
wenzelm@23583
    76
      | TERM _ => cv2 ct
wenzelm@23583
    77
      | TYPE _ => cv2 ct);
wenzelm@22926
    78
wenzelm@22937
    79
fun first_conv cvs = fold_rev (curry op else_conv) cvs no_conv;
wenzelm@22937
    80
fun every_conv cvs = fold_rev (curry op then_conv) cvs all_conv;
wenzelm@22926
    81
wenzelm@22937
    82
fun try_conv cv = cv else_conv all_conv;
wenzelm@22937
    83
fun repeat_conv cv ct = try_conv (cv then_conv repeat_conv cv) ct;
wenzelm@22926
    84
wenzelm@32843
    85
fun cache_conv (cv: conv) = Thm.cterm_cache cv;
wenzelm@32843
    86
wenzelm@22905
    87
wenzelm@22905
    88
wenzelm@22926
    89
(** Pure conversions **)
wenzelm@22926
    90
wenzelm@22926
    91
(* lambda terms *)
wenzelm@22926
    92
wenzelm@24834
    93
fun abs_conv cv ctxt ct =
wenzelm@23587
    94
  (case Thm.term_of ct of
wenzelm@22926
    95
    Abs (x, _, _) =>
wenzelm@23596
    96
      let
wenzelm@24834
    97
        val ([u], ctxt') = Variable.variant_fixes ["u"] ctxt;
wenzelm@24834
    98
        val (v, ct') = Thm.dest_abs (SOME u) ct;
wenzelm@26571
    99
        val eq = cv (v, ctxt') ct';
wenzelm@23596
   100
      in if Thm.is_reflexive eq then all_conv ct else Thm.abstract_rule x v eq end
wenzelm@22926
   101
  | _ => raise CTERM ("abs_conv", [ct]));
wenzelm@22926
   102
wenzelm@22926
   103
fun combination_conv cv1 cv2 ct =
wenzelm@22926
   104
  let val (ct1, ct2) = Thm.dest_comb ct
wenzelm@22926
   105
  in Thm.combination (cv1 ct1) (cv2 ct2) end;
wenzelm@22926
   106
wenzelm@22926
   107
fun comb_conv cv = combination_conv cv cv;
wenzelm@22926
   108
fun arg_conv cv = combination_conv all_conv cv;
wenzelm@22926
   109
fun fun_conv cv = combination_conv cv all_conv;
wenzelm@22926
   110
wenzelm@22926
   111
val arg1_conv = fun_conv o arg_conv;
wenzelm@22926
   112
val fun2_conv = fun_conv o fun_conv;
wenzelm@22926
   113
chaieb@23034
   114
fun binop_conv cv = combination_conv (arg_conv cv) cv;
wenzelm@22926
   115
wenzelm@36936
   116
fun binder_conv cv ctxt = arg_conv (abs_conv cv ctxt);
wenzelm@36936
   117
wenzelm@36936
   118
wenzelm@36936
   119
(* subterm structure *)
wenzelm@36936
   120
wenzelm@36936
   121
(*cf. SUB_CONV in HOL*)
wenzelm@36936
   122
fun sub_conv conv ctxt =
wenzelm@36936
   123
  comb_conv (conv ctxt) else_conv
wenzelm@36936
   124
  abs_conv (conv o snd) ctxt else_conv
wenzelm@36936
   125
  all_conv;
wenzelm@36936
   126
wenzelm@36936
   127
(*cf. BOTTOM_CONV in HOL*)
wenzelm@36936
   128
fun bottom_conv conv ctxt ct =
wenzelm@36936
   129
  (sub_conv (bottom_conv conv) ctxt then_conv conv ctxt) ct;
wenzelm@36936
   130
wenzelm@36936
   131
(*cf. TOP_CONV in HOL*)
wenzelm@36936
   132
fun top_conv conv ctxt ct =
wenzelm@36936
   133
  (conv ctxt then_conv sub_conv (top_conv conv) ctxt) ct;
wenzelm@36936
   134
wenzelm@36936
   135
(*cf. TOP_SWEEP_CONV in HOL*)
wenzelm@36936
   136
fun top_sweep_conv conv ctxt ct =
wenzelm@36936
   137
  (conv ctxt else_conv sub_conv (top_sweep_conv conv) ctxt) ct;
wenzelm@36936
   138
wenzelm@23169
   139
wenzelm@26571
   140
(* primitive logic *)
wenzelm@26571
   141
wenzelm@26571
   142
fun forall_conv cv ctxt ct =
wenzelm@26571
   143
  (case Thm.term_of ct of
wenzelm@26571
   144
    Const ("all", _) $ Abs _ => arg_conv (abs_conv cv ctxt) ct
wenzelm@26571
   145
  | _ => raise CTERM ("forall_conv", [ct]));
wenzelm@26571
   146
wenzelm@26571
   147
fun implies_conv cv1 cv2 ct =
wenzelm@26571
   148
  (case Thm.term_of ct of
wenzelm@26571
   149
    Const ("==>", _) $ _ $ _ => combination_conv (arg_conv cv1) cv2 ct
wenzelm@26571
   150
  | _ => raise CTERM ("implies_conv", [ct]));
wenzelm@26571
   151
wenzelm@26571
   152
fun implies_concl_conv cv ct =
wenzelm@26571
   153
  (case Thm.term_of ct of
wenzelm@26571
   154
    Const ("==>", _) $ _ $ _ => arg_conv cv ct
wenzelm@26571
   155
  | _ => raise CTERM ("implies_concl_conv", [ct]));
wenzelm@26571
   156
wenzelm@26571
   157
wenzelm@26571
   158
(* single rewrite step, cf. REWR_CONV in HOL *)
wenzelm@26571
   159
wenzelm@26571
   160
fun rewr_conv rule ct =
wenzelm@26571
   161
  let
wenzelm@26571
   162
    val rule1 = Thm.incr_indexes (#maxidx (Thm.rep_cterm ct) + 1) rule;
wenzelm@26571
   163
    val lhs = Thm.lhs_of rule1;
wenzelm@26571
   164
    val rule2 = Thm.rename_boundvars (Thm.term_of lhs) (Thm.term_of ct) rule1;
wenzelm@26571
   165
  in
wenzelm@26571
   166
    Drule.instantiate (Thm.match (lhs, ct)) rule2
wenzelm@26571
   167
      handle Pattern.MATCH => raise CTERM ("rewr_conv", [lhs, ct])
wenzelm@26571
   168
  end;
wenzelm@26571
   169
wenzelm@36936
   170
fun rewrs_conv rules = first_conv (map rewr_conv rules);
wenzelm@36936
   171
wenzelm@26571
   172
wenzelm@26571
   173
(* conversions on HHF rules *)
wenzelm@22905
   174
wenzelm@22905
   175
(*rewrite B in !!x1 ... xn. B*)
wenzelm@26571
   176
fun params_conv n cv ctxt ct =
wenzelm@27332
   177
  if n <> 0 andalso Logic.is_all (Thm.term_of ct)
wenzelm@26571
   178
  then arg_conv (abs_conv (params_conv (n - 1) cv o #2) ctxt) ct
wenzelm@24834
   179
  else cv ctxt ct;
wenzelm@22905
   180
wenzelm@26571
   181
(*rewrite the A's in A1 ==> ... ==> An ==> B*)
wenzelm@26571
   182
fun prems_conv 0 _ ct = all_conv ct
wenzelm@26571
   183
  | prems_conv n cv ct =
wenzelm@26571
   184
      (case try Thm.dest_implies ct of
wenzelm@26571
   185
        NONE => all_conv ct
wenzelm@26571
   186
      | SOME (A, B) => Drule.imp_cong_rule (cv A) (prems_conv (n - 1) cv B));
wenzelm@26571
   187
wenzelm@22905
   188
(*rewrite B in A1 ==> ... ==> An ==> B*)
wenzelm@22905
   189
fun concl_conv 0 cv ct = cv ct
wenzelm@22905
   190
  | concl_conv n cv ct =
wenzelm@22905
   191
      (case try Thm.dest_implies ct of
wenzelm@22905
   192
        NONE => cv ct
wenzelm@22926
   193
      | SOME (A, B) => Drule.imp_cong_rule (all_conv A) (concl_conv (n - 1) cv B));
wenzelm@22905
   194
wenzelm@23596
   195
wenzelm@26571
   196
(* conversions as inference rules *)
wenzelm@22905
   197
wenzelm@23596
   198
(*forward conversion, cf. FCONV_RULE in LCF*)
wenzelm@23596
   199
fun fconv_rule cv th =
wenzelm@23596
   200
  let val eq = cv (Thm.cprop_of th) in
wenzelm@23596
   201
    if Thm.is_reflexive eq then th
wenzelm@23596
   202
    else Thm.equal_elim eq th
wenzelm@23596
   203
  end;
wenzelm@22905
   204
wenzelm@23596
   205
(*goal conversion*)
wenzelm@23596
   206
fun gconv_rule cv i th =
wenzelm@23596
   207
  (case try (Thm.cprem_of th) i of
wenzelm@23596
   208
    SOME ct =>
wenzelm@23596
   209
      let val eq = cv ct in
wenzelm@23596
   210
        if Thm.is_reflexive eq then th
wenzelm@23596
   211
        else Drule.with_subgoal i (fconv_rule (arg1_conv (K eq))) th
wenzelm@23596
   212
      end
wenzelm@23596
   213
  | NONE => raise THM ("gconv_rule", i, [th]));
chaieb@23411
   214
haftmann@38668
   215
haftmann@38668
   216
fun tap_thy conv ct = conv (Thm.theory_of_cterm ct) ct;
haftmann@38668
   217
wenzelm@22905
   218
end;
boehmes@30136
   219
wenzelm@32843
   220
structure Basic_Conv: BASIC_CONV = Conv;
wenzelm@32843
   221
open Basic_Conv;