Multiple typeclass instances in Haskell via tagging method
{-
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ExplicitForAll #-}
-- MyMonoid
class MyMonoid s a where
mmempty :: a
mmappend :: a -> a -> a
instance (IntegerMonoid s) => MyMonoid s Integer where
mmempty = im_mmempty @s
mmappend = im_mmappend @s
instance (BoolMonoid s) => MyMonoid s Bool where
mmempty = bm_mmempty @s
mmappend = bm_mmappend @s
--
-- IntegerMonoid
data SumMonoid
data ProdMonoid
class IntegerMonoidSelector s
instance IntegerMonoidSelector SumMonoid
instance IntegerMonoidSelector ProdMonoid
class (IntegerMonoidSelector s) => IntegerMonoid s where
im_mmempty :: Integer
im_mmappend :: Integer -> Integer -> Integer
instance IntegerMonoid SumMonoid where
im_mmempty = 0
im_mmappend = (+)
instance IntegerMonoid ProdMonoid where
im_mmempty = 1
im_mmappend = (*)
--
-- BoolMonoid
data AnyMonoid
data AllMonoid
class BoolMonoidSelector s
instance BoolMonoidSelector AnyMonoid
instance BoolMonoidSelector AllMonoid
class (BoolMonoidSelector s) => BoolMonoid s where
bm_mmempty :: Bool
bm_mmappend :: Bool -> Bool -> Bool
instance BoolMonoid AnyMonoid where
bm_mmempty = False
bm_mmappend = (||)
instance BoolMonoid AllMonoid where
bm_mmempty = True
bm_mmappend = (&&)
--
-- MySemiring
class (MyMonoid m1 a, MyMonoid m2 a) => MySemiring s m1 m2 a where
add :: a -> a -> a
zero :: a
mul :: a -> a -> a
one :: a
instance (MyMonoid m1 Integer, MyMonoid m2 Integer, IntegerSemiring s m1 m2) => MySemiring s m1 m2 Integer where
add = is_add @s @m1 @m2
zero = is_zero @s @m1 @m2
mul = is_mul @s @m1 @m2
one = is_one @s @m1 @m2
--
-- IntegerSemiring
class (MyMonoid m1 Integer, MyMonoid m2 Integer) => IntegerSemiring s m1 m2 | s -> m1, s -> m2 where
is_add :: Integer -> Integer -> Integer
is_zero :: Integer
is_mul :: Integer -> Integer -> Integer
is_one :: Integer
instance IntegerSemiring s SumMonoid ProdMonoid where
is_add = mmappend @SumMonoid
is_zero = mmempty @SumMonoid
is_mul = mmappend @ProdMonoid
is_one = mmempty @ProdMonoid
--
-- MyOrd
class MyOrd s a where
my_compare :: a -> a -> Ordering
lessThan :: a -> a -> Bool
lessThanEq :: a -> a -> Bool
greaterThan :: a -> a -> Bool
greaterThanEq :: a -> a -> Bool
lessThan x y =
case (my_compare @s @a) x y of
LT -> True
_ -> False
lessThanEq x y =
case (my_compare @s @a) x y of
LT -> True
EQ -> True
_ -> False
greaterThan x y =
case (my_compare @s @a) x y of
GT -> True
_ -> False
greaterThanEq x y =
case (my_compare @s @a) x y of
GT -> True
EQ -> True
_ -> False
--
-- IntegerOrd
instance MyOrd s Integer where
my_compare x y = (compare :: Integer -> Integer -> Ordering) x y
--
-- PairOrd
-- The s selector within LeftFirst
data LeftFirst sl sr
data RightFirst sl sr
class PairOrdSelector s
instance PairOrdSelector (LeftFirst sl sr)
instance PairOrdSelector (RightFirst sl sr)
instance (PairCompare s (a, b), MyOrd s a, MyOrd s b, PairOrdSelector s) => MyOrd s (a, b) where
my_compare x y = (pair_compare @s @(a, b)) x y
class PairCompare s p where
pair_compare :: p -> p -> Ordering
instance (MyOrd sl a, MyOrd sr b) => PairCompare (LeftFirst sl sr) (a, b) where
pair_compare (x, y) (x', y') =
case (my_compare @sl @a) x x' of
EQ -> (my_compare @sr @b) y y'
result -> result
instance (MyOrd sl a, MyOrd sr b) => PairCompare (RightFirst sl sr) (a, b) where
pair_compare (x, y) (x', y') =
case (my_compare @sr @b) y y' of
EQ -> (my_compare @sl @a) x x'
result -> result
--
-- Sample usage of ord: quicksort
quicksort :: forall s a . (MyOrd s a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) = (quicksort @s @a) [y | y <- xs, (lessThanEq @s @a) y x] ++ [x] ++ (quicksort @s @a) [y | y <- xs, (greaterThan @s @a) y x]
--
main = do
print $ quicksort $ toInteger <$> [5, 4, 3, 6, 9]
let arrayPair = zip (toInteger <$> [5, 4, 3, 6, 9]) (toInteger <$> [1, 10, 3, -10, 1000])
print $ (quicksort @(LeftFirst _ _) @_) $ arrayPair
print $ (quicksort @(RightFirst _ _) @_) $ arrayPair
print $ (mmappend @SumMonoid) (toInteger 1) (toInteger 2)
print $ (((mmappend @ProdMonoid) 3 4) :: Integer)
print $ (mmappend @AnyMonoid) False False
print $ (mmempty @AllMonoid :: Bool)
print $ mul (toInteger 7) (toInteger 8)