Skip to content

Commit a902587

Browse files
committed
Use Lens.Micro.Lens' and Lens.Micro.lens where appropriate
1 parent 68eb359 commit a902587

File tree

4 files changed

+60
-80
lines changed

4 files changed

+60
-80
lines changed

dhall/src/Dhall.hs

+28-44
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ import Dhall.Parser (Src (..))
6969
import Dhall.Syntax (Expr (..), Import)
7070
import Dhall.TypeCheck (DetailedTypeError (..), TypeError)
7171
import GHC.Generics
72-
import Lens.Micro (LensLike')
72+
import Lens.Micro (Lens', lens)
7373
import Lens.Micro.Extras (view)
7474
import Prelude hiding (maybe, sequence)
7575
import System.FilePath (takeDirectory)
@@ -112,22 +112,16 @@ defaultInputSettings = InputSettings
112112
-- | Access the directory to resolve imports relative to.
113113
--
114114
-- @since 1.16
115-
rootDirectory
116-
:: (Functor f)
117-
=> LensLike' f InputSettings FilePath
118-
rootDirectory k s =
119-
fmap (\x -> s { _rootDirectory = x }) (k (_rootDirectory s))
115+
rootDirectory :: Lens' InputSettings FilePath
116+
rootDirectory = lens _rootDirectory (\s x -> s { _rootDirectory = x })
120117

121118
-- | Access the name of the source to report locations from; this is
122119
-- only used in error messages, so it's okay if this is a best guess
123120
-- or something symbolic.
124121
--
125122
-- @since 1.16
126-
sourceName
127-
:: (Functor f)
128-
=> LensLike' f InputSettings FilePath
129-
sourceName k s =
130-
fmap (\x -> s { _sourceName = x}) (k (_sourceName s))
123+
sourceName :: Lens' InputSettings FilePath
124+
sourceName = lens _sourceName (\s x -> s { _sourceName = x})
131125

132126
-- | @since 1.16
133127
data EvaluateSettings = EvaluateSettings
@@ -153,59 +147,49 @@ defaultEvaluateSettings = EvaluateSettings
153147
--
154148
-- @since 1.16
155149
startingContext
156-
:: (Functor f, HasEvaluateSettings s)
157-
=> LensLike' f s (Dhall.Context.Context (Expr Src Void))
158-
startingContext = evaluateSettings . l
159-
where
160-
l :: (Functor f)
161-
=> LensLike' f EvaluateSettings (Dhall.Context.Context (Expr Src Void))
162-
l k s = fmap (\x -> s { _startingContext = x}) (k (_startingContext s))
150+
:: (HasEvaluateSettings s)
151+
=> Lens' s (Dhall.Context.Context (Expr Src Void))
152+
startingContext =
153+
evaluateSettings
154+
. lens _startingContext (\s x -> s { _startingContext = x})
163155

164156
-- | Access the custom substitutions.
165157
--
166158
-- @since 1.30
167159
substitutions
168-
:: (Functor f, HasEvaluateSettings s)
169-
=> LensLike' f s (Dhall.Substitution.Substitutions Src Void)
170-
substitutions = evaluateSettings . l
171-
where
172-
l :: (Functor f)
173-
=> LensLike' f EvaluateSettings (Dhall.Substitution.Substitutions Src Void)
174-
l k s = fmap (\x -> s { _substitutions = x }) (k (_substitutions s))
160+
:: (HasEvaluateSettings s)
161+
=> Lens' s (Dhall.Substitution.Substitutions Src Void)
162+
substitutions =
163+
evaluateSettings
164+
. lens _substitutions (\s x -> s { _substitutions = x })
175165

176166
-- | Access the custom normalizer.
177167
--
178168
-- @since 1.16
179169
normalizer
180-
:: (Functor f, HasEvaluateSettings s)
181-
=> LensLike' f s (Maybe (Core.ReifiedNormalizer Void))
182-
normalizer = evaluateSettings . l
183-
where
184-
l :: (Functor f)
185-
=> LensLike' f EvaluateSettings (Maybe (Core.ReifiedNormalizer Void))
186-
l k s = fmap (\x -> s { _normalizer = x }) (k (_normalizer s))
170+
:: (HasEvaluateSettings s)
171+
=> Lens' s (Maybe (Core.ReifiedNormalizer Void))
172+
normalizer =
173+
evaluateSettings
174+
. lens _normalizer (\s x -> s { _normalizer = x })
187175

188176
-- | Access the HTTP manager initializer.
189177
--
190178
-- @since 1.36
191179
newManager
192-
:: (Functor f, HasEvaluateSettings s)
193-
=> LensLike' f s (IO Dhall.Import.Manager)
194-
newManager = evaluateSettings . l
195-
where
196-
l :: (Functor f)
197-
=> LensLike' f EvaluateSettings (IO Dhall.Import.Manager)
198-
l k s = fmap (\x -> s { _newManager = x }) (k (_newManager s))
180+
:: (HasEvaluateSettings s)
181+
=> Lens' s (IO Dhall.Import.Manager)
182+
newManager =
183+
evaluateSettings
184+
. lens _newManager (\s x -> s { _newManager = x })
199185

200186
-- | @since 1.16
201187
class HasEvaluateSettings s where
202-
evaluateSettings
203-
:: (Functor f)
204-
=> LensLike' f s EvaluateSettings
188+
evaluateSettings :: Lens' s EvaluateSettings
205189

206190
instance HasEvaluateSettings InputSettings where
207-
evaluateSettings k s =
208-
fmap (\x -> s { _evaluateSettings = x }) (k (_evaluateSettings s))
191+
evaluateSettings =
192+
lens _evaluateSettings (\s x -> s { _evaluateSettings = x })
209193

210194
instance HasEvaluateSettings EvaluateSettings where
211195
evaluateSettings = id

dhall/src/Dhall/Import/Types.hs

+19-24
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import Dhall.Core
2323
)
2424
import Dhall.Map (Map)
2525
import Dhall.Parser (Src)
26-
import Lens.Micro (LensLike')
26+
import Lens.Micro (Lens', lens)
2727
import Prettyprinter (Pretty (..))
2828

2929
#ifdef WITH_HTTP
@@ -164,45 +164,40 @@ emptyStatusWith _newManager _loadOriginHeaders _remote _remoteBytes rootImport =
164164
_cacheWarning = CacheNotWarned
165165

166166
-- | Lens from a `Status` to its `_stack` field
167-
stack :: Functor f => LensLike' f Status (NonEmpty Chained)
168-
stack k s = fmap (\x -> s { _stack = x }) (k (_stack s))
167+
stack :: Lens' Status (NonEmpty Chained)
168+
stack = lens _stack (\s x -> s { _stack = x })
169169

170170
-- | Lens from a `Status` to its `_graph` field
171-
graph :: Functor f => LensLike' f Status [Depends]
172-
graph k s = fmap (\x -> s { _graph = x }) (k (_graph s))
171+
graph :: Lens' Status [Depends]
172+
graph = lens _graph (\s x -> s { _graph = x })
173173

174174
-- | Lens from a `Status` to its `_cache` field
175-
cache :: Functor f => LensLike' f Status (Map Chained ImportSemantics)
176-
cache k s = fmap (\x -> s { _cache = x }) (k (_cache s))
175+
cache :: Lens' Status (Map Chained ImportSemantics)
176+
cache = lens _cache (\s x -> s { _cache = x })
177177

178178
-- | Lens from a `Status` to its `_remote` field
179-
remote
180-
:: Functor f
181-
=> LensLike' f Status (URL -> StateT Status IO Data.Text.Text)
182-
remote k s = fmap (\x -> s { _remote = x }) (k (_remote s))
179+
remote :: Lens' Status (URL -> StateT Status IO Data.Text.Text)
180+
remote = lens _remote (\s x -> s { _remote = x })
183181

184182
-- | Lens from a `Status` to its `_remote` field
185-
remoteBytes
186-
:: Functor f
187-
=> LensLike' f Status (URL -> StateT Status IO Data.ByteString.ByteString)
188-
remoteBytes k s = fmap (\x -> s { _remoteBytes = x }) (k (_remoteBytes s))
183+
remoteBytes :: Lens' Status (URL -> StateT Status IO Data.ByteString.ByteString)
184+
remoteBytes = lens _remoteBytes (\s x -> s { _remoteBytes = x })
189185

190186
-- | Lens from a `Status` to its `_substitutions` field
191-
substitutions :: Functor f => LensLike' f Status (Dhall.Substitution.Substitutions Src Void)
192-
substitutions k s = fmap (\x -> s { _substitutions = x }) (k (_substitutions s))
187+
substitutions :: Lens' Status (Dhall.Substitution.Substitutions Src Void)
188+
substitutions = lens _substitutions (\s x -> s { _substitutions = x })
193189

194190
-- | Lens from a `Status` to its `_normalizer` field
195-
normalizer :: Functor f => LensLike' f Status (Maybe (ReifiedNormalizer Void))
196-
normalizer k s = fmap (\x -> s {_normalizer = x}) (k (_normalizer s))
191+
normalizer :: Lens' Status (Maybe (ReifiedNormalizer Void))
192+
normalizer = lens _normalizer (\s x -> s {_normalizer = x})
197193

198194
-- | Lens from a `Status` to its `_startingContext` field
199-
startingContext :: Functor f => LensLike' f Status (Context (Expr Src Void))
200-
startingContext k s =
201-
fmap (\x -> s { _startingContext = x }) (k (_startingContext s))
195+
startingContext :: Lens' Status (Context (Expr Src Void))
196+
startingContext = lens _startingContext (\s x -> s { _startingContext = x })
202197

203198
-- | Lens from a `Status` to its `_cacheWarning` field
204-
cacheWarning :: Functor f => LensLike' f Status CacheWarning
205-
cacheWarning k s = fmap (\x -> s { _cacheWarning = x }) (k (_cacheWarning s))
199+
cacheWarning :: Lens' Status CacheWarning
200+
cacheWarning = lens _cacheWarning (\s x -> s { _cacheWarning = x })
206201

207202
{-| This exception indicates that there was an internal error in Dhall's
208203
import-related logic

dhall/src/Dhall/Optics.hs

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
for convenience
44
-}
55

6+
{-# LANGUAGE RankNTypes #-}
7+
68
module Dhall.Optics
79
( Optic
810
, Optic'
@@ -23,7 +25,7 @@ import Data.Coerce (coerce)
2325
import Data.Monoid (Any (..))
2426
import Data.Functor.Contravariant (Contravariant (contramap))
2527
import Data.Profunctor (Profunctor (dimap))
26-
import Lens.Micro (ASetter, LensLike, LensLike')
28+
import Lens.Micro (ASetter, LensLike, Traversal)
2729
import Lens.Micro.Internal (foldMapOf, (#.))
2830

2931
import qualified Lens.Micro
@@ -78,7 +80,7 @@ mapMOf = coerce
7880
{-# INLINE mapMOf #-}
7981

8082
-- | Identical to @"Control.Lens.Plated".`Control.Lens.Plated.cosmosOf`@
81-
cosmosOf :: (Applicative f, Contravariant f) => LensLike' f a a -> LensLike' f a a
83+
cosmosOf :: Traversal s t s t -> Traversal s t s b
8284
cosmosOf d f s = f s *> d (cosmosOf d f) s
8385
{-# INLINE cosmosOf #-}
8486

dhall/src/Dhall/Package.hs

+9-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import qualified Dhall.Map as Map
3939
import Dhall.Pretty (CharacterSet (..))
4040
import qualified Dhall.Pretty
4141
import Dhall.Util (_ERROR, renderExpression)
42-
import Lens.Micro (LensLike')
42+
import Lens.Micro (Lens', lens)
4343
import System.Directory
4444
import System.FilePath
4545

@@ -65,20 +65,19 @@ defaultOptions = Options
6565
}
6666

6767
-- | Access the character set used to render the package content.
68-
characterSet :: Functor f => LensLike' f Options CharacterSet
69-
characterSet k s =
70-
fmap (\x -> s { optionsCharacterSet = x }) (k (optionsCharacterSet s))
68+
characterSet :: Lens' Options CharacterSet
69+
characterSet = lens optionsCharacterSet (\s x -> s { optionsCharacterSet = x })
7170

7271
-- | Access the file name used for the package file.
73-
packageFileName :: Functor f => LensLike' f Options String
74-
packageFileName k s =
75-
fmap (\x -> s { optionsPackageFileName = x }) (k (optionsPackageFileName s))
72+
packageFileName :: Lens' Options String
73+
packageFileName =
74+
lens optionsPackageFileName (\s x -> s { optionsPackageFileName = x })
7675

7776
-- | Access the packaging mode.
7877
-- See the documentation of 'getPackagePathAndContent'.
79-
packagingMode :: Functor f => LensLike' f Options PackagingMode
80-
packagingMode k s =
81-
fmap (\x -> s { optionsPackagingMode = x }) (k (optionsPackagingMode s))
78+
packagingMode :: Lens' Options PackagingMode
79+
packagingMode =
80+
lens optionsPackagingMode (\s x -> s { optionsPackagingMode = x })
8281

8382
-- | Whether to recursively create a package for each subdirectory or not.
8483
-- See the documentation of 'getPackagePathAndContent'.

0 commit comments

Comments
 (0)