Deriving Typeable and Data for GADTs?
Asked Answered
W

1

7

Using:

{-# LANGUAGE GADTs #-}                                                                                                                                                                                                                       
{-# LANGUAGE StandaloneDeriving #-}                                                                                                                                                                                                          
{-# LANGUAGE DeriveDataTypeable #-} 

And given the following datatype:

data Event a where                                                                                                                                                                                                                           
    PureE  :: a                   -> Event a                                                                                                                                                                                                 
    MapE   :: (a -> b) -> Event a -> Event b                                                                                                                                                                                                 

deriving instance Typeable Event                                                                                                                                                                                                             
deriving instance Data a => Data (Event a)  

My goal is to use the uniplate package which requires the Data instance.

Is GHC able to derive Typeable and Data automatically? Since 7.8 GHC should be able to do so and afaik at least for Typeable it is mandatory.

I could probably write my own Data instance ... but why do if GHC can just derive it for me?

Warga answered 30/11, 2014 at 14:21 Comment(5)
What is the question here? You seem to know how to use StandaloneDeriving, so I'm not sure what you're asking.Hammett
deriving does not work for non Haskell98 data types. By using b in MapE I left that territory.Warga
#12574169Hammett
#15531920Hammett
Yep ... found those before, but as one even states: "I found a rather unclean way to work around the problem" :(Warga
T
0

This seems to work with GHC 8.10.7:

{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveDataTypeable #-}

import Data.Data

data Event a where
    PureE  :: a                   -> Event a
    MapE   :: (a -> b) -> Event a -> Event b

deriving instance Typeable Event
deriving instance (forall b. Data b) => Data (Event a)

The trick is the quantified constraint (forall b. Data b) => ..., which allows GHC to instantiate Data b for any type b on-demand. It's like a "local instance declaration."

Temikatemp answered 1/11, 2022 at 22:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.