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

Censor & listen #1049

Open
Jashweii opened this issue Oct 8, 2023 · 0 comments
Open

Censor & listen #1049

Jashweii opened this issue Oct 8, 2023 · 0 comments

Comments

@Jashweii
Copy link

Jashweii commented Oct 8, 2023

I have recently gotten a great deal out of censor mempty . listen with MonadWriter.
Essentially I want to delimit part of the log in a subcomputation, but make use of that part of the log (in my case capture some of it in the result). My main example is HM esque type checking. A simplified example:

  generalise :: MonadWriter (Set Var) m => m Type -> m Scheme
  generalise mt = do
    (t, vars) <- (censor mempty . listen) mt
    pure (Scheme vars t)

Here generalise captures all of the metavariables the subcomputation would introduce, and uses them to quantify the result.
It is the flip side of an instantiate that would open a scheme with fresh variables and tell them.

Main idea (general case is a field of the log):

f :: MonadWriter w m => Lens' w u -> u -> m a -> m (a, u)
f l x = censoring l (const x) . listening l

The const could be a function, but I almost always use mempty (aka const mempty).
It would be kind of arbitrary to use a function here, since you're not retrieving a function of it.
This is normally a field of the log, but doesn't necessarily have to be a monoid itself, hence I haven't made it mempty.
Maybe it would make sense to have the mempty version additionally, since a record monoid is often a pointwise record of monoids.

f' :: (MonadWriter w m, Monoid u) => Lens' w u -> m a -> m (a, u)
f' l = censoring l mempty . listening l
--   = f l mempty

(The mempty version is to this what scribe is to tell)
Bikeshedding: Not sure what to call either of these. capturing, delimiting, censored, silently, de-scribing, observing, simulating, recording?
Ideally something that indicates both that the log doesn't escape & that it is captured and returned.

With MTL (censor mempty . listen) is actually a form of runWriterT - otherwise I would have also proposed this for mtl.
There is probably a zoom/magnify writer analogue with a setter, but it doesn't address this use case, as there aren't analagous (eval/exec/)runStateT zooms that merely take the present state and don't change the overall state.

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

1 participant