I plan to demonstrate an example of the power of classrooms in a short introduction that I will give to Haskell tomorrow.
I thought I would double check what I was going to introduce, because I am not 100% sure.
So let's say I have two functions:
firstSort :: Int -> Int -> Ordering firstSort ab = compare (even a) (even b) secondSort :: Int -> Int -> Ordering secondSort = compare
Now I can combine these two types thanks to my Monoid instances:
sort :: Int -> Int -> Ordering sort = firstSort <> secondSort
Now what I would like to confirm is the process used during the mappend two types.
From what I understand, it works as follows:
instance first:
instance Monoid b => Monoid (a -> b) where mempty _ = mempty mappend fgx = fx `mappend` gx
. This is done by creating a this instance using (Int -> Int) . Also, the mappend declaration puzzled me for a while, as it takes 3 arguments instead of 2, but then I remembered that (a -> b) -> (a -> b) -> (a -> b) matches (a -> b) -> (a -> b) -> a -> b .
Thanks to this instance, we get that firstSort <> secondSort :
\ab -> (firstSort ab) <> (secondSort ab)
then instance:
instance Monoid Ordering where mempty = EQ LT `mappend` _ = LT EQ `mappend` y = y GT `mappend` _ = GT
used with the specified mappend based on the results of firstSort and secondSort
So, to summarize, it will correspond to the Monoid b => Monoid (a -> b) instance Monoid b => Monoid (a -> b) with b = Ordering and a = (Int -> Int) , and then the Monoid Ordering instance.
Is it correct?
source share