Skip to content

Commit 25d60c6

Browse files
committed
Moved EvaluateSettings and InputSettings to own module
Those settings as well as the lenses necessary to access their fields live now in an own internal module Dhall.Settings. This enables us to remove all fields from Dhall.Import.Status that were in fact duplicates of the fields of EvaluateSettings.
1 parent 92b3b6a commit 25d60c6

File tree

8 files changed

+327
-219
lines changed

8 files changed

+327
-219
lines changed

dhall/dhall.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ Library
369369
Dhall.Normalize
370370
Dhall.Parser.Combinators
371371
Dhall.Pretty.Internal
372+
Dhall.Settings
372373
Dhall.Syntax
373374
Dhall.Syntax.Binding
374375
Dhall.Syntax.Chunks

dhall/ghc-src/Dhall/Import/HTTP.hs

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import qualified Data.HashMap.Strict as HashMap
5151
import qualified Data.Text as Text
5252
import qualified Data.Text.Encoding
5353
import qualified Dhall.Import.Types
54+
import qualified Dhall.Settings
5455
import qualified Dhall.Util
5556
import qualified Network.HTTP.Client as HTTP
5657
import qualified Network.HTTP.Types
@@ -170,8 +171,8 @@ renderPrettyHttpException url (HttpExceptionRequest _ e) =
170171

171172
newManager :: StateT Status IO Manager
172173
newManager = do
173-
manager <- liftIO =<< use Dhall.Import.Types.newManager
174-
assign Dhall.Import.Types.newManager (return manager)
174+
manager <- liftIO =<< use Dhall.Settings.newManager
175+
assign Dhall.Settings.newManager (return manager)
175176
return manager
176177

177178
data NotCORSCompliant = NotCORSCompliant

