键入系列:顶级与关联

时间:2015-01-31 03:26:57

标签: haskell types

我刚刚开始学习类型系列。 GHC文档指出顶级和相关类型系列具有相同的功能,但我写的代码在顶层的行为与家族关联时的行为不同。编译并运行良好:

{-# LANGUAGE TypeFamilies #-}
module Test where

-- type family R a
-- type instance R Maybe = Int

class C' a where
  type R a
  getInt' :: a Int
  getBool' :: R a -> a Bool

instance C' Maybe where
  type R Maybe = Int
  getInt' = Just 3
  getBool' i = Just $ i < 10

printer :: IO ()
printer = print $ (getBool' 5 :: Maybe Bool)

但是这给了我一个类型错误:

{-# LANGUAGE TypeFamilies #-}
module Test where

type family R a
type instance R Maybe = Int

class C' a where
  -- type R a
  getInt' :: a Int
  getBool' :: R a -> a Bool

instance C' Maybe where
  -- type R Maybe = Int
  getInt' = Just 3
  getBool' i = Just $ i < 10

printer :: IO ()
printer = print $ (getBool' 5 :: Maybe Bool)

这些看起来和我一模一样;为什么要编译而另一个不编?

1 个答案:

答案 0 :(得分:5)

如果你注释那种,那么第二个就可以了:

type family R (a :: * -> *)

我认为没有任何理由说明为什么只能推断出相关类型系列的正确类型。