考虑一个类似列表的类型:
{-# LANGUAGE ExistentialQuantification #-}
data ShowList = Nil | forall a. Show a => a :* ShowList
infixr 5 :*
myShowList :: ShowList
myShowList = 'x' :* () :* Nil
我希望能够使用语法糖来构建这些列表,如下所示:
myShowList :: ShowList
myShowList = ['x', ()]
但即使使用RebindableSyntax
and OverloadedLists
,我似乎仍然无法通过内置列表类型,试图统一所有元素的类型,这让我很困惑:
$ ghci -XRebindableSyntax -XOverloadedLists
GHCi, version 9.2.1: https://www.haskell.org/ghc/ :? for help
ghci> fromListN = fromListN
ghci> myShowList = ['x', ()]
<interactive>:2:20: error:
• Couldn't match expected type ‘GHC.Types.Char’
with actual type ‘()’
• In the expression: ()
In the expression: ['x', ()]
In an equation for ‘myShowList’: myShowList = ['x', ()]
ghci> :info []
type [] :: * -> *
data [] a = [] | a : [a]
-- Defined in ‘GHC.Types’
ghci>
有什么办法可以解决这个问题,还是我坚持写这些不加糖?