[Haskell-cafe] Foldable, Traversable and choosing an order
juan.casanova at ed.ac.uk
Wed Sep 25 17:03:20 UTC 2019
Looking at your suggested solution, it looks very similar to mine
albeit two differences.
One is "just" (as if it were not important) code clarity in that you
use <$> instead of fmap which enables you to avoid lambda abstraction,
but that part is operationally the same as mine.
The other is that you do (traverse f sargs), whereas I did (traverse
id (map f sargs)). This made me think. Is this always the same? Is it
the same only for  or for any Traversable? If it is, then,
considering map is just fmap for lists, and that all Traversables must
be Functors, why isn't traverse just defined as
traverse_alt :: (Traversable t, Applicative f) => t (f a) -> f (t a)
traverse_alt = traverse id
and let fmap deal with the mapping of the function? Of course this
wouldn't be the implementation, it would be the other way around.
Instances of Traversable would implement traverse_alt, and then
whenever I wanted to do what traverse currently does, I would just do:
traverse_alt (fmap f inputs). What is there to gain by including the
mapping into the traversal *in the implementation of traverse itself*?
Quoting Tom Ellis <tom-lists-haskell-cafe-2017 at jaguarpaw.co.uk> on
Tue, 24 Sep 2019 21:30:07 +0100:
> On Mon, Sep 23, 2019 at 08:05:38PM +0100, Juan Casanova wrote:
>> data SOTermPF fn p f = ConstF fn | Proj Int | CompF p [f] deriving Eq
>> newtype SOTermF fn f = SOF (SOTermPF fn f f) deriving Eq
>> My Traversable instance (with comments on intermediate types because
>> otherwise it can get pretty obscure):
> Everyone else has made good comments, but to add a little and to be a little
> more explicit: your data type uses  so it makes sense to directly inherit
> the Traversable (and hence Foldable) instance from that. Your instance was
> a little convoluted. Here's what I would suggest:
> instance Foldable (SOTermF fn) where
> foldMap = foldMapDefault
> instance Traversable (SOTermF fn) where
> traverse f (SOF (ConstF c)) = pure (SOF (ConstF c))
> traverse f (SOF (Proj idx)) = pure (SOF (Proj idx))
> traverse f (SOF (CompF g sargs)) =
> SOF <$> (CompF <$> f g <*> traverse f sargs)
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> Only members subscribed via the mailman list are allowed to post.
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.
More information about the Haskell-Cafe