src/Pure/Proof/proof_rewrite_rules.ML
author berghofe
Mon Sep 30 16:40:57 2002 +0200 (2002-09-30 ago)
changeset 13608 9a6f43b8eae1
parent 13341 f15ed50d16cf
child 13646 46ed3d042ba5
permissions -rw-r--r--
Added function elim_vars.
berghofe@11522
     1
(*  Title:      Pure/Proof/proof_rewrite_rules.ML
berghofe@11522
     2
    ID:         $Id$
wenzelm@11539
     3
    Author:     Stefan Berghofer, TU Muenchen
wenzelm@11539
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
berghofe@11522
     5
berghofe@12906
     6
Simplification functions for proof terms involving meta level rules.
berghofe@11522
     7
*)
berghofe@11522
     8
berghofe@11522
     9
signature PROOF_REWRITE_RULES =
berghofe@11522
    10
sig
berghofe@12866
    11
  val rew : bool -> typ list -> Proofterm.proof -> Proofterm.proof option
berghofe@12866
    12
  val rprocs : bool -> (string * (typ list -> Proofterm.proof -> Proofterm.proof option)) list
berghofe@12906
    13
  val rewrite_terms : (term -> term) -> Proofterm.proof -> Proofterm.proof
berghofe@13341
    14
  val elim_defs : Sign.sg -> bool -> thm list -> Proofterm.proof -> Proofterm.proof
berghofe@13608
    15
  val elim_vars : (typ -> term) -> Proofterm.proof -> Proofterm.proof
berghofe@12237
    16
  val setup : (theory -> theory) list
berghofe@11522
    17
end;
berghofe@11522
    18
berghofe@11522
    19
structure ProofRewriteRules : PROOF_REWRITE_RULES =
berghofe@11522
    20
struct
berghofe@11522
    21
berghofe@11522
    22
open Proofterm;
berghofe@11522
    23
berghofe@12866
    24
fun rew b =
berghofe@12866
    25
  let
berghofe@12866
    26
    fun ? x = if b then Some x else None;
berghofe@12866
    27
    fun ax (prf as PAxm (s, prop, _)) Ts =
berghofe@12866
    28
      if b then PAxm (s, prop, Some Ts) else prf;
berghofe@12866
    29
    fun ty T = if b then
berghofe@12866
    30
        let val Type (_, [Type (_, [U, _]), _]) = T
berghofe@13257
    31
        in Some U end
berghofe@12866
    32
      else None;
berghofe@12866
    33
    val equal_intr_axm = ax equal_intr_axm [];
berghofe@12866
    34
    val equal_elim_axm = ax equal_elim_axm [];
berghofe@12866
    35
    val symmetric_axm = ax symmetric_axm [propT];
berghofe@11522
    36
berghofe@12866
    37
    fun rew' _ (PThm (("ProtoPure.rev_triv_goal", _), _, _, _) % _ %%
berghofe@12866
    38
        (PThm (("ProtoPure.triv_goal", _), _, _, _) % _ %% prf)) = Some prf
berghofe@12866
    39
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % _ % _ %%
berghofe@12866
    40
        (PAxm ("ProtoPure.equal_intr", _, _) % _ % _ %% prf %% _)) = Some prf
berghofe@12866
    41
      | rew' _ (PAxm ("ProtoPure.symmetric", _, _) % _ % _ %%
berghofe@12866
    42
        (PAxm ("ProtoPure.equal_intr", _, _) % A % B %% prf1 %% prf2)) =
berghofe@12866
    43
            Some (equal_intr_axm % B % A %% prf2 %% prf1)
berghofe@12002
    44
berghofe@12866
    45
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % Some (_ $ A) % Some (_ $ B) %%
berghofe@12002
    46
        (PAxm ("ProtoPure.combination", _, _) % Some (Const ("Goal", _)) %
berghofe@12866
    47
          _ % _ % _ %% (PAxm ("ProtoPure.reflexive", _, _) % _) %% prf1) %%
