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

Add evalState(T) #411

Merged
merged 4 commits into from
Apr 28, 2022
Merged

Add evalState(T) #411

merged 4 commits into from
Apr 28, 2022

Conversation

andreasabel
Copy link
Contributor

Fix #404: add evalState and evalStateT.

Use case: stateful interpreter (eval) for C-like expressions e:

let Ur value = HashMap.empty capacity (\ env -> move (evalState (eval e) env))

This pattern breaks the "jail" set by the type signature of HashMap-allocation:

empty :: Int -> (HashMap k v %1 -> Ur b) %1 -> Ur b

HashMap is not Movable but Consumable, so we can get rid of it before moveing the result to Ur.

PS: Your ormolu CI workflow is quite a gatekeeper, running for 12min... I tried to run it locally after stack install ormolu, but this didn't produce the same formatting.

$ ormolu --version
ormolu 0.3.1.0 UNKNOWN UNKNOWN
using ghc-lib-parser 9.0.2.20211226

@aspiwack
Copy link
Member

The CI doesn't run for 12 minutes, fortunately (that would be more than a little bug). It was just waiting for someone with push rights to approve the CI. This is a Github thing, to prevent people from using pull requests as a free computing resource (typically to mine bitcoin or the like).

@andreasabel
Copy link
Contributor Author

andreasabel commented Apr 12, 2022

The CI doesn't run for 12 minutes,

Maybe I was unlucky? Maybe it does so until it succeeds for the first time (caching logic):

Screenshot 2022-04-12 at 18 03 07

tasty-hedgehog,
tasty-hedgehog < 1.2,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little concerning. I assume you've needed that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, otherwise cabal build would fail. I am using GHC 9.2.2.
I think this restriction can be lifted if the underlying problem is fixed:

Building test suite 'test' for linear-base-0.2.0..
[1 of 9] Compiling Test.Data.Destination ...

test/Test/Data/Destination.hs:22:7: error: [-Wdeprecations, -Werror=deprecations]
    In the use of ‘testProperty’ (imported from Test.Tasty.Hedgehog):
    Deprecated: "testProperty will cause Hedgehog to provide incorrect instructions for re-checking properties"
   |
22 |     [ testProperty "alloc . mirror = id" roundTrip,
   |       ^^^^^^^^^^^^

This deprecation was introduced in tasty-hedgehog-1.2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment to the .cabal file to explain why this bound is needed.

-- | Use with care!
-- This consumes the final state, so might be costly at runtime.
evalStateT :: (Functor m, Consumable s) => StateT s m a %1 -> s %1 -> m a
evalStateT f = fmap Linear.fst . runStateT f
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah now I see the problem with #405 . I didn't remember we had this version of fst available as well.

@aspiwack
Copy link
Member

aspiwack commented Apr 12, 2022

The CI doesn't run for 12 minutes,

Maybe I was unlucky? Maybe it does so until it succeeds for the first time (caching logic):

Ah yes… we've been needing to provide Ormolu via stack, so it needs to be built once which probably takes a little bit of time. I wonder how the cache logic works on forks… maybe that's the rub…

Please let me know if it happens again next time you make a PR.

@treeowl
Copy link
Collaborator

treeowl commented Apr 12, 2022

How does this help the HashMap situation? We have no way to ensure that a HashMap doesn't escape through the "context" of the monad.

@andreasabel
Copy link
Contributor Author

andreasabel commented Apr 13, 2022

@treeowl wrote:

How does this help the HashMap situation? We have no way to ensure that a HashMap doesn't escape through the "context" of the monad.

Er, I cannot follow. Maybe you had some prior discussion of this issue? Do you have a pointer to it?

Atm, the continuation of HashMap.empty has to live in Ur b, do you suggest bad things happen if b is instantiated with, say, IO a?

`fst` is the name of the first projection, `first` is the name of a
bifunctor action.
Use case: stateful interpreter (`eval`) for C-like expressions `e`:
```haskell
let Ur value = HashMap.empty capacity (\ env -> move (evalState (eval e) env))
```
This pattern breaks the "jail" set by the type signature of
`HashMap`-allocation:
```haskell
empty :: Int -> (HashMap k v %1 -> Ur b) %1 -> Ur b
```
`HashMap` is not `Movable` but `Consumable`, so we can get rid of it before
`move`ing the result to `Ur`.
@aspiwack
Copy link
Member

(I'll get to merging this soon, I haven't had time so far, but it will be merged, sorry about the delay)

@aspiwack
Copy link
Member

Thanks @andreasabel . Let me merge.

@aspiwack aspiwack merged commit 113a758 into tweag:master Apr 28, 2022
@andreasabel andreasabel deleted the evalState branch April 28, 2022 09:26
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

Successfully merging this pull request may close these issues.

Add evalState (to Control.Functor.Linear)
3 participants