Copyright | © Frank Jung 2024 |
---|---|
License | GPL-3.0-only |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
PolyList
Description
From Thinking with Types, Sandy Maguire, 2021, section 5.3 Heterogeneous Types.
Source
See Thinking with Types for code and solutions to exercises.
Unable to Derive Functor for Hetereogeneous Lists
Unfortunately, for your PolyList type, it's not possible to create a Functor
instance because PolyList is indexed by a type-level list of types, and Functor
requires a type constructor of kind * -> *
, i.e., a type constructor that
takes exactly one type argument.
The PolyList type is a heterogeneous list that can contain values of different types, and the type of each value is encoded in the type of the list itself. This is fundamentally different from the concept of a Functor, which operates on homogeneous containers that contain values of a single type.
See also RankNTypes for other examples.
Types
data PolyList (ts :: [Type]) where Source #
A list that contain polymorphic types.
Instances
All Show ts => Show (PolyList ts) Source # | |
All Eq ts => Eq (PolyList ts) Source # | |
(All Eq ts, All Ord ts) => Ord (PolyList ts) Source # | |
Defined in PolyList |
An alternate implementation of a heterogenous item.
The HEntry
type is an existential wrapper around a value of any type that
has a Show
instance. This allows us to store values of different types in a
list, but we can't derive Eq
or Ord
for HEntry
because the types are
potentially different.
See also RankNTypes for other examples.