<div dir="ltr"><div>Below I have a contrived example. Please do not take this to be real world code.</div><div><br></div><div>f :: (Show a) => Int -> (Int -> a) -> Int -> IO a</div><div>f i g x = do</div><div> print i</div><div> return $ g (x + i)</div><div><br></div><div>foo :: Bool -> IO (Either [Int] [String])</div><div>foo b = do</div><div> let helper = f 2</div><div> if b then</div><div> Left <$> sequence (fmap (helper negate) [0,1])</div><div> else</div><div> Right <$> sequence (fmap (helper show) [0,1])</div><div><br></div><div>The above will fail stating</div><div><br></div><div>
<p class="">Example.hs:14:35:</p>
<p class=""> Couldn't match type ‘Int’ with ‘[Char]’</p>
<p class=""> Expected type: Int -> IO String</p>
<p class=""> Actual type: Int -> IO Int</p>
<p class=""> In the first argument of ‘fmap’, namely ‘(helper show)’</p>
<p class=""> In the first argument of ‘sequence’, namely</p>
<p class=""> ‘(fmap (helper show) [0, 1])’</p>
<p class=""><br></p>
<p class="">Example.hs:14:42:</p>
<p class=""> Couldn't match type ‘[Char]’ with ‘Int’</p>
<p class=""> Expected type: Int -> Int</p>
<p class=""> Actual type: Int -> String</p>
<p class=""> In the first argument of ‘helper’, namely ‘show’</p>
<p class=""> In the first argument of ‘fmap’, namely ‘(helper show)’</p>
<p class=""><br></p><p class="">However, if I simply change f's type signature to not have the typeclass constraint, the type checker is happy:</p><div>f :: Int -> (Int -> a) -> Int -> IO a<br></div></div><div><br></div><div>Another possibility to remove the problem is to remove the let statement and instead put the entire expression in.</div><div><br></div><div><div>foo :: Bool -> IO (Either [Int] [String])</div><div>foo b = do</div><div> if b then</div><div> Left <$> sequence (fmap (f 2 negate) [0,1])</div><div> else</div><div> Right <$> sequence (fmap (f 2 show) [0,1])</div></div><div><br></div><div>This seems to be a bug in the typechecker, and maybe it is a well known issue.</div><div><br></div><div>Can someone please confirm that this is a bug and whether or not it is known?</div><div><br></div><div>James</div><div><br></div></div>