Generalized algebraic data types, GADTs, are a more powerful form of algebraic data types that support custom constructor types.
Questions tagged [gadt]
397 questions
1
vote
2 answers
Explanation of untyped terms
I have a continuation exercise from uni for my Haskell subject where I've been given the following:
data Expr = Con Value
| And Expr Expr
data Value = IntValue Int
| BoolValue Bool
est :: Expr -> Val
est (Con v) = v
est (And…

SNpn
- 2,157
- 8
- 36
- 53
0
votes
1 answer
Cannot solve a type mismatching with GADT
I have following type definition of x with a GADT.
type x = X : 'a option -> x
and I'm trying to write a function to get the value of option accompanying with tag X
I first tried the following
let get = fun (X a)->a
and obtained the following…

mofu
- 1
- 1
0
votes
1 answer
How close can I get to GADTs in Rust?
As for 2023 GADTs are not officially supported in Rust. However, I wonder how much of their power I can get using other utilities, such as traits and type-members.
To throw in some context and make the question less vague, here are some examples of…

radrow
- 6,419
- 4
- 26
- 53
0
votes
1 answer
Succinct way to use GADTs for exhaustiveness-checking in Scala?
I'm looking for the same behavior as the following OCaml code, where the compiler understands the match is exhaustive because we've expressed that the two scrutinees must have the same type:
type circle
type rectangle
type _ figure =
| Circle :…

Max Heiber
- 14,346
- 12
- 59
- 97
0
votes
2 answers
Mapping rules with GADTS/Type Families
These are simple representations of sample Input and Output types:
Input = I1 Int | I2 String
Output = OA String | OB Bool | OC
Parameters here are just for the sake of greater realism. :)
I would like to get a function that maps Input to…

WHITECOLOR
- 24,996
- 37
- 121
- 181
0
votes
1 answer
scala3: Methods on enum case, possible?
I'm trying to define an ADT (tagged union type, actually).
enum MyEnum{
case MyFirstCase(value:String) extends MyEnum{
def someMethod = "whatever"
}
}
But the compiler complains... Is it possible to add methods specific cases of a Scala3…

caeus
- 3,084
- 1
- 22
- 36
0
votes
2 answers
(Scala 2.12.8) pattern type is incompatible with expected type for parameterized type inside of parameterized class
(Scala 2.12.8)
Full Example
So lets say you have some "TypeEvidence" for some specific concrete types:
sealed trait TypeEvidence[+T]
object TypeEvidence{
case object DoubleType extends TypeEvidence[Double]
case object LongType extends…

memorableusername
- 442
- 2
- 11
0
votes
1 answer
What typing logic compels me to have the same return-types for GADT function alternatives?
In this question, it was explained to me how to correctly pattern-match GADT types to get alternative return types using universally quantified type variables and locally abstract types. Notwithstanding the misconceptions in my original example, the…

wss
- 13
- 4
0
votes
1 answer
Ambiguous type using parameterized types Haskell
I have a pretty straightforward function that takes a parameterized data type and returns the same type:
{-# LANGUAGE ScopedTypeVariables #-}
class IntegerAsType a where
value :: a -> Integer
newtype (Num a, IntegerAsType n) => PolyRing a n =…

crockeea
- 21,651
- 10
- 48
- 101
0
votes
1 answer
Why use objects instead of records in GADT?
I'm looking at GADT definitions, and they use objects instead of records. I was wondering why, as it seems like objects are pretty much never used in general in OCaml. Is there a difference?
For example:
type (_, _, _) Basic.t +=
| Field :…

David 天宇 Wong
- 3,724
- 4
- 35
- 47
0
votes
1 answer
How to only implement part of the parameters of the MultiParamTypeClasses
How to write Haskell code like (with language extension GADTs and MultiParamTypeClasses):
class MyClass f a where
func :: a -> f a
data MyData a where
Cons1 :: a -> MyData a
Cons2 :: MyData a
instance MyClass MyData a where
…

Jw C
- 171
- 2
- 6
0
votes
1 answer
How to define a function that uses a list which product of numbers is the same as the amount of element in matrix in Haskell using GADTs?
I have a matrix representation of
data ListN (dim :: Nat) a where
Nil :: ListN Zero a
Cons :: a -> ListN n a -> ListN (Succ n) a
infixr 5 `Cons`
data Tensor (dims :: [Nat]) a where
Dense :: (Product dims ~ n) => ListN n a -> Tensor…

Joes de Jonge
- 161
- 6
0
votes
0 answers
Use generalized function on value defined at runtime
I'm trying to generalize one step of state machine computations by using sub-state machines.
Problem X is that I want some of the state machine functions to have different implementations based on some value A, that is only known at runtime.
My…

Yarick
- 326
- 3
- 14
0
votes
0 answers
How to change the type with GADTs in Haskell
Following the answer in https://stackoverflow.com/a/26084087/8142021
I used the following haskell code to define a Modal Logic with GADTs.
data Plain
data Mod
data Formula t where
Prop :: {propName :: String} -> Formula t
Neg :: Formula t ->…

sfx
- 103
- 9
0
votes
1 answer
GADTs but not existential quantification
The following code containing existential types does not compile
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ExistentialQuantification #-}
module TestGadt ()…

fakedrake
- 6,528
- 8
- 41
- 64