Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concoct a correct "Free Alternative" #53

Closed
ekmett opened this issue Mar 16, 2014 · 8 comments
Closed

Concoct a correct "Free Alternative" #53

ekmett opened this issue Mar 16, 2014 · 8 comments
Assignees
Milestone

Comments

@ekmett
Copy link
Owner

ekmett commented Mar 16, 2014

My working assumption is it'd look something like:

data AltF f a where
  Pure :: a -> AltF f a
  Ap   :: f a -> Alt f (a -> b) -> AltF f b
#if __GLASGOW_HASKELL__ >= 707
  deriving Typeable
#endif

data Alt f a = Alt [AltF f a]
#if __GLASGOW_HASKELL__ >= 707
  deriving Typeable
#endif

-- | Given a natural transformation from @f@ to @g@, this gives a canonical monoidal natural transformation from @'Alt' f@ to @g@.
runAlt :: forall f g a. Alternative g => (forall x. f x -> g x) -> Alt f a -> g a
runAlt u xs0 = go xs0 where
  go :: Alt f a -> g a
  go (Alt xs) = foldr (\a r -> go2 a <|> r) empty xs

  go2 :: AltF f a -> g a
  go2 (Pure a) = pure a
  go2 (Ap f x) = flip id <$> u f <*> runAlt u x

instance Functor (AltF f) where
  fmap f (Pure a)   = Pure (f a)
  fmap f (Ap x y)   = Ap x (fmap f <$> y)

instance Functor (Alt f) where
  fmap f (Alt as)   = Alt (fmap f <$> as)

Thoughts?

@ekmett ekmett added this to the 4.6 milestone Mar 16, 2014
@vlopezj
Copy link
Collaborator

vlopezj commented Mar 16, 2014

empty <|> a  == a <|> empty == a
(a <|> b) <|> c == a <|> (b <|> c)
empty <*> a == empty

Are there any other laws that every Alternative instance should follow?

@ekmett
Copy link
Owner Author

ekmett commented Mar 16, 2014

Basically we need to choose between the equivalent of the left distribution and left catch laws from the old MonadPlus reform proposal

I'd say the distribution law is the one that matters, hence the use of [] above.

@ekmett
Copy link
Owner Author

ekmett commented Mar 16, 2014

So we'd want

(a <|> b) <*> c = (a <*> c) <|> (b <*> c)

I think you can get the catch law from the abuse of lists there if you target an Alternative for runAlt that satisfies that law as well, but I haven't made that rigorous.

@vlopezj
Copy link
Collaborator

vlopezj commented Mar 18, 2014

I just pushed a working prototype to a new branch. It's not very clean, with lots of currying, but I ran some tests and it seems to work fine.

Left catch might hold, but runAlt would not be an homomorphism anymore. There's an example using a ZipList alternative functor in the test file.

@ekmett
Copy link
Owner Author

ekmett commented Mar 18, 2014

Hrmm, I wonder if we can accomplish this and still retain Twan's f a -> Alt f (a -> b) -> Alt f b backend somehow.

@ekmett
Copy link
Owner Author

ekmett commented Mar 23, 2014

Merged 12a9c00. Thanks @vklj!

@vlopezj
Copy link
Collaborator

vlopezj commented Mar 23, 2014

My pleasure :) . I'll close the issue then.

@vlopezj vlopezj closed this as completed Mar 23, 2014
@ekmett
Copy link
Owner Author

ekmett commented Mar 23, 2014

Shipped in free 4.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants