-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathDhall.hs
More file actions
567 lines (487 loc) · 17.3 KB
/
Dhall.hs
File metadata and controls
567 lines (487 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-| Please read the "Dhall.Tutorial" module, which contains a tutorial explaining
how to use the language, the compiler, and this library
-}
module Dhall
(
-- * Input
input
, inputWithSettings
, inputFile
, inputFileWithSettings
, inputExpr
, inputExprWithSettings
, interpretExpr
, interpretExprWithSettings
, fromExpr
, fromExprWithSettings
, rootDirectory
, sourceName
, startingContext
, substitutions
, normalizer
, newManager
, defaultInputSettings
, InputSettings
, defaultEvaluateSettings
, EvaluateSettings
, HasEvaluateSettings(..)
, detailed
-- * Decoders
, module Dhall.Marshal.Decode
-- * Encoders
, module Dhall.Marshal.Encode
-- * Individual phases
, parseWithSettings
, resolveWithSettings
, typecheckWithSettings
, expectWithSettings
, normalizeWithSettings
-- * Miscellaneous
, rawInput
) where
import Control.Applicative (Alternative, empty)
import Data.Either.Validation (Validation (..))
import Data.Void (Void)
import Dhall.Import (Imported (..))
import Dhall.Parser (Src (..))
import Dhall.Syntax (Expr (..), Import)
import Dhall.TypeCheck (DetailedTypeError (..), TypeError)
import GHC.Generics
import Lens.Family (LensLike', view)
import Prelude hiding (maybe, sequence)
import System.FilePath (takeDirectory)
import qualified Control.Exception
import qualified Control.Monad.Trans.State.Strict as State
import qualified Data.Text.IO
import qualified Dhall.Context
import qualified Dhall.Core as Core
import qualified Dhall.Import
import qualified Dhall.Parser
import qualified Dhall.Pretty.Internal
import qualified Dhall.Substitution
import qualified Dhall.TypeCheck
import qualified Lens.Family
import Dhall.Marshal.Decode
import Dhall.Marshal.Encode
-- | @since 1.16
data InputSettings = InputSettings
{ _rootDirectory :: FilePath
, _sourceName :: FilePath
, _evaluateSettings :: EvaluateSettings
}
-- | Default input settings: resolves imports relative to @.@ (the
-- current working directory), report errors as coming from @(input)@,
-- and default evaluation settings from 'defaultEvaluateSettings'.
--
-- @since 1.16
defaultInputSettings :: InputSettings
defaultInputSettings = InputSettings
{ _rootDirectory = "."
, _sourceName = "(input)"
, _evaluateSettings = defaultEvaluateSettings
}
-- | Access the directory to resolve imports relative to.
--
-- @since 1.16
rootDirectory
:: (Functor f)
=> LensLike' f InputSettings FilePath
rootDirectory k s =
fmap (\x -> s { _rootDirectory = x }) (k (_rootDirectory s))
-- | Access the name of the source to report locations from; this is
-- only used in error messages, so it's okay if this is a best guess
-- or something symbolic.
--
-- @since 1.16
sourceName
:: (Functor f)
=> LensLike' f InputSettings FilePath
sourceName k s =
fmap (\x -> s { _sourceName = x}) (k (_sourceName s))
-- | @since 1.16
data EvaluateSettings = EvaluateSettings
{ _substitutions :: Dhall.Substitution.Substitutions Src Void
, _startingContext :: Dhall.Context.Context (Expr Src Void)
, _normalizer :: Maybe (Core.ReifiedNormalizer Void)
, _newManager :: IO Dhall.Import.Manager
}
-- | Default evaluation settings: no extra entries in the initial
-- context, and no special normalizer behaviour.
--
-- @since 1.16
defaultEvaluateSettings :: EvaluateSettings
defaultEvaluateSettings = EvaluateSettings
{ _substitutions = Dhall.Substitution.empty
, _startingContext = Dhall.Context.empty
, _normalizer = Nothing
, _newManager = Dhall.Import.defaultNewManager
}
-- | Access the starting context used for evaluation and type-checking.
--
-- @since 1.16
startingContext
:: (Functor f, HasEvaluateSettings s)
=> LensLike' f s (Dhall.Context.Context (Expr Src Void))
startingContext = evaluateSettings . l
where
l :: (Functor f)
=> LensLike' f EvaluateSettings (Dhall.Context.Context (Expr Src Void))
l k s = fmap (\x -> s { _startingContext = x}) (k (_startingContext s))
-- | Access the custom substitutions.
--
-- @since 1.30
substitutions
:: (Functor f, HasEvaluateSettings s)
=> LensLike' f s (Dhall.Substitution.Substitutions Src Void)
substitutions = evaluateSettings . l
where
l :: (Functor f)
=> LensLike' f EvaluateSettings (Dhall.Substitution.Substitutions Src Void)
l k s = fmap (\x -> s { _substitutions = x }) (k (_substitutions s))
-- | Access the custom normalizer.
--
-- @since 1.16
normalizer
:: (Functor f, HasEvaluateSettings s)
=> LensLike' f s (Maybe (Core.ReifiedNormalizer Void))
normalizer = evaluateSettings . l
where
l :: (Functor f)
=> LensLike' f EvaluateSettings (Maybe (Core.ReifiedNormalizer Void))
l k s = fmap (\x -> s { _normalizer = x }) (k (_normalizer s))
-- | Access the HTTP manager initializer.
--
-- @since 1.36
newManager
:: (Functor f, HasEvaluateSettings s)
=> LensLike' f s (IO Dhall.Import.Manager)
newManager = evaluateSettings . l
where
l :: (Functor f)
=> LensLike' f EvaluateSettings (IO Dhall.Import.Manager)
l k s = fmap (\x -> s { _newManager = x }) (k (_newManager s))
-- | @since 1.16
class HasEvaluateSettings s where
evaluateSettings
:: (Functor f)
=> LensLike' f s EvaluateSettings
instance HasEvaluateSettings InputSettings where
evaluateSettings k s =
fmap (\x -> s { _evaluateSettings = x }) (k (_evaluateSettings s))
instance HasEvaluateSettings EvaluateSettings where
evaluateSettings = id
-- | Parse an expression, using the supplied `InputSettings`
parseWithSettings :: InputSettings -> Text -> IO (Expr Src Import)
parseWithSettings settings text = do
Core.throws (Dhall.Parser.exprFromText (view sourceName settings) text)
-- | Type-check an expression, using the supplied `InputSettings`
typecheckWithSettings :: InputSettings -> Expr Src Void -> IO ()
typecheckWithSettings settings expression = do
_ <- Core.throws (Dhall.TypeCheck.typeWith (view startingContext settings) expression)
return ()
{-| Type-check an expression against a `Decoder`'s expected type, using the
supplied `InputSettings`
-}
expectWithSettings :: InputSettings -> Decoder a -> Expr Src Void -> IO ()
expectWithSettings settings Decoder{..} expression = do
expected' <- case expected of
Success x -> return x
Failure e -> Control.Exception.throwIO e
let suffix = Dhall.Pretty.Internal.prettyToStrictText expected'
let annotated = case expression of
Note (Src begin end bytes) _ ->
Note (Src begin end bytes') (Annot expression expected')
where
bytes' = bytes <> " : " <> suffix
_ ->
Annot expression expected'
typecheckWithSettings settings annotated
return ()
{-| Resolve an expression, using the supplied `InputSettings`
Note that this also applies any substitutions specified in the
`InputSettings`
-}
resolveWithSettings :: InputSettings -> Expr Src Import -> IO (Expr Src Void)
resolveWithSettings settings expression = do
let InputSettings{..} = settings
let EvaluateSettings{..} = _evaluateSettings
let transform =
Lens.Family.set Dhall.Import.substitutions _substitutions
. Lens.Family.set Dhall.Import.normalizer _normalizer
. Lens.Family.set Dhall.Import.startingContext _startingContext
let status = transform (Dhall.Import.emptyStatusWithManager _newManager _rootDirectory)
resolved <- State.evalStateT (Dhall.Import.loadWith expression) status
pure (Dhall.Substitution.substitute resolved (view substitutions settings))
-- | Normalize an expression, using the supplied `InputSettings`
normalizeWithSettings :: InputSettings -> Expr Src Void -> Expr Src Void
normalizeWithSettings settings =
Core.normalizeWith (view normalizer settings)
{-| Type-check and evaluate a Dhall program, decoding the result into Haskell
The first argument determines the type of value that you decode:
>>> input integer "+2"
2
>>> input (vector double) "[1.0, 2.0]"
[1.0,2.0]
Use `auto` to automatically select which type to decode based on the
inferred return type:
>>> input auto "True" :: IO Bool
True
This uses the settings from 'defaultInputSettings'.
-}
input
:: Decoder a
-- ^ The decoder for the Dhall value
-> Text
-- ^ The Dhall program
-> IO a
-- ^ The decoded value in Haskell
input =
inputWithSettings defaultInputSettings
{-| Extend 'input' with a root directory to resolve imports relative
to, a file to mention in errors as the source, a custom typing
context, and a custom normalization process.
@since 1.16
-}
inputWithSettings
:: InputSettings
-> Decoder a
-- ^ The decoder for the Dhall value
-> Text
-- ^ The Dhall program
-> IO a
-- ^ The decoded value in Haskell
inputWithSettings settings decoder@Decoder{..} text = do
parsed <- parseWithSettings settings text
resolved <- resolveWithSettings settings parsed
expectWithSettings settings decoder resolved
let normalized = normalizeWithSettings settings resolved
case extract normalized of
Success x -> return x
Failure e -> Control.Exception.throwIO e
{-| Type-check and evaluate a Dhall program that is read from the
file-system.
This uses the settings from 'defaultEvaluateSettings'.
@since 1.16
-}
inputFile
:: Decoder a
-- ^ The decoder for the Dhall value
-> FilePath
-- ^ The path to the Dhall program.
-> IO a
-- ^ The decoded value in Haskell.
inputFile =
inputFileWithSettings defaultEvaluateSettings
{-| Extend 'inputFile' with a custom typing context and a custom
normalization process.
@since 1.16
-}
inputFileWithSettings
:: EvaluateSettings
-> Decoder a
-- ^ The decoder for the Dhall value
-> FilePath
-- ^ The path to the Dhall program.
-> IO a
-- ^ The decoded value in Haskell.
inputFileWithSettings settings ty path = do
text <- Data.Text.IO.readFile path
let inputSettings = InputSettings
{ _rootDirectory = takeDirectory path
, _sourceName = path
, _evaluateSettings = settings
}
inputWithSettings inputSettings ty text
{-| Similar to `input`, but without interpreting the Dhall `Expr` into a Haskell
type.
Uses the settings from 'defaultInputSettings'.
-}
inputExpr
:: Text
-- ^ The Dhall program
-> IO (Expr Src Void)
-- ^ The fully normalized AST
inputExpr =
inputExprWithSettings defaultInputSettings
{-| Extend 'inputExpr' with a root directory to resolve imports relative
to, a file to mention in errors as the source, a custom typing
context, and a custom normalization process.
@since 1.16
-}
inputExprWithSettings
:: InputSettings
-> Text
-- ^ The Dhall program
-> IO (Expr Src Void)
-- ^ The fully normalized AST
inputExprWithSettings settings text = do
parsed <- parseWithSettings settings text
resolved <- resolveWithSettings settings parsed
_ <- typecheckWithSettings settings resolved
pure (Core.normalizeWith (view normalizer settings) resolved)
{-| Interpret a Dhall Expression
This takes care of import resolution, type-checking, and normalization
-}
interpretExpr :: Expr Src Import -> IO (Expr Src Void)
interpretExpr = interpretExprWithSettings defaultInputSettings
-- | Like `interpretExpr`, but customizable using `InputSettings`
interpretExprWithSettings
:: InputSettings -> Expr Src Import -> IO (Expr Src Void)
interpretExprWithSettings settings parsed = do
resolved <- resolveWithSettings settings parsed
typecheckWithSettings settings resolved
pure (Core.normalizeWith (view normalizer settings) resolved)
{- | Decode a Dhall expression
This takes care of import resolution, type-checking and normalization
-}
fromExpr :: Decoder a -> Expr Src Import -> IO a
fromExpr = fromExprWithSettings defaultInputSettings
-- | Like `fromExpr`, but customizable using `InputSettings`
fromExprWithSettings :: InputSettings -> Decoder a -> Expr Src Import -> IO a
fromExprWithSettings settings decoder@Decoder{..} expression = do
resolved <- resolveWithSettings settings expression
expectWithSettings settings decoder resolved
let normalized = Core.normalizeWith (view normalizer settings) resolved
case extract normalized of
Success x -> return x
Failure e -> Control.Exception.throwIO e
-- | Use this function to extract Haskell values directly from Dhall AST.
-- The intended use case is to allow easy extraction of Dhall values for
-- making the function `Core.normalizeWith` easier to use.
--
-- For other use cases, use `input` from "Dhall" module. It will give you
-- a much better user experience.
rawInput
:: Alternative f
=> Decoder a
-- ^ The decoder for the Dhall value
-> Expr s Void
-- ^ a closed form Dhall program, which evaluates to the expected type
-> f a
-- ^ The decoded value in Haskell
rawInput (Decoder {..}) expr =
case extract (Core.normalize expr) of
Success x -> pure x
Failure _e -> empty
{-| Use this to provide more detailed error messages
>> input auto "True" :: IO Integer
> *** Exception: Error: Expression doesn't match annotation
>
> True : Integer
>
> (input):1:1
>> detailed (input auto "True") :: IO Integer
> *** Exception: Error: Expression doesn't match annotation
>
> Explanation: You can annotate an expression with its type or kind using the
> ❰:❱ symbol, like this:
>
>
> ┌───────┐
> │ x : t │ ❰x❱ is an expression and ❰t❱ is the annotated type or kind of ❰x❱
> └───────┘
>
> The type checker verifies that the expression's type or kind matches the
> provided annotation
>
> For example, all of the following are valid annotations that the type checker
> accepts:
>
>
> ┌─────────────┐
> │ 1 : Natural │ ❰1❱ is an expression that has type ❰Natural❱, so the type
> └─────────────┘ checker accepts the annotation
>
>
> ┌───────────────────────┐
> │ Natural/even 2 : Bool │ ❰Natural/even 2❱ has type ❰Bool❱, so the type
> └───────────────────────┘ checker accepts the annotation
>
>
> ┌────────────────────┐
> │ List : Type → Type │ ❰List❱ is an expression that has kind ❰Type → Type❱,
> └────────────────────┘ so the type checker accepts the annotation
>
>
> ┌──────────────────┐
> │ List Text : Type │ ❰List Text❱ is an expression that has kind ❰Type❱, so
> └──────────────────┘ the type checker accepts the annotation
>
>
> However, the following annotations are not valid and the type checker will
> reject them:
>
>
> ┌──────────┐
> │ 1 : Text │ The type checker rejects this because ❰1❱ does not have type
> └──────────┘ ❰Text❱
>
>
> ┌─────────────┐
> │ List : Type │ ❰List❱ does not have kind ❰Type❱
> └─────────────┘
>
>
> You or the interpreter annotated this expression:
>
> ↳ True
>
> ... with this type or kind:
>
> ↳ Integer
>
> ... but the inferred type or kind of the expression is actually:
>
> ↳ Bool
>
> Some common reasons why you might get this error:
>
> ● The Haskell Dhall interpreter implicitly inserts a top-level annotation
> matching the expected type
>
> For example, if you run the following Haskell code:
>
>
> ┌───────────────────────────────┐
> │ >>> input auto "1" :: IO Text │
> └───────────────────────────────┘
>
>
> ... then the interpreter will actually type check the following annotated
> expression:
>
>
> ┌──────────┐
> │ 1 : Text │
> └──────────┘
>
>
> ... and then type-checking will fail
>
> ────────────────────────────────────────────────────────────────────────────────
>
> True : Integer
>
> (input):1:1
-}
detailed :: IO a -> IO a
detailed =
Control.Exception.handle handler1 . Control.Exception.handle handler0
where
handler0 :: Imported (TypeError Src Void) -> IO a
handler0 (Imported ps e) =
Control.Exception.throwIO (Imported ps (DetailedTypeError e))
handler1 :: TypeError Src Void -> IO a
handler1 e = Control.Exception.throwIO (DetailedTypeError e)