带括号的Haskell newtype

时间:2013-07-25 08:48:34

标签: haskell newtype

我试图理解Monads made difficult中的解释,我很难搞清楚以下新类型定义:

newtype (FComp g f) x = FComp { unCompose :: g (f x) }

instance (Functor b c f, Functor a b g) => Functor a c (FComp g f) where
  fmap f (FComp xs) = FComp $ fmap (fmap f) xs

我无处可见解释newtype意味着用括号中的表达式代替类型声明。因此,我无法弄清楚fmap函数的定义是什么意思。我也不明白为什么定义unCompose字段访问器但从未使用过。我觉得我缺少一些newtype的基本语义。

2 个答案:

答案 0 :(得分:15)

一点点测试:

newtype (FComp g f) x = FComp { unCompose :: g (f x) }
newtype FComp2 g f x = FComp2 { unCompose2 :: g (f x) }

*Main> :i FComp
newtype FComp g f x = FComp {unCompose :: g (f x)}
        -- Defined at Test.hs:34:10
*Main> :i FComp2
newtype FComp2 g f x = FComp2 {unCompose2 :: g (f x)}
    -- Defined at Test.hs:35:9

所以括号真的不会改变任何东西。没有它们就是一样。

对于uncompose,它只是一个名称来展开newtype而不使数据构造函数显式。在您发布的代码段中,他们使用模式匹配,但不希望导出实现详细信息,因此提供unCompose以使用FComp的内容。这与data定义中的相同,只是newtype只需要一个字段而不是 0..n

答案 1 :(得分:11)

你可以这样写:

newtype (FComp g f) x = FComp { unCompose :: g (f x) }
像这样:

newtype FComp g f x = FComp (g (f x))
unCompose (FComp it) = it

这是因为类型应用程序具有与普通应用程序相同的语法属性,即:

a b c  = (a b) c

适用于值a,b,c和类型a,b,c。