berghofe@12866
    48
        ((tg as PThm (("ProtoPure.triv_goal", _), _, _, _)) % _ %% prf2)) =
berghofe@12866
    49
        Some (tg %> B %% (equal_elim_axm %> A %> B %% prf1 %% prf2))
berghofe@12002
    50
berghofe@12866
    51
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % Some (_ $ A) % Some (_ $ B) %%
berghofe@12866
    52
        (PAxm ("ProtoPure.symmetric", _, _) % _ % _ %%
berghofe@12866
    53
          (PAxm ("ProtoPure.combination", _, _) % Some (Const ("Goal", _)) %
berghofe@12866
    54
             _ % _ % _ %% (PAxm ("ProtoPure.reflexive", _, _) % _) %% prf1)) %%
berghofe@12866
    55
        ((tg as PThm (("ProtoPure.triv_goal", _), _, _, _)) % _ %% prf2)) =
berghofe@12866
    56
        Some (tg %> B %% (equal_elim_axm %> A %> B %%
berghofe@12866
    57
          (symmetric_axm % ? B % ? A %% prf1) %% prf2))
berghofe@11522
    58
berghofe@12866
    59
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % Some X % Some Y %%
berghofe@11612
    60
        (PAxm ("ProtoPure.combination", _, _) % _ % _ % _ % _ %%
berghofe@11612
    61
          (PAxm ("ProtoPure.combination", _, _) % Some (Const ("==>", _)) % _ % _ % _ %%
berghofe@12866
    62
             (PAxm ("ProtoPure.reflexive", _, _) % _) %% prf1) %% prf2)) =
berghofe@12866
    63
        let
berghofe@12866
    64
          val _ $ A $ C = Envir.beta_norm X;
berghofe@12866
    65
          val _ $ B $ D = Envir.beta_norm Y
berghofe@12866
    66
        in Some (AbsP ("H1", ? X, AbsP ("H2", ? B,
berghofe@12866
    67
          equal_elim_axm %> C %> D %% incr_pboundvars 2 0 prf2 %%
berghofe@12866
    68
            (PBound 1 %% (equal_elim_axm %> B %> A %%
berghofe@12866
    69
              (symmetric_axm % ? A % ? B %% incr_pboundvars 2 0 prf1) %% PBound 0)))))
berghofe@12866
    70
        end
berghofe@11522
    71
berghofe@12866
    72
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % Some X % Some Y %%
berghofe@12866
    73
        (PAxm ("ProtoPure.symmetric", _, _) % _ % _ %%
berghofe@12866
    74
          (PAxm ("ProtoPure.combination", _, _) % _ % _ % _ % _ %%
berghofe@12866
    75
            (PAxm ("ProtoPure.combination", _, _) % Some (Const ("==>", _)) % _ % _ % _ %%
berghofe@12866
    76
               (PAxm ("ProtoPure.reflexive", _, _) % _) %% prf1) %% prf2))) =
berghofe@12866
    77
        let
berghofe@12866
    78
          val _ $ A $ C = Envir.beta_norm Y;
berghofe@12866
    79
          val _ $ B $ D = Envir.beta_norm X
berghofe@12866
    80
        in Some (AbsP ("H1", ? X, AbsP ("H2", ? A,
berghofe@12866
    81
          equal_elim_axm %> D %> C %%
berghofe@12866
    82
            (symmetric_axm % ? C % ? D %% incr_pboundvars 2 0 prf2)
berghofe@12866
    83
              %% (PBound 1 %% (equal_elim_axm %> A %> B %% incr_pboundvars 2 0 prf1 %% PBound 0)))))
berghofe@12866
    84
        end
berghofe@11522
    85
berghofe@12866
    86
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % Some X % Some Y %%
berghofe@11612
    87
        (PAxm ("ProtoPure.combination", _, _) % Some (Const ("all", _)) % _ % _ % _ %%
berghofe@11612
    88
          (PAxm ("ProtoPure.reflexive", _, _) % _) %%
berghofe@12866
    89
            (PAxm ("ProtoPure.abstract_rule", _, _) % _ % _ %% prf))) =