dhall/ghcjs-src/Dhall/Import/Manager.hs

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
55
For the GHC implementation the `Dhall.Import.Manager.Manager` type is a real
66
`Network.HTTP.Client.Manager` from the @http-client@ package. For the GHCJS
7-
implementation the `Dhall.Import.Manager.Manager` type is a synonym for
8-
@`Data.Void.Void`@ since GHCJS does not use a
9-
`Network.HTTP.Client.Manager` for HTTP requests.
7+
implementation the `Dhall.Import.Manager.Manager` type is a stub since GHCJS
8+
does not use a `Network.HTTP.Client.Manager` for HTTP requests.
109
-}
1110
module Dhall.Import.Manager
1211
( -- * Manager
@@ -16,11 +15,11 @@ module Dhall.Import.Manager
1615

1716
{-| The GHCJS implementation does not require a `Network.HTTP.Client.Manager`
1817
19-
The purpose of this synonym is so that "Dhall.Import.Types" can import a
18+
The purpose of this type is so that "Dhall.Import.Types" can import a
2019
`Dhall.Import.Manager.Manager` type from "Dhall.Import.HTTP" that does the
21-
correct thing for both the GHC and GHCJS implementations
20+
correct thing for both the GHC and GHCJS implementations.
2221
-}
23-
type Manager = ()
22+
data Manager = Manager
2423

2524
defaultNewManager :: IO Manager
26-
defaultNewManager = pure ()
25+
defaultNewManager = pure Manager

dhall/src/Dhall.hs

+82-148
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,24 @@ module Dhall
2828
, interpretExprWithSettings
2929
, fromExpr
3030
, fromExprWithSettings
31-
, rootDirectory
32-
, sourceName
33-
, startingContext
34-
, substitutions
35-
, normalizer
36-
, newManager
37-
, defaultInputSettings
38-
, InputSettings
39-
, defaultEvaluateSettings
40-
, EvaluateSettings
41-
, HasEvaluateSettings(..)
4231
, detailed
4332

33+
-- ** Input settings
34+
, Dhall.Settings.InputSettings
35+
, Dhall.Settings.defaultInputSettings
36+
, Dhall.Settings.rootDirectory
37+
, Dhall.Settings.sourceName
38+
, Dhall.Settings.HasInputSettings(..)
39+
40+
-- ** Evaluation settings
41+
, Dhall.Settings.EvaluateSettings
42+
, Dhall.Settings.defaultEvaluateSettings
43+
, Dhall.Settings.newManager
44+
, Dhall.Settings.normalizer
45+
, Dhall.Settings.startingContext
46+
, Dhall.Settings.substitutions
47+
, Dhall.Settings.HasEvaluateSettings(..)
48+
4449
-- * Decoders
4550
, module Dhall.Marshal.Decode
4651

@@ -66,151 +71,66 @@ import Data.Either.Validation (Validation (..))
6671
import Data.Void (Void)
6772
import Dhall.Import (Imported (..), Status)
6873
import Dhall.Parser (Src (..))
74+
import Dhall.Settings
75+
( EvaluateSettings
76+
, HasEvaluateSettings
77+
, HasInputSettings
78+
, InputSettings
79+
, defaultEvaluateSettings
80+
, defaultInputSettings
81+
)
6982
import Dhall.Syntax (Expr (..), Import)
7083
import Dhall.TypeCheck (DetailedTypeError (..), TypeError)
7184
import GHC.Generics
72-
import Lens.Micro (Lens', lens)
7385
import Lens.Micro.Extras (view)
7486
import Prelude hiding (maybe, sequence)
7587
import System.FilePath (takeDirectory)
7688

7789
import qualified Control.Exception
7890
import qualified Control.Monad.Trans.State.Strict as State
7991
import qualified Data.Text.IO
80-
import qualified Dhall.Context
8192
import qualified Dhall.Core as Core
8293
import qualified Dhall.Import
8394
import qualified Dhall.Parser
8495
import qualified Dhall.Pretty.Internal
96+
import qualified Dhall.Settings
8597
import qualified Dhall.Substitution
8698
import qualified Dhall.TypeCheck
8799
import qualified Lens.Micro as Lens
88100

89101
import Dhall.Marshal.Decode
90102
import Dhall.Marshal.Encode
91103

92-
-- | @since 1.16
93-
data InputSettings = InputSettings
94-
{ _rootDirectory :: FilePath
95-
, _sourceName :: FilePath
96-
, _evaluateSettings :: EvaluateSettings
97-
}
98-
99-
-- | Default input settings: resolves imports relative to @.@ (the
100-
-- current working directory), report errors as coming from @(input)@,
101-
-- and default evaluation settings from 'defaultEvaluateSettings'.
102-
--
103-
-- @since 1.16
104-
defaultInputSettings :: InputSettings
105-
defaultInputSettings = InputSettings
106-
{ _rootDirectory = "."
107-
, _sourceName = "(input)"
108-
, _evaluateSettings = defaultEvaluateSettings
109-
}
110-
111-
112-
-- | Access the directory to resolve imports relative to.
113-
--
114-
-- @since 1.16
115-
rootDirectory :: Lens' InputSettings FilePath
116-
rootDirectory = lens _rootDirectory (\s x -> s { _rootDirectory = x })
117-
118-
-- | Access the name of the source to report locations from; this is
119-
-- only used in error messages, so it's okay if this is a best guess
120-
-- or something symbolic.
121-
--
122-
-- @since 1.16
123-
sourceName :: Lens' InputSettings FilePath
124-
sourceName = lens _sourceName (\s x -> s { _sourceName = x})
125-
126-
-- | @since 1.16
127-
data EvaluateSettings = EvaluateSettings
128-
{ _substitutions :: Dhall.Substitution.Substitutions Src Void
129-
, _startingContext :: Dhall.Context.Context (Expr Src Void)
130-
, _normalizer :: Maybe (Core.ReifiedNormalizer Void)
131-
, _newManager :: IO Dhall.Import.Manager
132-
}
133-
134-
-- | Default evaluation settings: no extra entries in the initial
135-
-- context, and no special normalizer behaviour.
136-
--
137-
-- @since 1.16
138-
defaultEvaluateSettings :: EvaluateSettings
139-
defaultEvaluateSettings = EvaluateSettings
140-
{ _substitutions = Dhall.Substitution.empty
141-
, _startingContext = Dhall.Context.empty
142-
, _normalizer = Nothing
143-
, _newManager = Dhall.Import.defaultNewManager
144-
}
145-
146-
-- | Access the starting context used for evaluation and type-checking.
147-
--
148-
-- @since 1.16
149-
startingContext
150-
:: (HasEvaluateSettings s)
151-
=> Lens' s (Dhall.Context.Context (Expr Src Void))
152-
startingContext =
153-
evaluateSettings
154-
. lens _startingContext (\s x -> s { _startingContext = x})
155-
156-
-- | Access the custom substitutions.
157-
--
158-
-- @since 1.30
159-
substitutions
160-
:: (HasEvaluateSettings s)
161-
=> Lens' s (Dhall.Substitution.Substitutions Src Void)
162-
substitutions =
163-
evaluateSettings
164-
. lens _substitutions (\s x -> s { _substitutions = x })
165-
166-
-- | Access the custom normalizer.
167-
--
168-
-- @since 1.16
169-
normalizer
170-
:: (HasEvaluateSettings s)
171-
=> Lens' s (Maybe (Core.ReifiedNormalizer Void))
172-
normalizer =
173-
evaluateSettings
174-
. lens _normalizer (\s x -> s { _normalizer = x })
175-
176-
-- | Access the HTTP manager initializer.
177-
--
178-
-- @since 1.36
179-
newManager
180-
:: (HasEvaluateSettings s)
181-
=> Lens' s (IO Dhall.Import.Manager)
182-
newManager =
183-
evaluateSettings
184-
. lens _newManager (\s x -> s { _newManager = x })
185-
186-
-- | @since 1.16
187-
class HasEvaluateSettings s where
188-
evaluateSettings :: Lens' s EvaluateSettings
189-
190-
instance HasEvaluateSettings InputSettings where
191-
evaluateSettings =
192-
lens _evaluateSettings (\s x -> s { _evaluateSettings = x })
193-
194-
instance HasEvaluateSettings EvaluateSettings where
195-
evaluateSettings = id
104+
--------------------------------------------------------------------------------
105+
-- Individual phases
106+
--------------------------------------------------------------------------------
196107

197108
-- | Parse an expression, using the supplied `InputSettings`
198-
parseWithSettings :: MonadThrow m => InputSettings -> Text -> m (Expr Src Import)
199-
parseWithSettings settings text =
200-
either throwM return (Dhall.Parser.exprFromText (view sourceName settings) text)
109+
parseWithSettings
110+
:: (HasInputSettings s, MonadThrow m)
111+
=> s -> Text -> m (Expr Src Import)
112+
parseWithSettings settings text = do
113+
let sourceName = view Dhall.Settings.sourceName settings
114+
115+
either throwM return (Dhall.Parser.exprFromText sourceName text)
201116

202117
-- | Type-check an expression, using the supplied `InputSettings`
203-
typecheckWithSettings :: MonadThrow m => InputSettings -> Expr Src Void -> m ()
204-
typecheckWithSettings settings expression =
205-
either throwM (return . const ()) (Dhall.TypeCheck.typeWith (view startingContext settings) expression)
118+
typecheckWithSettings
119+
:: (HasEvaluateSettings s, MonadThrow m)
120+
=> s -> Expr Src Void -> m ()
121+
typecheckWithSettings settings expression = do
122+
let startingContext = view Dhall.Settings.startingContext settings
123+
124+
either throwM (return . const ())
125+
(Dhall.TypeCheck.typeWith startingContext expression)
206126

207127
{-| Type-check an expression against a type provided as a Dhall expreession,
208128
using the supplied `InputSettings`
209129
-}
210130
checkWithSettings ::
211-
MonadThrow m =>
131+
(HasEvaluateSettings s, MonadThrow m) =>
212132
-- | The input settings
213-
InputSettings ->
133+
s ->
214134
-- | The expected type of the expression
215135
Expr Src Void ->
216136
-- | The expression to check
@@ -234,7 +154,9 @@ checkWithSettings settings type_ expression = do
234154
This is equivalent of using the 'expected' type of a @Decoder@ as the second
235155
argument to 'checkWithSettings'.
236156
-}
237-
expectWithSettings :: MonadThrow m => InputSettings -> Decoder a -> Expr Src Void -> m ()
157+
expectWithSettings
158+
:: (HasEvaluateSettings s, MonadThrow m)
159+
=> s -> Decoder a -> Expr Src Void -> m ()
238160
expectWithSettings settings Decoder{..} expression = do
239161
expected' <- case expected of
240162
Success x -> return x
@@ -247,38 +169,44 @@ expectWithSettings settings Decoder{..} expression = do
247169
Note that this also applies any substitutions specified in the
248170
`InputSettings`
249171
-}
250-
resolveWithSettings :: InputSettings -> Expr Src Import -> IO (Expr Src Void)
172+
resolveWithSettings
173+
:: (HasInputSettings s)
174+
=> s -> Expr Src Import -> IO (Expr Src Void)
251175
resolveWithSettings settings expression =
252176
fst <$> resolveAndStatusWithSettings settings expression
253177

