Questions tagged [gadt]

Generalized algebraic data types, GADTs, are a more powerful form of algebraic data types that support custom constructor types.

397 questions
15
votes
2 answers

Total real-time persistent queues

Okasaki describes persistent real-time queues which can be realized in Haskell using the type data Queue a = forall x . Queue { front :: [a] , rear :: [a] , schedule :: [x] } where incremental rotations maintain the invariant length…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
15
votes
1 answer

Type inference with GADTs - a0 is untouchable

Lets say I have this program {-# LANGUAGE GADTs #-} data My a where A :: Int -> My Int B :: Char -> My Char main :: IO () main = do let x = undefined :: My a case x of A v -> print v -- print x compiles fine. But when I comment…
nh2
  • 24,526
  • 11
  • 79
  • 128
15
votes
1 answer

Write GADT record with constrained type

I have the following code that compiles in my program: {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE TypeFamilies #-} class (Show (Data a)) => HasData (a ::…
Thomas Eding
  • 35,312
  • 13
  • 75
  • 106
15
votes
3 answers

GADT's failed exhaustiveness checking

Consider the following code: data (:+:) f g a = Inl (f a) | Inr (g a) data A data B data Foo l where Foo :: Foo A data Bar l where Bar :: Bar B type Sig = Foo :+: Bar fun :: Sig B -> Int fun (Inr Bar) = 1 Even though fun is an exhaustive…
James Koppel
  • 1,587
  • 1
  • 10
  • 16
14
votes
1 answer

Phantom type makes pattern matching irrefutable, but that seemingly does not work inside do notation

Please look at the code. I believe using phantom type makes the pattern matching irrefutable so there is no need in MonadFail instance. {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} {-# OPTIONS_GHC -Wall #-} {-# OPTIONS_GHC…
amakarov
  • 524
  • 3
  • 16
14
votes
1 answer

Simplifying a GADT with Uniplate

I'm trying to answer this stackoverflow question, using uniplate as I suggested, but the only solution I've come up with so far is pretty ugly. This seems like a fairly common issue, so I wanted to know if there was a more elegant…
rampion
  • 87,131
  • 49
  • 199
  • 315
14
votes
1 answer

Encoding "Less Than" with Haskell

am hoping some Haskell experts can help clarify something. Is it possible to define Nat in the usual way (via @dorchard Singleton types in Haskell) data S n = Succ n data Z = Zero class Nat n instance Nat Z instance Nat n => Nat (S n) (or some…
Ranjit Jhala
  • 1,242
  • 8
  • 18
14
votes
1 answer

Haskell pattern matching on GADTs with Data Kinds

I have found that I really like combining GADTs with Data Kinds, as it gives me further type safety than before (for most uses, almost as good as Coq, Agda et al.). Sadly, pattern matching fails on the simplest of examples, and I could think of no…
Ramon Snir
  • 7,520
  • 3
  • 43
  • 61
14
votes
2 answers

GADTs vs. MultiParamTypeClasses

I'm trying to grasp GADTs, and I have looked at the GADTs example in GHC's manual. As far as I can tell, it is possible to do the same thing with MultiParamTypeClasses: {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, …
user1078763
  • 728
  • 5
  • 15
13
votes
1 answer

Can I make haskell GADT data constructor infix in derived Show?

Consider two data declarations: {-# LANGUAGE GADTs #-} data X = Int `Y` Int deriving Show data Z where W :: Int -> Int -> Z deriving Show main = do print (1 `Y` 2) print (3 `W` 4) Running the above program produces: 1…
Alexander Gorshenev
  • 2,769
  • 18
  • 33
13
votes
4 answers

Material for Learning GADT

I started reading about GADT in Haskell Wiki but didn't feel quite comfortable understanding it. Do you recommend a specific book chapter or a blog post explaining GADT for a Haskell beginner?
user210870
13
votes
1 answer

Type-safe Flow (State Machine)

I am trying to create a type-safe Question-Answer flow in Haskell. I am modeling QnA as a directed graph, similar to a FSM. Each node in the graph represent a question: data Node s a s' = Node { question :: Question a, process :: s -> a ->…
homam
  • 1,945
  • 1
  • 19
  • 26
13
votes
2 answers

What does GADT offer that cannot be done with OOP and generics?

Are GADTs in functional languages equivalent to traditional OOP + generics, or there is a scenario where there are correctness constrants easily enforced by GADT but hard or impossible to achieve using Java or C#? For example, this "well-typed…
Alex
  • 1,184
  • 7
  • 15
13
votes
1 answer

does this GADT actually have type role representational

This data type can have type role HCons' representational representational, which allows using coerce to add or remove newtypes applied to the elements, without needing to traverse the list. data HNil' = HNil' data HCons' a b = HCons' a b However…
aavogt
  • 1,308
  • 6
  • 14
13
votes
1 answer

How do you formulate n-ary product and sum types in this typed lambda calculus universe?

Here is the code where I'm having an issue: {-# LANGUAGE GADTs, LANGUAGE DataKinds #-} -- * Universe of Terms * -- type Id = String data Term a where Var :: Id -> Term a Lam :: Id -> Type -> Term b -> Term (a :-> b) App :: Term (a…
xiaolingxiao
  • 4,793
  • 5
  • 41
  • 88
1
2
3
26 27