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

Access to a in StM m a #30

Open
nikomi opened this issue Jan 5, 2016 · 2 comments
Open

Access to a in StM m a #30

nikomi opened this issue Jan 5, 2016 · 2 comments

Comments

@nikomi
Copy link

nikomi commented Jan 5, 2016

I'm trying to create a lifted version of function f in base monad b (imported qualified as B):

f :: (Monad m, MonadBaseControl b m) => m Bool -> m Bool
f g = control $ \runInBase -> B.f $ runInBase g

runInBase returns b (StM m a) which is expected by restoreM, therefore B.f should obviously be of type b (StM m a) -> b (StM m a). This works if B.f actually is of type b c -> b c with c ~ StM m a and B.f most likely just passing the data through. If B.f requires an explicit type - e.g. b Bool -> b Bool as in this sample - this fails with a compiler error on Bool vs. StM m Bool.

What am I missing?
Is there a way to apply B.f to the a in StM m a while keeping the monadic state?

f g = control $ \runInBase -> apply B.f $ runInBase g

where apply converts the given function from b a -> b a to b (StM m a) -> b (StM m a), i.e.

apply :: (b a -> b a) -> b (StM m a) -> b (StM m a)

If there is not I think it might be good if there were - together with an apply_ for

apply_ :: (b a -> b ()) -> b (StM m a) -> b (StM m ())

What do you think?

@athanclark
Copy link

I was just needing this. I think I'm going to fork it, expose StT and StM to the typeclass with fundeps, then make an Extractable class, which gives a stT a -> a function. Then, in case you were using MaybeT or something, only support with that class would grant you access. I'll link my PR here when it's made.

@athanclark
Copy link

athanclark commented May 13, 2017

I made a controversial port, let me know what you think:

Law:

forall x. (liftBaseWith $ \runInBase -> runSingleton <$> runInBase x) == x

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