Petras Purlys
Petras Purlys

Reputation: 1155

Polymorphic Data Families in Haskell

I would like to define a data family that supports polymorphism for explicitly uninstantiated cases:

data family Foo a

-- handles some specific case
data instance Foo Int = CreateInt Int Int String

-- handles all other cases
data instance Foo bar = CreateBar bar

Is this possible?

Upvotes: 2

Views: 74

Answers (1)

leftaroundabout
leftaroundabout

Reputation: 120711

Use a closed type family. Unfortunately, this requires an extra newtype wrapper:

newtype Foo a = CreateFoo {getFoo :: Foo' a}

type family Foo' a where
  Foo' Int = IntFoo
  Foo' bar = Barbar bar

data IntFoo = CreateInt Int Int String
data Barbar bar = CreateBar bar

Upvotes: 6

Related Questions