# ​Parallel and Concurrent Haskell (3)

3.1 Algebraic Data Types: Product Type, Sum Type

Projection by Pattern Match:

fst :: (a , b )  -> a
fst  (x, _) = x

snd (_,y) = y

:t  ( , ) data constructor

( , ) :: a -> b -> (a,b) type constructor

Kind
:k (,) type constructor
(,) :: * -> * ->  *

Laziness

3.2 Sum Type

data Either a b = Left a | Right b
Left :: a -> Either a b
Right :: b -> Either a b

safeSqrt :: Either String Double -> Either String Double

safeSqrt (Left str) = Left str
safeSqrt (Right x) =
if x <0
then Left “Error”
else Right (sqrt x)

safeSqrt  sx =
case sx of
Left str -> Left str
Right str ->if x < 0
then Left “Error”
else Right (sqrt x)

data Bool = True | False
case x < 0 of
True -> …
False -> …

(<) :: Ord a => a -> a -> Bool

Note: Ord = Order

a + 0 = a

data X a = X a | Y Void

a + 0

a * 1 = a

type Y a = (a , () )

a * 0 = 0
type Z a = (a, Void) ~ Void

Note: (a, Void) is an impossible pair of 2 elements, because Void has no element, so (a, Void) is equivalent to Void.

2 = 1 + 1
2 = Bool
Bool = True | False