...
 
Commits (2)
......@@ -28,6 +28,8 @@ instance Eq Color where
-- Alternatively, use automatic derivation!
-- Say: how haskell derives the equality operator
-- Todo: look through ordering derivation
data Color2 = Red2 | Green2 | Blue2
deriving (Eq)
......@@ -72,6 +74,8 @@ class (Eq a) => Ord a where
-- Hoogle: Ord a => [a] -> [a]
-- Example instance
--
-- By hand: write instnaces for Eq / Ord / Show
instance Ord Color where
compare Red Red = EQ
compare Green Green = EQ
......@@ -85,7 +89,7 @@ instance Ord Color where
-- Deriving Ord works here.
-------------------------------------
-- 3. Num: the basc numeric typeclass
-- 3. Num: the basic numeric typeclass
-------------------------------------
class Num a where
......@@ -112,7 +116,7 @@ class Enum a where
succ, pred :: a -> a -- Successor / predecessor of a value
toEnum :: Int -> a -- Convert from an Int
fromEnum :: a -> Int -- Convert to an Int
enumFrom :: a -> [a] -- [n..]
enumFrom :: a -> [a] -- [n..]
enumFromThen :: a -> a -> [a] -- [n,n'..]
enumFromTo :: a -> a -> [a] -- [n..m]
enumFromThenTo :: a -> a -> a -> [a] -- [n,n'..m]
......@@ -131,6 +135,8 @@ class Enum a where
data Color = Red | Green | Blue
deriving (Read, Show, Eq, Ord, Enum)
-- TODO: add example Red .. Blue
-- GHCI examples
-- :t toEnum
-- :t fromEnum
......@@ -144,17 +150,11 @@ data Color = Red | Green | Blue
-- This is the machinery we invoke when we type
-- "deriving Show" after declaring a custom type.
-- A little complicated, and the details don't matter too much.
-- TODO: add examples on read and show when they're introduced.
-- omit read from here; but talk about show.
type ReadS a = String -> [(a,String)]
type ShowS = String -> String
class Read a where
readsPrec :: Int -> ReadS a
readList :: ReadS [a]
-- ... default decl for readList given in Prelude
-- Minimal complete definition: readsPrec or readPrec. Complicated parsing logic. Often easier to use Parsec.
class Show a where
showsPrec :: Int -> a -> ShowS
show :: a -> String
......
......@@ -7,6 +7,7 @@ main = do
-- Suppose we define a new type, Color.
data Color = Red | Green | Blue
-- (==)
-- We might want to define an equality check for it.
colorEq :: Color -> Color -> Bool
colorEq Red Red = True
......@@ -47,6 +48,9 @@ instance BasicEq Bool where
-- Expanding on this: we can define a more sophisticated typeclass
-- with multiple functions. For a type to be in the "BasicEq2" type class,
-- it must implement _one of_ "isEqual2" and "isNotEqual2".
-- Need to put types for all functions
-- But the implementations are optional
class BasicEq2 a where
isEqual2 :: a -> a -> Bool
isEqual2 x y = not (isNotEqual2 x y)
......@@ -57,6 +61,7 @@ class BasicEq2 a where
-- We can make `Color` an instance of
-- `BasicEq2` by implementing `isEqual2`.
-- Compiler is really smart, it will figure out the other.
-- everything to left of => is a type constraint
instance BasicEq2 Color where
isEqual2 Red Red = True
isEqual2 Green Green = True
......@@ -70,12 +75,13 @@ instance BasicEq2 Color where
-- Look at Show
-- :t show
instance Show Color where
show Red = "Red"
show Green = "Green"
show Blue = "Blue"
-- Automatic typeclass derivation!
-- Ord: typeclass for ordering
-- Read: "inverse" of Show
data Color2 = Red2 | Green2 | Blue2
deriving (Read, Show, Eq, Ord)
......@@ -68,22 +68,6 @@ class (Eq a) => Ord a where
Defining either `compare` or `<=` is sufficient for a minimal complete definition. See [here](http://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Ord.html#t:Ord) for more properties.
### Read
The typeclass `Read` handles conversion of strings to values.
```haskell
type ReadS a = String -> [(a,String)]
type ShowS = String -> String
class Read a where
readsPrec :: Int -> ReadS a
readList :: ReadS [a]
-- ... default decl for readList given in Prelude
```
Defining either `readsPrec` or `readPrec` is sufficient for a minimal complete definition. See [here](http://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#t:Read) for more properties.
### Show
The typeclass `Show` handles conversion of values to readable `String`s. This is the machinery used whenever we write `deriving (Show)` for custom types.
......