Questions tagged [data-kinds]

For Haskell questions about involving the `-XDataKinds` extension in GHC. With -XDataKinds, GHC automatically promotes every suitable datatype to be a kind, and its (value) constructors to be type constructors.

155 questions
7
votes
2 answers

How to create a "kind class" in Haskell, or ad-hoc polymorphism at the type-level using type families

I'm studying the type family features of Haskell, and type level computation. It appears it's quite easy to get parametric polymorphism at the type-level using PolyKinds: {-# LANGUAGE DataKinds, TypeFamilies, KindSignatures, GADTs, TypeOperators,…
CMCDragonkai
  • 6,222
  • 12
  • 56
  • 98
7
votes
0 answers

Normalize type family instance within Template Haskell splice

I'm using the genifunctors package to generate a functor instance for a type whose definition involves type families. The first module defines the data type itself: {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE DataKinds…
vlopez
  • 594
  • 4
  • 9
6
votes
0 answers

Syntax for using datatype indexed by Nat

The following code tries to refine Clash's Unsigned type family at index 4 into Digit: import Clash.Prelude {-@ type Digit = {v : Unsigned 4 | v <= 9 } @-} type Digit = Unsigned 4 {-@ foo :: Digit -> Digit @-} foo = id @Digit This leads to the…
Cactus
  • 27,075
  • 9
  • 69
  • 149
6
votes
2 answers

Using a promoted data constructor as a phantom parameter

In Maguire's Thinking with Types, p. 29, there's an example of how to use a promoted data constructor as a phantom parameter. Here's a module that I wrote based on the example in the book. {-# LANGUAGE DataKinds #-} module Main where import …
mhwombat
  • 8,026
  • 28
  • 53
6
votes
1 answer

Type-level list of a single type level-tuple in Haskell

Using DataKinds and TypeOperators, I can make type level-tuples of types, and type-level lists of types, but I cannot nest them: > :k '['(Int, Int), '(Int, Int)] error: parse error on input ‘'’` I can make a list of multiple tuples: > :k…
Greg C
  • 75
  • 1
  • 4
6
votes
3 answers

Sequence over heterogeneous list in Haskell

Consider following definition of a HList: infixr 5 :> data HList (types :: [*]) where HNil :: HList '[] (:>) :: a -> HList l -> HList (a:l) And a type family Map to map over typelevel lists: type family Map (f :: * -> *) (xs :: [*]) where …
6
votes
1 answer

Pattern match phantom type

I'm trying to implement a CurrencyQty type that acts like a number tagged at compile time: data Currency = Usd | Eur | Gbp data CurrencyQty (a :: Currency) = CurrencyQty Double deriving (Num) And now I want to implement a generic conversion…
Eddie Wang
  • 63
  • 3
6
votes
2 answers

Understanding this definition of HList

I'm relatively new to Haskell, and I'm trying to understand one of the definitions of HList. data instance HList '[] = HNil newtype instance HList (x ': xs) = HCons1 (x, HList xs) pattern HCons x xs = HCons1 (x, xs) I have a couple specific…
lcmylin
  • 2,552
  • 2
  • 19
  • 31
6
votes
1 answer

How to specify the type for a heterogenous collection in a GADT formulated AST?

I'd like to make a typed AST for a dynamic language. At present, I'm stuck on handling collections. Here's a representative code sample: {-# LANGUAGE GADTs #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE…
troutwine
  • 3,721
  • 3
  • 28
  • 62
6
votes
3 answers

DataKinds and type class instances

The following example is a boiled-down version of my real-life problem. It seems to be in some way similar to Retrieving information from DataKinds constrained existential types, but I could not quite get the answers I was seeking. Suppose we have a…
Tobias Weck
  • 269
  • 1
  • 8
6
votes
1 answer

Defining custom type families over the Nat kind

How does one define new computation over types of kind GHC.TypeLits.Nat? I am hoping to be able to define a type family type family WIDTH (n :: Nat) :: Nat such that WIDTH 0 ~ 0 and WIDTH (n+1) ~ log2 n
Cactus
  • 27,075
  • 9
  • 69
  • 149
6
votes
2 answers

Can I write a function using DataKinds that returns a value of type encoded by the parameter?

Let's say I have a Currency type: data Currency = USD | EUR | YEN and a Money type that stores an int, and is parameterized by a given Currency (Currency is promoted to a kind with the DataKinds extension). data Money :: Currency -> * where …
Ramith Jayatilleka
  • 2,132
  • 16
  • 25
6
votes
2 answers

Building values dynamically with GADTs using Data Kinds

Why is it harder to build values with datakinds, while it's relatively easy to pattern match with them? {-# LANGUAGE KindSignatures , GADTs , DataKinds , Rank2Types #-} data Nat = Zero | Succ Nat data…
banx
  • 4,376
  • 4
  • 30
  • 34
6
votes
2 answers

How exactly do kind lists work?

I've been reading on vinyl recently, which uses weird "list of kinds" kinda types. After reading a bit on kinds and vinyl, I've gotten somewhat of an intuitive understanding of them, and I've been able to hack this together {-# LANGUAGE DataKinds, …
Cubic
  • 14,902
  • 5
  • 47
  • 92
6
votes
1 answer

Type Inference with Reflection and DataKinds

I'm having problems getting GHC to infer a type in a place where it should be obvious. Below is a complete snippet demonstrating the problem. {-# LANGUAGE DataKinds, ScopedTypeVariables, KindSignatures, TypeOperators, GADTs #-} import…
crockeea
  • 21,651
  • 10
  • 48
  • 101
1 2
3
10 11