-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathTypeCheck.hs
More file actions
5009 lines (4417 loc) · 330 KB
/
TypeCheck.hs
File metadata and controls
5009 lines (4417 loc) · 330 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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wall #-}
-- | This module contains the logic for type checking Dhall code
module Dhall.TypeCheck (
-- * Type-checking
typeWith
, typeOf
, typeWithA
, checkContext
, messageExpressions
-- * Types
, Typer
, X
, absurd
, TypeError(..)
, DetailedTypeError(..)
, Censored(..)
, TypeMessage(..)
, prettyTypeMessage
, ErrorMessages(..)
) where
import Control.Exception (Exception)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Writer.Strict (execWriterT, tell)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Monoid (Endo (..))
import Data.Semigroup (Max (..))
import Data.Sequence (Seq, ViewL (..))
import Data.Set (Set)
import Data.Text (Text)
import Data.Typeable (Typeable)
import Data.Void (Void, absurd)
import Dhall.Context (Context)
import Dhall.Eval
( Environment (..)
, Names (..)
, Val (..)
, (~>)
)
import Dhall.Pretty (Ann)
import Dhall.Src (Src)
import Lens.Family (over)
import Prettyprinter (Doc, Pretty (..), vsep)
import Dhall.Syntax
( Binding (..)
, Chunks (..)
, Const (..)
, Expr (..)
, FunctionBinding (..)
, PreferAnnotation (..)
, RecordField (..)
, Var (..)
)
import qualified Data.Foldable as Foldable
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Map
import qualified Data.Sequence
import qualified Data.Set
import qualified Data.Text as Text
import qualified Data.Traversable
import qualified Dhall.Context
import qualified Dhall.Core
import qualified Dhall.Diff
import qualified Dhall.Eval as Eval
import qualified Dhall.Map
import qualified Dhall.Pretty
import qualified Dhall.Pretty.Internal
import qualified Dhall.Syntax as Syntax
import qualified Dhall.Util
import qualified Lens.Family
import qualified Prettyprinter as Pretty
import qualified Prettyprinter.Render.String as Pretty
{-| A type synonym for `Void`
This is provided for backwards compatibility, since Dhall used to use its
own `X` type instead of @"Data.Void".`Void`@. You should use `Void` instead
of `X` now
-}
type X = Void
{-# DEPRECATED X "Use Data.Void.Void instead" #-}
traverseWithIndex_ :: Applicative f => (Int -> a -> f b) -> Seq a -> f ()
traverseWithIndex_ k xs = Foldable.sequenceA_ (Data.Sequence.mapWithIndex k xs)
axiom :: Const -> Either (TypeError s a) Const
axiom Type = return Kind
axiom Kind = return Sort
axiom Sort = Left (TypeError Dhall.Context.empty (Const Sort) Untyped)
rule :: Const -> Const -> Const
rule Type Type = Type
rule Kind Type = Type
rule Sort Type = Type
rule Type Kind = Kind
rule Kind Kind = Kind
rule Sort Kind = Sort
rule Type Sort = Sort
rule Kind Sort = Sort
rule Sort Sort = Sort
{-| Type-check an expression and return the expression's type if type-checking
succeeds or an error if type-checking fails
`typeWith` does not necessarily normalize the type since full normalization
is not necessary for just type-checking. If you actually care about the
returned type then you may want to `Dhall.Core.normalize` it afterwards.
The supplied `Context` records the types of the names in scope. If
these are ill-typed, the return value may be ill-typed.
-}
typeWith :: Context (Expr s X) -> Expr s X -> Either (TypeError s X) (Expr s X)
typeWith ctx expr = do
checkContext ctx
typeWithA absurd ctx expr
{-| Function that converts the value inside an `Embed` constructor into a new
expression
-}
type Typer a = forall s. a -> Expr s a
{-| Generalization of `typeWith` that allows type-checking the `Embed`
constructor with custom logic
-}
typeWithA
:: (Eq a, Pretty a)
=> Typer a
-> Context (Expr s a)
-> Expr s a
-> Either (TypeError s a) (Expr s a)
typeWithA tpa context expression =
fmap (Dhall.Core.renote . Eval.quote EmptyNames) (infer tpa ctx expression)
where
ctx = contextToCtx context
contextToCtx :: Eq a => Context (Expr s a) -> Ctx a
contextToCtx context = loop (Dhall.Context.toList context)
where
loop [] =
Ctx Empty TypesEmpty
loop ((x, t):rest) =
Ctx (Skip vs x) (TypesBind ts x (Eval.eval vs (Dhall.Core.denote t)))
where
Ctx vs ts = loop rest
ctxToContext :: Eq a => Ctx a -> Context (Expr s a)
ctxToContext (Ctx {..}) = loop types
where
loop (TypesBind ts x t) = Dhall.Context.insert x t' (loop ts)
where
ns = typesToNames ts
t' = Dhall.Core.renote (Eval.quote ns t)
loop TypesEmpty = Dhall.Context.empty
typesToNames :: Types a -> Names
typesToNames (TypesBind ts x _) = Bind ns x
where
ns = typesToNames ts
typesToNames TypesEmpty = EmptyNames
data Types a = TypesEmpty | TypesBind !(Types a) {-# UNPACK #-} !Text (Val a)
data Ctx a = Ctx { values :: !(Environment a), types :: !(Types a) }
addType :: Text -> Val a -> Ctx a -> Ctx a
addType x t (Ctx vs ts) = Ctx (Skip vs x) (TypesBind ts x t)
addTypeValue :: Text -> Val a -> Val a -> Ctx a -> Ctx a
addTypeValue x t v (Ctx vs ts) = Ctx (Extend vs x v) (TypesBind ts x t)
fresh :: Ctx a -> Text -> Val a
fresh Ctx{..} x = VVar x (Eval.countNames x (Eval.envNames values))
{-| `typeWithA` is implemented internally in terms of @infer@ in order to speed
up equivalence checking.
Specifically, we extend the `Context` to become a @Ctx@, which can store
the entire contents of a `let` expression (i.e. the type *and* the value
of the bound variable). By storing this extra information in the @Ctx@ we
no longer need to substitute `let` expressions at all (which is very
expensive!).
However, this means that we need to use `Dhall.Eval.conv` to perform
equivalence checking instead of `Dhall.Core.judgmentallyEqual` since
only `Dhall.Core.judgmentallyEqual` is unable to use the information stored
in the extended context for accurate equivalence checking.
-}
infer
:: forall a s
. (Eq a, Pretty a)
=> Typer a
-> Ctx a
-> Expr s a
-> Either (TypeError s a) (Val a)
infer typer = loop
where
{- The convention for primes (i.e. `'`s) is:
* No primes (`x` ): An `Expr` that has not been `eval`ed yet
* One prime (`x'` ): A `Val`
* Two primes (`x''`): An `Expr` generated from `quote`ing a `Val`
-}
loop :: Ctx a -> Expr s a -> Either (TypeError s a) (Val a)
loop ctx@Ctx{..} expression = case expression of
Const c ->
fmap VConst (axiom c)
Var (V x0 n0) -> do
let go TypesEmpty _ =
die (UnboundVariable x0)
go (TypesBind ts x t) n
| x == x0 = if n == 0 then return t else go ts (n - 1)
| otherwise = go ts n
go types n0
Lam _ (FunctionBinding { functionBindingVariable = x, functionBindingAnnotation = _A}) b -> do
tA' <- loop ctx _A
case tA' of
VConst _ -> return ()
_ -> die (InvalidInputType _A)
let _A' = eval values _A
let ctx' = addType x _A' ctx
_B' <- loop ctx' b
let _B'' = quote (Bind (Eval.envNames values) x) _B'
tB' <- loop ctx' (Dhall.Core.renote _B'')
case tB' of
VConst _ -> return ()
_ -> die (InvalidOutputType _B'')
return (VHPi x _A' (\u -> Eval.eval (Extend values x u) _B''))
Pi _ x _A _B -> do
tA' <- loop ctx _A
kA <- case tA' of
VConst kA -> return kA
_ -> die (InvalidInputType _A)
let _A' = eval values _A
let ctx' = addType x _A' ctx
tB' <- loop ctx' _B
kB <- case tB' of
VConst kB -> return kB
_ -> die (InvalidOutputType _B)
return (VConst (rule kA kB))
App f a -> do
tf' <- loop ctx f
case Eval.toVHPi tf' of
Just (_x, _A₀', _B') -> do
_A₁' <- loop ctx a
if Eval.conv values _A₀' _A₁'
then do
let a' = eval values a
return (_B' a')
else do
let _A₀'' = quote names _A₀'
let _A₁'' = quote names _A₁'
die (TypeMismatch f _A₀'' a _A₁'')
Nothing ->
die (NotAFunction f (quote names tf'))
Let (Binding { value = a₀, variable = x, ..}) body -> do
let a₀' = eval values a₀
ctxNew <- case annotation of
Nothing -> do
_A' <- loop ctx a₀
return (addTypeValue x _A' a₀' ctx)
Just (_, _A₀) -> do
_ <- loop ctx _A₀
let _A₀' = eval values _A₀
_A₁' <- loop ctx a₀
if Eval.conv values _A₀' _A₁'
then
return ()
else do
let _A₀'' = quote names _A₀'
let _A₁'' = quote names _A₁'
Left (TypeError context a₀ (AnnotMismatch a₀ _A₀'' _A₁''))
return (addTypeValue x _A₀' a₀' ctx)
loop ctxNew body
Annot t _T₀ -> do
case Dhall.Core.denote _T₀ of
Const _ -> return ()
_ -> do
_ <- loop ctx _T₀
return ()
let _T₀' = eval values _T₀
_T₁' <- loop ctx t
if Eval.conv values _T₀' _T₁'
then
return _T₁'
else do
let _T₀'' = quote names _T₀'
let _T₁'' = quote names _T₁'
die (AnnotMismatch t _T₀'' _T₁'')
Bool ->
return (VConst Type)
BoolLit _ ->
return VBool
BoolAnd l r -> do
tl' <- loop ctx l
case tl' of
VBool -> return ()
_ -> die (CantAnd l (quote names tl'))
tr' <- loop ctx r
case tr' of
VBool -> return ()
_ -> die (CantAnd r (quote names tr'))
return VBool
BoolOr l r -> do
tl' <- loop ctx l
case tl' of
VBool -> return ()
_ -> die (CantOr l (quote names tl'))
tr' <- loop ctx r
case tr' of
VBool -> return ()
_ -> die (CantOr r (quote names tr'))
return VBool
BoolEQ l r -> do
tl' <- loop ctx l
case tl' of
VBool -> return ()
_ -> die (CantEQ l (quote names tl'))
tr' <- loop ctx r
case tr' of
VBool -> return ()
_ -> die (CantEQ r (quote names tr'))
return VBool
BoolNE l r -> do
tl' <- loop ctx l
case tl' of
VBool -> return ()
_ -> die (CantNE l (quote names tl'))
tr' <- loop ctx r
case tr' of
VBool -> return ()
_ -> die (CantNE r (quote names tr'))
return VBool
BoolIf t l r -> do
tt' <- loop ctx t
case tt' of
VBool -> return ()
_ -> die (InvalidPredicate t (quote names tt'))
_L' <- loop ctx l
_R' <- loop ctx r
_ <- loop ctx (quote names _L')
let _L'' = quote names _L'
_ <- loop ctx (quote names _R')
let _R'' = quote names _R'
if Eval.conv values _L' _R'
then return ()
else die (IfBranchMismatch l r _L'' _R'')
return _L'
Natural ->
return (VConst Type)
NaturalLit _ ->
return VNatural
NaturalFold ->
return
( VNatural
~> VHPi "natural" (VConst Type) (\natural ->
VHPi "succ" (natural ~> natural) (\_succ ->
VHPi "zero" natural (\_zero ->
natural
)
)
)
)
NaturalBuild ->
return
( VHPi "natural" (VConst Type) (\natural ->
VHPi "succ" (natural ~> natural) (\_succ ->
VHPi "zero" natural (\_zero ->
natural
)
)
)
~> VNatural
)
NaturalIsZero ->
return (VNatural ~> VBool)
NaturalEven ->
return (VNatural ~> VBool)
NaturalOdd ->
return (VNatural ~> VBool)
NaturalToInteger ->
return (VNatural ~> VInteger)
NaturalShow ->
return (VNatural ~> VText)
NaturalSubtract ->
return (VNatural ~> VNatural ~> VNatural)
NaturalPlus l r -> do
tl' <- loop ctx l
case tl' of
VNatural -> return ()
_ -> die (CantAdd l (quote names tl'))
tr' <- loop ctx r
case tr' of
VNatural -> return ()
_ -> die (CantAdd r (quote names tr'))
return VNatural
NaturalTimes l r -> do
tl' <- loop ctx l
case tl' of
VNatural -> return ()
_ -> die (CantMultiply l (quote names tl'))
tr' <- loop ctx r
case tr' of
VNatural -> return ()
_ -> die (CantMultiply r (quote names tr'))
return VNatural
Integer ->
return (VConst Type)
IntegerLit _ ->
return VInteger
IntegerClamp ->
return (VInteger ~> VNatural)
IntegerNegate ->
return (VInteger ~> VInteger)
IntegerShow ->
return (VInteger ~> VText)
IntegerToDouble ->
return (VInteger ~> VDouble)
Double ->
return (VConst Type)
DoubleLit _ ->
return VDouble
DoubleShow ->
return (VDouble ~> VText)
Text ->
return (VConst Type)
TextLit (Chunks xys _) -> do
let process (_, y) = do
_Y' <- loop ctx y
case _Y' of
VText -> return ()
_ -> die (CantInterpolate y (quote names _Y'))
mapM_ process xys
return VText
TextAppend l r -> do
tl' <- loop ctx l
case tl' of
VText -> return ()
_ -> die (CantTextAppend l (quote names tl'))
tr' <- loop ctx r
case tr' of
VText -> return ()
_ -> die (CantTextAppend r (quote names tr'))
return VText
TextReplace ->
return
( VHPi "needle" VText (\_needle ->
VHPi "replacement" VText (\_replacement ->
VHPi "haystack" VText (\_haystack ->
VText
)
)
)
)
TextShow ->
return (VText ~> VText)
Date ->
return (VConst Type)
DateLiteral _ ->
return VDate
Time ->
return (VConst Type)
TimeLiteral _ _ ->
return VTime
TimeZone ->
return (VConst Type)
TimeZoneLiteral _ ->
return VTimeZone
List ->
return (VConst Type ~> VConst Type)
ListLit Nothing ts₀ ->
case Data.Sequence.viewl ts₀ of
t₀ :< ts₁ -> do
_T₀' <- loop ctx t₀
let _T₀'' = quote names _T₀'
tT₀' <- loop ctx _T₀''
case tT₀' of
VConst Type -> return ()
_ -> die (InvalidListType (App List _T₀''))
let process i t₁ = do
_T₁' <- loop ctx t₁
if Eval.conv values _T₀' _T₁'
then
return ()
else do
let _T₀'' = quote names _T₀'
let _T₁'' = quote names _T₁'
-- Carefully note that we don't use `die`
-- here so that the source span is narrowed
-- to just the offending element
let err = MismatchedListElements (i+1) _T₀'' t₁ _T₁''
Left (TypeError context t₁ err)
traverseWithIndex_ process ts₁
return (VList _T₀')
_ ->
die MissingListType
ListLit (Just _T₀) ts ->
if Data.Sequence.null ts
then do
_ <- loop ctx _T₀
let _T₀' = eval values _T₀
let _T₀'' = quote names _T₀'
case _T₀' of
VList _ -> return _T₀'
_ -> die (InvalidListType _T₀'')
-- See https://github.com/dhall-lang/dhall-haskell/issues/1359.
else die ListLitInvariant
ListAppend x y -> do
tx' <- loop ctx x
_A₀' <- case tx' of
VList _A₀' -> return _A₀'
_ -> die (CantListAppend x (quote names tx'))
ty' <- loop ctx y
_A₁' <- case ty' of
VList _A₁' -> return _A₁'
_ -> die (CantListAppend y (quote names ty'))
if Eval.conv values _A₀' _A₁'
then return ()
else do
let _A₀'' = quote names _A₀'
let _A₁'' = quote names _A₁'
die (ListAppendMismatch _A₀'' _A₁'')
return (VList _A₀')
ListBuild ->
return
( VHPi "a" (VConst Type) (\a ->
VHPi "list" (VConst Type) (\list ->
VHPi "cons" (a ~> list ~> list) (\_cons ->
(VHPi "nil" list (\_nil -> list))
)
)
~> VList a
)
)
ListFold ->
return
( VHPi "a" (VConst Type) (\a ->
VList a
~> VHPi "list" (VConst Type) (\list ->
VHPi "cons" (a ~> list ~> list) (\_cons ->
(VHPi "nil" list (\_nil -> list))
)
)
)
)
ListLength ->
return (VHPi "a" (VConst Type) (\a -> VList a ~> VNatural))
ListHead ->
return (VHPi "a" (VConst Type) (\a -> VList a ~> VOptional a))
ListLast ->
return (VHPi "a" (VConst Type) (\a -> VList a ~> VOptional a))
ListIndexed ->
return
( VHPi "a" (VConst Type) (\a ->
VList a
~> VList
(VRecord
(Dhall.Map.unorderedFromList
[ ("index", VNatural)
, ("value", a )
]
)
)
)
)
ListReverse ->
return (VHPi "a" (VConst Type) (\a -> VList a ~> VList a))
Optional ->
return (VConst Type ~> VConst Type)
None ->
return (VHPi "A" (VConst Type) (\_A -> VOptional _A))
Some a -> do
_A' <- loop ctx a
tA' <- loop ctx (quote names _A')
case tA' of
VConst Type -> return ()
_ -> do
let _A'' = quote names _A'
let tA'' = quote names tA'
die (InvalidSome a _A'' tA'')
return (VOptional _A')
Record xTs -> do
let process x (RecordField {recordFieldValue = _T}) = do
tT' <- lift (loop ctx _T)
case tT' of
VConst c -> tell (Max c)
_ -> lift (die (InvalidFieldType x _T))
Max c <- execWriterT (Dhall.Map.unorderedTraverseWithKey_ process xTs)
return (VConst c)
RecordLit xts -> do
let process t = do
_T' <- loop ctx $ recordFieldValue t
let _T'' = quote names _T'
_ <- loop ctx _T''
return _T'
xTs <- traverse process (Dhall.Map.sort xts)
return (VRecord xTs)
Union xTs -> do
let process _ Nothing =
return mempty
process x₁ (Just _T₁) = do
tT₁' <- loop ctx _T₁
case tT₁' of
VConst c -> return (Max c)
_ -> die (InvalidAlternativeType x₁ _T₁)
Max c <- fmap Foldable.fold (Dhall.Map.unorderedTraverseWithKey process xTs)
return (VConst c)
Combine _ mk l r -> do
_L' <- loop ctx l
let l'' = quote names (eval values l)
_R' <- loop ctx r
let r'' = quote names (eval values r)
xLs' <- case _L' of
VRecord xLs' ->
return xLs'
_ -> do
let _L'' = quote names _L'
case mk of
Nothing -> die (MustCombineARecord '∧' l'' _L'')
Just t -> die (InvalidDuplicateField t l _L'')
xRs' <- case _R' of
VRecord xRs' ->
return xRs'
_ -> do
let _R'' = quote names _R'
case mk of
Nothing -> die (MustCombineARecord '∧' r'' _R'')
Just t -> die (InvalidDuplicateField t r _R'')
let combineTypes xs xLs₀' xRs₀' = do
let combine x (VRecord xLs₁') (VRecord xRs₁') =
combineTypes (x : xs) xLs₁' xRs₁'
combine x _ _ =
case mk of
Nothing -> die (FieldCollision (NonEmpty.reverse (x :| xs)))
Just t -> die (DuplicateFieldCannotBeMerged (t :| reverse (x : xs)))
let xEs =
Dhall.Map.outerJoin Right Right combine xLs₀' xRs₀'
xTs <- Dhall.Map.unorderedTraverseWithKey (\_x _E -> _E) xEs
return (VRecord xTs)
combineTypes [] xLs' xRs'
CombineTypes _ l r -> do
_L' <- loop ctx l
let l' = eval values l
let l'' = quote names l'
cL <- case _L' of
VConst cL -> return cL
_ -> die (CombineTypesRequiresRecordType l l'')
_R' <- loop ctx r
let r' = eval values r
let r'' = quote names r'
cR <- case _R' of
VConst cR -> return cR
_ -> die (CombineTypesRequiresRecordType r r'')
let c = max cL cR
xLs' <- case l' of
VRecord xLs' -> return xLs'
_ -> die (CombineTypesRequiresRecordType l l'')
xRs' <- case r' of
VRecord xRs' -> return xRs'
_ -> die (CombineTypesRequiresRecordType r r'')
let combineTypes xs xLs₀' xRs₀' = do
let combine x (VRecord xLs₁') (VRecord xRs₁') =
combineTypes (x : xs) xLs₁' xRs₁'
combine x _ _ =
die (FieldTypeCollision (NonEmpty.reverse (x :| xs)))
let mL = Dhall.Map.toMap xLs₀'
let mR = Dhall.Map.toMap xRs₀'
Foldable.sequence_ (Data.Map.intersectionWithKey combine mL mR)
combineTypes [] xLs' xRs'
return (VConst c)
Prefer _ a l r -> do
_L' <- loop ctx l
_R' <- loop ctx r
xLs' <- case _L' of
VRecord xLs' -> return xLs'
_ -> do
let _L'' = quote names _L'
let l'' = quote names (eval values l)
case a of
PreferFromWith withExpression ->
die (MustUpdateARecord withExpression l'' _L'')
_ ->
die (MustCombineARecord '⫽' l'' _L'')
xRs' <- case _R' of
VRecord xRs' -> return xRs'
_ -> do
let _R'' = quote names _R'
let r'' = quote names (eval values r)
die (MustCombineARecord '⫽' r'' _R'')
return (VRecord (Dhall.Map.union xRs' xLs'))
RecordCompletion l r -> do
_L' <- loop ctx l
case _L' of
VRecord xLs'
| not (Dhall.Map.member "default" xLs')
-> die (InvalidRecordCompletion "default" l)
| not (Dhall.Map.member "Type" xLs')
-> die (InvalidRecordCompletion "Type" l)
| otherwise
-> loop ctx (Annot (Prefer mempty PreferFromCompletion (Field l def) r) (Field l typ))
_ -> die (CompletionSchemaMustBeARecord l (quote names _L'))
where
def = Syntax.makeFieldSelection "default"
typ = Syntax.makeFieldSelection "Type"
Merge t u mT₁ -> do
_T' <- loop ctx t
yTs' <- case _T' of
VRecord yTs' ->
return yTs'
_ -> do
let _T'' = quote names _T'
die (MustMergeARecord t _T'')
_U' <- loop ctx u
yUs' <- case _U' of
VUnion yUs' ->
return yUs'
VOptional _O' ->
-- This is a bit of hack, but it allows us to reuse the
-- rather complex type-matching logic for Optionals.
return (Dhall.Map.unorderedFromList [("None", Nothing), ("Some", Just _O')])
_ -> do
let _U'' = quote names _U'
die (MustMergeUnionOrOptional u _U'')
let ysT = Dhall.Map.keysSet yTs'
let ysU = Dhall.Map.keysSet yUs'
let diffT = Data.Set.difference ysT ysU
let diffU = Data.Set.difference ysU ysT
if Data.Set.null diffT
then return ()
else die (UnusedHandler diffT)
if Data.Set.null diffU
then return ()
else let (exemplar,rest) = Data.Set.deleteFindMin diffU
in die (MissingHandler exemplar rest)
let match _y _T₀' Nothing =
return _T₀'
match y handler' (Just _A₁') =
case Eval.toVHPi handler' of
Just (x, _A₀', _T₀') ->
if Eval.conv values _A₀' _A₁'
then do
let _T₁' = _T₀' (fresh ctx x)
let _T₁'' = quote names _T₁'
-- x appearing in _T₁'' would indicate a disallowed
-- handler type (see
-- https://github.com/dhall-lang/dhall-lang/issues/749).
--
-- If x appears in _T₁'', quote will have given it index
-- -1. Any well-typed variable has a non-negative index,
-- so we can simply look for negative indices to detect x.
let containsBadVar (Var (V _ n)) =
n < 0
containsBadVar e =
Lens.Family.anyOf
Dhall.Core.subExpressions
containsBadVar
e