[Haskell-cafe] Foldable, Traversable and choosing an order

Juan Casanova juan.casanova at ed.ac.uk
Wed Sep 25 17:05:53 UTC 2019


That is lovely. Makes it super clear. Hadn't thought about  
Bitraversable and Bifoldable. Thanks a lot.

Juan.

Quoting David Feuer <david.feuer at gmail.com> on Tue, 24 Sep 2019  
18:09:08 -0400:

> On Mon, Sep 23, 2019, 3:05 PM Juan Casanova <juan.casanova at ed.ac.uk> wrote:
>
> My types:
>>
>> 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
>>
>
> Here's a more modular option for the Traversable instance.
>
> import Data.Bitraversable
> import Data.Bifoldable
> import Control.Applicative
> import Data.Traversable
>
> instance Bifoldable (SOTermPF fn) where
>   bifoldMap = bifoldMapDefault
>
> instance Bitraversable (SOTermPF fn) where
>   bitraverse _f _g (ConstF fn) = pure (ConstF fn)
>   bitraverse _f _g (Proj i) = pure (Proj i)
>   bitraverse f g (CompF p fs) =
>     liftA2 CompF (f p) (traverse g fs)
>
> instance Foldable (SOTermF fn) where
>   foldMap = foldMapDefault
>
> instance Traversable (SOTermF fn) where
>   traverse f (SOF q) = SOF <$> bitraverse f f q
>
>>
>



-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.




More information about the Haskell-Cafe mailing list