Skip to content

Commit cdbeb61

Browse files
sjakobimergify[bot]
authored andcommitted
Add ImportAlt to Arbitrary instance, fix isNormalized and diff (#1276)
Also: * Add @Gabriel439's explanation regarding whitespace as a comment * Fix precedence in diffs of subexpressions
1 parent 0ebf705 commit cdbeb61

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

dhall/src/Dhall/Core.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2050,7 +2050,7 @@ isNormalized e0 = loop (denote e0)
20502050
Assert t -> loop t
20512051
Equivalent l r -> loop l && loop r
20522052
Note _ e' -> loop e'
2053-
ImportAlt l _r -> loop l
2053+
ImportAlt _ _ -> False
20542054
Embed _ -> True
20552055

20562056
{-| Detect if the given variable is free within the given expression

dhall/src/Dhall/Diff.hs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -756,17 +756,47 @@ diffAnnotatedExpression l r@(Annot {}) =
756756
diffAnnotatedExpression l r =
757757
diffOperatorExpression l r
758758

759+
{- Whitespace in diffs of operator expressions:
760+
761+
All indentation (whether pretty-printing or diffing) is a multiple of two
762+
spaces, so if the operator is one character long (like ?) then the diff pads
763+
the left margin to two space:
764+
765+
␣␣e₀
766+
?␣e₁
767+
768+
... but if the operator is two characters long (like ||) then the diff pads
769+
the left margin to four spaces:
770+
771+
␣␣␣␣e₀
772+
||␣␣e₁
773+
-}
759774
diffOperatorExpression :: (Eq a, Pretty a) => Expr Void a -> Expr Void a -> Diff
760-
diffOperatorExpression = diffOrExpression
775+
diffOperatorExpression = diffImportAltExpression
776+
777+
diffImportAltExpression :: (Pretty a, Eq a) => Expr Void a -> Expr Void a -> Diff
778+
diffImportAltExpression l@(ImportAlt {}) r@(ImportAlt {}) =
779+
enclosed' " " (operator "?" <> " ") (docs l r)
780+
where
781+
docs (ImportAlt aL bL) (ImportAlt aR bR) =
782+
Data.List.NonEmpty.cons (diffOrExpression aL aR) (docs bL bR)
783+
docs aL aR =
784+
pure (diffOrExpression aL aR)
785+
diffImportAltExpression l@(ImportAlt {}) r =
786+
mismatch l r
787+
diffImportAltExpression l r@(ImportAlt {}) =
788+
mismatch l r
789+
diffImportAltExpression l r =
790+
diffOrExpression l r
761791

762792
diffOrExpression :: (Eq a, Pretty a) => Expr Void a -> Expr Void a -> Diff
763793
diffOrExpression l@(BoolOr {}) r@(BoolOr {}) =
764794
enclosed' " " (operator "||" <> " ") (docs l r)
765795
where
766796
docs (BoolOr aL bL) (BoolOr aR bR) =
767-
Data.List.NonEmpty.cons (diffTextAppendExpression aL aR) (docs bL bR)
797+
Data.List.NonEmpty.cons (diffPlusExpression aL aR) (docs bL bR)
768798
docs aL aR =
769-
pure (diffTextAppendExpression aL aR)
799+
pure (diffPlusExpression aL aR)
770800
diffOrExpression l@(BoolOr {}) r =
771801
mismatch l r
772802
diffOrExpression l r@(BoolOr {}) =
@@ -779,9 +809,9 @@ diffPlusExpression l@(NaturalPlus {}) r@(NaturalPlus {}) =
779809
enclosed' " " (operator "+" <> " ") (docs l r)
780810
where
781811
docs (NaturalPlus aL bL) (NaturalPlus aR bR) =
782-
Data.List.NonEmpty.cons (diffListAppendExpression aL aR) (docs bL bR)
812+
Data.List.NonEmpty.cons (diffTextAppendExpression aL aR) (docs bL bR)
783813
docs aL aR =
784-
pure (diffListAppendExpression aL aR)
814+
pure (diffTextAppendExpression aL aR)
785815
diffPlusExpression l@(NaturalPlus {}) r =
786816
mismatch l r
787817
diffPlusExpression l r@(NaturalPlus {}) =
@@ -794,9 +824,9 @@ diffTextAppendExpression l@(TextAppend {}) r@(TextAppend {}) =
794824
enclosed' " " (operator "++" <> " ") (docs l r)
795825
where
796826
docs (TextAppend aL bL) (TextAppend aR bR) =
797-
Data.List.NonEmpty.cons (diffPlusExpression aL aR) (docs bL bR)
827+
Data.List.NonEmpty.cons (diffListAppendExpression aL aR) (docs bL bR)
798828
docs aL aR =
799-
pure (diffPlusExpression aL aR)
829+
pure (diffListAppendExpression aL aR)
800830
diffTextAppendExpression l@(TextAppend {}) r =
801831
mismatch l r
802832
diffTextAppendExpression l r@(TextAppend {}) =

dhall/tests/Dhall/Test/QuickCheck.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ instance (Arbitrary s, Arbitrary a) => Arbitrary (Expr s a) where
245245
, ( 1, lift1 Assert)
246246
, ( 1, lift2 Equivalent)
247247
, ( 7, lift1 Embed)
248+
, ( 7, lift2 ImportAlt)
248249
]
249250
)
250251
standardizedExpression

0 commit comments

Comments
 (0)