Skip to content

Adding branch to the class definition #74

Description

@j-mie6

The branch operation can be implemented efficiently on its own, and select can be efficiently implemented in terms of it. This is similar to Applicative's (<*>)/liftA2 for which a more efficient implementation may exist with one compared to the other. I propose doing something similar with Selective such that:

class Applicative f => Selective f where
    {-# MINIMAL (select | branch) #-}
    select :: f (Either a b) -> f (a -> b) -> f b
    select x y = branch x y (pure id)
    
    branch :: f (Either a b) -> f (a -> c) -> f (b -> c) -> f c
    branch x l r = select (select (fmap (fmap Left) x) (fmap (fmap Right) l)) r

At the very least, adding branch to the class would enable a more efficient definition to be given, even if the MINIMAL is not used.
This is done for Applicative with (*>), (<*); Alternative with many and some; Monad with (>>).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions