# HG changeset patch # User haftmann # Date 1681214346 0 # Node ID fb3d81bd9803b04e6bde98c839b52ef67d69c293 # Parent ae9e6218443dda4b7da49e5c842d9fd8254f803d some remarks on division diff -r ae9e6218443d -r fb3d81bd9803 src/HOL/Euclidean_Rings.thy --- a/src/HOL/Euclidean_Rings.thy Tue Apr 11 11:59:02 2023 +0000 +++ b/src/HOL/Euclidean_Rings.thy Tue Apr 11 11:59:06 2023 +0000 @@ -1654,6 +1654,13 @@ subsection \Division on \<^typ>\int\\ +text \ + The following specification of integer division rounds towards minus infinity + and is advocated by Donald Knuth. See \cite{leijen01} for an overview and + terminology of different possibilities to specify integer division; + there division rounding towards minus infinitiy is named ``F-division''. +\ + subsubsection \Basic instantiation\ instantiation int :: "{normalization_semidom, idom_modulo}" diff -r ae9e6218443d -r fb3d81bd9803 src/HOL/Library/Rounded_Division.thy --- a/src/HOL/Library/Rounded_Division.thy Tue Apr 11 11:59:02 2023 +0000 +++ b/src/HOL/Library/Rounded_Division.thy Tue Apr 11 11:59:06 2023 +0000 @@ -11,12 +11,34 @@ \odd l \ \l\ mod 2 = 1\ for l :: int by (simp flip: odd_iff_mod_2_eq_one) +text \ + \noindent The following specification of division on integers centers + the modulus around zero. This is useful e.g.~to define division + on Gauss numbers. + N.b.: This is not mentioned \cite{leijen01}. +\ + definition rounded_divide :: \int \ int \ int\ (infixl \rdiv\ 70) where \k rdiv l = sgn l * ((k + \l\ div 2) div \l\)\ definition rounded_modulo :: \int \ int \ int\ (infixl \rmod\ 70) where \k rmod l = (k + \l\ div 2) mod \l\ - \l\ div 2\ +text \ + \noindent Example: @{lemma \k rmod 5 \ {-2, -1, 0, 1, 2}\ by (auto simp add: rounded_modulo_def)} +\ + +lemma signed_take_bit_eq_rmod: + \signed_take_bit n k = k rmod (2 ^ Suc n)\ + by (simp only: rounded_modulo_def power_abs abs_numeral flip: take_bit_eq_mod) + (simp add: signed_take_bit_eq_take_bit_shift) + +text \ + \noindent Property @{thm signed_take_bit_eq_rmod [no_vars]} is the key to generalize + rounded division to arbitrary structures satisfying \<^class>\ring_bit_operations\, + but so far it is not clear what practical relevance that would have. +\ + lemma rdiv_mult_rmod_eq: \k rdiv l * l + k rmod l = k\ proof - @@ -118,11 +140,6 @@ by (simp add: rounded_modulo_def algebra_simps) qed -lemma signed_take_bit_eq_rmod: - \signed_take_bit n k = k rmod (2 ^ Suc n)\ - by (simp only: rounded_modulo_def power_abs abs_numeral flip: take_bit_eq_mod) - (simp add: signed_take_bit_eq_take_bit_shift) - lemma rmod_less_divisor: \k rmod l < \l\ - \l\ div 2\ if \l \ 0\ using that pos_mod_bound [of \\l\\] by (simp add: rounded_modulo_def) diff -r ae9e6218443d -r fb3d81bd9803 src/HOL/Library/Signed_Division.thy --- a/src/HOL/Library/Signed_Division.thy Tue Apr 11 11:59:02 2023 +0000 +++ b/src/HOL/Library/Signed_Division.thy Tue Apr 11 11:59:06 2023 +0000 @@ -47,6 +47,13 @@ end +text \ + \noindent The following specification of division is named ``T-division'' in \cite{leijen01}. + It is motivated by ISO C99, which in turn adopted the typical behavior of + hardware modern in the beginning of the 1990ies; but note ISO C99 describes + the instance on machine words, not mathematical integers. +\ + instantiation int :: signed_division begin diff -r ae9e6218443d -r fb3d81bd9803 src/HOL/Library/Word.thy --- a/src/HOL/Library/Word.thy Tue Apr 11 11:59:02 2023 +0000 +++ b/src/HOL/Library/Word.thy Tue Apr 11 11:59:06 2023 +0000 @@ -624,6 +624,11 @@ subsection \Bit-wise operations\ +text \ + The following specification of word division just lifts the pre-existing + division on integers named ``F-Division'' in \cite{leijen01}. +\ + instantiation word :: (len) semiring_modulo begin diff -r ae9e6218443d -r fb3d81bd9803 src/HOL/Library/document/root.bib --- a/src/HOL/Library/document/root.bib Tue Apr 11 11:59:02 2023 +0000 +++ b/src/HOL/Library/document/root.bib Tue Apr 11 11:59:06 2023 +0000 @@ -43,6 +43,12 @@ publisher = {Springer}, series = {LNCS 664}} +@article{leijen01, + author = {Leijen, Daan}, + title = {Division and Modulus for Computer Scientists}, + year = 2001, + url = {https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf}} + @InProceedings{LochbihlerStoop2018, author = {Andreas Lochbihler and Pascal Stoop}, title = {Lazy Algebraic Types in {Isabelle/HOL}}, diff -r ae9e6218443d -r fb3d81bd9803 src/HOL/document/root.bib --- a/src/HOL/document/root.bib Tue Apr 11 11:59:02 2023 +0000 +++ b/src/HOL/document/root.bib Tue Apr 11 11:59:06 2023 +0000 @@ -60,3 +60,9 @@ year = 1993, publisher = {Springer}, series = {LNCS 664}} + +@article{leijen01, + author = {Leijen, Daan}, + title = {Division and Modulus for Computer Scientists}, + year = 2001, + url = {https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf}}