Questions tagged [type-families]
Type families are Haskell language extension to facilitate type-level programming, via ad-hoc overloading of *data types*. They are the data equivalent of type classes (which allow overloading of functions).
type-families
356
questions
259
votes
1
answer
6k
views
Getting associated type synonyms with template Haskell
Can Template Haskell find out the names and/or the declarations of the associated type synonyms declared in a type class? I expected reify would do what I want, but it doesn't seem to provide all the ...
60
votes
2
answers
13k
views
'type family' vs 'data family', in brief?
I'm confused about how to choose between data family and type family. The wiki page on TypeFamilies goes into a lot of detail. Occasionally it informally refers to Haskell's data family as a "...
54
votes
1
answer
3k
views
“Illegal type synonym family application in instance” with functional dependency
I have a multi-parameter typeclass with a functional dependency:
class Multi a b | a -> b
I also have a simple, non-injective type synonym family:
type family Fam a
I want to write an instance ...
25
votes
1
answer
2k
views
Expand type synonyms, type families with GHCi
I am wondering if there is functionality that exists within GHCi (or elsewhere) to expand type synonyms and families out of an arbitrary type expression.
For example, if I have these types,
data A = ...
24
votes
3
answers
3k
views
Data families vs Injective type families
Now that we have injective type families, is there any remaining use case for using data families over type families?
Looking at past StackOverflow questions about data families, there is this ...
23
votes
1
answer
849
views
Matching on type level Nat in GHC 7.6
My question is probably easiest to explain in the form of an example:
type family Take (n :: Nat) (xs :: [k]) :: [k]
type instance Take 0 xs = '[]
type instance Take (n+1) (x ': xs) = x '...
21
votes
1
answer
2k
views
data families use cases
Benefits of using type synonym families is clear - it is type-level functions.
But it is not the case with data families - so my question is, what is use-cases for data families? Where should I use ...
17
votes
1
answer
2k
views
Are GHC's Type Famlies An Example of System F-omega?
I'm reading up about the Lambda-Cube, and I'm particularly interested in System F-omega, which allows for "type operators" i.e. types depending on types. This sounds a lot like GHC's type families. ...
16
votes
1
answer
2k
views
Closed type families and type inference in Haskell
In GHC-7.7 (and 7.8) closed type families were introduced:
A closed type family has all of its equations defined in one place and
cannot be extended, whereas an open family can have instances ...
16
votes
1
answer
231
views
Is it possible to introduce additional type variables into a superclass-constraint?
When dealing with type families, it is often handy to use equality constraints to avoid having to repeat some type-function's name in a signature:
class Foo f where
type BulkyAssociatedType f :: *
...
15
votes
1
answer
1k
views
Type class constraint on type family instances
Is it possible to specify a type class constraint that must be satisfied by all instances of a type family?
For example, given the following declaration, how would I ensure that all instances are ...
15
votes
3
answers
862
views
How to put constraints on the associated data?
I would like to state that the associated data is always an instance of a certain class.
class (Context (Associated a b)) => Class a where
data Associated a :: * -> *
instance Context (...
15
votes
1
answer
362
views
Informing Haskell that `(Reverse (Reverse xs)) ~ xs`
If Reverse :: [k] -> [k] is a type family then Haskell cannot tell that (Reverse (Reverse xs)) ~ xs. Is there a way to let the type system know this without any runtime cost?
I'm tempted to just ...
14
votes
2
answers
631
views
Simple type family example errors about non injective type function
I'm trying to understand type families without much success. Here's a minimal example:
{-# LANGUAGE TypeFamilies #-}
class Object obj where
type Unit obj :: *
unit :: Unit obj
instance (Object ...
14
votes
2
answers
550
views
Ambigous instance resolution in Haskell
Introduction and example use case
Hello! I've got a problem in Haskell. Let's consider following code
class PolyMonad m1 m2 m3 | m1 m2 -> m3 where
polyBind :: m1 a -> (a -> m2 b) -> ...
14
votes
1
answer
172
views
Type (in)equalities in the presence of data families
I've got a type family which determines whether something is at the head of a type-level list.
type family AtHead x xs where
AtHead x (x ': xs) = True
AtHead y (x ': xs) = False
I want to ...
14
votes
2
answers
239
views
Why does introducing associated types kill my performance?
In my kdtree project I just replaced a depth counter from being Int-based to an explicit Key a based on the type a in KDTree v a. This is the diff.
Now while I believe this should be a type-level ...
13
votes
1
answer
672
views
Writing A Function Polymorphic In A Type Family
I was experimenting with type families yesterday and ran into an obstacle with the following code:
{-# LANGUAGE TypeFamilies #-}
class C a where
type A a
myLength :: A a -> Int
...
13
votes
1
answer
831
views
Why can't we define closed data families?
All of the following work:
{-# LANGUAGE TypeFamilies #-}
type family TF a
type instance TF Int = String
type instance TF Bool = Char
data family DF a
data instance DF Int = DFInt String
data ...
13
votes
1
answer
461
views
Converting Functional Dependency class to Type Family instances
Is it possible to create type family instances from a fundep class? For example, let's say that I have the class
class A a b | a -> b
with some instances (imported an external library) and want ...
13
votes
1
answer
170
views
Is there a way to get a compile-time error if there's no matching closed type family instance?
I have a closed type family which has no catch-all case:
{-# LANGUAGE TypeFamilies #-}
type family Foo a where
Foo Bool = Int
Foo Int = Bool
Is there a way to force the type checker to ...
12
votes
1
answer
209
views
Is it possible to write fmap for this data type involving a type family?
Given the following type family (supposed to reflect the isomorphism A×1 ≅ A)
type family P (x :: *) (a :: *) :: * where
P x () = x
P x a = (x, a)
and data type defined in terms thereof
data T ...
12
votes
1
answer
208
views
Understanding the casts involved in patterns matching a datatype that is indexed over a user defined kind
So, I was playing around with DataKinds and TypeFamilies in Haskell and started to look at the Core GHC generated.
Here is a little TestCase to motivate my question:
{-# LANGUAGE GADTs #-}
{-# ...
12
votes
1
answer
172
views
GHC 8.0.1 hangs. Explanation or compiler bug?
Can someone explain why the following code causes GHC 8.0.1 to loop forever on compiling, or is this a bug?
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ...
11
votes
2
answers
431
views
Restrictions of unboxed types
I wonder why unboxed types in Haskell have these restrictions:
You cannot define a newtype for unboxed type:
newtype Vec = Vec (# Float#, Float# #)
but you can define type synonim:
type Vec = (# ...
11
votes
2
answers
478
views
Quantified constraints vs. (closed) type families
I am trying to use this blogpost's approach to higher-kinded data without dangling Identity functors for the trival case together with quantified-constraint deriving:
{-# LANGUAGE TypeFamilies #-}
{-#...
11
votes
2
answers
2k
views
Illegal polymorphic or qualified type using RankNTypes and TypeFamilies
I've been slowly working on porting the llvm package to use data kinds, type families and type-nats and ran into a minor issue when trying to remove the two newtypes used for classifying values (...
11
votes
1
answer
310
views
Inverse injective type families
Lets say I have an injective type family T
type family T a = b | b -> a
My first question is there an way to write:
type family T' = the inverse of T
Without having to repeat all the instances ...
11
votes
2
answers
113
views
Why does `forall (a :: j) (b:: k)` work differently than `forall (p :: (j,k))`?
I'm trying to understand the difference between using forall to quantify two type variables and using forall to quantify a single type variable of tuple kind.
For example, given the following type ...
11
votes
1
answer
326
views
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 ...
11
votes
1
answer
812
views
Type Families with GHC.Generics or Data.Data
This is a question related to my module here, and is simplified a bit. It's also related to this previous question, in which I oversimplified my problem and didn't get the answer I was looking for. I ...
11
votes
1
answer
227
views
Is there any way to convince GHC that this (injective) type family is injective?
Messing around with GHC's DataKinds, I tried implementing type-level binary nats. They're simple enough to implement, but if I want them to be useful in common cases, then GHC needs to believe that ...
10
votes
1
answer
604
views
Deriving instances with TypeFamilies
I have a type class Foo with an associated type:
{-# LANGUAGE TypeFamilies #-}
class Foo a where
type Bar a
foo :: a -> Bar a
Now I want to define a data type that holds one of the ...
10
votes
1
answer
806
views
Haskell-style type families
In Haskell, I might write a typeclass with a type declaration to create a type family, like so:
class ListLike k where
type Elem :: * -> *
fromList :: [Elem k] -> k
And then write ...
10
votes
2
answers
427
views
Constrained closed type family
Can I convince the compiler that a constraint is always satisfied by the type synonyms in a closed type family? The family is indexed by a finite set of promoted values.
Something along the lines of
...
10
votes
1
answer
277
views
Default type instances referring to each other
Is there a way to have default type instances defined in terms of each other? I'm trying to get something like this working:
{-# LANGUAGE DataKinds, KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-...
10
votes
2
answers
484
views
Why does this Haskell code typecheck with fundeps but produce an untouchable error with type families?
Given some type definitions:
data A
data B (f :: * -> *)
data X (k :: *)
…and this typeclass:
class C k a | k -> a
…these (highly contrived for the purposes of a minimal example) function ...
9
votes
3
answers
2k
views
When do I need type annotations?
Consider these functions
{-# LANGUAGE TypeFamilies #-}
tryMe :: Maybe Int -> Int -> Int
tryMe (Just a) b = a
tryMe Nothing b = b
class Test a where
type TT a
doIt :: TT a -> a -&...
9
votes
1
answer
529
views
Type Family as Argument to Type Synonym
I have a data type that looks like
data G f n a where
G :: a -> G f n a -> G f (f n) a
It's a container indexed by naturals, which takes a function that determines how to proceed ...
9
votes
3
answers
1k
views
Problem when mixing type classes and type families
This code compiles fine:
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances,
UndecidableInstances, FlexibleContexts, EmptyDataDecls, ScopedTypeVariables,
TypeOperators, ...
9
votes
1
answer
387
views
Type Families extension does not work as described
On the Haskell wiki page for Type Families, there is the following list of examples:
type family F a :: *
type instance F [Int] = Int -- OK!
type instance F String = ...
9
votes
3
answers
1k
views
Type constraints on all type family instances
I suppose what I want is impossible without Template Haskell but I'll ask anyway.
I have an interface for types like Data.Set and Data.IntSet:
type family Elem s :: *
class SetLike s where
insert :...
9
votes
3
answers
212
views
Expressing infinite kinds
When expressing infinite types in Haskell:
f x = x x -- This doesn't type check
One can use a newtype to do it:
newtype Inf = Inf { runInf :: Inf -> * }
f x = x (Inf x)
Is there a newtype ...
9
votes
1
answer
217
views
adding ghci's inferred type signature causes an error
Edit: Here's a truly simple example. Motivation for this example below.
This compiles:
{-# LANGUAGE TypeFamilies #-}
type family F a b
f :: a -> F a b
f = undefined
f' [a] = f a
And ghci ...
9
votes
2
answers
1k
views
Haskell type family instance with type constraints
I am trying to represent expressions with type families, but I cannot seem to figure out how to write the constraints that I want, and I'm starting to feel like it's just not possible. Here is my ...
9
votes
2
answers
570
views
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 ...
9
votes
1
answer
231
views
Why is this injective type family not actually injective?
I tried this:
{-# LANGUAGE TypeFamilyDependencies #-}
module Injective where
type family F (a :: *) = (fa :: *) | fa -> a
convert :: F a ~ F b => a -> b
convert x = x
GHC 8.6.4 gave me ...
9
votes
2
answers
954
views
Constraints on closed type families?
I'd like to write a horribly non-parametric version of a function of type
pretty :: (Show a) => a -> Text
such that
pretty :: Text -> Text = id
pretty :: String -> Text = T.pack
pretty :: ...
9
votes
1
answer
654
views
Example on the use of backpack
I'm looking at the references at the backpack wiki trying to understand in which cases the use of backpack would be considered appropriate over other Haskell features like type-classes and type-...
9
votes
1
answer
316
views
Haskell type family applications are not evaluated
I found an interesting situation, when using data kinds with type families.
The compiler's error message is No instance for (C (ID ())) arising from a use of W. It suggests that a type family ...