src/Provers/Arith/cancel_div_mod.ML
author haftmann
Wed May 05 18:25:34 2010 +0200 (2010-05-05)
changeset 36692 54b64d4ad524
parent 35267 8dfd816713c6
child 43594 ef1ddc59b825
permissions -rw-r--r--
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
nipkow@13516
     1
(*  Title:      Provers/Arith/cancel_div_mod.ML
nipkow@13516
     2
    Author:     Tobias Nipkow, TU Muenchen
nipkow@13516
     3
nipkow@13516
     4
Cancel div and mod terms:
nipkow@13516
     5
nipkow@13516
     6
  A + n*(m div n) + B + (m mod n) + C  ==  A + B + C + m
nipkow@13516
     7
haftmann@22997
     8
FIXME: Is parameterized but assumes for simplicity that + and * are named
haftmann@34974
     9
as in HOL
nipkow@13516
    10
*)
nipkow@13516
    11
nipkow@13516
    12
signature CANCEL_DIV_MOD_DATA =
nipkow@13516
    13
sig
nipkow@13516
    14
  (*abstract syntax*)
nipkow@13516
    15
  val div_name: string
nipkow@13516
    16
  val mod_name: string
nipkow@13516
    17
  val mk_binop: string -> term * term -> term
nipkow@13516
    18
  val mk_sum: term list -> term
nipkow@13516
    19
  val dest_sum: term -> term list
nipkow@13516
    20
  (*logic*)
nipkow@13516
    21
  val div_mod_eqs: thm list
nipkow@13516
    22
  (* (n*(m div n) + m mod n) + k == m + k and
nipkow@13516
    23
     ((m div n)*n + m mod n) + k == m + k *)
wenzelm@20044
    24
  val prove_eq_sums: simpset -> term * term -> thm
nipkow@13516
    25
  (* must prove ac0-equivalence of sums *)
nipkow@13516
    26
end;
nipkow@13516
    27
nipkow@13516
    28
signature CANCEL_DIV_MOD =
nipkow@13516
    29
sig
wenzelm@20044
    30
  val proc: simpset -> term -> thm option
nipkow@13516
    31
end;
nipkow@13516
    32
nipkow@13516
    33
nipkow@13516
    34
functor CancelDivModFun(Data: CANCEL_DIV_MOD_DATA): CANCEL_DIV_MOD =
nipkow@13516
    35
struct
nipkow@13516
    36
haftmann@35267
    37
fun coll_div_mod (Const(@{const_name Groups.plus},_) $ s $ t) dms =
nipkow@13516
    38
      coll_div_mod t (coll_div_mod s dms)
haftmann@35267
    39
  | coll_div_mod (Const(@{const_name Groups.times},_) $ m $ (Const(d,_) $ s $ n))
nipkow@13516
    40
                 (dms as (divs,mods)) =
nipkow@13516
    41
      if d = Data.div_name andalso m=n then ((s,n)::divs,mods) else dms
haftmann@35267
    42
  | coll_div_mod (Const(@{const_name Groups.times},_) $ (Const(d,_) $ s $ n) $ m)
nipkow@13516
    43
                 (dms as (divs,mods)) =
nipkow@13516
    44
      if d = Data.div_name andalso m=n then ((s,n)::divs,mods) else dms
nipkow@13516
    45
  | coll_div_mod (Const(m,_) $ s $ n) (dms as (divs,mods)) =
nipkow@13516
    46
      if m = Data.mod_name then (divs,(s,n)::mods) else dms
nipkow@13516
    47
  | coll_div_mod _ dms = dms;
nipkow@13516
    48
nipkow@13516
    49
nipkow@13516
    50
(* Proof principle:
nipkow@13516
    51
   1. (...div...)+(...mod...) == (div + mod) + rest
nipkow@13516
    52
      in function rearrange
nipkow@13516
    53
   2. (div + mod) + ?x = d + ?x
nipkow@13516
    54
      Data.div_mod_eq
nipkow@13516
    55
   ==> thesis by transitivity
nipkow@13516
    56
*)
nipkow@13516
    57
haftmann@35267
    58
val mk_plus = Data.mk_binop @{const_name Groups.plus};
haftmann@35267
    59
val mk_times = Data.mk_binop @{const_name Groups.times};
nipkow@13516
    60
nipkow@13516
    61
fun rearrange t pq =
nipkow@13516
    62
  let val ts = Data.dest_sum t;
nipkow@13516
    63
      val dpq = Data.mk_binop Data.div_name pq
nipkow@13516
    64
      val d1 = mk_times (snd pq,dpq) and d2 = mk_times (dpq,snd pq)
haftmann@36692
    65
      val d = if member (op =) ts d1 then d1 else d2
nipkow@13516
    66
      val m = Data.mk_binop Data.mod_name pq
haftmann@33040
    67
  in mk_plus(mk_plus(d,m),Data.mk_sum(ts |> remove (op =) d |> remove (op =) m)) end
nipkow@13516
    68
wenzelm@20044
    69
fun cancel ss t pq =
wenzelm@20044
    70
  let val teqt' = Data.prove_eq_sums ss (t, rearrange t pq)
haftmann@30937
    71
  in hd (Data.div_mod_eqs RL [teqt' RS transitive_thm]) end;
nipkow@13516
    72
wenzelm@20044
    73
fun proc ss t =
nipkow@13516
    74
  let val (divs,mods) = coll_div_mod t ([],[])
skalberg@15531
    75
  in if null divs orelse null mods then NONE
haftmann@33049
    76
     else case inter (op =) mods divs of
wenzelm@20044
    77
            pq :: _ => SOME (cancel ss t pq)
skalberg@15531
    78
          | [] => NONE
nipkow@13516
    79
  end
nipkow@13516
    80
wenzelm@17613
    81
end