berghofe@12866
    90
        let
berghofe@12866
    91
          val Const (_, T) $ P = Envir.beta_norm X;
berghofe@12866
    92
          val _ $ Q = Envir.beta_norm Y;
berghofe@12866
    93
        in Some (AbsP ("H", ? X, Abst ("x", ty T,
berghofe@12866
    94
            equal_elim_axm %> incr_boundvars 1 P $ Bound 0 %> incr_boundvars 1 Q $ Bound 0 %%
berghofe@12866
    95
              (incr_pboundvars 1 1 prf %> Bound 0) %% (PBound 0 %> Bound 0))))
berghofe@12866
    96
        end
berghofe@12866
    97
berghofe@12866
    98
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % Some X % Some Y %%
berghofe@12866
    99
        (PAxm ("ProtoPure.symmetric", _, _) % _ % _ %%        
berghofe@12866
   100
          (PAxm ("ProtoPure.combination", _, _) % Some (Const ("all", _)) % _ % _ % _ %%
berghofe@12866
   101
            (PAxm ("ProtoPure.reflexive", _, _) % _) %%
berghofe@12866
   102
              (PAxm ("ProtoPure.abstract_rule", _, _) % _ % _ %% prf)))) =
berghofe@12866
   103
        let
berghofe@12866
   104
          val Const (_, T) $ P = Envir.beta_norm X;
berghofe@12866
   105
          val _ $ Q = Envir.beta_norm Y;
berghofe@12866
   106
          val t = incr_boundvars 1 P $ Bound 0;
berghofe@12866
   107
          val u = incr_boundvars 1 Q $ Bound 0
berghofe@12866
   108
        in Some (AbsP ("H", ? X, Abst ("x", ty T,
berghofe@12866
   109
          equal_elim_axm %> t %> u %%
berghofe@12866
   110
            (symmetric_axm % ? u % ? t %% (incr_pboundvars 1 1 prf %> Bound 0))
berghofe@12866
   111
              %% (PBound 0 %> Bound 0))))
berghofe@12866
   112
        end
berghofe@12866
   113
berghofe@12866
   114
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % Some A % Some C %%
berghofe@12866
   115
        (PAxm ("ProtoPure.transitive", _, _) % _ % Some B % _ %% prf1 %% prf2) %% prf3) =
berghofe@12866
   116
           Some (equal_elim_axm %> B %> C %% prf2 %%
berghofe@12866
   117
             (equal_elim_axm %> A %> B %% prf1 %% prf3))
berghofe@12866
   118
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % Some A % Some C %%
berghofe@12866
   119
        (PAxm ("ProtoPure.symmetric", _, _) % _ % _ %%
berghofe@12866
   120
          (PAxm ("ProtoPure.transitive", _, _) % _ % Some B % _ %% prf1 %% prf2)) %% prf3) =
berghofe@12866
   121
           Some (equal_elim_axm %> B %> C %% (symmetric_axm % ? C % ? B %% prf1) %%
berghofe@12866
   122
             (equal_elim_axm %> A %> B %% (symmetric_axm % ? B % ? A %% prf2) %% prf3))
berghofe@12866
   123
berghofe@12866
   124
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % _ % _ %%
berghofe@12866
   125
        (PAxm ("ProtoPure.reflexive", _, _) % _) %% prf) = Some prf
berghofe@12866
   126
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % _ % _ %%
berghofe@12866
   127
        (PAxm ("ProtoPure.symmetric", _, _) % _ % _ %%
berghofe@12866
   128
          (PAxm ("ProtoPure.reflexive", _, _) % _)) %% prf) = Some prf
berghofe@12866
   129
berghofe@12866
   130
      | rew' _ (PAxm ("ProtoPure.symmetric", _, _) % _ % _ %%
berghofe@12866
   131
        (PAxm ("ProtoPure.symmetric", _, _) % _ % _ %% prf)) = Some prf
berghofe@11522
   132
