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 var syntax for convenient access to the state effect #1137

Closed
axch opened this issue Oct 25, 2022 · 5 comments · Fixed by #1147
Closed

Add var syntax for convenient access to the state effect #1137

axch opened this issue Oct 25, 2022 · 5 comments · Fixed by #1147

Comments

@axch
Copy link
Collaborator

axch commented Oct 25, 2022

As suggested by @apaszke,

var x = stuff
...

would desugar into

with_state stuff \x.
  ...
@axch
Copy link
Collaborator Author

axch commented Nov 4, 2022

Generalization, from discussion with @dougalm: How about adding general syntactic sugar for de-denting continuation lambdas? To wit, introduce a <- lexeme that's syntactic sugar where

<pat> <- <expr>
<block>

becomes

<expr> \<pat>.
  <block>

Then for the var use-case, one can write

x <- with_state <init>
...

(and if we want more syntactic sugar to make that medicine go down even easier, we can add it).

@dougalm
Copy link
Collaborator

dougalm commented Nov 4, 2022

I think this would go a long way towards mitigating indentation hell. It's similar in spirit to Haskell's do notation, which turns <e1> >>= \x -> <e2> into do {x <- <e1> ; <e2>}. (It might even be the same thing as do-notation for the special case of the continuation monad...)

We might also want syntax to create a local scoped block, so that the scope of the reference doesn't have to be the entire rest of the function. Perhaps:

x = ...
ans = do
  y <- with_state init_val
  z = f x y
  g z

which would desugar to

x = ...
ans = with_state init_val \y.
  z = f x y
  g z

[EDIT] never mind! I just realized that the former (apart from <-) is already valid Dex syntax without the do. We don't need to add anything extra. (Although we might want to tweak the parser so that you can have a scoped block without having to bind the result to a name.)

@apaszke
Copy link
Collaborator

apaszke commented Nov 5, 2022

Yeah, I agree that would be nice. I think Koka reserves the with keyword for that.

@axch
Copy link
Collaborator Author

axch commented Nov 7, 2022

Another case we don't handle yet:

AsList _ $ with_state 0 \listIdx.
  ...

can't be spelled as

AsList _ $
  listIdx <- with_state 0
  ...

because the expression doesn't recognize that it's opening a block. It might be fixable, but it's also not completely clear how important it is, because there is no indentation difference between these two forms anyway.

axch added a commit to axch/dex-lang that referenced this issue Nov 7, 2022
Fixes google-research#1137 in spirit, if not precisely as filed (i.e., this
implements the generalization in the comments, not the original ask).

Also add `Lexer.debug`, leaning on Megaparsec's `dbg`, which actually
turns out to be very helpful for debugging the concrete syntax parser.
@axch
Copy link
Collaborator Author

axch commented Nov 7, 2022

Also, I noticed that

i <- iter
...

is legal Dex now. I hereby suggest that it be bad style to use <- syntax for any continuation lambda that is transparently meant to be called a different number of times than once.[*]

[*] I say "transparently" because one is of course free to implement the list monad using this syntax; I think it's not bad style to write x <- select <options>, because in that monad the notion is that the continuation is "logically" called just once, with the correct value. The fact that the monad is really doing search is sort of an implementation detail. Ditto for various proposed probabilistic programming monads. But I'd rather not see a lot if i <- iter in Dex code.

@axch axch closed this as completed in 22ad6a4 Nov 8, 2022
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 a pull request may close this issue.

3 participants