Skip to content

Commit

Permalink
Merge pull request #876 from treeowl/patch-2
Browse files Browse the repository at this point in the history
Generalize magnify
  • Loading branch information
ekmett authored Jul 16, 2019
2 parents 965f830 + e86c76d commit 06a79d8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
* Add `filteredBy`.
* Add `adjoin` to `Control.Lens.Unsound`.
* Add `Each (Either a a) (Either b b) a b` instance.
* Make `magnify` offer its getter argument the `Contravariant` and `Functor`
instances it will require. This allows `magnify` to be used without
knowing the concrete monad being magnified.

4.17.1 [2019.04.26]
-------------------
Expand Down
19 changes: 17 additions & 2 deletions src/Control/Lens/Zoom.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE RankNTypes #-}
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}

#ifndef MIN_VERSION_mtl
Expand Down Expand Up @@ -49,6 +51,7 @@ import Control.Monad.Trans.List
import Control.Monad.Trans.Identity
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Free
import Data.Functor.Contravariant (Contravariant)
import Data.Monoid
import Data.Profunctor.Unsafe
import Prelude
Expand Down Expand Up @@ -227,6 +230,15 @@ class (Magnified m ~ Magnified n, MonadReader b m, MonadReader a n) => Magnify m
-- >>> flip Reader.runReader (1,2,[10..20]) $ magnify (_3._tail) Reader.ask
-- [11,12,13,14,15,16,17,18,19,20]
--
-- The type can be read as
--
-- @
-- magnify :: LensLike' (Magnified m c) a b -> m c -> n c
-- @
--
-- but the higher-rank constraints make it easier to apply @magnify@ to a
-- 'Getter' in highly-polymorphic code.
--
-- @
-- 'magnify' :: 'Getter' s a -> (a -> r) -> s -> r
-- 'magnify' :: 'Monoid' r => 'Fold' s a -> (a -> r) -> s -> r
Expand All @@ -237,7 +249,10 @@ class (Magnified m ~ Magnified n, MonadReader b m, MonadReader a n) => Magnify m
-- 'magnify' :: ('Monoid' w, 'Monoid' c) => 'Fold' s a -> 'RWS' a w st c -> 'RWS' s w st c
-- ...
-- @
magnify :: LensLike' (Magnified m c) a b -> m c -> n c
magnify :: ((Functor (Magnified m c), Contravariant (Magnified m c))
=> LensLike' (Magnified m c) a b)
-> m c -> n c


instance Monad m => Magnify (ReaderT b m) (ReaderT a m) b a where
magnify l (ReaderT m) = ReaderT $ getEffect #. l (Effect #. m)
Expand All @@ -247,7 +262,7 @@ instance Monad m => Magnify (ReaderT b m) (ReaderT a m) b a where
-- 'magnify' = 'views'
-- @
instance Magnify ((->) b) ((->) a) b a where
magnify = views
magnify l = views l
{-# INLINE magnify #-}

instance (Monad m, Monoid w) => Magnify (Strict.RWST b w s m) (Strict.RWST a w s m) b a where
Expand Down

0 comments on commit 06a79d8

Please sign in to comment.