Q:

Is this a Ghc(i) Bug?!, 19 - 1 not equal to 18?

please help me understand this:
I want to generate all prime-roots for a "quotient group" (I hope that's "prime Restklassengruppe" in english ;))
import Data.List (nub)  
primeroots rclass = filter (\(x,y) -> (18) == y) produceAllCandidates where
  produceAllCandidates = map makeOneCandidate [1..rclass] where
    makeOneCandidate elem = (elem,  (length (nub (map (\x -> (mod (elem ^ x) rclass)) [0..(rclass -1)]))))

Then I run this in ghci and get
> primeroots 19
> [(2,18),(3,18),(10,18),(13,18),(14,18),(15,18)]

but if I change the 18 in the first line to rclass - 1 (which should yield 18, and does this for all but 15 anyways):
primeroots rclass = filter (\(x,y) -> (rclass - 1) == y) produceAllCandidates where

and run it with the same Argument
> primeroots 19
> [(2,18),(3,18),(10,18),(13,18),(14,18)]

Why do I get different results when changing 18 to (rclass - 1) when I call it so, that rclass will be 19 (which then should make (rclass -1) 18?!
Thanks in advance

A:

It seems to be a matter of types; forcing the output type to be Int makes a difference:
Prelude Data.List> primeroots 19
[(2,18),(3,18),(10,18),(13,18),(14,18),(15,18)]
Prelude Data.List> primeroots 19 :: [(Int, Int)]
[(2,18),(3,18),(10,18),(13,18),(14,18)]

Narrowing it down:
Prelude Data.List> let foo rclass elem = map (\x -> (mod (elem ^ x) rclass)) [0..(rclass -1)]
Prelude Data.List> foo (19 :: Int) 15
[1,15,16,12,9,2,11,13,5,18,4,3,7,10,17,8,6,5,9]
Prelude Data.List> foo (19 :: Integer) 15
[1,15,16,12,9,2,11,13,5,18,4,3,7,10,17,8,6,14,1]

Note that the last two elements are different. These are formed by mod (15 ^ 17) 19 and mod (15 ^ 18) 19, respectively, which overflow the Int type.
To do this correctly for small numbers, write some kind of powMod function, or be a bit wasteful and force the type to be Integer.

