-
Notifications
You must be signed in to change notification settings - Fork 274
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
Add strict over variants #1017
Open
treeowl
wants to merge
2
commits into
ekmett:master
Choose a base branch
from
treeowl:strict-over
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add strict over variants #1017
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{-# LANGUAGE CPP #-} | ||
{-# LANGUAGE DeriveTraversable #-} | ||
module Control.Lens.Internal.BoxT where | ||
import Control.Applicative | ||
import Data.Functor.Apply (Apply (..)) | ||
import Data.Functor.Contravariant (Contravariant (..)) | ||
import Data.Tuple.Solo (Solo (..)) | ||
|
||
-- | A very simple applicative transformer that gives us more control over when | ||
-- things get forced. Note: this type /should not/ be made an instance of | ||
-- @Settable@, because then users could accidentally use | ||
-- 'Control.Lens.Traversable.strictly' with a 'Control.Lens.Setter.Setter', | ||
-- which will not work at all. There is no way to strictify a @Setter@. | ||
newtype BoxT f a = BoxT | ||
{ runBoxT :: f (Solo a) } | ||
deriving (Functor, Foldable, Traversable) | ||
|
||
-- The Contravariant instance allows `strictly` to be used on a getter or fold. | ||
-- It's not at all obvious that this is *useful* (since `strictly` doesn't | ||
-- change these at all), but it's also not obviously *harmful*. | ||
instance Contravariant f => Contravariant (BoxT f) where | ||
contramap f (BoxT m) = BoxT $ contramap (fmap f) m | ||
{-# INLINE contramap #-} | ||
instance Apply f => Apply (BoxT f) where | ||
BoxT m <.> BoxT n = BoxT (liftF2 (<*>) m n) | ||
#if MIN_VERSION_semigroupoids(5,3,0) | ||
liftF2 f (BoxT m) (BoxT n) = BoxT (liftF2 (liftA2 f) m n) | ||
{-# INLINE liftF2 #-} | ||
#endif | ||
instance Applicative f => Applicative (BoxT f) where | ||
pure = BoxT . pure . Solo | ||
{-# INLINE pure #-} | ||
BoxT m <*> BoxT n = BoxT (liftA2 (<*>) m n) | ||
{-# INLINE (<*>) #-} | ||
#if MIN_VERSION_base(4,10,0) | ||
liftA2 f (BoxT m) (BoxT n) = BoxT (liftA2 (liftA2 f) m n) | ||
{-# INLINE liftA2 #-} | ||
#endif | ||
-- Caution: We *can't* implement *> or <* in terms of the underlying *> and | ||
-- <*. We need to force the Solos, not discard them. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also on the edge with this "warning".
Shouldn't the strict version also have a similar warning that it actually forces all the computations up front. I.e.
over' traverse f aList
is most likely a wrong thing to do. (And the examples used are indeed contrived).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, they're contrived. Should I give a datatype definition and
makeLenses
and all that jazz? A more realistic case might be a record with a lazy field. If you useover
several times, you'll build a thunk chain in there.over' traverse
isn't too far from what, say,Data.Map.Strict.map
does, though it's probably less efficient.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a problem (to give an non contrived examples). I think in majority of relevant cases you'd rather make the field strict. In fact, there are people which would prefer
Data.Map.Strict
to be a separate datatype. (The type system doesn't help to keep theMap
thunk free.)i.e. while
over'
is useful, I don't think it should be commonly used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only case I can think out of my head is
Seq
. Whereover' traverse
doesn't leave thunks, but AFAIK ruins asymptotics.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@phadej
over'
could quite reasonably be used withix
forArray
,SmallArray
(if that has an instance),Vector
,Seq
,Map
, orIntMap
. I imagine some folks just useat
instead, but that's semantically overkill (and potentially expensive) for just forcing.