berghofe@12866
   133
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % _ % _ %%
berghofe@12866
   134
        (PAxm ("ProtoPure.equal_elim", _, _) % Some (_ $ A $ C) % Some (_ $ B $ D) %%
berghofe@12866
   135
          (PAxm ("ProtoPure.combination", _, _) % _ % _ % _ % _ %%
berghofe@12866
   136
            (PAxm ("ProtoPure.combination", _, _) % Some (Const ("==", _)) % _ % _ % _ %%
berghofe@12866
   137
              (PAxm ("ProtoPure.reflexive", _, _) % _) %% prf1) %% prf2) %% prf3) %% prf4) =
berghofe@12866
   138
          Some (equal_elim_axm %> C %> D %% prf2 %%
berghofe@12866
   139
            (equal_elim_axm %> A %> C %% prf3 %%
berghofe@12866
   140
              (equal_elim_axm %> B %> A %% (symmetric_axm % ? A % ? B %% prf1) %% prf4)))
berghofe@12866
   141
berghofe@12866
   142
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % _ % _ %%
berghofe@12866
   143
        (PAxm ("ProtoPure.symmetric", _, _) % _ % _ %%
berghofe@12866
   144
          (PAxm ("ProtoPure.equal_elim", _, _) % Some (_ $ A $ C) % Some (_ $ B $ D) %%
berghofe@12866
   145
            (PAxm ("ProtoPure.combination", _, _) % _ % _ % _ % _ %%
berghofe@12866
   146
              (PAxm ("ProtoPure.combination", _, _) % Some (Const ("==", _)) % _ % _ % _ %%
berghofe@12866
   147
                (PAxm ("ProtoPure.reflexive", _, _) % _) %% prf1) %% prf2) %% prf3)) %% prf4) =
berghofe@12866
   148
          Some (equal_elim_axm %> A %> B %% prf1 %%
berghofe@12866
   149
            (equal_elim_axm %> C %> A %% (symmetric_axm % ? A % ? C %% prf3) %%
berghofe@12866
   150
              (equal_elim_axm %> D %> C %% (symmetric_axm % ? C % ? D %% prf2) %% prf4)))
berghofe@11522
   151
berghofe@12866
   152
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % _ % _ %%
berghofe@12866
   153
        (PAxm ("ProtoPure.equal_elim", _, _) % Some (_ $ B $ D) % Some (_ $ A $ C) %%
berghofe@12866
   154
          (PAxm ("ProtoPure.symmetric", _, _) % _ % _ %%
berghofe@12866
   155
            (PAxm ("ProtoPure.combination", _, _) % _ % _ % _ % _ %%
berghofe@12866
   156
              (PAxm ("ProtoPure.combination", _, _) % Some (Const ("==", _)) % _ % _ % _ %%
berghofe@12866
   157
                (PAxm ("ProtoPure.reflexive", _, _) % _) %% prf1) %% prf2)) %% prf3) %% prf4) =
berghofe@12866
   158
          Some (equal_elim_axm %> D %> C %% (symmetric_axm % ? C % ? D %% prf2) %%
berghofe@12866
   159
            (equal_elim_axm %> B %> D %% prf3 %%
berghofe@12866
   160
              (equal_elim_axm %> A %> B %% prf1 %% prf4)))
berghofe@11522
   161
berghofe@12866
   162
      | rew' _ (PAxm ("ProtoPure.equal_elim", _, _) % _ % _ %%
berghofe@12866
   163
        (PAxm ("ProtoPure.symmetric", _, _) % _ % _ %%
berghofe@12866
   164
          (PAxm ("ProtoPure.equal_elim", _, _) % Some (_ $ B $ D) % Some (_ $ A $ C) %%
berghofe@12866
   165
            (PAxm ("ProtoPure.symmetric", _, _) % _ % _ %%
berghofe@12866
   166
              (PAxm ("ProtoPure.combination", _, _) % _ % _ % _ % _ %%
berghofe@12866
   167
                (PAxm ("ProtoPure.combination", _, _) % Some (Const ("==", _)) % _ % _ % _ %%
berghofe@12866
   168
                  (PAxm ("ProtoPure.reflexive", _, _) % _) %% prf1) %% prf2)) %% prf3)) %% prf4) =
