我对这个棘手问题表示歉意。 我真心是想,但我是一只新鲜事,它显示了这一点。
I m 试图实施一种类似于一般的环形材料,并将其应用于Paul Hudak在《》书中描述的音乐数据结构。 The Haskell School of 音乐. 自然,半群体/大类动物群被遗漏,但我有以下相关法规:
newtype Duo a b = Duo {duo1 :: a} -- A ring-like structure
data Song a =
Primitive a
| Song a :+: Song a -- Composing Music Sequentially
| Song a :=: Song a deriving Eq -- Composing Music Concurrently (in parallel)
instance Functor Song where
fmap f (x :+: y) = fmap f x :+: fmap f y
fmap f (x :=: y) = fmap f x :=: fmap f y
fmap f (Primitive x) = Primitive $ f x
newtype Concurrent a = Concurrent {fromConcurrent :: Song a} deriving (Show)
newtype Sequential a = Sequential {fromSequential :: Song a} deriving (Show)
type Music a = Duo (Maybe (Concurrent a)) (Maybe (Sequential a))
I m attempting to write an instance of Functor for Music, as Duo doesn t have a Functor I thought this wouldn t be a problem.
I wrote the following implementation:
instance Functor Music where
fmap :: (a -> b) -> Music a -> Music b
fmap f = Duo . fmap (fmap f . fromConcurrent) . duo1
But I get the following error: • The type synonym ‘Music’ should have 1 argument, but has been given none • In the instance declaration for ‘Functor Music’ | 167 | instance Functor Music where | ^^^^^^^^^^^^^
Perhaps the problem is just that I m essentially writing a Functor for only a Subset of Duo, maybe this can only work if I make music a newtype instead of just a type synonym. I m really hoping to avoid that on account of the already egregious number of wrappers going on in this code, I d really hate to add another. Maybe you all can think of a way to implement a functor for Duo that makes sense, and makes this all work?
One thing I really don t understand is why it let s me make an instance for show:
instance (Show a) => Show (Music a) where
show (Duo Nothing) = "Silence"
show (Duo (Just (Concurrent x))) = show x