我正在尝试做一个'UnMaybe'类型系列,但它没有编译(实例系列冲突)。
这是我的代码
{-# LANGUAGE TypeFamilies #-}
type family UnMaybe a :: *
type instance UnMaybe (Maybe a) = a
type instance UnMaybe a = a
错误消息
test.hs:4:16:
Conflicting family instance declarations:
type instance UnMaybe (Maybe a)
-- Defined at test.hs:4:16
type instance UnMaybe a
-- Defined at test.hs:5:15
我理解为什么它不起作用,无论如何还有另一种方法可以达到相同的结果(或者激活它的扩展会允许它吗?)
答案 0 :(得分:11)
如果你真的需要这个,你可以使用封闭式家庭(至少需要GHC 7.8):
{-# LANGUAGE TypeFamilies #-}
type family UnMaybe a :: * where
UnMaybe (Maybe a) = a
UnMaybe a = a
现在:
GHCi> :kind! UnMaybe (Maybe Int)
UnMaybe (Maybe Int) :: *
= Int
GHCi> :kind! UnMaybe Bool
UnMaybe Bool :: *
= Bool