I am trying to write a prime number generator and use the MillerRabin formula to check if a number is prime before it returns the number back to me. Here is my code below:
primegen :: Int -> IO Integer primegen bits = fix $ \again -> do x <- fmap (.|. 1) $ randomRIO (2^(bits - 1), 2^bits - 1) if primecheck x then return x else again primesTo100 = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97] powerMod :: (Integral a, Integral b) => a -> a -> b -> a powerMod m _ 0 = 1 powerMod mxn | n > 0 = join (flip f (n - 1)) x `rem` m where f _ 0 y = y fady = gad where gbi | even i = g (b*b `rem` m) (i `quot` 2) | otherwise = fb (i-1) (b*y `rem` m) witns :: (Num a, Ord a, Random a) => Int -> a -> IO [a] witns xy = do g <- newStdGen let r = [9080191, 4759123141, 2152302898747, 3474749600383, 341550071728321] fs = [[31,73],[2,7,61],[2,3,5,7,11],[2,3,5,7,11,13],[2,3,5,7,11,13,17]] if y >= 341550071728321 then return $ take x $ randomRs (2,y-1) g else return $ snd.head.dropWhile ((<= y).fst) $ zip r fs primecheck :: Integer -> IO Bool primecheck n | n `elem` primesTo100 = return True | otherwise = do let pn = pred n e = uncurry (++) . second(take 1) . span even . iterate (`div` 2) $ pn try = return . all (\a -> let c = map (powerMod na) e in pn `elem` c || last c == 1) witns 100 n >>= try
I do not understand what is happening with IO Bool. And I get the following error ...
Couldn't match expected type `Bool' with actual type `IO Bool' In the return type of a call of `primecheck' In the expression: primecheck x In a stmt of a 'do' block: if primecheck x then return x else again
If I change IO Bool to regular Bool, they will give me the following:
Couldn't match expected type `Bool' with actual type `m0 a0'
Thanks for the help guys! I appreciate it.