Created
January 28, 2014 16:01
-
-
Save osa1/8670423 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE MultiParamTypeClasses, | |
FunctionalDependencies, | |
FlexibleInstances #-} | |
class Option a b | a -> b where | |
toOption :: a -> Maybe b | |
instance Option (Maybe a) a where | |
toOption = id | |
instance Option Int Int where | |
toOption = Just | |
instance Option String String where | |
toOption = Just | |
-- GHCi | |
-- *Main> :r | |
-- [1 of 1] Compiling Main ( Dynamic.hs, interpreted ) | |
-- Ok, modules loaded: Main. | |
-- *Main> toOption (Just 42) | |
-- Just 42 | |
-- *Main> toOption "test" | |
-- Just "test" | |
-- *Main> toOption (42 :: Int) | |
-- Just 42 | |
-- *Main> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment