看似正确的类型签名拒绝在哪里阻止

时间:2017-06-03 20:46:00

标签: haskell ghc

我一直在讨论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块中的任何(现在注释的)类型签名,我就会收到错误。例如,如果我取消注释该行

      -- 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,但似乎没有什么区别。

这里发生了什么?

修改 这类似于问题“Why does this function that uses a scoped type variable in a where clause not typecheck?”,因为它需要显式forall来绑定类型变量。但是,这不是重复,因为此问题的答案也需要InstanceSigs扩展名。

1 个答案:

答案 0 :(得分:4)

有道理。毕竟,那些ab来自哪里?我们无法知道它们与(>>=)return的多态性有关。但是,正如评论中所述,它很容易修复:提供(>>=)return类型签名,提及ab,并使用必要的语言扩展,嘿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

我觉得在所有这些kaha中有一个龙珠笑话,但这个笑话逃脱了me