[Haskell-cafe] could not deduce Show compile error
Zoran Bošnjak
zoran.bosnjak at via.si
Fri Jun 2 13:32:47 UTC 2023
Dear haskell cafe members,
I would appreciate a suggestion how to fix compile error on this simple test program (I am using ghc 9.0.2).
The idea is to have 'data Flow a b' unrestricted and create necessary constraints only when running/interpreting the flow. The problem is obviously an intermediate type 'b' in 'Compose', where the 'Show' instance is not deduced.
I have a vague clue that some type families might be necessary to propagate Show constraint, or a type class with associated type family, but I don't
know exactly how.
---
import Prelude hiding ((.), id)
import Control.Category
data Flow a b where
Id :: Flow a a
Compose :: Flow a b -> Flow b c -> Flow a c
instance Category Flow where
id = Id
(.) = flip Compose
runFlow :: Show a => Flow a b -> a -> IO b
runFlow f x = case f of
Id -> print x >> pure x
Compose f1 f2 -> runFlow f1 x >>= runFlow f2
main :: IO ()
main = runFlow (Id >>> Id) ()
---
The error is:
• Could not deduce (Show b1) arising from a use of ‘runFlow’
from the context: Show a
bound by the type signature for:
runFlow :: forall a b. Show a => Flow a b -> a -> IO b
at a02.hs:13:1-42
Possible fix:
add (Show b1) to the context of the data constructor ‘Compose’
• In the second argument of ‘(>>=)’, namely ‘runFlow f2’
In the expression: runFlow f1 x >>= runFlow f2
In a case alternative: Compose f1 f2 -> runFlow f1 x >>= runFlow f2
|
16 | Compose f1 f2 -> runFlow f1 x >>= runFlow f2
| ^^^^^^^^^^
kind regards,
Zoran
More information about the Haskell-Cafe
mailing list