Skip to content

Commit 26bb77e

Browse files
authored
Move lens-like utilities to Dhall.Optics module (#986)
1 parent bb490a7 commit 26bb77e

File tree

5 files changed

+59
-58
lines changed

5 files changed

+59
-58
lines changed

dhall/dhall.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ Library
478478
Dhall.Lint,
479479
Dhall.Main,
480480
Dhall.Map,
481+
Dhall.Optics,
481482
Dhall.Set,
482483
Dhall.Src,
483484
Dhall.Parser,

dhall/src/Dhall/Core.hs

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ module Dhall.Core (
5454
-- * Optics
5555
, subExpressions
5656
, chunkExprs
57-
, rewriteOf
58-
, transformOf
59-
, over
60-
, rewriteMOf
61-
, transformMOf
62-
, mapMOf
6357

6458
-- * Miscellaneous
6559
, internalError
@@ -70,9 +64,8 @@ module Dhall.Core (
7064
) where
7165

7266
#if MIN_VERSION_base(4,8,0)
73-
import Control.Applicative (WrappedMonad(..))
7467
#else
75-
import Control.Applicative (Applicative(..), WrappedMonad(..), (<$>))
68+
import Control.Applicative (Applicative(..), (<$>))
7669
#endif
7770
import Control.Applicative (empty)
7871
import Control.Exception (Exception)
@@ -84,7 +77,6 @@ import Data.Foldable
8477
import Data.Functor.Identity (Identity(..))
8578
import Data.HashSet (HashSet)
8679
import Data.List.NonEmpty (NonEmpty(..))
87-
import Data.Profunctor.Unsafe ((#.))
8880
import Data.String (IsString(..))
8981
import Data.Semigroup (Semigroup(..))
9082
import Data.Sequence (Seq, ViewL(..), ViewR(..))
@@ -96,7 +88,6 @@ import Dhall.Set (Set)
9688
import Dhall.Src (Src)
9789
import {-# SOURCE #-} Dhall.Pretty.Internal
9890
import GHC.Generics (Generic)
99-
import Lens.Family (ASetter, LensLike, over)
10091
import Numeric.Natural (Natural)
10192
import Prelude hiding (succ)
10293

@@ -2154,49 +2145,3 @@ prettyURIComponent text
21542145
throws :: (Exception e, MonadIO io) => Either e a -> io a
21552146
throws (Left e) = liftIO (Control.Exception.throwIO e)
21562147
throws (Right r) = return r
2157-
2158-
{-| Convenience utility identical to @"Control.Lens".`Control.Lens.rewriteOf`@
2159-
re-exported for convenience in order to minimize dependencies on @lens@
2160-
-}
2161-
rewriteOf :: ASetter a b a b -> (b -> Maybe a) -> a -> b
2162-
rewriteOf l f = go
2163-
where
2164-
go = transformOf l (\x -> maybe x go (f x))
2165-
{-# INLINE rewriteOf #-}
2166-
2167-
{-| Convenience utility identical to @"Control.Lens".`Control.Lens.transformOf`@
2168-
re-exported for convenience in order to minimize dependencies on @lens@
2169-
-}
2170-
transformOf :: ASetter a b a b -> (b -> b) -> a -> b
2171-
transformOf l f = go
2172-
where
2173-
go = f . over l go
2174-
{-# INLINE transformOf #-}
2175-
2176-
{-| Convenience utility identical to @"Control.Lens".`Control.Lens.rewriteMOf`@
2177-
re-exported for convenience in order to minimize dependencies on @lens@
2178-
-}
2179-
rewriteMOf
2180-
:: Monad m
2181-
=> LensLike (WrappedMonad m) a b a b -> (b -> m (Maybe a)) -> a -> m b
2182-
rewriteMOf l f = go
2183-
where
2184-
go = transformMOf l (\x -> f x >>= maybe (return x) go)
2185-
{-# INLINE rewriteMOf #-}
2186-
2187-
{-| Convenience utility identical to @"Control.Lens".`Control.Lens.transformMOf`@
2188-
re-exported for convenience in order to minimize dependencies on @lens@
2189-
-}
2190-
transformMOf
2191-
:: Monad m => LensLike (WrappedMonad m) a b a b -> (b -> m b) -> a -> m b
2192-
transformMOf l f = go
2193-
where
2194-
go t = mapMOf l go t >>= f
2195-
{-# INLINE transformMOf #-}
2196-
2197-
{-| Convenience utility identical to @"Control.Lens".`Control.Lens.mapMOf`@
2198-
re-exported for convenience in order to minimize dependencies on @lens@
2199-
-}
2200-
mapMOf :: LensLike (WrappedMonad m) s t a b -> (a -> m b) -> s -> m t
2201-
mapMOf l cmd = unwrapMonad #. l (WrapMonad #. cmd)
2202-
{-# INLINE mapMOf #-}

dhall/src/Dhall/Freeze.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty
3434
import qualified Data.Text.IO
3535
import qualified Dhall.Core
3636
import qualified Dhall.Import
37+
import qualified Dhall.Optics
3738
import qualified Dhall.TypeCheck
3839
import qualified System.FilePath
3940
import qualified System.IO
@@ -208,7 +209,7 @@ freeze inplace scope intent characterSet _standardVersion = do
208209
Secure ->
209210
traverse freezeFunction expression
210211
Cache ->
211-
Dhall.Core.transformMOf
212+
Dhall.Optics.transformMOf
212213
Dhall.Core.subExpressions
213214
cache
214215
(Dhall.Core.denote expression)

dhall/src/Dhall/Lint.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Data.Semigroup ((<>))
1414
import Dhall.Core (Binding(..), Expr(..), Import, Var(..), subExpressions)
1515

1616
import qualified Dhall.Core
17+
import qualified Dhall.Optics
1718

1819
{-| Automatically improve a Dhall expression
1920
@@ -25,7 +26,7 @@ import qualified Dhall.Core
2526
-}
2627
lint :: Expr s Import -> Expr t Import
2728
lint =
28-
Dhall.Core.rewriteOf
29+
Dhall.Optics.rewriteOf
2930
subExpressions
3031
( \e ->
3132
removeLetInLet e

dhall/src/Dhall/Optics.hs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{-| This module contains some useful utilities copy-and-pasted from the @lens@
2+
library to avoid a dependency which are used internally and also re-exported
3+
for convenience
4+
-}
5+
6+
module Dhall.Optics
7+
( -- * Utilities
8+
rewriteOf
9+
, transformOf
10+
, rewriteMOf
11+
, transformMOf
12+
, mapMOf
13+
) where
14+
15+
import Control.Applicative (WrappedMonad(..))
16+
import Data.Profunctor.Unsafe ((#.))
17+
import Lens.Family (ASetter, LensLike, over)
18+
19+
-- | Identical to @"Control.Lens".`Control.Lens.rewriteOf`@
20+
rewriteOf :: ASetter a b a b -> (b -> Maybe a) -> a -> b
21+
rewriteOf l f = go
22+
where
23+
go = transformOf l (\x -> maybe x go (f x))
24+
{-# INLINE rewriteOf #-}
25+
26+
-- | Identical to @"Control.Lens".`Control.Lens.transformOf`@
27+
transformOf :: ASetter a b a b -> (b -> b) -> a -> b
28+
transformOf l f = go
29+
where
30+
go = f . over l go
31+
{-# INLINE transformOf #-}
32+
33+
-- | Identical to @"Control.Lens".`Control.Lens.rewriteMOf`@
34+
rewriteMOf
35+
:: Monad m
36+
=> LensLike (WrappedMonad m) a b a b -> (b -> m (Maybe a)) -> a -> m b
37+
rewriteMOf l f = go
38+
where
39+
go = transformMOf l (\x -> f x >>= maybe (return x) go)
40+
{-# INLINE rewriteMOf #-}
41+
42+
-- | Identical to @"Control.Lens".`Control.Lens.transformMOf`@
43+
transformMOf
44+
:: Monad m => LensLike (WrappedMonad m) a b a b -> (b -> m b) -> a -> m b
45+
transformMOf l f = go
46+
where
47+
go t = mapMOf l go t >>= f
48+
{-# INLINE transformMOf #-}
49+
50+
-- | Identical to @"Control.Lens".`Control.Lens.mapMOf`@
51+
mapMOf :: LensLike (WrappedMonad m) s t a b -> (a -> m b) -> s -> m t
52+
mapMOf l cmd = unwrapMonad #. l (WrapMonad #. cmd)
53+
{-# INLINE mapMOf #-}

0 commit comments

Comments
 (0)