monads - Haskell error: Couldn't match expected type `Bool' against inferred type `IO b' -
i not understand problem is. 'a' not bool , should not bool. why bool expected?
code:
probablyprime n 0 = false probablyprime n t = <- randomrio(3, n-1 :: integer) let comp = defcomp(a,n) let ret = (not comp) && (probablyprime n t-1) return ret defcomp n = xcon1 && xcon2 (s,m) = findsm n x = a^m `mod` n xcon1 = x /= 1 || x /= n-1 xcon2 = comploop x n s comploop x n 0 = false comploop x n s = x1 || (comploop x n (s-1)) x1 = (x^2 `mod` n) == 1 findsm n = (s,m) m = findm n s = n/m findm n = m f = (logbase 2 n) - (truncate (logbase 2 n)) m' = 2^f m = m_ify m' m_ify m | m mod 1 == 0 = m | otherwise = m_ify (m*2)
error:
couldn't match expected type `bool' against inferred type `io b' in stmt of 'do' expression: <- randomrio (3, n - 1 :: integer) in expression: { <- randomrio (3, n - 1 :: integer); let comp = defcomp ...; let ret = (not comp) && (probablyprime n t - 1); return ret } in definition of `probablyprime': probablyprime n t = { <- randomrio (3, n - 1 :: integer); let comp = ...; let ret = ...; .... }
probablyprime n 0 = false
this tells haskell return type of probablyprime
bool
. in second case, you're dealing monads , returning io bool
, types don't match.
change false
return false
, work.
you have change
let ret = (not comp) && (probablyprime n t-1)
to
prob <- probablyprime n (t-1) let ret = (not comp) && prob
or like
ret <- liftm ((not comp) &&) (probablyprime n (t-1))
as andrew jaffe pointed out.
Comments
Post a Comment