看起来正确的类型签名被拒绝在哪里
我一直在Haskell中继续传递样式。 将Cont
monad分开并移除类型包装器有助于我理解实现。 代码如下:
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Monad (ap)
newtype Cont r a = Cont {runCont :: (a -> r) -> r}
instance Functor (Cont r) where
fmap f ka = ka >>= pure . f
instance Applicative (Cont r) where
(<*>) = ap
pure = return
instance Monad (Cont r) where
ka >>= kab = Cont kb'
where
-- kb' :: (b -> r) -> r
kb' hb = ka' ha
where
-- ha :: (a -> r)
ha a = (kab' a) hb
-- ka' :: (a -> r) -> r
ka' = runCont ka
-- kab' :: a -> (b -> r) -> r
kab' a = runCont (kab a)
return a = Cont ka'
where
-- ka' :: (a -> r) -> r
ka' ha = ha a
这段代码编译(使用GHC 8.0.2),一切看起来都很好。 但是,只要我取消注释where块中的任何(现在已注释)类型签名,就会出现错误。 对于exapmle,如果我取消注释该行
-- ka' :: (a -> r) -> r
我得到:
• Couldn't match type ‘a’ with ‘a1’
‘a’ is a rigid type variable bound by
the type signature for:
(>>=) :: forall a b. Cont r a -> (a -> Cont r b) -> Cont r b
at cont.hs:19:6
‘a1’ is a rigid type variable bound by
the type signature for:
ka' :: forall a1. (a1 -> r) -> r
at cont.hs:27:14
Expected type: (a1 -> r) -> r
Actual type: (a -> r) -> r
• In the expression: runCont ka
In an equation for ‘ka'’: ka' = runCont ka
In an equation for ‘>>=’:
ka >>= kab
= Cont kb'
where
kb' hb
= ka' ha
where
ha a = (kab' a) hb
ka' :: (a -> r) -> r
ka' = runCont ka
kab' a = runCont (kab a)
• Relevant bindings include
ka' :: (a1 -> r) -> r (bound at cont.hs:28:7)
kab' :: a -> (b -> r) -> r (bound at cont.hs:31:7)
kab :: a -> Cont r b (bound at cont.hs:19:10)
ka :: Cont r a (bound at cont.hs:19:3)
(>>=) :: Cont r a -> (a -> Cont r b) -> Cont r b
(bound at cont.hs:19:3)
Failed, modules loaded: none.
所以我尝试使用类型通配符让编译器告诉我应该在那里放置什么类型的签名。 因此我尝试了以下签名:
ka' :: _
其中给出了以下错误:
• Found type wildcard ‘_’ standing for ‘(a -> r) -> r’
Where: ‘r’ is a rigid type variable bound by
the instance declaration at cont.hs:15:10
‘a’ is a rigid type variable bound by
the type signature for:
(>>=) :: forall a b. Cont r a -> (a -> Cont r b) -> Cont r b
at cont.hs:19:6
To use the inferred type, enable PartialTypeSignatures
• In the type signature:
ka' :: _
In an equation for ‘>>=’:
ka >>= kab
= Cont kb'
where
kb' hb
= ka' ha
where
ha a = (kab' a) hb
ka' :: _
ka' = runCont ka
kab' a = runCont (kab a)
In the instance declaration for ‘Monad (Cont r)’
• Relevant bindings include
ka' :: (a -> r) -> r (bound at cont.hs:28:7)
kab' :: a -> (b -> r) -> r (bound at cont.hs:31:7)
kab :: a -> Cont r b (bound at cont.hs:19:10)
ka :: Cont r a (bound at cont.hs:19:3)
(>>=) :: Cont r a -> (a -> Cont r b) -> Cont r b
(bound at cont.hs:19:3)
Failed, modules loaded: none.
现在我感到非常困惑,编译器告诉我ka'
的类型是(a -> r) -> r
但只要我尝试用这种类型明确注释ka'
,编译失败。 首先,我以为我错过了ScopedTypeVariables
但它似乎没有什么区别。
这里发生了什么?
编辑这类似的问题:“为什么这个功能,在where子句不进行类型检查?使用范围的类型变量”,它需要一个明确的forall
绑定类型变量。 但它不是重复的,因为这个问题的答案也需要InstanceSigs
扩展。
说得通。 毕竟,这些a
和b
来自哪里? 我们无法知道它们与(>>=)
的多态性和return
。 不过,如评论中所述,很容易修复:give (>>=)
和return
类型签名提及a
和b
,抛出必要的语言扩展,并且嘿presto:
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE InstanceSigs #-}
import Control.Monad (ap)
newtype Cont r a = Cont {runCont :: (a -> r) -> r}
instance Functor (Cont r) where
fmap f ka = ka >>= pure . f
instance Applicative (Cont r) where
(<*>) = ap
pure = return
instance Monad (Cont r) where
(>>=) :: forall a b. Cont r a -> (a -> Cont r b) -> Cont r b
ka >>= kab = Cont kb'
where
kb' :: (b -> r) -> r
kb' hb = ka' ha
where
ha :: (a -> r)
ha a = (kab' a) hb
ka' :: (a -> r) -> r
ka' = runCont ka
kab' :: a -> (b -> r) -> r
kab' a = runCont (kab a)
return :: forall a. a -> Cont r a
return a = Cont ka'
where
ka' :: (a -> r) -> r
ka' ha = ha a
我觉得有一个在所有这些一个龙珠笑话ka
S和ha
S,但逃脱笑话me
。
上一篇: Seemingly correct type signature refused in where block
下一篇: Why is my implementation of a map on a custom type not correct?