
Hi All: I am reading http://www.haskell.org/haskellwiki/Prime_numbers The code in sector "1 Bitwise prime sieve". I have 3 questions about it. 1) In function go, what does the number 46340 mean ? Is it sqrt(MAX_LONG) ? 2) We have this type definition : pureSieve :: Int -> Int Why there is no error (type mismatch) of this call in func main : pureSieve 10000000 3) In main again, what does expression [| x |] mean ? Why this cannot be execute in GHCi ? Thanks for any advice. Regards -------------- L.Guo 2007-08-13

On Tuesday 14 August 2007 00:22, L.Guo wrote:
2) We have this type definition : pureSieve :: Int -> Int Why there is no error (type mismatch) of this call in func main : pureSieve 10000000
The Haskell Report says that an Int covers at least the range [- 2^29, 2^29 - 1], which that number is well within . . . . why do you think it should report a type error? Alexis.

Because 10,000,000 is too large for a Int, it is always in type of Integer or some higher level data type. ------------------ L.Guo 2007-08-13 ------------------------------------------------------------- From: Alexis Hazell At: 2007-08-13 22:46:46 Subject: Re: [Haskell-cafe] A few questions on primes generating. On Tuesday 14 August 2007 00:22, L.Guo wrote:
2) We have this type definition : pureSieve :: Int -> Int Why there is no error (type mismatch) of this call in func main : pureSieve 10000000
The Haskell Report says that an Int covers at least the range [- 2^29, 2^29 - 1], which that number is well within . . . . why do you think it should report a type error? Alexis. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Mon, Aug 13, 2007 at 11:23:59PM +0800, L.Guo wrote:
Because 10,000,000 is too large for a Int, it is always in type of Integer or some higher level data type.
In Haskell, Int always supports at least -536,870,912 to 536,870,911. Also, large numbers don't (this is arguably a bug...) have restricted types: stefan@stefans:~$ ghc -e '1000000000000000000 :: Int' -1486618624 stefan@stefans:~$ Stefan

Stefan O'Rear wrote:
Also, large numbers don't (this is arguably a bug...) have restricted types:
stefan@stefans:~$ ghc -e '1000000000000000000 :: Int' -1486618624
So many other programming languages allow weird things to happen with numeric overflows... it would be nice if Haskell didn't.

Andrew Coppin wrote:
Stefan O'Rear wrote:
Also, large numbers don't (this is arguably a bug...) have restricted types:
stefan@stefans:~$ ghc -e '1000000000000000000 :: Int' -1486618624
So many other programming languages allow weird things to happen with numeric overflows... it would be nice if Haskell didn't.
Shall we have a GHC warning if it can detect those cases, either the Haskell report bounds or that of the target GHC is currently compiling to? (of course someone would have to implement it, and there would always be cases it didn't check, and the (non-portable) behavior is sometimes desired...) Hugs often does throw an exception (runtime error) rather than allow Int overflow (but not always). Isaac

L.Guo wrote:
Because 10,000,000 is too large for a Int
On my pitiful system,
maxBound::Int 2147483647 is certainly greater than 10000000 .
it is always in type of Integer or some higher level data type.
Haskell doesn't do static checking like that. In GHC on my system (where 10,000,000,000 is too large for an Int - note ten zeroes),
10000000000::Int 1410065408 (Hugs gives "Program error: arithmetic overflow")
Isaac

On 8/13/07, L.Guo
Hi All:
Hello,
I am reading http://www.haskell.org/haskellwiki/Prime_numbers
The code in sector "1 Bitwise prime sieve".
I have 3 questions about it.
1) In function go, what does the number 46340 mean ? Is it sqrt(MAX_LONG) ?
Yes, it appears so. In a 32 bit implementation I get: Prelude> sqrt $ fromIntegral (maxBound :: Int) 46340.950001051984
2) We have this type definition : pureSieve :: Int -> Int Why there is no error (type mismatch) of this call in func main : pureSieve 10000000
If you have integer literals in your program, the compiler sees a fromInteger in front of them. So the value is just converted to type Int automatically, because that is expected here. You can give different numeric default declarations in your own modules. Please see sections 10.3 (for overloaded literals) and 10.4 (for defaults) here: http://www.haskell.org/tutorial/numbers.html Sometimes you can get an overflow like this: Prelude> 100000000000000000000000 :: Int -159383552
3) In main again, what does expression [| x |] mean ? Why this cannot be execute in GHCi ?
It's Template Haskell, and is used there for some kind of optimisation (I think). Template Haskell needs to be enabled with a command line switch for it to work. Please see the documentation for more information. It's section 7.6 in your User's Guide. Though in this case you can probably just remove it to try out the program. Perhaps someone else can explain what actual effect it has here. Pekka

On 13/08/07, Pekka Karjalainen
On 8/13/07, L.Guo
wrote: Hi All:
Hello,
I am reading http://www.haskell.org/haskellwiki/Prime_numbers
The code in sector "1 Bitwise prime sieve".
I have 3 questions about it.
1) In function go, what does the number 46340 mean ? Is it sqrt(MAX_LONG) ?
Yes, it appears so. In a 32 bit implementation I get:
Prelude> sqrt $ fromIntegral (maxBound :: Int) 46340.950001051984
2) We have this type definition : pureSieve :: Int -> Int Why there is no error (type mismatch) of this call in func main : pureSieve 10000000
If you have integer literals in your program, the compiler sees a fromInteger in front of them. So the value is just converted to type Int automatically, because that is expected here.
You can give different numeric default declarations in your own modules. Please see sections 10.3 (for overloaded literals) and 10.4 (for defaults) here: http://www.haskell.org/tutorial/numbers.html
Sometimes you can get an overflow like this:
Prelude> 100000000000000000000000 :: Int -159383552
3) In main again, what does expression [| x |] mean ? Why this cannot be execute in GHCi ?
It's Template Haskell, and is used there for some kind of optimisation (I think). Template Haskell needs to be enabled with a command line switch for it to work. Please see the documentation for more information. It's section 7.6 in your User's Guide.
Though in this case you can probably just remove it to try out the program. Perhaps someone else can explain what actual effect it has here.
I think it just computes a single function call to pureSieve at compile time. I believe its origin is from making a point that when you stop comparing apples to apples it's easy to cheat (this code comes from a discussion on this list where someone insisted on adding optimizations to the, admittedly naive, algorithm in C# and comparing it without making the same optimizations in Haskell -- so someone, I forget who but I'm a search will turn it up, wrote a quick template Haskell "optimization" to make a point that we don't really get useful results unless we compare the same algorithms in both languages). In general you should probably ignore that TH bit and just call pureSieve normally. -- Sebastian Sylvan +44(0)7857-300802 UIN: 44640862
participants (8)
-
ajb@spamcop.net
-
Alexis Hazell
-
Andrew Coppin
-
Isaac Dupree
-
L.Guo
-
Pekka Karjalainen
-
Sebastian Sylvan
-
Stefan O'Rear