mirror of
https://github.com/sdiehl/write-you-a-haskell.git
synced 2024-08-16 15:11:06 +03:00
1.4 KiB
1.4 KiB
Datatypes
Algebraic data types
Algebraic datatypes are a family of constructions arising out of two operations, sums and products. A product encodes multiple arguments to constructors and sums encode choice between constructors.
data Unit = Unit -- 1
data Empty -- 0
data (a * b) = Product a b -- a * b
data (a + b) = Inl a | Inr b -- a + b
data Exp a b = Exp (a -> b) -- a^b
data Rec f = Rec (f (Rec f)) -- \mu
Catamorphisms
Syntax
GHC.Generics
class Generic a where
type family Rep a :: * -> *
to :: a -> Rep a x
from :: Rep a x -> a
Constructor Models
V1
Void: used for datatypes without constructors
U1
Unit: used for constructors without arguments
K1
Constants, additional parameters.
:*:
Products: encode multiple arguments to constructors
:+:
Sums: encode choice between constructors
L1
Left hand side of a sum.
R1
Right hand side of a sum.
M1
Meta-information (constructor names, etc.)
newtype M1 i c f p = M1 (f p)
newtype K1 i c p = K1 c
data U p = U
data (:*:) a b p = a p :*: b p
data (:+:) a b p = L1 (a p) | R1 (b p)
Full Source
\clearpage