tl;dr: I'm trying to rewrite some dependently-typed code that has a list of sigma-types in Haskell, and I can't seem to generate singletons for an existential, in other words this code fails:
data Foo :: Type where
Foo :: forall foo. Sing foo -> Foo
$(genSingletons [''Foo])
Longer version follows.
Assume this Idris code as a model:
data AddrType = Post | Email | Office
data AddrFields : AddrType -> Type where
PostFields : (city : String) -> (street : String) -> AddrFields Post
EmailFields : (email : String) -> AddrFields Email
OfficeFields : (floor : Int) -> (desk : Nat) -> AddrFields Office
Addr : Type
Addr = (t : AddrType ** AddrFields t)
someCoolPredicate : List AddrType -> Bool
data AddrList : List Addr -> Type where
MkAddrList : (lst : List Addr) -> {auto prf : So (someCoolPredicate lst)} -> AddrList lst
Basically, when we're given a value of type AddrList lst
, we know that lst : List Addr
, and that someCoolPredicate
holds for that list.
The closest I managed to achieve in modern Haskell is, assuming singletons-2.5:
import Data.Singletons.TH
import Data.Singletons.Prelude
import Data.Singletons.Prelude.List
data AddrType = Post | Email | Office
deriving (Eq, Ord, Show)
$(genSingletons [''AddrType])
$(singEqInstances [''AddrType])
data family AddrFields (a :: AddrType)
data instance AddrFields 'Post = PostFields { city :: String, street :: String } deriving (Eq, Ord, Show)
data instance AddrFields 'Email = EmailFields { email :: String } deriving (Eq, Ord, Show)
data instance AddrFields 'Office = OfficeFields { flr :: Int, desk :: Int} deriving (Eq, Ord, Show)
data Addr :: Type where
Addr :: { addrType :: Sing addrType
, addrTypeVal :: AddrType
, fields :: AddrFields addrType
} -> Addr
$(promote [d|
someCoolPredicate :: [Addr] -> Bool
someCoolPredicate = ...
|])
data AddrList :: [Addr] -> Type where
AddrList :: { addrs :: Sing addrs, prf :: SomeCoolPredicate addrs :~: 'True } -> AddrList addrs
But how do I actually construct a value of this type given an [Addr]
? In other words, how do I express something like the following in Idris?
*Addrs> MkAddrList [(Post ** PostFields "Foo" "Bar")]
MkAddrList [(Post ** PostFields "Foo" "Bar")] : AddrList [(Post ** PostFields "Foo" "Bar")]
The problem is that looks like I have to be able to do toSing
or equivalent on the list of Addr
, but $(genSingletons [''Addr])
fails. Indeed, even the code in the tl;dr section fails. So what should I do, except for abandoning this idea?