Is there a way to use syntactic sugar to build lists of existential types?

Consider a list-like type like this one:

{-# LANGUAGE ExistentialQuantification #-}

data ShowList = Nil | forall a. Show a => a :* ShowList
infixr 5 :*

myShowList :: ShowList
myShowList = 'x' :* () :* Nil

I'd like to be able to use syntactic sugar to build these lists like this:

myShowList :: ShowList
myShowList = ['x', ()]

But even with RebindableSyntax and OverloadedLists, it looks like I'm stuck going through the built-in list type, which messes me up by trying to unify the types of all the elements:

$ 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> 

Is there any way around this, or am I stuck writing these out without sugar?

Upvotes: 3

Views: 84

Answers (0)

Related Questions