Skip to content

Instantly share code, notes, and snippets.

@mkohlhaas
Last active January 3, 2022 17:01
Show Gist options
  • Save mkohlhaas/3afdb95bbccabbec71dcfc2a6085d727 to your computer and use it in GitHub Desktop.
Save mkohlhaas/3afdb95bbccabbec71dcfc2a6085d727 to your computer and use it in GitHub Desktop.
module Ch07a2 where
import Prelude (Unit, discard, show, (==), (<=), (<), (>), (>=), ($))
import Data.Eq (class Eq)
import Data.Ord (class Ord)
import Data.Show (class Show)
import Data.Generic.Rep (class Generic)
import Data.Show.Generic (genericShow)
import Effect (Effect)
import Effect.Console (log)
-------------------- Data Types -----------------------------------------------------------
data Maybe a = Nothing | Just a
data Either a b = Left a | Right b
-------------------- (Derived) Instances --------------------------------------------------
derive instance eqMaybe :: Eq a => Eq (Maybe a)
derive instance ordMaybe :: Ord a => Ord (Maybe a)
derive instance genericEither :: Generic (Either a b) _
instance showEither :: (Show a, Show b) => Show (Either a b) where
show = genericShow
derive instance genericMaybe :: Generic (Maybe a) _
instance showMaybe :: Show a => Show (Maybe a) where
show = genericShow
-------------------- Tests ----------------------------------------------------------------
test :: Effect Unit
test = do
log "Uncomment each line. DERIVE missing functions !!!"
log $ show $ Just 5 == Just 5 -- true
log $ show $ Just 5 == Just 2 -- false
log $ show $ Just 5 == Nothing -- false
log $ show $ Nothing == Just 5 -- false
log $ show $ Nothing == (Nothing :: Maybe Unit) -- true
log $ show $ (Left "left" :: Either _ Unit) -- (Left "left")
log $ show $ (Right (Just 42) :: Either Unit _) -- (Right (Just 42))
log $ show $ Just 1 < Just 5 -- true
log $ show $ Just 5 <= Just 5 -- true
log $ show $ Just 5 > Just 10 -- false
log $ show $ Just 10 >= Just 10 -- true
log $ show $ Just 99 > Nothing -- true
log $ show $ Just 99 < Nothing -- false
log $ show $ Just "abc" -- (Just "abc")
log $ show $ (Nothing :: Maybe Unit) -- Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment