-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathNormalize.hs
More file actions
1048 lines (982 loc) · 44.2 KB
/
Normalize.hs
File metadata and controls
1048 lines (982 loc) · 44.2 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 BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ViewPatterns #-}
module Dhall.Normalize (
alphaNormalize
, normalize
, normalizeWith
, normalizeWithM
, Normalizer
, NormalizerM
, ReifiedNormalizer (..)
, judgmentallyEqual
, subst
, Syntax.shift
, isNormalized
, isNormalizedWith
, freeIn
) where
import Control.Applicative (empty)
import Data.Foldable
import Data.Functor.Identity (Identity (..))
import Data.List.NonEmpty (NonEmpty (..))
import Data.Sequence (ViewL (..), ViewR (..))
import Data.Traversable
import Instances.TH.Lift ()
import Prelude hiding (succ)
import Dhall.Syntax
( Binding (Binding)
, Chunks (..)
, DhallDouble (..)
, Expr (..)
, FieldSelection (..)
, FunctionBinding (..)
, PreferAnnotation (..)
, RecordField (..)
, WithComponent (..)
, Var (..)
)
import qualified Data.Sequence
import qualified Data.Set
import qualified Data.Text as Text
import qualified Dhall.Eval as Eval
import qualified Dhall.Map
import qualified Dhall.Syntax as Syntax
import qualified Lens.Family as Lens
{-| Returns `True` if two expressions are α-equivalent and β-equivalent and
`False` otherwise
`judgmentallyEqual` can fail with an `error` if you compare ill-typed
expressions
-}
judgmentallyEqual :: Eq a => Expr s a -> Expr t a -> Bool
judgmentallyEqual = Eval.judgmentallyEqual
{-# INLINE judgmentallyEqual #-}
{-| Substitute all occurrences of a variable with an expression
> subst x C B ~ B[x := C]
-}
subst :: Var -> Expr s a -> Expr s a -> Expr s a
subst _ _ (Const a) = Const a
subst (V x n) e (Lam cs (FunctionBinding src0 y src1 src2 _A) b) =
Lam cs (FunctionBinding src0 y src1 src2 _A') b'
where
_A' = subst (V x n ) e _A
b' = subst (V x n') (Syntax.shift 1 (V y 0) e) b
n' = if x == y then n + 1 else n
subst (V x n) e (Pi cs y _A _B) = Pi cs y _A' _B'
where
_A' = subst (V x n ) e _A
_B' = subst (V x n') (Syntax.shift 1 (V y 0) e) _B
n' = if x == y then n + 1 else n
subst v e (Var v') = if v == v' then e else Var v'
subst (V x n) e (Let (Binding src0 f src1 mt src2 r) b) =
Let (Binding src0 f src1 mt' src2 r') b'
where
b' = subst (V x n') (Syntax.shift 1 (V f 0) e) b
where
n' = if x == f then n + 1 else n
mt' = fmap (fmap (subst (V x n) e)) mt
r' = subst (V x n) e r
subst x e expression = Lens.over Syntax.subExpressions (subst x e) expression
{-| This function is used to determine whether folds like @Natural/fold@ or
@List/fold@ should be lazy or strict in their accumulator based on the type
of the accumulator
If this function returns `True`, then they will be strict in their
accumulator since we can guarantee an upper bound on the amount of work to
normalize the accumulator on each step of the loop. If this function
returns `False` then they will be lazy in their accumulator and only
normalize the final result at the end of the fold
-}
boundedType :: Expr s a -> Bool
boundedType Bool = True
boundedType Natural = True
boundedType Integer = True
boundedType Double = True
boundedType Text = True
boundedType (App List _) = False
boundedType (App Optional t) = boundedType t
boundedType (Record kvs) = all (boundedType . recordFieldValue) kvs
boundedType (Union kvs) = all (all boundedType) kvs
boundedType _ = False
{-| α-normalize an expression by renaming all bound variables to @\"_\"@ and
using De Bruijn indices to distinguish them
>>> mfb = Syntax.makeFunctionBinding
>>> alphaNormalize (Lam mempty (mfb "a" (Const Type)) (Lam mempty (mfb "b" (Const Type)) (Lam mempty (mfb "x" "a") (Lam mempty (mfb "y" "b") "x"))))
Lam Nothing (FunctionBinding {functionBindingSrc0 = Nothing, functionBindingVariable = "_", functionBindingSrc1 = Nothing, functionBindingSrc2 = Nothing, functionBindingAnnotation = Const Type}) (Lam Nothing (FunctionBinding {functionBindingSrc0 = Nothing, functionBindingVariable = "_", functionBindingSrc1 = Nothing, functionBindingSrc2 = Nothing, functionBindingAnnotation = Const Type}) (Lam Nothing (FunctionBinding {functionBindingSrc0 = Nothing, functionBindingVariable = "_", functionBindingSrc1 = Nothing, functionBindingSrc2 = Nothing, functionBindingAnnotation = Var (V "_" 1)}) (Lam Nothing (FunctionBinding {functionBindingSrc0 = Nothing, functionBindingVariable = "_", functionBindingSrc1 = Nothing, functionBindingSrc2 = Nothing, functionBindingAnnotation = Var (V "_" 1)}) (Var (V "_" 1)))))
α-normalization does not affect free variables:
>>> alphaNormalize "x"
Var (V "x" 0)
-}
alphaNormalize :: Expr s a -> Expr s a
alphaNormalize = Eval.alphaNormalize
{-# INLINE alphaNormalize #-}
{-| Reduce an expression to its normal form, performing beta reduction
`normalize` does not type-check the expression. You may want to type-check
expressions before normalizing them since normalization can convert an
ill-typed expression into a well-typed expression.
`normalize` can also fail with `error` if you normalize an ill-typed
expression
-}
normalize :: Eq a => Expr s a -> Expr t a
normalize = Eval.normalize
{-# INLINE normalize #-}
{-| Reduce an expression to its normal form, performing beta reduction and applying
any custom definitions.
`normalizeWith` is designed to be used with function `Dhall.TypeCheck.typeWith`. The `Dhall.TypeCheck.typeWith`
function allows typing of Dhall functions in a custom typing context whereas
`normalizeWith` allows evaluating Dhall expressions in a custom context.
To be more precise `normalizeWith` applies the given normalizer when it finds an
application term that it cannot reduce by other means.
Note that the context used in normalization will determine the properties of normalization.
That is, if the functions in custom context are not total then the Dhall language, evaluated
with those functions is not total either.
`normalizeWith` can fail with an `error` if you normalize an ill-typed
expression
-}
normalizeWith :: Eq a => Maybe (ReifiedNormalizer a) -> Expr s a -> Expr t a
normalizeWith (Just ctx) t = runIdentity (normalizeWithM (getReifiedNormalizer ctx) t)
normalizeWith _ t = Eval.normalize t
{-# INLINABLE normalizeWith #-}
{-| This function generalizes `normalizeWith` by allowing the custom normalizer
to use an arbitrary `Monad`
`normalizeWithM` can fail with an `error` if you normalize an ill-typed
expression
-}
normalizeWithM
:: (Monad m, Eq a) => NormalizerM m a -> Expr s a -> m (Expr t a)
normalizeWithM ctx e0 = loop (Syntax.denote e0)
where
loop = \case
Const k -> pure (Const k)
Var v -> pure (Var v)
Lam cs (FunctionBinding { functionBindingVariable = x, functionBindingAnnotation = _A }) b ->
Lam cs <$> (Syntax.makeFunctionBinding x <$> _A') <*> b'
where
_A' = loop _A
b' = loop b
Pi cs x _A _B -> Pi cs x <$> _A' <*> _B'
where
_A' = loop _A
_B' = loop _B
App f a -> do
res <- ctx (App f a)
case res of
Just e1 -> loop e1
Nothing -> do
f' <- loop f
a' <- loop a
case f' of
Lam _ (FunctionBinding _ x _ _ _A) b₀ -> do
let a₂ = Syntax.shift 1 (V x 0) a'
let b₁ = subst (V x 0) a₂ b₀
let b₂ = Syntax.shift (-1) (V x 0) b₁
loop b₂
_ ->
case App f' a' of
App (App (App (App NaturalFold (NaturalLit n0)) t) succ') zero -> do
t' <- loop t
if boundedType t' then strict else lazy
where
-- Use an `Integer` for the loop, due to the following
-- issue:
--
-- https://github.com/ghcjs/ghcjs/issues/782
strict = strictLoop (fromIntegral n0 :: Integer)
lazy = loop ( lazyLoop (fromIntegral n0 :: Integer))
strictLoop 0 = loop zero
strictLoop !n = App succ' <$> strictLoop (n - 1) >>= loop
lazyLoop 0 = zero
lazyLoop !n = App succ' (lazyLoop (n - 1))
App NaturalBuild g -> loop (App (App (App g Natural) succ) zero)
where
succ = Lam mempty (Syntax.makeFunctionBinding "n" Natural) (NaturalPlus "n" (NaturalLit 1))
zero = NaturalLit 0
App NaturalIsZero (NaturalLit n) -> pure (BoolLit (n == 0))
App NaturalEven (NaturalLit n) -> pure (BoolLit (even n))
App NaturalOdd (NaturalLit n) -> pure (BoolLit (odd n))
App NaturalToInteger (NaturalLit n) -> pure (IntegerLit (toInteger n))
App NaturalShow (NaturalLit n) ->
pure (TextLit (Chunks [] (Text.pack (show n))))
App (App NaturalSubtract (NaturalLit x)) (NaturalLit y)
-- Use an `Integer` for the subtraction, due to the
-- following issue:
--
-- https://github.com/ghcjs/ghcjs/issues/782
| y >= x ->
pure (NaturalLit (fromIntegral (subtract (fromIntegral x :: Integer) (fromIntegral y :: Integer))))
| otherwise ->
pure (NaturalLit 0)
App (App NaturalSubtract (NaturalLit 0)) y -> pure y
App (App NaturalSubtract _) (NaturalLit 0) -> pure (NaturalLit 0)
App (App NaturalSubtract x) y | Eval.judgmentallyEqual x y -> pure (NaturalLit 0)
App IntegerClamp (IntegerLit n)
| 0 <= n -> pure (NaturalLit (fromInteger n))
| otherwise -> pure (NaturalLit 0)
App IntegerNegate (IntegerLit n) ->
pure (IntegerLit (negate n))
App IntegerShow (IntegerLit n)
| 0 <= n -> pure (TextLit (Chunks [] ("+" <> Text.pack (show n))))
| otherwise -> pure (TextLit (Chunks [] (Text.pack (show n))))
-- `(read . show)` is used instead of `fromInteger` because `read` uses
-- the correct rounding rule.
-- See https://gitlab.haskell.org/ghc/ghc/issues/17231.
App IntegerToDouble (IntegerLit n) -> pure (DoubleLit ((DhallDouble . read . show) n))
App DoubleShow (DoubleLit (DhallDouble n)) ->
pure (TextLit (Chunks [] (Text.pack (show n))))
App (App ListBuild _A₀) g -> loop (App (App (App g list) cons) nil)
where
_A₁ = Syntax.shift 1 "a" _A₀
list = App List _A₀
cons =
Lam mempty (Syntax.makeFunctionBinding "a" _A₀)
(Lam mempty
(Syntax.makeFunctionBinding "as" (App List _A₁))
(ListAppend (ListLit Nothing (pure "a")) "as")
)
nil = ListLit (Just (App List _A₀)) empty
App (App (App ListDrop n) a_) as
| ListLit _ as' <- as
, Data.Sequence.null as' ->
return as
| NaturalLit 0 <- n ->
return as
| ListLit _ as' <- as
, NaturalLit m <- n -> do
let as'' = Data.Sequence.drop (fromIntegral m) as'
if Data.Sequence.null as''
then return (ListLit (Just a_) as')
else return (ListLit Nothing as')
| otherwise ->
return (App (App (App ListDrop n) a_) as)
App (App (App (App (App ListFold _) (ListLit _ xs)) t) cons) nil -> do
t' <- loop t
if boundedType t' then strict else lazy
where
strict = foldr strictCons strictNil xs
lazy = loop (foldr lazyCons lazyNil xs)
strictNil = loop nil
lazyNil = nil
strictCons y ys =
App (App cons y) <$> ys >>= loop
lazyCons y ys = App (App cons y) ys
App (App ListLength _) (ListLit _ ys) ->
pure (NaturalLit (fromIntegral (Data.Sequence.length ys)))
App (App ListHead t) (ListLit _ ys) -> loop o
where
o = case Data.Sequence.viewl ys of
y :< _ -> Some y
_ -> App None t
App (App ListLast t) (ListLit _ ys) -> loop o
where
o = case Data.Sequence.viewr ys of
_ :> y -> Some y
_ -> App None t
App (App ListIndexed _A₀) (ListLit _ as₀) -> loop (ListLit t as₁)
where
as₁ = Data.Sequence.mapWithIndex adapt as₀
_A₂ = Record (Dhall.Map.fromList kts)
where
kts = [ ("index", Syntax.makeRecordField Natural)
, ("value", Syntax.makeRecordField _A₀)
]
t | null as₀ = Just (App List _A₂)
| otherwise = Nothing
adapt n a_ =
RecordLit (Dhall.Map.fromList kvs)
where
kvs = [ ("index", Syntax.makeRecordField $ NaturalLit (fromIntegral n))
, ("value", Syntax.makeRecordField a_)
]
App (App ListReverse _) (ListLit t xs) ->
loop (ListLit t (Data.Sequence.reverse xs))
App (App (App ListTake n) a_) as
| ListLit _ as' <- as
, Data.Sequence.null as' ->
return as
| NaturalLit 0 <- n ->
return as
| ListLit _ as' <- as
, NaturalLit m <- n -> do
let as'' = Data.Sequence.take (fromIntegral m) as'
return (ListLit Nothing as'')
| otherwise ->
return (App (App (App ListTake n) a_) as)
App TextShow (TextLit (Chunks [] oldText)) ->
loop (TextLit (Chunks [] newText))
where
newText = Eval.textShow oldText
App
(App (App TextReplace (TextLit (Chunks [] ""))) _)
haystack ->
return haystack
App (App
(App TextReplace (TextLit (Chunks [] needleText)))
(TextLit (Chunks [] replacementText))
)
(TextLit (Chunks xys z)) -> do
let xys' = do
(x, y) <- xys
let x' = Text.replace needleText replacementText x
return (x', y)
let z' = Text.replace needleText replacementText z
return (TextLit (Chunks xys' z'))
App (App
(App TextReplace (TextLit (Chunks [] needleText)))
replacement
)
(TextLit (Chunks [] lastText)) -> do
let (prefix, suffix) =
Text.breakOn needleText lastText
if Text.null suffix
then return (TextLit (Chunks [] lastText))
else do
let remainder =
Text.drop
(Text.length needleText)
suffix
loop (TextAppend (TextLit (Chunks [(prefix, replacement)] "")) (App (App (App TextReplace (TextLit (Chunks [] needleText))) replacement) (TextLit (Chunks [] remainder))))
App (App
(App TextReplace (TextLit (Chunks [] needleText)))
replacement
)
(TextLit
(Chunks
((firstText, firstInterpolation) : chunks)
lastText
)
) -> do
let (prefix, suffix) =
Text.breakOn needleText firstText
if Text.null suffix
then do
loop (TextAppend (TextLit (Chunks [(firstText, firstInterpolation)] "")) (App (App (App TextReplace (TextLit (Chunks [] needleText))) replacement) (TextLit (Chunks chunks lastText))))
else do
let remainder =
Text.drop
(Text.length needleText)
suffix
loop (TextAppend (TextLit (Chunks [(prefix, replacement)] "")) (App (App (App TextReplace (TextLit (Chunks [] needleText))) replacement) (TextLit (Chunks ((remainder, firstInterpolation) : chunks) lastText))))
_ -> do
res2 <- ctx (App f' a')
case res2 of
Nothing -> pure (App f' a')
Just app' -> loop app'
Let (Binding _ f _ _ _ r) b -> loop b''
where
r' = Syntax.shift 1 (V f 0) r
b' = subst (V f 0) r' b
b'' = Syntax.shift (-1) (V f 0) b'
Annot x _ -> loop x
Bool -> pure Bool
BoolLit b -> pure (BoolLit b)
BoolAnd x y -> decide <$> loop x <*> loop y
where
decide (BoolLit True ) r = r
decide (BoolLit False) _ = BoolLit False
decide l (BoolLit True ) = l
decide _ (BoolLit False) = BoolLit False
decide l r
| Eval.judgmentallyEqual l r = l
| otherwise = BoolAnd l r
BoolOr x y -> decide <$> loop x <*> loop y
where
decide (BoolLit False) r = r
decide (BoolLit True ) _ = BoolLit True
decide l (BoolLit False) = l
decide _ (BoolLit True ) = BoolLit True
decide l r
| Eval.judgmentallyEqual l r = l
| otherwise = BoolOr l r
BoolEQ x y -> decide <$> loop x <*> loop y
where
decide (BoolLit True ) r = r
decide l (BoolLit True ) = l
decide l r
| Eval.judgmentallyEqual l r = BoolLit True
| otherwise = BoolEQ l r
BoolNE x y -> decide <$> loop x <*> loop y
where
decide (BoolLit False) r = r
decide l (BoolLit False) = l
decide l r
| Eval.judgmentallyEqual l r = BoolLit False
| otherwise = BoolNE l r
BoolIf bool true false -> decide <$> loop bool <*> loop true <*> loop false
where
decide (BoolLit True ) l _ = l
decide (BoolLit False) _ r = r
decide b (BoolLit True) (BoolLit False) = b
decide b l r
| Eval.judgmentallyEqual l r = l
| otherwise = BoolIf b l r
Natural -> pure Natural
NaturalLit n -> pure (NaturalLit n)
NaturalFold -> pure NaturalFold
NaturalBuild -> pure NaturalBuild
NaturalIsZero -> pure NaturalIsZero
NaturalEven -> pure NaturalEven
NaturalOdd -> pure NaturalOdd
NaturalToInteger -> pure NaturalToInteger
NaturalShow -> pure NaturalShow
NaturalSubtract -> pure NaturalSubtract
NaturalPlus x y -> decide <$> loop x <*> loop y
where
decide (NaturalLit 0) r = r
decide l (NaturalLit 0) = l
decide (NaturalLit m) (NaturalLit n) = NaturalLit (m + n)
decide l r = NaturalPlus l r
NaturalTimes x y -> decide <$> loop x <*> loop y
where
decide (NaturalLit 1) r = r
decide l (NaturalLit 1) = l
decide (NaturalLit 0) _ = NaturalLit 0
decide _ (NaturalLit 0) = NaturalLit 0
decide (NaturalLit m) (NaturalLit n) = NaturalLit (m * n)
decide l r = NaturalTimes l r
Integer -> pure Integer
IntegerLit n -> pure (IntegerLit n)
IntegerClamp -> pure IntegerClamp
IntegerNegate -> pure IntegerNegate
IntegerShow -> pure IntegerShow
IntegerToDouble -> pure IntegerToDouble
Double -> pure Double
DoubleLit n -> pure (DoubleLit n)
DoubleShow -> pure DoubleShow
Text -> pure Text
TextLit (Chunks xys z) -> do
chunks' <- mconcat <$> chunks
case chunks' of
Chunks [("", x)] "" -> pure x
c -> pure (TextLit c)
where
chunks =
((++ [Chunks [] z]) . concat) <$> traverse process xys
process (x, y) = do
y' <- loop y
case y' of
TextLit c -> pure [Chunks [] x, c]
_ -> pure [Chunks [(x, y')] mempty]
TextAppend x y -> loop (TextLit (Chunks [("", x), ("", y)] ""))
TextReplace -> pure TextReplace
TextShow -> pure TextShow
Date -> pure Date
DateLiteral d -> pure (DateLiteral d)
Time -> pure Time
TimeLiteral t p -> pure (TimeLiteral t p)
TimeZone -> pure TimeZone
TimeZoneLiteral z -> pure (TimeZoneLiteral z)
List -> pure List
ListLit t es
| Data.Sequence.null es -> ListLit <$> t' <*> pure Data.Sequence.empty
| otherwise -> ListLit Nothing <$> es'
where
t' = traverse loop t
es' = traverse loop es
ListAppend x y -> decide <$> loop x <*> loop y
where
decide (ListLit _ m) r | Data.Sequence.null m = r
decide l (ListLit _ n) | Data.Sequence.null n = l
decide (ListLit t m) (ListLit _ n) = ListLit t (m <> n)
decide l r = ListAppend l r
ListBuild -> pure ListBuild
ListDrop -> pure ListDrop
ListFold -> pure ListFold
ListLength -> pure ListLength
ListHead -> pure ListHead
ListLast -> pure ListLast
ListIndexed -> pure ListIndexed
ListReverse -> pure ListReverse
ListTake -> pure ListTake
Optional -> pure Optional
Some a -> Some <$> a'
where
a' = loop a
None -> pure None
Record kts -> Record . Dhall.Map.sort <$> kts'
where
f (RecordField s0 expr s1 s2) = (\e -> RecordField s0 e s1 s2) <$> loop expr
kts' = traverse f kts
RecordLit kvs -> RecordLit . Dhall.Map.sort <$> kvs'
where
f (RecordField s0 expr s1 s2) = (\e -> RecordField s0 e s1 s2) <$> loop expr
kvs' = traverse f kvs
Union kts -> Union . Dhall.Map.sort <$> kts'
where
kts' = traverse (traverse loop) kts
Combine cs mk x y -> decide <$> loop x <*> loop y
where
decide (RecordLit m) r | Data.Foldable.null m =
r
decide l (RecordLit n) | Data.Foldable.null n =
l
decide (RecordLit m) (RecordLit n) =
RecordLit (Dhall.Map.unionWith f m n)
where
f (RecordField _ expr _ _) (RecordField _ expr' _ _) =
Syntax.makeRecordField $ decide expr expr'
decide l r =
Combine cs mk l r
CombineTypes cs x y -> decide <$> loop x <*> loop y
where
decide (Record m) r | Data.Foldable.null m =
r
decide l (Record n) | Data.Foldable.null n =
l
decide (Record m) (Record n) =
Record (Dhall.Map.unionWith f m n)
where
f (RecordField _ expr _ _) (RecordField _ expr' _ _) =
Syntax.makeRecordField $ decide expr expr'
decide l r =
CombineTypes cs l r
Prefer cs _ x y -> decide <$> loop x <*> loop y
where
decide (RecordLit m) r | Data.Foldable.null m =
r
decide l (RecordLit n) | Data.Foldable.null n =
l
decide (RecordLit m) (RecordLit n) =
RecordLit (Dhall.Map.union n m)
decide l r | Eval.judgmentallyEqual l r =
l
decide l r =
Prefer cs PreferFromSource l r
RecordCompletion x y ->
loop (Annot (Prefer mempty PreferFromCompletion (Field x def) y) (Field x typ))
where
def = Syntax.makeFieldSelection "default"
typ = Syntax.makeFieldSelection "Type"
Merge x y t -> do
x' <- loop x
y' <- loop y
case x' of
RecordLit kvsX ->
case y' of
Field (Union ktsY) (Syntax.fieldSelectionLabel -> kY) ->
case Dhall.Map.lookup kY ktsY of
Just Nothing ->
case recordFieldValue <$> Dhall.Map.lookup kY kvsX of
Just vX -> return vX
Nothing -> Merge x' y' <$> t'
_ ->
Merge x' y' <$> t'
App (Field (Union ktsY) (Syntax.fieldSelectionLabel -> kY)) vY ->
case Dhall.Map.lookup kY ktsY of
Just (Just _) ->
case recordFieldValue <$> Dhall.Map.lookup kY kvsX of
Just vX -> loop (App vX vY)
Nothing -> Merge x' y' <$> t'
_ ->
Merge x' y' <$> t'
Some a ->
case recordFieldValue <$> Dhall.Map.lookup "Some" kvsX of
Just vX -> loop (App vX a)
Nothing -> Merge x' y' <$> t'
App None _ ->
case recordFieldValue <$> Dhall.Map.lookup "None" kvsX of
Just vX -> return vX
Nothing -> Merge x' y' <$> t'
_ -> Merge x' y' <$> t'
_ -> Merge x' y' <$> t'
where
t' = traverse loop t
ToMap x t -> do
x' <- loop x
t' <- traverse loop t
case x' of
RecordLit kvsX -> do
let entry (key, value) =
RecordLit
(Dhall.Map.fromList
[ ("mapKey" , Syntax.makeRecordField $ TextLit (Chunks [] key))
, ("mapValue", Syntax.makeRecordField value )
]
)
let keyValues = Data.Sequence.fromList (map entry (Dhall.Map.toList $ recordFieldValue <$> kvsX))
let listType = case t' of
Just _ | null keyValues ->
t'
_ ->
Nothing
return (ListLit listType keyValues)
_ ->
return (ToMap x' t')
ShowConstructor x -> do
x' <- loop x
return $ case x' of
Field (Union ktsY) (Syntax.fieldSelectionLabel -> kY) ->
case Dhall.Map.lookup kY ktsY of
Just Nothing -> TextLit (Chunks [] kY)
_ -> ShowConstructor x'
App (Field (Union ktsY) (Syntax.fieldSelectionLabel -> kY)) _ ->
case Dhall.Map.lookup kY ktsY of
Just (Just _) -> TextLit (Chunks [] kY)
_ -> ShowConstructor x'
Some _ ->
TextLit (Chunks [] "Some")
App None _ ->
TextLit (Chunks [] "None")
_ -> ShowConstructor x'
Field r k@FieldSelection{fieldSelectionLabel = x} -> do
let singletonRecordLit v = RecordLit (Dhall.Map.singleton x v)
r' <- loop r
case r' of
RecordLit kvs ->
case Dhall.Map.lookup x kvs of
Just v -> pure $ recordFieldValue v
Nothing -> Field <$> (RecordLit <$> traverse (Syntax.recordFieldExprs loop) kvs) <*> pure k
Project r_ _ -> loop (Field r_ k)
Prefer cs _ (RecordLit kvs) r_ -> case Dhall.Map.lookup x kvs of
Just v -> pure (Field (Prefer cs PreferFromSource (singletonRecordLit v) r_) k)
Nothing -> loop (Field r_ k)
Prefer _ _ l (RecordLit kvs) -> case Dhall.Map.lookup x kvs of
Just v -> pure $ recordFieldValue v
Nothing -> loop (Field l k)
Combine cs m (RecordLit kvs) r_ -> case Dhall.Map.lookup x kvs of
Just v -> pure (Field (Combine cs m (singletonRecordLit v) r_) k)
Nothing -> loop (Field r_ k)
Combine cs m l (RecordLit kvs) -> case Dhall.Map.lookup x kvs of
Just v -> pure (Field (Combine cs m l (singletonRecordLit v)) k)
Nothing -> loop (Field l k)
_ -> pure (Field r' k)
Project x (Left fields)-> do
x' <- loop x
let fieldsSet = Data.Set.fromList fields
case x' of
RecordLit kvs ->
pure (RecordLit (Dhall.Map.restrictKeys kvs fieldsSet))
Project y _ ->
loop (Project y (Left fields))
Prefer cs _ l (RecordLit rKvs) -> do
let rKs = Dhall.Map.keysSet rKvs
let l' = Project l (Left (Data.Set.toList (Data.Set.difference fieldsSet rKs)))
let r' = RecordLit (Dhall.Map.restrictKeys rKvs fieldsSet)
loop (Prefer cs PreferFromSource l' r')
_ | null fields -> pure (RecordLit mempty)
| otherwise -> pure (Project x' (Left (Data.Set.toList (Data.Set.fromList fields))))
Project r (Right e1) -> do
e2 <- loop e1
case e2 of
Record kts ->
loop (Project r (Left (Data.Set.toList (Dhall.Map.keysSet kts))))
_ -> do
r' <- loop r
pure (Project r' (Right e2))
Assert t -> do
t' <- loop t
pure (Assert t')
Equivalent cs l r -> do
l' <- loop l
r' <- loop r
pure (Equivalent cs l' r')
With e ks v -> do
e' <- loop e
v' <- loop v
case e' of
RecordLit kvs ->
case ks of
WithLabel k :| [] ->
return (RecordLit (Dhall.Map.insert k (Syntax.makeRecordField v') kvs))
WithLabel k₀ :| k₁ : ks' -> do
let e₁ =
case Dhall.Map.lookup k₀ kvs of
Nothing -> RecordLit mempty
Just r -> Syntax.recordFieldValue r
e₂ <- loop (With e₁ (k₁ :| ks') v')
return (RecordLit (Dhall.Map.insert k₀ (Syntax.makeRecordField e₂) kvs))
WithQuestion :| _ -> do
return (With e' ks v')
Some t ->
case ks of
WithQuestion :| [] -> do
return (Some v')
WithQuestion :| k : ks' -> do
w <- loop (With t (k :| ks') v)
return (Some w)
WithLabel _ :| _ ->
return (With e' ks v')
App None _T ->
case ks of
WithQuestion :| _ ->
return (App None _T)
WithLabel _ :| _ ->
return (With e' ks v')
_ ->
return (With e' ks v')
Note _ e' -> loop e'
ImportAlt l _r -> loop l
Embed a -> pure (Embed a)
-- | Use this to wrap you embedded functions (see `normalizeWith`) to make them
-- polymorphic enough to be used.
type NormalizerM m a = forall s. Expr s a -> m (Maybe (Expr s a))
-- | An variation on `NormalizerM` for pure normalizers
type Normalizer a = NormalizerM Identity a
-- | A reified 'Normalizer', which can be stored in structures without
-- running into impredicative polymorphism.
newtype ReifiedNormalizer a = ReifiedNormalizer
{ getReifiedNormalizer :: Normalizer a }
-- | Check if an expression is in a normal form given a context of evaluation.
-- Unlike `isNormalized`, this will fully normalize and traverse through the expression.
--
-- It is much more efficient to use `isNormalized`.
--
-- `isNormalizedWith` can fail with an `error` if you check an ill-typed
-- expression
isNormalizedWith :: (Eq s, Eq a) => Normalizer a -> Expr s a -> Bool
isNormalizedWith ctx e = e == normalizeWith (Just (ReifiedNormalizer ctx)) e
-- | Quickly check if an expression is in normal form
--
-- Given a well-typed expression @e@, @'isNormalized' e@ is equivalent to
-- @e == 'normalize' e@.
--
-- Given an ill-typed expression, 'isNormalized' may fail with an error, or
-- evaluate to either False or True!
isNormalized :: Eq a => Expr s a -> Bool
isNormalized e0 = loop (Syntax.denote e0)
where
loop e = case e of
Const _ -> True
Var _ -> True
Lam _ (FunctionBinding Nothing _ Nothing Nothing a) b -> loop a && loop b
Lam _ _ _ -> False
Pi _ _ a b -> loop a && loop b
App f a -> loop f && loop a && case App f a of
App (Lam _ _ _) _ -> False
App (App (App (App NaturalFold (NaturalLit _)) _) _) _ -> False
App NaturalBuild _ -> False
App NaturalIsZero (NaturalLit _) -> False
App NaturalEven (NaturalLit _) -> False
App NaturalOdd (NaturalLit _) -> False
App NaturalShow (NaturalLit _) -> False
App (App NaturalSubtract (NaturalLit _)) (NaturalLit _) -> False
App (App NaturalSubtract (NaturalLit 0)) _ -> False
App (App NaturalSubtract _) (NaturalLit 0) -> False
App (App NaturalSubtract x) y -> not (Eval.judgmentallyEqual x y)
App NaturalToInteger (NaturalLit _) -> False
App IntegerNegate (IntegerLit _) -> False
App IntegerClamp (IntegerLit _) -> False
App IntegerShow (IntegerLit _) -> False
App IntegerToDouble (IntegerLit _) -> False
App DoubleShow (DoubleLit _) -> False
App (App ListBuild _) _ -> False
App (App (App ListDrop n) _) as
| NaturalLit 0 <- n -> False
| ListLit _ as' <- as
, Data.Sequence.null as' -> False
| NaturalLit _ <- n
, ListLit _ _ <- as -> False
App (App (App (App (App (App ListFold _) (ListLit _ _)) _) _) _) _ -> False
App (App ListLength _) (ListLit _ _) -> False
App (App ListHead _) (ListLit _ _) -> False
App (App ListLast _) (ListLit _ _) -> False
App (App ListIndexed _) (ListLit _ _) -> False
App (App ListReverse _) (ListLit _ _) -> False
App (App (App ListTake n) _) as
| NaturalLit 0 <- n -> False
| ListLit _ as' <- as
, Data.Sequence.null as' -> False
| NaturalLit _ <- n
, ListLit _ _ <- as -> False
App TextShow (TextLit (Chunks [] _)) ->
False
App (App (App TextReplace (TextLit (Chunks [] ""))) _) _ ->
False
App (App (App TextReplace (TextLit (Chunks [] _))) _) (TextLit _) ->
False
_ -> True
Let _ _ -> False
Annot _ _ -> False
Bool -> True
BoolLit _ -> True
BoolAnd x y -> loop x && loop y && decide x y
where
decide (BoolLit _) _ = False
decide _ (BoolLit _) = False
decide l r = not (Eval.judgmentallyEqual l r)
BoolOr x y -> loop x && loop y && decide x y
where
decide (BoolLit _) _ = False
decide _ (BoolLit _) = False
decide l r = not (Eval.judgmentallyEqual l r)
BoolEQ x y -> loop x && loop y && decide x y
where
decide (BoolLit True) _ = False
decide _ (BoolLit True) = False
decide l r = not (Eval.judgmentallyEqual l r)
BoolNE x y -> loop x && loop y && decide x y
where
decide (BoolLit False) _ = False
decide _ (BoolLit False ) = False
decide l r = not (Eval.judgmentallyEqual l r)
BoolIf x y z ->
loop x && loop y && loop z && decide x y z
where
decide (BoolLit _) _ _ = False
decide _ (BoolLit True) (BoolLit False) = False
decide _ l r = not (Eval.judgmentallyEqual l r)
Natural -> True
NaturalLit _ -> True
NaturalFold -> True
NaturalBuild -> True
NaturalIsZero -> True
NaturalEven -> True
NaturalOdd -> True
NaturalShow -> True
NaturalSubtract -> True
NaturalToInteger -> True
NaturalPlus x y -> loop x && loop y && decide x y
where
decide (NaturalLit 0) _ = False
decide _ (NaturalLit 0) = False
decide (NaturalLit _) (NaturalLit _) = False
decide _ _ = True
NaturalTimes x y -> loop x && loop y && decide x y
where
decide (NaturalLit 0) _ = False
decide _ (NaturalLit 0) = False
decide (NaturalLit 1) _ = False
decide _ (NaturalLit 1) = False
decide (NaturalLit _) (NaturalLit _) = False
decide _ _ = True
Integer -> True
IntegerLit _ -> True
IntegerClamp -> True
IntegerNegate -> True
IntegerShow -> True
IntegerToDouble -> True
Double -> True
DoubleLit _ -> True
DoubleShow -> True
Text -> True
TextLit (Chunks [("", _)] "") -> False
TextLit (Chunks xys _) -> all (all check) xys
where
check y = loop y && case y of
TextLit _ -> False
_ -> True
TextAppend _ _ -> False
TextReplace -> True
TextShow -> True
Date -> True
DateLiteral _ -> True
Time -> True
TimeLiteral _ _ -> True
TimeZone -> True
TimeZoneLiteral _ -> True
List -> True
ListLit t es -> all loop t && all loop es
ListAppend x y -> loop x && loop y && decide x y
where
decide (ListLit _ m) _ | Data.Sequence.null m = False
decide _ (ListLit _ n) | Data.Sequence.null n = False
decide (ListLit _ _) (ListLit _ _) = False
decide _ _ = True
ListBuild -> True
ListDrop -> True
ListFold -> True
ListLength -> True
ListHead -> True
ListLast -> True
ListIndexed -> True
ListReverse -> True
ListTake -> True
Optional -> True
Some a -> loop a
None -> True
Record kts -> Dhall.Map.isSorted kts && all decide kts
where
decide (RecordField Nothing exp' Nothing Nothing) = loop exp'
decide _ = False
RecordLit kvs -> Dhall.Map.isSorted kvs && all decide kvs
where
decide (RecordField Nothing exp' Nothing Nothing) = loop exp'
decide _ = False
Union kts -> Dhall.Map.isSorted kts && all (all loop) kts
Combine _ _ x y -> loop x && loop y && decide x y
where
decide (RecordLit m) _ | Data.Foldable.null m = False
decide _ (RecordLit n) | Data.Foldable.null n = False
decide (RecordLit _) (RecordLit _) = False
decide _ _ = True
CombineTypes _ x y -> loop x && loop y && decide x y
where
decide (Record m) _ | Data.Foldable.null m = False
decide _ (Record n) | Data.Foldable.null n = False
decide (Record _) (Record _) = False
decide _ _ = True
Prefer _ _ x y -> loop x && loop y && decide x y
where
decide (RecordLit m) _ | Data.Foldable.null m = False
decide _ (RecordLit n) | Data.Foldable.null n = False
decide (RecordLit _) (RecordLit _) = False
decide l r = not (Eval.judgmentallyEqual l r)
RecordCompletion _ _ -> False
Merge x y t -> loop x && loop y && all loop t && case x of
RecordLit _ -> case y of
Field (Union _) _ -> False
App (Field (Union _) _) _ -> False
Some _ -> False
App None _ -> False
_ -> True
_ -> True
ToMap x t -> case x of
RecordLit _ -> False
_ -> loop x && all loop t
ShowConstructor x -> loop x && case x of
Field (Union kts) (Syntax.fieldSelectionLabel -> k) ->
case Dhall.Map.lookup k kts of
Just Nothing -> False
_ -> True
App (Field (Union kts) (Syntax.fieldSelectionLabel -> k)) _ ->
case Dhall.Map.lookup k kts of
Just (Just _) -> False
_ -> True
Some _ -> False
App None _ -> False
_ -> True
Field r (FieldSelection Nothing k Nothing) -> case r of