Skip to content

Instantly share code, notes, and snippets.

@osa1
Created January 28, 2014 16:01
Show Gist options
  • Save osa1/8670423 to your computer and use it in GitHub Desktop.
Save osa1/8670423 to your computer and use it in GitHub Desktop.
{-# 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