
Hello all, Maybe there is something obvious I can't see, but I have this behavior for 6.8.2 ghci: $ghci ttest1p.hs GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. [1 of 1] Compiling Main ( ttest1p.hs, interpreted ) Ok, modules loaded: Main. *Main> encode' [1..100] Loading package array-0.1.0.0 ... linking ... done. Loading package bytestring-0.9.0.1 ... linking ... done. [1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,... // deleted *Main> B.pack [0..100] "\NUL\SOH\STX\ETX\EOT\ENQ\ACK\a\b\t\n\v\f\r\SO\SI\DLE\DC1\DC2\DC3\DC4\NAK\SYN\ETB\CAN\EM\SUB\ESC\FS\GS\RS\US !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcd" *Main> B.pack $ encode' [1..100] "*** Exception: divide by zero where ttest1p.hs: import qualified Data.ByteString as B encode' [] = [] encode' (x:xs) = if x==0 then 0:0:encode' xs else (x `mod` 256) : (x `div` 256) : encode' xs What is the difference, except list length and value structure? Where is my error? Thanks for any hint, Dusan

kolar:
Hello all,
Maybe there is something obvious I can't see, but I have this behavior for 6.8.2 ghci:
$ghci ttest1p.hs GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. [1 of 1] Compiling Main ( ttest1p.hs, interpreted ) Ok, modules loaded: Main. *Main> encode' [1..100] Loading package array-0.1.0.0 ... linking ... done. Loading package bytestring-0.9.0.1 ... linking ... done. [1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,... // deleted *Main> B.pack [0..100] "\NUL\SOH\STX\ETX\EOT\ENQ\ACK\a\b\t\n\v\f\r\SO\SI\DLE\DC1\DC2\DC3\DC4\NAK\SYN\ETB\CAN\EM\SUB\ESC\FS\GS\RS\US !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcd" *Main> B.pack $ encode' [1..100] "*** Exception: divide by zero
where ttest1p.hs:
import qualified Data.ByteString as B
encode' [] = [] encode' (x:xs) = if x==0 then 0:0:encode' xs else (x `mod` 256) : (x `div` 256) : encode' xs
What is the difference, except list length and value structure? Where is my error?
ByteStrings take Word8 values as input, so x `div` 256 , where 256 :: Word8, overflows to 0. -- Don

Thanks. I've realized that as soon as I was in my bed. ;-) Nevertheless, a question comes to me - shouldn't compiler report a warning? I know it cannot in the current state, but it should. :-) Quite dummy C compilers tell me I'm "loosing significant digits of number literals" if I'm doing that. Maybe, already seen in some other thread some time ago, the compiler should be less general/should know more about data types... Thanks and regards Dusan Don Stewart wrote:
kolar:
Hello all,
Maybe there is something obvious I can't see, but I have this behavior for 6.8.2 ghci:
$ghci ttest1p.hs GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. [1 of 1] Compiling Main ( ttest1p.hs, interpreted ) Ok, modules loaded: Main. *Main> encode' [1..100] Loading package array-0.1.0.0 ... linking ... done. Loading package bytestring-0.9.0.1 ... linking ... done. [1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,... // deleted *Main> B.pack [0..100] "\NUL\SOH\STX\ETX\EOT\ENQ\ACK\a\b\t\n\v\f\r\SO\SI\DLE\DC1\DC2\DC3\DC4\NAK\SYN\ETB\CAN\EM\SUB\ESC\FS\GS\RS\US !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcd" *Main> B.pack $ encode' [1..100] "*** Exception: divide by zero
where ttest1p.hs:
import qualified Data.ByteString as B
encode' [] = [] encode' (x:xs) = if x==0 then 0:0:encode' xs else (x `mod` 256) : (x `div` 256) : encode' xs
What is the difference, except list length and value structure? Where is my error?
ByteStrings take Word8 values as input, so x `div` 256 , where 256 :: Word8, overflows to 0.
-- Don
-- Dusan Kolar tel: +420 54 114 1238 UIFS FIT VUT Brno fax: +420 54 114 1270 Bozetechova 2 e-mail: kolar@fit.vutbr.cz Brno 612 66 Czech Republic --

Dusan Kolar
Thanks. I've realized that as soon as I was in my bed. ;-)
Nevertheless, a question comes to me - shouldn't compiler report a warning? I know it cannot in the current state, but it should. :-) Quite dummy C compilers tell me I'm "loosing significant digits of number literals" if I'm doing that. Maybe, already seen in some other thread some time ago, the compiler should be less general/should know more about data types...
While doing that is easy in this case, it becomes quite delicate in the general case. More precisely, it grows into the halting problem. -- (c) this sig last receiving data processing entity. Inspect headers for past copyright information. All rights reserved. Unauthorised copying, hiring, renting, public performance and/or broadcasting of this signature prohibited.

Achim Schneider
Nevertheless, a question comes to me - shouldn't compiler report a warning?
While doing that is easy in this case, it becomes quite delicate in the general case. More precisely, it grows into the halting problem.
I think it would be nice with a warning when a literal will overflow. I'd also like run time warnings for overflows, the fact that all the finite integral types wrap around is often an annoyance. I believe that for Int (where the exact precision is unspecified) programs that wrap will be non-portable. I wonder if it would be possible to write a debugging Data.Word library, where you could have something like: instance Num Word8 where (+) w1 w2 = let (i :: Integer) = fromIntegral w1 + fromIntegral w2 (w :: Word8) = w1 Word8.plus w2 in if (fromIntegral w /= i) then error ... else w : Then at least it could be used for testing (including QuickCheck) and lower the probability of overflow and wrap-around. I suspect that the Num instances are too tightly coupled with the data definitions to make such a module possible, though. -k -- If I haven't seen further, it is by standing in the footprints of giants

