[英]Haskell typeclass
I have a Haskell typeclass question. 我有一个Haskell类型类问题。 I can't munge the syntax to get this (seemingly reasonable) program to compile under GHC. 我无法通过语法来获得在GHC下编译的这个(看似合理的)程序。
import Control.Concurrent.MVar
blah1 :: [a] -> IO ([a])
blah1 = return
blah2 :: [a] -> IO (MVar [a])
blah2 = newMVar
class Blah b where
blah :: [a] -> IO (b a)
instance Blah [] where
blah = blah1
-- BOOM
instance Blah (MVar []) where
blah = blah2
main :: IO ()
main = do
putStrLn "Ok"
I get the following error message, which kind of makes sense, but I don't know how to fix it: 我收到以下错误消息,哪种有意义,但我不知道如何解决它:
`[]' is not applied to enough type arguments
Expected kind `*', but `[]' has kind `* -> *'
In the type `MVar []'
In the instance declaration for `Blah (MVar [])'
What you want isn't directly expressible. 你想要的不是直接表达的。 This is probably as close as you'll get: 这可能就像你得到的那样接近:
newtype MVarList a = MVarList (MVar [a])
instance Blah MVarList where
blah = fmap MVarList . newMVar
I was reading about Conal Elliott's TypeCompose library, and was reminded of this question. 我正在阅读Conal Elliott的TypeCompose库,并提醒了这个问题。 Here's an example of how you can do type-level composition. 这是一个如何进行类型级组合的示例。
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
module Main where
...
import Control.Compose
...
instance Blah (MVar `O` []) where
blah = liftM O . blah2
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.