berghofe@12866
   169
          Some (equal_elim_axm %> B %> A %% (symmetric_axm % ? A % ? B %% prf1) %%
berghofe@12866
   170
            (equal_elim_axm %> D %> B %% (symmetric_axm % ? B % ? D %% prf3) %%
berghofe@12866
   171
              (equal_elim_axm %> C %> D %% prf2 %% prf4)))
berghofe@11522
   172
berghofe@13257
   173
      | rew' _ ((prf as PAxm ("ProtoPure.combination", _, _) %
berghofe@13257
   174
        Some ((eq as Const ("==", T)) $ t) % _ % _ % _) %%
berghofe@13257
   175
          (PAxm ("ProtoPure.reflexive", _, _) % _)) =
berghofe@13257
   176
        let val (U, V) = (case T of
berghofe@13257
   177
          Type (_, [U, V]) => (U, V) | _ => (dummyT, dummyT))
berghofe@13257
   178
        in Some (prf %% (ax combination_axm [V, U] %> eq % ? eq % ? t % ? t %%
berghofe@13257
   179
          (ax reflexive_axm [T] % ? eq) %% (ax reflexive_axm [U] % ? t)))
berghofe@13257
   180
        end
berghofe@13257
   181
berghofe@12866
   182
      | rew' _ _ = None;
berghofe@12866
   183
  in rew' end;
berghofe@12866
   184
berghofe@12866
   185
fun rprocs b = [("Pure/meta_equality", rew b)];
berghofe@12866
   186
val setup = [Proofterm.add_prf_rprocs (rprocs false)];
berghofe@11522
   187
berghofe@12906
   188
berghofe@12906
   189
(**** apply rewriting function to all terms in proof ****)
berghofe@12906
   190
berghofe@12906
   191
fun rewrite_terms r =
berghofe@12906
   192
  let
berghofe@12906
   193
    fun rew_term Ts t =
berghofe@12906
   194
      let
berghofe@12906
   195
        val frees = map Free (variantlist
berghofe@12906
   196
          (replicate (length Ts) "x", add_term_names (t, [])) ~~ Ts);
berghofe@12906
   197
        val t' = r (subst_bounds (frees, t));
berghofe@12906
   198
        fun strip [] t = t
berghofe@12906
   199
          | strip (_ :: xs) (Abs (_, _, t)) = strip xs t;
berghofe@12906
   200
      in
