Skip to content

Instantly share code, notes, and snippets.

@wyager
Created July 28, 2016 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wyager/a021f7e5d9f23643bc90a9866b5c0628 to your computer and use it in GitHub Desktop.
Save wyager/a021f7e5d9f23643bc90a9866b5c0628 to your computer and use it in GitHub Desktop.
{-# LANGUAGE MultiParamTypeClasses, ScopedTypeVariables #-}
import Prelude hiding (compare)
data Order f = Lt | Gt | Eq
class OrdI a f where
compare :: a -> a -> Order f -- Could use a proxy too
data IntOrd
instance OrdI Int IntOrd where
compare x y
| x == y = Eq
| x < y = Lt
| x > y = Gt
data FlippedIntOrd
instance OrdI Int FlippedIntOrd where
compare x y
| x == y = Eq
| x < y = Gt
| x > y = Lt
data Minimizer f a = Minimizer a deriving Show
combine :: forall a f . OrdI a f => Minimizer f a -> Minimizer f a -> Minimizer f a
combine (Minimizer x) (Minimizer y) = case compare x y :: Order f of
Lt -> Minimizer x
Eq -> Minimizer x
Gt -> Minimizer y
main = do
print $ combine (Minimizer 0) (Minimizer 1 :: Minimizer IntOrd Int)
print $ combine (Minimizer 0) (Minimizer 1 :: Minimizer FlippedIntOrd Int)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment