haskell - Composing function composition: How does (.).(.) work? -
(.)
takes 2 functions take one value , return value:
(.) :: (b -> c) -> (a -> b) -> -> c
since (.)
takes two arguments, feel (.).(.)
should invalid, it's fine:
(.).(.) :: (b -> c) -> (a -> a1 -> b) -> -> a1 -> c
what going on here? realize question badly worded...all functions take 1 argument currying. maybe better way types don't match up.
let's first play typechecker mechanical proof. i'll describe intuitive way of thinking afterward.
i want apply (.)
(.)
, i'll apply (.)
result. first application helps define equivalences of variables.
((.) :: (b -> c) -> (a -> b) -> -> c) ((.) :: (b' -> c') -> (a' -> b') -> a' -> c') ((.) :: (b'' -> c'') -> (a'' -> b'') -> a'' -> c'') let b = (b' -> c') c = (a' -> b') -> a' -> c' ((.) (.) :: (a -> b) -> -> c) ((.) :: (b'' -> c'') -> (a'' -> b'') -> a'' -> c'')
then begin second, stuck quickly...
let = (b'' -> c'')
this key: want let b = (a'' -> b'') -> a'' -> c''
, defined b
, instead must try unify --- match our 2 definitions best can. fortunately, do match
unify b = (b' -> c') =:= (a'' -> b'') -> a'' -> c'' implies b' = a'' -> b'' c' = a'' -> c''
and definitions/unifications can continue application
((.) (.) (.) :: (b'' -> c'') -> (a' -> b') -> (a' -> c'))
then expand
((.) (.) (.) :: (b'' -> c'') -> (a' -> a'' -> b'') -> (a' -> a'' -> c''))
and clean up
substitute b'' -> b c'' -> c a' -> a'' -> a1 (.).(.) :: (b -> c) -> (a -> a1 -> b) -> (a -> a1 -> c)
which, honest, bit of counterintuitive result.
here's intuition. first take @ fmap
fmap :: (a -> b) -> (f -> f b)
it "lifts" function functor
. can apply repeatedly
fmap.fmap.fmap :: (functor f, functor g, functor h) => (a -> b) -> (f (g (h a)) -> f (g (h b)))
allowing lift function deeper , deeper layers of functors
.
it turns out data type (r ->)
functor
.
instance functor ((->) r) fmap = (.)
which should pretty familiar. means fmap.fmap
translates (.).(.)
. thus, (.).(.)
letting transform parametric type of deeper , deeper layers of (r ->)
functor
. (r ->)
functor
reader
monad
, layered reader
s having multiple independent kinds of global, immutable state.
or having multiple input arguments aren't being affected fmap
ing. sort of composing new continuation function on "just result" of (>1) arity function.
it's worth noting if think stuff interesting, forms core intuition behind deriving lenses in control.lens.
Comments
Post a Comment