Skip to content

Commit 200ca83

Browse files
committed
Value.Equal: refactor
1 parent 2a4b7eb commit 200ca83

File tree

1 file changed

+106
-56
lines changed

1 file changed

+106
-56
lines changed

src/Nix/Value/Equal.hs

Lines changed: 106 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -52,57 +52,90 @@ alignEqM
5252
-> f a
5353
-> f b
5454
-> m Bool
55-
alignEqM eq fa fb = fmap (either (const False) (const True)) $ runExceptT $ do
56-
pairs <- forM (Data.Align.align fa fb) $ \case
57-
These a b -> pure (a, b)
58-
_ -> throwE ()
59-
for_ pairs $ \(a, b) -> guard =<< lift (eq a b)
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
6065

6166
alignEq :: (Align f, Traversable f) => (a -> b -> Bool) -> f a -> f b -> Bool
6267
alignEq eq fa fb = runIdentity $ alignEqM (\x y -> Identity (eq x y)) fa fb
6368

64-
isDerivationM :: Monad m => (t -> m (Maybe NixString)) -> AttrSet t -> m Bool
65-
isDerivationM f m = case HashMap.Lazy.lookup "type" m of
66-
Nothing -> pure False
67-
Just t -> do
68-
mres <- f t
69-
case mres of
70-
-- We should probably really make sure the context is empty here
71-
-- but the C++ implementation ignores it.
72-
Just s -> pure $ stringIgnoreContext s == "derivation"
73-
Nothing -> pure False
74-
75-
isDerivation :: Monad m => (t -> Maybe NixString) -> AttrSet t -> Bool
69+
isDerivationM
70+
:: Monad m
71+
=> ( t
72+
-> m (Maybe NixString)
73+
)
74+
-> AttrSet t
75+
-> m Bool
76+
isDerivationM f m =
77+
maybe
78+
(pure False)
79+
(\ t ->
80+
do
81+
mres <- f t
82+
83+
maybe
84+
-- We should probably really make sure the context is empty here
85+
-- but the C++ implementation ignores it.
86+
(pure False)
87+
(pure . (==) "derivation" . stringIgnoreContext)
88+
mres
89+
)
90+
(HashMap.Lazy.lookup "type" m)
91+
92+
isDerivation
93+
:: Monad m
94+
=> ( t
95+
-> Maybe NixString
96+
)
97+
-> AttrSet t
98+
-> Bool
7699
isDerivation f = runIdentity . isDerivationM (Identity . f)
77100