254178
-- | A version of 'resolveWithSettings' that also returns the import 'Status'
255179
-- together with the resolved expression.
256180
resolveAndStatusWithSettings
257-
:: InputSettings
258-
-> Expr Src Import
259-
-> IO (Expr Src Void, Status)
181+
:: (HasInputSettings s)
182+
=> s -> Expr Src Import -> IO (Expr Src Void, Status)
260183
resolveAndStatusWithSettings settings expression = do
261-
let InputSettings{..} = settings
184+
let inputSettings = view Dhall.Settings.inputSettings settings
262185

263-
let EvaluateSettings{..} = _evaluateSettings
186+
let evaluateSettings = view Dhall.Settings.evaluateSettings inputSettings
264187

265-
let transform =
266-
Lens.set Dhall.Import.substitutions _substitutions
267-
. Lens.set Dhall.Import.normalizer _normalizer
268-
. Lens.set Dhall.Import.startingContext _startingContext
188+
let rootDirectory = view Dhall.Settings.rootDirectory inputSettings
269189

270-
let status = transform (Dhall.Import.emptyStatusWithManager _newManager _rootDirectory)
190+
let substitutions = view Dhall.Settings.substitutions evaluateSettings
191+
192+
let status = Dhall.Import.emptyStatusWith evaluateSettings rootDirectory
271193

