Questions tagged [gadt]

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

397 questions
12
votes
1 answer

The pattern with functions like `bool`, `either`, etc

I recently learned about GADTs and their notation: E.g. data Maybe a where Nothing :: Maybe a Just :: a -> Maybe a data Either a b where Left :: a -> Either a b Right :: b -> Either a b data Bool where False :: Bool True ::…
hgiesel
  • 5,430
  • 2
  • 29
  • 56
12
votes
2 answers

How to make catamorphisms work with parameterized/indexed types?

I recently learned a bit about F-algebras: https://www.fpcomplete.com/user/bartosz/understanding-algebras. I wanted to lift this functionality to more advanced types (indexed and higher-kinded). Furthermore, I checked "Giving Haskell a Promotion"…
Mathijs Kwik
  • 1,227
  • 9
  • 12
12
votes
4 answers

Static Guarantee on Key/Value Relationships in Data.Map

I want to make a special smart constructor for Data.Map with a certain constraint on the types of key/value pair relationships. This is the constraint I tried to express: {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, DataKinds…
cdk
  • 6,698
  • 24
  • 51
11
votes
1 answer

Proving a type inequality to GHC

For educational purposes, I have been trying to reconstruct an example from the book "Type-Driven Development with Idris" (namely RemoveElem.idr) in Haskell via use of various language extensions and singleton types. The gist of it is a function…
romanb
  • 6,391
  • 2
  • 20
  • 19
11
votes
2 answers

Simple lambda calculus DSL using GADTs in OCaml

How do you define a simple lambda calculus-like DSL in OCaml using GADTs? Specifically, I can't figure out how to properly define the type checker to translate from an untyped AST to a typed AST nor can I figure out the correct type for the context…
wyer33
  • 6,060
  • 4
  • 23
  • 53
10
votes
4 answers

Haskell: Heterogeneous list for data with phantom variable

I'm learning about existential quantification, phantom types, and GADTs at the moment. How do I go about creating a heterogeneous list of a data type with a phantom variable? For example: {-# LANGUAGE GADTs #-} {-# LANGUAGE…
gspindles
  • 187
  • 6
10
votes
2 answers

Transform a GADT without constraints to another GADT with constraints when such constraints hold

Can we transform a GADT without a given constraint on its constructors to a GADT that does have the said constraint? I want to do this because I want to get a deep-embedding of Arrows and do some interesting things with the representation that (for…
Alessandro Vermeulen
  • 1,321
  • 1
  • 9
  • 28
9
votes
2 answers

Clarification on Existential Types in Haskell

I am trying to understand Existential types in Haskell and came across a PDF http://www.ii.uni.wroc.pl/~dabi/courses/ZPF15/rlasocha/prezentacja.pdf Please correct my below understandings that I have till now. Existential Types not seem to be…
Pawan Kumar
  • 1,443
  • 2
  • 16
  • 30
9
votes
1 answer

Performance implications of using GADTs

When answering a question with a suggestion to use GADTs, some questions with regards to performance came up in the comments. The question involved a typeclass PlotValue: class PlotValue a where value :: a -> Double and my answer suggested…
yatima2975
  • 6,580
  • 21
  • 42
9
votes
2 answers

OCaml's GADT and many type variables

I'm trying to model a card game in OCaml (let's assume that it is a solitaire game for the sake of simplicity). A given state of this game is represented by a value of type game. Then I will define a function moves : game -> move list that gives…
Pteromys
  • 1,441
  • 2
  • 12
  • 29
9
votes
1 answer

Runtime comparison of types for lifting polymorphic data structures into GADTs

Suppose we define a GADT for comparison of types: data EQT a b where Witness :: EQT a a Is it then possible to declare a function eqt with the following type signature: eqt :: (Typeable a, Typeable b) => a -> b -> Maybe (EQT a b) ...such that…
user492451
9
votes
2 answers

Modular Arithmetic using Haskell Type-Families or GADTs?

I frequently have occasion to perform modular arithmetic in Haskell, where the modulus is usually large and often prime (like 2000000011). Currently, I just use functions like (modAdd m a b), (modMul m a b), (modDiv m a b) etc. But that is rather…
CarlEdman
  • 398
  • 2
  • 14
9
votes
2 answers

Converting an untyped AST for a simple typed language into a GADT

I have an ADT representing the AST for a simple language: data UTerm = UTrue | UFalse | UIf UTerm UTerm UTerm | UZero | USucc UTerm | UIsZero UTerm This data structure can represent invalid terms that don't follow the…
Gavin Wahl
  • 1,193
  • 8
  • 21
9
votes
2 answers

Creating GADT expression in OCaml

There is my toy GADT expression: type _ expr = | Num : int -> int expr | Add : int expr * int expr -> int expr | Sub : int expr * int expr -> int expr | Mul : int expr * int expr -> int expr | Div : int expr * int expr -> int expr | Lt …
Stas
  • 11,571
  • 9
  • 40
  • 58
9
votes
0 answers

Promoting complex GADTs

I've been toying around with -XDataKinds recently, and was wondering why Foo below won't be automatically promoted: {-# LANGUAGE GADTs , DataKinds , KindSignatures #-} import Data.HList data Foo a where Foo :: Bar a => a -> Foo…
Athan Clark
  • 3,886
  • 2
  • 21
  • 39
1 2
3
26 27