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

Instances for single-constructor types aren't lazy #1

Closed
Deewiant opened this issue Sep 18, 2011 · 3 comments
Closed

Instances for single-constructor types aren't lazy #1

Deewiant opened this issue Sep 18, 2011 · 3 comments

Comments

@Deewiant
Copy link
Collaborator

The instances of Bifunctor and Biapply aren't as lazy as they could be for single-constructor types. For example, comparing (Control.Arrow.***) and Data.Bifunctor.bimap:

> (Control.Arrow.***) (const 0) (const 1) undefined :: (Int,Int)
(0,1)
> Data.Bifunctor.bimap (const 0) (const 1) undefined :: (Int,Int)
*** Exception: Prelude.undefined

The Bifunctor instances for the various tuple types and Const should be using irrefutable patterns. Likewise for the Biapply (,) instance.

@ekmett
Copy link
Owner

ekmett commented Sep 19, 2011

The problem with making this change is that the instances then violate the (bi)functor laws.

bimap f g | = (|, |) rather than |

I currently believe the irrefutable versions of these to be an anti-pattern as it causes us to duplicate a great deal of code to get things that aren't technically monads, functors, etc.

@Deewiant
Copy link
Collaborator Author

In practice I just find the irrefutable versions more useful. For example, I often write recursive functions that return tuples whose components should have lazy results, and they're much more convenient to write with e.g. bimap (x:) (y:) (recurse z) instead of manually pattern-matching (let ~(a,b) = recurse z in (x:a, y:b)).

If you want to stick to the theory that's fine, feel free to close this issue. Just pointing out something that'd allow me to use bifunctors instead of rolling my own tuple combinators (or using Control.Arrow for pairs) :-)

@ekmett
Copy link
Owner

ekmett commented Dec 16, 2012

I've repented on this issue.

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

No branches or pull requests

2 participants