by evincarofautumn · 9y ago · 0 comments
-- Endo is…
newtype Endo a
= Endo { appEndo :: a -> a }
-- ^
-- |
-- …the monoid of endomorphisms…
-- |
-- V
instance Monoid (Endo a) where
mappend (Endo f) (Endo g)
= Endo (f . g)
-- ^
-- |
-- …under composition.
mempty = Endo id
But not so much about how to actually use it: -- If you’ve got a list of functions…
pipeline :: [Int -> Int]
pipeline = [(+ 3), (* 2), abs]
-- …you can compose them with the generic mconcat.
run :: [a -> a] -> a -> a
run = appEndo . mconcat . map Endo
run pipeline (-1) == 5-- Using Elm syntax
pipeline : List (Int -> Int)
pipeline = [(\x -> x + 3), (\x -> x * 2), abs]
applyPipeline : Int -> Int
applyPipeline value = List.foldr (\func val -> func val) value pipeline
-- or more succinctly
applyPipeline_ value = List.foldr (<|) value pipeline
Getting lost in the mathematical abstractions is really no help here. In niche circumstances I'm sure it's great, but like.... not here.
The language itself doesn't demand we write code this way, but the community, understandably, loves to be as expressive as possible in many cases.
But all of this comes with a cost of cognitive overhead that seems to rarely be worth paying.
Elm was an enlightening experience for me, having started with Haskell, because it helped me to realize just how little I ever took advantage of a lot of the very generic Haskell code, and how far the simplest of functions and types could take you in the overwhelming majority of cases.
To quote from Paul's post: "The trouble is that very often, the sorts of examples that are easy to discuss aren’t of sufficient scale to reveal any major differences between A and B."
y1 = f_1 x
y = f_2 y1
-- or,
y = f_2 (f_1 x)
But what if you have an arbitrary list of functions as your pipeline? y = f_n (f_{n-1} (... (f_2 (f_1 x)) ...))
Well, the solution is that functions of type `a -> a` are composeable, so you can 'add them up' as if they were real values (which they are). The Haskell function composition operator is `.`, so: y = f_2 (f_1 x)
-- is the same as,
y = (f_2 . f_1) x
Now, how do you go from 'composing two functions' to 'composing a list of functions'? Well, monoids are a typeclass (a statically-enforceable design pattern) that encode the idea of being able to combine two things into one, and 'for free' give you a function `mconcat` that combines a list of those things into one. So if you have a monoid instance for functions of type `a -> a`, you're now able to combine many of them together, get a single composed function, and apply your input value `x` to that.`Endo` is just a fancy name for 'function of type `a -> a`'. I agree to a large extent that all this category theory stuff can get distracting. I personally think type theory is a much more rewarding field of study. To 'endomorphism', a type theorist would say, 'Oh, you mean `a -> a`?'
This module is inspired by the following paper: <DOI>(I know "+1" posts are a sin, but if a few more haskellers take heed of the documentation problem, it'll be worth me getting hellbanned.)
If you aren't planning on submitting that to those docs I will. In fact I'm sure many would find you adding examples along those lines to the most popular but math inspired Haskell libraries.
https://www.youtube.com/watch?v=I8LbkfSSR58 https://bartoszmilewski.com/2014/10/28/category-theory-for-p...
For example, the poster you replied to can only see the advantage being that his question is answered. The tangible benefit for taking time out of the day to go and read those foundations needs to be made more clear I think.
---
Reply to montatonic here:
I'm neither a Haskeller nor do I want to see Haskell “take over” the world. Some of my comments even got lots of downvotes from Haskellers.
---
Reply to sidlls here:
Yes, FWIW, I don't really agree with the use of the term “endomorphism”. It isn't even precise: the type `Endo` is inhabited by endomorphisms is one specific category (Hask), not endomorphisms in arbitrary categories. So “endofunction” would be both more accurate and less pompous.
As for “codomain”, I can't agree, though. This is really high school stuff.