Opened 4 years ago

Last modified 23 months ago

#10514 new feature request

Generic for existential types

Reported by: andreas.abel Owned by:
Priority: normal Milestone:
Component: Core Libraries Version: 7.10.1
Keywords: Generics Cc: Iceland_jack, core-libraries-committee@…, RyanGlScott, ekmett
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: #8560 Differential Rev(s):
Wiki Page:

Description (last modified by andreas.abel)

I have some use for Generic for an existential type which is constraint to be Generic.

{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE StandaloneDeriving #-}

import GHC.Generics

data U = forall a. (Generic a) => U a
  deriving (Generic)  -- TRY 1
    -- Can't make a derived instance of ‘Generic U’:
    --   Constructor ‘U’ has existentials or constraints in its type
    --   Possible fix: use a standalone deriving declaration instead

deriving instance Generic U -- TRY 2
    -- Can't make a derived instance of ‘Generic U’:
    --   U must be a vanilla data constructor
    -- In the stand-alone deriving instance for ‘Generic U’

data D1Ser
data C1_0Ser

instance Generic U where -- TRY 3
  type Rep U = D D1Ser (C1 C1_0Ser (S1 NoSelector (Rep a)))
    -- Not in scope: type variable ‘a’

-- How to bring the existential type `a' into scope?

Change History (9)

comment:1 Changed 4 years ago by andreas.abel

Description: modified (diff)

comment:2 Changed 4 years ago by thomie

Owner: ekmett deleted

comment:3 Changed 4 years ago by RyanGlScott

Cc: RyanGlScott ekmett added

comment:4 Changed 4 years ago by dreixel

I might be wrong, but I don't think there's any hope for supporting existentials with the current approach of GHC.Generics (especially when their kind is * and they show up as arguments to constructors).

comment:5 Changed 4 years ago by goldfire

Something tells me this is an open research problem...

comment:6 Changed 4 years ago by goldfire

See also #8560

comment:7 Changed 4 years ago by simonpj

Keywords: Generics added

comment:8 Changed 3 years ago by RyanGlScott

I think that we'd need new language extensions (QuantifiedConstraints and ImplicationConstraints) to do this properly. See my comment on #5927.

comment:9 Changed 23 months ago by Iceland_jack

Cc: Iceland_jack added
Note: See TracTickets for help on using tickets.