Skip to content

Commit 2b7938e

Browse files
committed
Standard: instance MonadValue (StdValue m) m: optimize
Thunking & Values have pretty complex type system & inference. Putting the function loops literally to show to GHC & to users that there is no type class jumps needed - instances go & run recursion until value is computed. Type applications are to just skip the `MonadThunk (StdThunk m) m (StdValue m)` instance all togather & so save the type class jump on these frequently called & recurced on operations.
1 parent d97a8a0 commit 2b7938e

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/Nix/Standard.hs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ instance
119119
derivationStrict = defaultDerivationStrict
120120
traceEffect = defaultTraceEffect
121121

122+
-- 2021-07-24:
123+
-- This instance currently is to satisfy @MonadThunk@ requirements for @normalForm@ function.
124+
-- As it is seen from the instance - it does superficial type class jump.
125+
-- It is just a type boundary for thunking.
122126
instance
123127
( Typeable m
124128
, MonadThunkId m
@@ -221,22 +225,27 @@ instance
221225
defer
222226
:: m (StdValue m)
223227
-> m (StdValue m)
224-
defer = fmap pure . thunk
228+
defer = fmap (pure . coerce) . thunk @(CitedStdThunk m)
225229

226230
demand
227231
:: StdValue m
228232
-> m (StdValue m)
229-
demand v =
230-
free
231-
(demand <=< force)
232-
(const $ pure v)
233-
v
233+
demand = go -- lock to ensure no type class jumps.
234+
where
235+
go :: StdValue m -> m (StdValue m)
236+
go =
237+
free
238+
(go <=< force @(CitedStdThunk m) . coerce)
239+
(pure . Free)
234240

235241
inform
236242
:: StdValue m
237243
-> m (StdValue m)
238-
inform (Pure t) = Pure <$> further t
239-
inform (Free v) = Free <$> bindNValue' id inform v
244+
inform = go -- lock to ensure no type class jumps.
245+
where
246+
go :: StdValue m -> m (StdValue m)
247+
go (Pure t) = (Pure . coerce <$>) . (further @(CitedStdThunk m) . coerce) $ t
248+
go (Free v) = (Free <$>) . bindNValue' id go $ v
240249

241250

242251
-- * @instance MonadValueF (StdValue m) m@

0 commit comments

Comments
 (0)