Hello, I’m attempting to create a straightforward example code that encapsulates a channel implementation.
Here is code:
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE TypeFamilies #-}
import Control.Concurrent
data HChan a = HChan { ch :: IO (Chan a) }
class Channable a where
data Impl a :: *
(-->) :: Impl a -> b -> IO () -- send
instance Channable (HChan a) where
data Impl (HChan a) = ImplChan (HChan a)
(-->) :: Impl (HChan a) -> a -> IO ()
(ImplChan hchan) --> val = do
ch <- ch hchan
writeChan ch val
The error output:
• Couldn't match expected type ‘forall b.
Impl (HChan a) -> b -> IO ()’
with actual type ‘Impl (HChan a) -> a -> IO ()’
• When checking that instance signature for ‘-->’
is more general than its signature in the class
Instance sig: Impl (HChan a) -> a -> IO ()
Class sig: forall b. Impl (HChan a) -> b -> IO ()
In the instance declaration for ‘Channable (HChan a)’
|
23 | (-->) :: Impl (HChan a) -> a -> IO ()
The method -->
signature seems not illegal in my view.
(-->) :: Impl a -> b -> IO () -- class defnition
(-->) :: Impl (HChan a) -> a -> IO () -- instance definition
I believed that the second type parameter b
could align with the second type parameter a
in the instance definition, but it appears the compiler does not share this view. Can anyone assist me with this issue?