Generalized algebraic data types, GADTs, are a more powerful form of algebraic data types that support custom constructor types.
Questions tagged [gadt]
397 questions
7
votes
1 answer
Can't deduce f = f₁ from f x = f₁ y?
{-# LANGUAGE GADTs #-}
data Foo x y where
Composition :: Foo b c -> Foo a b -> Foo a c
FMap :: Functor f => (a->b) -> Foo (f a) (f b)
asFunction :: Foo a b -> a->b
asFunction (FMap m) = fmap m
-- asFunction (Composition (FMap m) (FMap n)) =…

leftaroundabout
- 117,950
- 5
- 174
- 319
7
votes
1 answer
How can I get GHC to generate instances of Data.Typeable for GADTs with Typeable in the context?
Suppose I have the following code:
{-# LANGUAGE GADTs, DeriveDataTypeable, StandaloneDeriving #-}
import Data.Typeable
class Eq t => OnlyEq t
class (Eq t, Typeable t) => BothEqAndTypeable t
data Wrapper a where
Wrap :: BothEqAndTypeable a => a…

yatima2975
- 6,580
- 21
- 42
7
votes
1 answer
When were GADTs introduced in GHC?
When were GADTs introduced in GHC? (version + date)
Also, are they still considered a language extension or are they now part of the Haskell standard proper?

Jon Smark
- 2,528
- 24
- 31
6
votes
2 answers
Multiple types for f in this picture?
https://youtu.be/brE_dyedGm0?t=1362
data T a where
T1 :: Bool -> T Bool
T2 :: T a
f x y = case x of
T1 x -> True
T2 -> y
Simon is saying that f could be typed as T a -> a -> a, but I would think the return value MUST be a Bool since…

nick
- 119
- 4
6
votes
3 answers
How to 'show' unshowable types?
I am using data-reify and graphviz to transform an eDSL into a nice graphical representation, for introspection purposes.
As simple, contrived example, consider:
{-# LANGUAGE GADTs #-}
data Expr a where
Constant :: a -> Expr a
Map :: (other ->…

Qqwy
- 5,214
- 5
- 42
- 83
6
votes
4 answers
Unwrapping an existentially quantified GADT
I have a custom value type Value labeled by its type ValType:
data ValType
= Text
| Bool
data Value (tag :: ValType) where
T :: Text -> Value 'Text
B :: Bool -> Value 'Bool
and I would like to define a function that unwraps an…

Poscat
- 565
- 3
- 15
6
votes
2 answers
Why does a wildcard match work when enumerating all cases doesn't?
Consider this code:
{-# LANGUAGE GADTs #-}
data P t where
PA :: P Int
PB :: P Double
PC :: P Char
isA PA = True
isA _ = False
It compiles and works fine. Now consider this code:
{-# LANGUAGE GADTs #-}
data P t where
PA :: P Int
PB :: P…

Joseph Sible-Reinstate Monica
- 45,431
- 5
- 48
- 98
6
votes
1 answer
Failable match on a seemingly irrefutable GADT pattern
Continuing my crusade against MonadFail being a constraint in my monad, I'm now faced with the following:
data Thing :: Bool -> Type where
TrueThing :: Thing 'True
FalseThing :: Thing 'False
myFunc :: Monad m => m ()
myFunc = do
TrueThing <-…

JB.
- 40,344
- 12
- 79
- 106
6
votes
1 answer
Pattern match on a data family in Haskell
I have wrapped up whole data family in a single existential:
data Type = Numeric | Boolean
data family Operator (t :: Type)
data instance Operator 'Numeric = Add | Sub
data instance Operator 'Boolean = And | Or
data AnyOp where
AnyOp ::…

radrow
- 6,419
- 4
- 26
- 53
6
votes
2 answers
How can I ‘convince’ GHC that I've excluded a certain case?
I have the following toy implementation of a non-empty list (NEList) datatype:
-- A type to describe whether or not a list is empty.
data Emptiness :: Type where
Empty :: Emptiness
NotEmpty :: Emptiness
-- The list itself. Note the…

AJF
- 11,767
- 2
- 37
- 64
6
votes
3 answers
rigid type variable trouble/suspect Impredicativity
Following on from this q about GADTs, I'm trying to build an EDSL (for the example in the paper) but without GADTs. I have got something working that avoids doubling-up the datatypes for the AST; but instead it seems to be doubling-up the code. So I…

AntC
- 2,623
- 1
- 13
- 20
6
votes
1 answer
Coercible and existential
data T t where
A :: Show (t a) => t a -> T t
B :: Coercible Int (t a) => t a -> T t
f :: T t -> String
f (A t) = show t
g :: T t -> Int
g (B t) = coerce t
Why does f compile but g generate an error like follows? I'm using GHC 8.4.
• Couldn't…

NioBium
- 583
- 3
- 10
6
votes
4 answers
How to make fixed-length vectors instance of Applicative?
I recently learned about promotion and decided to try writing vectors.
{-# LANGUAGE DataKinds, GADTs, KindSignatures #-}
module Vector where
data Nat = Next Nat | Zero
data Vector :: Nat -> * -> * where
Construct :: t -> Vector n t -> Vector…

Liisi
- 329
- 1
- 6
6
votes
1 answer
Constructors with variable number of arguments
Using a few extensions, I can do something like this:
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
type family TF (a :: Bool) where
TF 'True = Int
TF 'False = Double
data D a…

Clinton
- 22,361
- 15
- 67
- 163
6
votes
1 answer
Haskell: GADT with UNPACK Pragma
UNPACK supports normal data types, as shown in the following:
data T = T {-# UNPACK #-} ! Int
But is there a way to use the UNPACK Pragma with GADT?

Ray Qiu
- 171
- 5