berghofe@12906
   201
        strip Ts (foldl (uncurry lambda o Library.swap) (t', frees))
berghofe@12906
   202
      end;
berghofe@12906
   203
berghofe@12906
   204
    fun rew Ts (prf1 %% prf2) = rew Ts prf1 %% rew Ts prf2
berghofe@12906
   205
      | rew Ts (prf % Some t) = rew Ts prf % Some (rew_term Ts t)
berghofe@12906
   206
      | rew Ts (Abst (s, Some T, prf)) = Abst (s, Some T, rew (T :: Ts) prf)
berghofe@12906
   207
      | rew Ts (AbsP (s, Some t, prf)) = AbsP (s, Some (rew_term Ts t), rew Ts prf)
berghofe@12906
   208
      | rew _ prf = prf
berghofe@12906
   209
berghofe@12906
   210
  in rew [] end;
berghofe@12906
   211
berghofe@12906
   212
berghofe@12906
   213
(**** eliminate definitions in proof ****)
berghofe@12906
   214
berghofe@12906
   215
fun vars_of t = rev (foldl_aterms
berghofe@12906
   216
  (fn (vs, v as Var _) => v ins vs | (vs, _) => vs) ([], t));
berghofe@12906
   217
berghofe@12906
   218
fun insert_refl defs Ts (prf1 %% prf2) =
berghofe@12906
   219
      insert_refl defs Ts prf1 %% insert_refl defs Ts prf2
berghofe@12906
   220
  | insert_refl defs Ts (Abst (s, Some T, prf)) =
berghofe@12906
   221
      Abst (s, Some T, insert_refl defs (T :: Ts) prf)
berghofe@12906
   222
  | insert_refl defs Ts (AbsP (s, t, prf)) =
berghofe@12906
   223
      AbsP (s, t, insert_refl defs Ts prf)
berghofe@12906
   224
  | insert_refl defs Ts prf = (case strip_combt prf of
berghofe@12906
   225
        (PThm ((s, _), _, prop, Some Ts), ts) =>
berghofe@12906
   226
          if s mem defs then
berghofe@12906
   227
            let
berghofe@12906
   228
              val vs = vars_of prop;
berghofe@12906
   229
              val tvars = term_tvars prop;
berghofe@12906
   230
              val (_, rhs) = Logic.dest_equals prop;
berghofe@12906
   231
              val rhs' = foldl betapply (subst_TVars (map fst tvars ~~ Ts)
berghofe@12906
   232
                (foldr (fn p => Abs ("", dummyT, abstract_over p)) (vs, rhs)),
berghofe@12906
   233
                map the ts);
berghofe@12906
   234
            in
berghofe@12906
   235
              change_type (Some [fastype_of1 (Ts, rhs')]) reflexive_axm %> rhs'
berghofe@12906
   236
            end
berghofe@12906
   237
          else prf
berghofe@12906
   238
      | (_, []) => prf
berghofe@12906
   239
      | (prf', ts) => proof_combt' (insert_refl defs Ts prf', ts));
berghofe@12906
   240
berghofe@13341
   241
fun elim_defs sign r defs prf =
berghofe@12906
   242
  let
berghofe@12906
   243
    val tsig = Sign.tsig_of sign;
berghofe@13341
   244
    val defs' = map (Logic.dest_equals o prop_of o Drule.abs_def) defs
berghofe@12906
   245
    val defnames = map Thm.name_of_thm defs;
berghofe@13341
   246
    val f = if not r then I else
berghofe@13341
   247
      let
berghofe@13341
   248
        val cnames = map (fst o dest_Const o fst) defs';
berghofe@13341
   249
        val thms = flat (map (fn (s, ps) =>
berghofe@13341
   250
            if s mem defnames then []
berghofe@13341
   251
            else map (pair s o Some o fst) (filter_out (fn (p, _) =>
berghofe@13341
   252
              null (add_term_consts (p, []) inter cnames)) ps))
berghofe@13341
   253
          (Symtab.dest (thms_of_proof Symtab.empty prf)))
berghofe@13341
   254
      in Reconstruct.expand_proof sign thms end
berghofe@12906
   255
  in
berghofe@13341
   256
    rewrite_terms (Pattern.rewrite_term tsig defs' [])
berghofe@13341
   257
      (insert_refl defnames [] (f prf))
berghofe@12906
   258
  end;
berghofe@12906
   259
berghofe@13608
   260
berghofe@13608
   261
(**** eliminate all variables that don't occur in the proposition ****)
berghofe@13608
   262
berghofe@13608
   263
fun elim_vars mk_default prf =
berghofe@13608
   264
  let
berghofe@13608
   265
    val prop = Reconstruct.prop_of prf;
berghofe@13608
   266
    val vars = fold_proof_terms add_term_vars snd ([], prf) \\ term_vars prop;
berghofe@13608
   267
    val inst = map (fn Var (ixn, T) => (ixn, list_abs
berghofe@13608
   268
      (apfst (map (pair "x")) (apsnd mk_default (strip_type T))))) vars
berghofe@13608
   269
  in
berghofe@13608
   270
    norm_proof (Envir.Envir {iTs = Vartab.empty, asol = Vartab.make inst,
berghofe@13608
   271
      maxidx = 0}) prf
berghofe@13608
   272
  end;
berghofe@13608
   273
berghofe@11522
   274
end;