272194
(resolved, status') <- State.runStateT (Dhall.Import.loadWith expression) status
273195

274-
let substituted = Dhall.Substitution.substitute resolved (view substitutions settings)
196+
let substituted = Dhall.Substitution.substitute resolved substitutions
275197

276198
pure (substituted, status')
277199

278200
-- | Normalize an expression, using the supplied `InputSettings`
279-
normalizeWithSettings :: InputSettings -> Expr Src Void -> Expr Src Void
201+
normalizeWithSettings
202+
:: (HasEvaluateSettings s)
203+
=> s -> Expr Src Void -> Expr Src Void
280204
normalizeWithSettings settings =
281-
Core.normalizeWith (view normalizer settings)
205+
Core.normalizeWith (view Dhall.Settings.normalizer settings)
206+
207+
--------------------------------------------------------------------------------
208+
-- High-level entrypoints
209+
--------------------------------------------------------------------------------
282210

283211
{-| Type-check and evaluate a Dhall program, decoding the result into Haskell
284212
@@ -366,11 +294,11 @@ inputFileWithSettings
366294
-- ^ The decoded value in Haskell.
367295
inputFileWithSettings settings ty path = do
368296
text <- Data.Text.IO.readFile path
369-
let inputSettings = InputSettings
370-
{ _rootDirectory = takeDirectory path
371-
, _sourceName = path
372-
, _evaluateSettings = settings
373-
}
297+
let inputSettings
298+
= Lens.set Dhall.Settings.evaluateSettings settings
299+
. Lens.set Dhall.Settings.rootDirectory (takeDirectory path)
300+
. Lens.set Dhall.Settings.sourceName path
301+
$ Dhall.Settings.defaultInputSettings
374302
inputWithSettings inputSettings ty text
375303

376304
{-| Similar to `input`, but without interpreting the Dhall `Expr` into a Haskell
@@ -405,7 +333,9 @@ inputExprWithSettings settings text = do
405333

406334
_ <- typecheckWithSettings settings resolved
407335

408-
pure (Core.normalizeWith (view normalizer settings) resolved)
336+
let normalizer = view Dhall.Settings.normalizer settings
337+
338+
pure (Core.normalizeWith normalizer resolved)
409339

410340
{-| Interpret a Dhall Expression
411341
@@ -422,7 +352,9 @@ interpretExprWithSettings settings parsed = do
422352

423353
typecheckWithSettings settings resolved
424354

425-
pure (Core.normalizeWith (view normalizer settings) resolved)
355+
let normalizer = view Dhall.Settings.normalizer settings
356+
357+
pure (Core.normalizeWith normalizer resolved)
426358

427359
{- | Decode a Dhall expression
428360
@@ -438,7 +370,9 @@ fromExprWithSettings settings decoder@Decoder{..} expression = do
438370

439371
expectWithSettings settings decoder resolved
440372

441-
let normalized = Core.normalizeWith (view normalizer settings) resolved
373+
let normalizer = view Dhall.Settings.normalizer settings
374+
375+
let normalized = Core.normalizeWith normalizer resolved
442376

443377
case extract normalized of
444378
Success x -> return x

0 commit comments

Comments
 (0)