Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
Created February 14, 2023 12:21
Show Gist options
  • Save sjoerdvisscher/c1c6edfd4acb114e649fbb1777985f96 to your computer and use it in GitHub Desktop.
Save sjoerdvisscher/c1c6edfd4acb114e649fbb1777985f96 to your computer and use it in GitHub Desktop.
{-# LANGUAGE TypeOperators, FlexibleContexts #-}
import GHC.Generics ((:.:)(..))
import Control.Applicative (liftA2)
class Applicative f => Selective f where
select :: f (Either a b) -> f (a -> b) -> f b
select fe fab = either id (uncurry (flip ($))) <$> biselect (either Right Left <$> fe) (Right <$> fab)
biselect :: f (Either a b) -> f (Either a c) -> f (Either a (b, c))
class Applicative f => CheckClosed f where
checkClosed :: f a -> Maybe (f b)
biselectC :: (Applicative f, CheckClosed (f :.: Either a)) => f (Either a b) -> f (Either a c) -> f (Either a (b, c))
biselectC fab fac = case checkClosed (Comp1 fab) of
Just (Comp1 fex) -> fex
Nothing -> liftA2 (liftA2 (,)) fab fac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment