Created
July 28, 2016 08:44
-
-
Save wyager/a021f7e5d9f23643bc90a9866b5c0628 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, 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