Safe Num wrappers for primitive types that throw exceptions on overflow would make a useful library. Any takers? -- Don kolar:
Thanks. I've realized that as soon as I was in my bed. ;-)
Nevertheless, a question comes to me - shouldn't compiler report a warning? I know it cannot in the current state, but it should. :-) Quite dummy C compilers tell me I'm "loosing significant digits of number literals" if I'm doing that. Maybe, already seen in some other thread some time ago, the compiler should be less general/should know more about data types...
Thanks and regards
Dusan
Don Stewart wrote:
kolar:
Hello all,
Maybe there is something obvious I can't see, but I have this behavior for 6.8.2 ghci:
$ghci ttest1p.hs GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. [1 of 1] Compiling Main ( ttest1p.hs, interpreted ) Ok, modules loaded: Main. *Main> encode' [1..100] Loading package array-0.1.0.0 ... linking ... done. Loading package bytestring-0.9.0.1 ... linking ... done. [1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,... // deleted *Main> B.pack [0..100] "\NUL\SOH\STX\ETX\EOT\ENQ\ACK\a\b\t\n\v\f\r\SO\SI\DLE\DC1\DC2\DC3\DC4\NAK\SYN\ETB\CAN\EM\SUB\ESC\FS\GS\RS\US !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcd" *Main> B.pack $ encode' [1..100] "*** Exception: divide by zero
where ttest1p.hs:
import qualified Data.ByteString as B
encode' [] = [] encode' (x:xs) = if x==0 then 0:0:encode' xs else (x `mod` 256) : (x `div` 256) : encode' xs
What is the difference, except list length and value structure? Where is my error?
ByteStrings take Word8 values as input, so x `div` 256 , where 256 :: Word8, overflows to 0.
-- Don
--
Dusan Kolar tel: +420 54 114 1238 UIFS FIT VUT Brno fax: +420 54 114 1270 Bozetechova 2 e-mail: kolar@fit.vutbr.cz Brno 612 66 Czech Republic
--

On Mon, May 19, 2008 at 1:22 PM, Don Stewart
Safe Num wrappers for primitive types that throw exceptions on overflow would make a useful library.
Any takers?
Sounds like fun. Neither the error messages nor the code will be pretty. Are we just interested in the Data.Int types and the Data.Word types? -Antoine

On Mon, May 19, 2008 at 9:44 PM, Antoine Latter
Sounds like fun. Neither the error messages nor the code will be pretty.
Are we just interested in the Data.Int types and the Data.Word types?
It's up on hackage here: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/checked I'm not sure how easy it would be to track down the error once you get it, but I suppose knowing is better than not knowing. I haven't made any attempt towards making the bounds-checking cheap, so this isn't a solution for deploying an application which needs fast integer operations with bounds checking. This is more for testing or verification. Let me know if you find this useful, or if you think it needs any work. -Antoine

On Tue, May 20, 2008 at 8:44 AM, Antoine Latter
It's up on hackage here: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/checked
One more thing - there's no "Data" instance for these types. Presumably it wouldn't be hard to add, I just wasn't familiar with the class, and the technique (if any) for lifting an instance of Data through a newtype wasn't immediately obvious form the class interface.

aslatter:
On Tue, May 20, 2008 at 8:44 AM, Antoine Latter
wrote: It's up on hackage here: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/checked
One more thing - there's no "Data" instance for these types. Presumably it wouldn't be hard to add, I just wasn't familiar with the class, and the technique (if any) for lifting an instance of Data through a newtype wasn't immediately obvious form the class interface.
With generalised newtype deriving, it should be fine to derive Data and Typeable: {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} module M where import Data.Generics newtype T = T Int deriving (Eq,Ord,Show,Read,Bounded,Enum,Num,Data,Typeable) x :: T x = 1 + 2 -- Don

Yes, this is obvious B.pack :: [GHC.Word.Word8] -> B.ByteString So, in B.pack $ encode' [1..100] GHCi expects "encode' [1..100]" to be of type [GHC.Word.Word8] Now, your encode' has type encode' :: Integral a => [a] -> [a] So, in encode' definition, all calculations are in Word8. Now, (256 :: Word8) is zero. No wonder you get a "divide by zero" error when trying to calculate "something `mod` 256" On 19 May 2008, at 00:14, Dušan Kolář wrote:
Hello all,
Maybe there is something obvious I can't see, but I have this behavior for 6.8.2 ghci:
$ghci ttest1p.hs GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. [1 of 1] Compiling Main ( ttest1p.hs, interpreted ) Ok, modules loaded: Main. *Main> encode' [1..100] Loading package array-0.1.0.0 ... linking ... done. Loading package bytestring-0.9.0.1 ... linking ... done. [1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,... // deleted *Main> B.pack [0..100] "\NUL\SOH\STX\ETX\EOT\ENQ\ACK\a\b\t\n\v\f\r\SO\SI\DLE \DC1\DC2\DC3\DC4\NAK\SYN\ETB\CAN\EM\SUB\ESC\FS\GS\RS\US !\"#$%&'()* +,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcd" *Main> B.pack $ encode' [1..100] "*** Exception: divide by zero
where ttest1p.hs:
import qualified Data.ByteString as B
encode' [] = [] encode' (x:xs) = if x==0 then 0:0:encode' xs else (x `mod` 256) : (x `div` 256) : encode' xs
What is the difference, except list length and value structure? Where is my error?
Thanks for any hint,
Dusan
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (7)
-
Achim Schneider
-
Antoine Latter
-
Don Stewart
-
Dusan Kolar
-
Dušan Kolář
-
Ketil Malde
-
Miguel Mitrofanov