Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please, inspect if demand & force is neede before {to,from}Value. #1003

Open
Anton-Latukha opened this issue Nov 4, 2021 · 2 comments
Open

Comments

@Anton-Latukha
Copy link
Collaborator

Anton-Latukha commented Nov 4, 2021

Unless I am deepky mistaken, demanding and forcing before calling fromValue is redundant. The pattern is to use fromValue alone. Not sure about coerce without looking at the code, but I see no reason for it to be different.

Originally posted by @layus in #948 (comment)

@Anton-Latukha
Copy link
Collaborator Author

Anton-Latukha commented Nov 4, 2021

To be honest - I am still lost in the type class jumps inside the forest of Convert module, it is hard to say if all of them lead to demand/force.

HLS jumps to type class definition & does not support jumping to instance implementation, there is no tool that show those instance connections.


Seems like demand is not always called during conversion:

instance Convertible e t f m
  => FromValue Float m (NValue' t f m (NValue t f m)) where

  fromValueMay =
    pure .
      \case
        NVConstant' (NFloat b) -> pure b
        NVConstant' (NInt   i) -> pure $ fromInteger i
        _                      -> Nothing

  fromValue = fromMayToValue TFloat

fromMayToValue
  :: forall t f m a e
  . ( Convertible e t f m
    , FromValue a m (NValue' t f m (NValue t f m))
    )
  => ValueType
  -> NValue' t f m (NValue t f m)
  -> m a
fromMayToValue t v =
  maybe
    (throwError $ Expectation @t @f @m t (Free v))
    pure
    =<< fromValueMay v

That Float instance fromValue jumps into fromMayToValue which jumps back to Float fromValueMay.


I'd reformulated the question: where demand/force should be used & if they required - they should be included into according instances.

@soulomoon
Copy link
Collaborator

soulomoon commented Jan 19, 2022

From my understanding:

Since MonadValue (StdValue m) m => type StdValue m = NValue (StdThunk m) (StdCited m) m

demand :: v -> m v
v = NValue t f m
m = NValue' t f m.

For fromValue/fromMayToValue

  • For case v we have FromValue a m (NValue t f m) and FromValue a m (Deeper (NValue t f m)) instances where demand/force would be called.
  • For case m v no demand is called.

(NValue t f m) would always be demand/force if chains of fromValue/fromMayToValue is used for every encounter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants