[Haskell-cafe] Confusion on the third monad law when using lambda
nccb2 at kent.ac.uk
Thu Jun 18 10:09:22 EDT 2009
Clicking on the source code link reveals that enum2 is used in the where
clause. It's not important to the transformation that Jake was performing.
In essence, <=< is the monadic version of . (function composition) and
as explained, it can be used to do some pointfree-like programming in
the presence of monads. It's also handy in the arguments to things like
f = mapM (\x -> foo x >>= bar)
f = mapM (bar <=< foo)
Colin Adams wrote:
> What is enum2 doing in all of this - it appears to be ignored.
> 2009/6/18 Jake McArthur <jake.mcarthur at gmail.com>:
>> Jake McArthur wrote:
>>> Generally, you can transform anything of the form:
>>> baz x1 = a =<< b =<< ... =<< z x1
>>> baz = a <=< b <=< ... <=< z
>> I was just looking through the source for the recently announced Hyena
>> library and decided to give a more concrete example from a real-world
>> project. Consider this function from the project's Data.Enumerator
>> compose enum1 enum2 f initSeed = enum1 f1 (Right initSeed) >>= k
>> f1 (Right seed) bs = ...
>> k (Right seed) = ...
>> First, I would flip the `(>>=)` into a `(=<<)` (and I will ignore the
>> `where` portion of the function from now on):
>> compose enum1 enum2 f initSeed = k =<< enum1 f1 (Right initSeed)
>> Next, transform the `(=<<)` into a `(<=<)`:
>> compose enum1 enum2 f initSeed = k <=< enum1 f1 $ Right initSeed
>> We can "move" the `($)` to the right by using `(.)`:
>> compose enum1 enum2 f initSeed = k <=< enum1 f1 . Right $ initSeed
>> Finally, we can drop the `initSeed` from both sides:
>> compose enum1 enum2 f = k <=< enum1 f1 . Right
>> I didn't test that my transformation preserved the semantics of the function
>> or even that the type is still the same, but even if it's wrong it should
>> give you the idea.
>> - Jake
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe