@@ -28,19 +28,24 @@ module Dhall
28
28
, interpretExprWithSettings
29
29
, fromExpr
30
30
, fromExprWithSettings
31
- , rootDirectory
32
- , sourceName
33
- , startingContext
34
- , substitutions
35
- , normalizer
36
- , newManager
37
- , defaultInputSettings
38
- , InputSettings
39
- , defaultEvaluateSettings
40
- , EvaluateSettings
41
- , HasEvaluateSettings (.. )
42
31
, detailed
43
32
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
+
44
49
-- * Decoders
45
50
, module Dhall.Marshal.Decode
46
51
@@ -66,151 +71,66 @@ import Data.Either.Validation (Validation (..))
66
71
import Data.Void (Void )
67
72
import Dhall.Import (Imported (.. ), Status )
68
73
import Dhall.Parser (Src (.. ))
74
+ import Dhall.Settings
75
+ ( EvaluateSettings
76
+ , HasEvaluateSettings
77
+ , HasInputSettings
78
+ , InputSettings
79
+ , defaultEvaluateSettings
80
+ , defaultInputSettings
81
+ )
69
82
import Dhall.Syntax (Expr (.. ), Import )
70
83
import Dhall.TypeCheck (DetailedTypeError (.. ), TypeError )
71
84
import GHC.Generics
72
- import Lens.Micro (Lens' , lens )
73
85
import Lens.Micro.Extras (view )
74
86
import Prelude hiding (maybe , sequence )
75
87
import System.FilePath (takeDirectory )
76
88
77
89
import qualified Control.Exception
78
90
import qualified Control.Monad.Trans.State.Strict as State
79
91
import qualified Data.Text.IO
80
- import qualified Dhall.Context
81
92
import qualified Dhall.Core as Core
82
93
import qualified Dhall.Import
83
94
import qualified Dhall.Parser
84
95
import qualified Dhall.Pretty.Internal
96
+ import qualified Dhall.Settings
85
97
import qualified Dhall.Substitution
86
98
import qualified Dhall.TypeCheck
87
99
import qualified Lens.Micro as Lens
88
100
89
101
import Dhall.Marshal.Decode
90
102
import Dhall.Marshal.Encode
91
103
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
+ --------------------------------------------------------------------------------
196
107
197
108
-- | 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)
201
116
202
117
-- | 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)
206
126
207
127
{-| Type-check an expression against a type provided as a Dhall expreession,
208
128
using the supplied `InputSettings`
209
129
-}
210
130
checkWithSettings ::
211
- MonadThrow m =>
131
+ ( HasEvaluateSettings s , MonadThrow m ) =>
212
132
-- | The input settings
213
- InputSettings ->
133
+ s ->
214
134
-- | The expected type of the expression
215
135
Expr Src Void ->
216
136
-- | The expression to check
@@ -234,7 +154,9 @@ checkWithSettings settings type_ expression = do
234
154
This is equivalent of using the 'expected' type of a @Decoder@ as the second
235
155
argument to 'checkWithSettings'.
236
156
-}
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 ()
238
160
expectWithSettings settings Decoder {.. } expression = do
239
161
expected' <- case expected of
240
162
Success x -> return x
@@ -247,38 +169,44 @@ expectWithSettings settings Decoder{..} expression = do
247
169
Note that this also applies any substitutions specified in the
248
170
`InputSettings`
249
171
-}
250
- resolveWithSettings :: InputSettings -> Expr Src Import -> IO (Expr Src Void )
172
+ resolveWithSettings
173
+ :: (HasInputSettings s )
174
+ => s -> Expr Src Import -> IO (Expr Src Void )
251
175
resolveWithSettings settings expression =
252
176
fst <$> resolveAndStatusWithSettings settings expression
253
177
254
178
-- | A version of 'resolveWithSettings' that also returns the import 'Status'
255
179
-- together with the resolved expression.
256
180
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 )
260
183
resolveAndStatusWithSettings settings expression = do
261
- let InputSettings { .. } = settings
184
+ let inputSettings = view Dhall.Settings. inputSettings settings
262
185
263
- let EvaluateSettings { .. } = _evaluateSettings
186
+ let evaluateSettings = view Dhall.Settings. evaluateSettings inputSettings
264
187
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
269
189
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
271
193
272
194
(resolved, status') <- State. runStateT (Dhall.Import. loadWith expression) status
273
195
274
- let substituted = Dhall.Substitution. substitute resolved (view substitutions settings)
196
+ let substituted = Dhall.Substitution. substitute resolved substitutions
275
197
276
198
pure (substituted, status')
277
199
278
200
-- | 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
280
204
normalizeWithSettings settings =
281
- Core. normalizeWith (view normalizer settings)
205
+ Core. normalizeWith (view Dhall.Settings. normalizer settings)
206
+
207
+ --------------------------------------------------------------------------------
208
+ -- High-level entrypoints
209
+ --------------------------------------------------------------------------------
282
210
283
211
{-| Type-check and evaluate a Dhall program, decoding the result into Haskell
284
212
@@ -366,11 +294,11 @@ inputFileWithSettings
366
294
-- ^ The decoded value in Haskell.
367
295
inputFileWithSettings settings ty path = do
368
296
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
374
302
inputWithSettings inputSettings ty text
375
303
376
304
{-| Similar to `input`, but without interpreting the Dhall `Expr` into a Haskell
@@ -405,7 +333,9 @@ inputExprWithSettings settings text = do
405
333
406
334
_ <- typecheckWithSettings settings resolved
407
335
408
- pure (Core. normalizeWith (view normalizer settings) resolved)
336
+ let normalizer = view Dhall.Settings. normalizer settings
337
+
338
+ pure (Core. normalizeWith normalizer resolved)
409
339
410
340
{-| Interpret a Dhall Expression
411
341
@@ -422,7 +352,9 @@ interpretExprWithSettings settings parsed = do
422
352
423
353
typecheckWithSettings settings resolved
424
354
425
- pure (Core. normalizeWith (view normalizer settings) resolved)
355
+ let normalizer = view Dhall.Settings. normalizer settings
356
+
357
+ pure (Core. normalizeWith normalizer resolved)
426
358
427
359
{- | Decode a Dhall expression
428
360
@@ -438,7 +370,9 @@ fromExprWithSettings settings decoder@Decoder{..} expression = do
438
370
439
371
expectWithSettings settings decoder resolved
440
372
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
442
376
443
377
case extract normalized of
444
378
Success x -> return x
0 commit comments