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

Lean: add placeholder for state monad to stateful functions #870

Merged
merged 7 commits into from
Jan 16, 2025

Conversation

javra
Copy link
Collaborator

@javra javra commented Jan 13, 2025

Adds a monad StateM which is added to all stateful functions. In the body of stateful function definitions, we put everything inside a return statement, but the idea is that in the future, doc_exp will return a pair of strings, one for the do block and one print of the pure part of the expression which goes into the return call

@javra javra changed the title Lean: add placeholer for state monad to stateful functions Lean: add placehodler for state monad to stateful functions Jan 13, 2025
Copy link

github-actions bot commented Jan 13, 2025

Test Results

   12 files  ±0     24 suites  ±0   0s ⏱️ ±0s
  748 tests ±0    748 ✅ ±0  0 💤 ±0  0 ❌ ±0 
2 491 runs  ±0  2 491 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 0c87344. ± Comparison against base commit bf921f9.

♻️ This comment has been updated with latest results.

@ineol
Copy link
Collaborator

ineol commented Jan 13, 2025

How do you plan to translate a function body like this?

let x = rX 0
if x then wX 0 3; return 11 
     else return 0

I don't get how the idea of returning a pair of string works.

@javra
Copy link
Collaborator Author

javra commented Jan 13, 2025

Something like

do
  let x ← rX 0
  if x then do
    wX 0 3
    return 11
  else do
    return 0

Only when doc_exp descends to 11 or 0 respectively does the result become non-monadic, so the caller of that instance of doc_exp would put it behind the return.

This PR does not make all expression constructor ready for this. I just thought we'd might get it merged since the work that remains will be a major overhaul of doc_exp.

@javra javra marked this pull request as ready for review January 16, 2025 10:51
@bacam
Copy link
Contributor

bacam commented Jan 16, 2025

Note that if you're using the usual rewrites, the Sail AST will already be in monadic form, with the monadic bind begin E_internal_plet nodes, and monadic return E_internal_return, so you can add the return in doc_exp rather than doc_funcl_body. I suppose that doesn't matter quite yet, though.

@ineol
Copy link
Collaborator

ineol commented Jan 16, 2025

I think there is a typo in the commit/PR titles placehodler -> placeholder

@javra javra changed the title Lean: add placehodler for state monad to stateful functions Lean: add placeholder for state monad to stateful functions Jan 16, 2025
@javra
Copy link
Collaborator Author

javra commented Jan 16, 2025

@bacam In the end I want to be able to translate return f(g(x)) to

... do
  return f (← g x)

if g is monadic, not quite sure yet, how to best implement that. Maybe we need to remove the rewrite that transforms it to the monadic form? The do blocks in Lean are quite a bit more expressive than just binds and returns, so maybe the current AST is not optimal because I guess it translates the applications to internal_plets?

@javra
Copy link
Collaborator Author

javra commented Jan 16, 2025

OTOH maybe that's a cosmetic thing that can be done afterwards

@bacam
Copy link
Contributor

bacam commented Jan 16, 2025

You could have a different version of that rewrite that leaves arguments like g x in place. Or maybe do something clever depending on what Lean's internal representation is.

@bacam bacam merged commit a1b339a into rems-project:sail2 Jan 16, 2025
3 checks passed
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.

4 participants