78101
valueFEqM
79102
:: Monad n
80-
=> (AttrSet a -> AttrSet a -> n Bool)
81-
-> (a -> a -> n Bool)
103+
=> ( AttrSet a
104+
-> AttrSet a
105+
-> n Bool
106+
)
107+
-> ( a
108+
-> a
109+
-> n Bool
110+
)
82111
-> NValueF p m a
83112
-> NValueF p m a
84113
-> n Bool
85-
valueFEqM attrsEq eq = curry $ \case
86-
(NVConstantF (NFloat x), NVConstantF (NInt y) ) -> pure $ x == fromInteger y
87-
(NVConstantF (NInt x), NVConstantF (NFloat y)) -> pure $ fromInteger x == y
88-
(NVConstantF lc , NVConstantF rc ) -> pure $ lc == rc
89-
(NVStrF ls, NVStrF rs) -> pure $ (\i -> i ls == i rs) stringIgnoreContext
90-
(NVListF ls , NVListF rs ) -> alignEqM eq ls rs
91-
(NVSetF lm _, NVSetF rm _) -> attrsEq lm rm
92-
(NVPathF lp , NVPathF rp ) -> pure $ lp == rp
93-
_ -> pure False
114+
valueFEqM attrsEq eq =
115+
curry $
116+
\case
117+
(NVConstantF (NFloat x), NVConstantF (NInt y)) -> pure $ x == fromInteger y
118+
(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
124+
_ -> pure False
94125

95126
valueFEq
96127
:: (AttrSet a -> AttrSet a -> Bool)
97128
-> (a -> a -> Bool)
98129
-> NValueF p m a
99130
-> NValueF p m a
100131
-> Bool
101-
valueFEq attrsEq eq x y = runIdentity $ valueFEqM
102-
(\x' y' -> Identity $ attrsEq x' y')
103-
(\x' y' -> Identity $ eq x' y')
104-
x
105-
y
132+
valueFEq attrsEq eq x y =
133+
runIdentity $
134+
valueFEqM
135+
(\x' y' -> Identity $ attrsEq x' y')
136+
(\x' y' -> Identity $ eq x' y')
137+
x
138+
y
106139

107140
compareAttrSetsM
108141
:: Monad m
@@ -111,16 +144,24 @@ compareAttrSetsM
111144
-> AttrSet t
112145
-> AttrSet t
113146
-> m Bool
114-
compareAttrSetsM f eq lm rm = do
115-
isDerivationM f lm >>= \case
116-
True -> isDerivationM f rm >>= \case
117-
True
118-
| Just lp <- HashMap.Lazy.lookup "outPath" lm, Just rp <- HashMap.Lazy.lookup "outPath" rm -> eq
119-
lp
120-
rp
121-
_ -> compareAttrs
122-
_ -> compareAttrs
123-
where compareAttrs = alignEqM eq lm rm
147+
compareAttrSetsM f eq lm rm =
148+
do
149+
l <- isDerivationM f lm
150+
bool
151+
compareAttrs
152+
(do
153+
r <- isDerivationM f rm
154+
case r of
155+
True
156+
| Just lp <- HashMap.Lazy.lookup "outPath" lm, Just rp <- HashMap.Lazy.lookup "outPath" rm ->
157+
eq
158+
lp
159+
rp
160+
_ -> compareAttrs
161+
)
162+
l
163+
where
164+
compareAttrs = alignEqM eq lm rm
124165

125166
compareAttrSets
126167
:: (t -> Maybe NixString)
@@ -144,23 +185,32 @@ valueEqM (Free (NValue (extract -> x))) (Free (NValue (extract -> y))) =
144185
where
145186
f =
146187
free
147-
(pure . (\case
188+
(pure .
189+
(\case
148190
NVStr s -> pure s
149191
_ -> mempty
150192
) <=< force
151193
)
152-
(pure . \case
153-
NVStr' s -> pure s
154-
_ -> mempty
194+
(pure .
195+
\case
196+
NVStr' s -> pure s
197+
_ -> mempty
155198
)
156199

157200
thunkEqM :: (MonadThunk t m (NValue t f m), Comonad f) => t -> t -> m Bool
158-
thunkEqM lt rt = (=<< force lt) $ \lv -> (=<< force rt) $ \rv ->
159-
let unsafePtrEq = case (lt, rt) of
160-
(thunkId -> lid, thunkId -> rid) | lid == rid -> pure True
161-
_ -> valueEqM lv rv
162-
in case (lv, rv) of
163-
(NVClosure _ _, NVClosure _ _) -> unsafePtrEq
164-
(NVList _ , NVList _ ) -> unsafePtrEq
165-
(NVSet _ _ , NVSet _ _ ) -> unsafePtrEq
166-
_ -> valueEqM lv rv
201+
thunkEqM lt rt =
202+
do
203+
lv <- force lt
204+
rv <- force rt
205+
206+
let
207+
unsafePtrEq =
208+
case (lt, rt) of
209+
(thunkId -> lid, thunkId -> rid) | lid == rid -> pure True
210+
_ -> valueEqM lv rv
211+
212+
case (lv, rv) of
213+
(NVClosure _ _, NVClosure _ _) -> unsafePtrEq
214+
(NVList _ , NVList _ ) -> unsafePtrEq
215+
(NVSet _ _ , NVSet _ _ ) -> unsafePtrEq
216+
_ -> valueEqM lv rv

0 commit comments

Comments
 (0)