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

Strictify traversals? #474

Open
treeowl opened this issue Dec 27, 2022 · 0 comments
Open

Strictify traversals? #474

treeowl opened this issue Dec 27, 2022 · 0 comments

Comments

@treeowl
Copy link
Contributor

treeowl commented Dec 27, 2022

I'm currently working on a PR for lens that adds

strictly :: (Profunctor p, Profunctor q, Functor f) => Optical p q (BoxT f) s t a b -> Optical p q f s t a b
strictly l f = rmap (fmap getSolo .# runBoxT) $ l (rmap (BoxT #. fmap (Solo $!)) f)

where BoxT is the following applicative transformer:

newtype BoxT f a = BoxT { runBoxT :: f (Solo a) }
  deriving Functor
instance Apply f => Apply (BoxT f) where
  liftF2 f (BoxT m) (BoxT n) = BoxT (liftF2 (liftA2 f) m n)
instance Applicative f => Applicative (BoxT f) where
  pure = BoxT . fmap Solo
  liftA2 f (BoxT m) (BoxT n) = BoxT (liftA2 (liftA2 f) m n)

-- The Contravariant instance probably isn't really useful (strictifying
-- a getter or fold doesn't change anything) but it at least *seems*
-- harmless.
instance Contravariant f => Contravariant (BoxT f) where
  contramap f (BoxT m) = BoxT (contramap (fmap f))

-- BoxT intentionally does *not* have a Settable instance,
-- because it's impossible to strictify things like `mapped` that
-- are really just setters.

I figured you might be interested in doing something similar here.

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

1 participant