Skip to content

Commit 84d9625

Browse files
committed
Value.Equal: refactor
1 parent 200ca83 commit 84d9625

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

src/Nix/Value/Equal.hs

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@ import Nix.Utils
3030
import Nix.Value
3131

3232
checkComparable
33-
:: (Framed e m, MonadDataErrorContext t f m)
33+
:: ( Framed e m
34+
, MonadDataErrorContext t f m
35+
)
3436
=> NValue t f m
3537
-> NValue t f m
3638
-> m ()
37-
checkComparable x y = case (x, y) of
38-
(NVConstant (NFloat _), NVConstant (NInt _)) -> pure ()
39-
(NVConstant (NInt _), NVConstant (NFloat _)) -> pure ()
40-
(NVConstant (NInt _), NVConstant (NInt _)) -> pure ()
41-
(NVConstant (NFloat _), NVConstant (NFloat _)) -> pure ()
42-
(NVStr _, NVStr _) -> pure ()
43-
(NVPath _, NVPath _) -> pure ()
44-
_ -> throwError $ Comparison x y
39+
checkComparable x y =
40+
case (x, y) of
41+
(NVConstant (NFloat _), NVConstant (NInt _)) -> pure ()
42+
(NVConstant (NInt _), NVConstant (NFloat _)) -> pure ()
43+
(NVConstant (NInt _), NVConstant (NInt _)) -> pure ()
44+
(NVConstant (NFloat _), NVConstant (NFloat _)) -> pure ()
45+
(NVStr _ , NVStr _ ) -> pure ()
46+
(NVPath _ , NVPath _ ) -> pure ()
47+
_ -> throwError $ Comparison x y
4548

4649
-- | Checks whether two containers are equal, using the given item equality
4750
-- predicate. If there are any item slots that don't match between the two
@@ -52,16 +55,22 @@ alignEqM
5255
-> f a
5356
-> f b
5457
-> m Bool
55-
alignEqM eq fa fb = fmap (either (const False) (const True)) $ runExceptT $
56-
do
57-
pairs <-
58-
traverse
59-
(\case
60-
These a b -> pure (a, b)
61-
_ -> throwE ()
62-
)
63-
(Data.Align.align fa fb)
64-
traverse_ (\ (a, b) -> guard =<< lift (eq a b)) pairs
58+
alignEqM eq fa fb =
59+
fmap
60+
(either
61+
(const False)
62+
(const True)
63+
)
64+
$ runExceptT $
65+
do
66+
pairs <-
67+
traverse
68+
(\case
69+
These a b -> pure (a, b)
70+
_ -> throwE ()
71+
)
72+
(Data.Align.align fa fb)
73+
traverse_ (\ (a, b) -> guard =<< lift (eq a b)) pairs
6574

6675
alignEq :: (Align f, Traversable f) => (a -> b -> Bool) -> f a -> f b -> Bool
6776
alignEq eq fa fb = runIdentity $ alignEqM (\x y -> Identity (eq x y)) fa fb
@@ -114,13 +123,13 @@ valueFEqM
114123
valueFEqM attrsEq eq =
115124
curry $
116125
\case
117-
(NVConstantF (NFloat x), NVConstantF (NInt y)) -> pure $ x == fromInteger y
126+
(NVConstantF (NFloat x), NVConstantF (NInt y)) -> pure $ x == fromInteger y
118127
(NVConstantF (NInt x), NVConstantF (NFloat y)) -> pure $ fromInteger x == y
119-
(NVConstantF lc , NVConstantF rc ) -> pure $ lc == rc
120-
(NVStrF ls , NVStrF rs ) -> pure $ (\i -> i ls == i rs) stringIgnoreContext
121-
(NVListF ls , NVListF rs ) -> alignEqM eq ls rs
122-
(NVSetF lm _ , NVSetF rm _ ) -> attrsEq lm rm
123-
(NVPathF lp , NVPathF rp ) -> pure $ lp == rp
128+
(NVConstantF lc , NVConstantF rc ) -> pure $ lc == rc
129+
(NVStrF ls , NVStrF rs ) -> pure $ (\ i -> i ls == i rs) stringIgnoreContext
130+
(NVListF ls , NVListF rs ) -> alignEqM eq ls rs
131+
(NVSetF lm _ , NVSetF rm _ ) -> attrsEq lm rm
132+
(NVPathF lp , NVPathF rp ) -> pure $ lp == rp
124133
_ -> pure False
125134

126135
valueFEq
@@ -179,9 +188,13 @@ valueEqM
179188
-> m Bool
180189
valueEqM ( Pure x) ( Pure y) = thunkEqM x y
181190
valueEqM ( Pure x) y@(Free _) = thunkEqM x =<< thunk (pure y)
182-
valueEqM x@(Free _) ( Pure y) = thunkEqM ?? y =<< thunk (pure x)
191+
valueEqM x@(Free _) ( Pure y) = (`thunkEqM` y) =<< thunk (pure x)
183192
valueEqM (Free (NValue (extract -> x))) (Free (NValue (extract -> y))) =
184-
valueFEqM (compareAttrSetsM f valueEqM) valueEqM x y
193+
valueFEqM
194+
(compareAttrSetsM f valueEqM)
195+
valueEqM
196+
x
197+
y
185198
where
186199
f =
187200
free

0 commit comments

Comments
 (0)