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 experimental lazy interpreter as a plugin #453

Merged
merged 2 commits into from
Apr 18, 2023

Conversation

AltGr
Copy link
Contributor

@AltGr AltGr commented Apr 14, 2023

To try it (without installing Catala):

$ make plugins
$ export CATALA_PLUGINS=_build/default/compiler/plugins
$ dune exec -- catala lazy
examples/aides_logement/tests/tests_calcul_apl_locatif.catala_fr -s
Exemple2

Keep in mind that this is a work-in-progress prototype :)

previously only lcalc and scalc where available
To try it (without installing Catala):
```shell-session
$ make plugins
$ export CATALA_PLUGINS=_build/default/compiler/plugins
$ dune exec -- catala lazy examples/aides_logement/tests/tests_calcul_apl_locatif.catala_fr -s Exemple2
```

Keep in mind that this is a work-in-progress prototype :)
@AltGr AltGr marked this pull request as draft April 14, 2023 15:04
@AltGr
Copy link
Contributor Author

AltGr commented Apr 14, 2023

Example output (very redundant since this is with lazy evaluation actually turned off and re-played each time):
image

EDIT: note that this was obtained by commenting out the effect on this line.
The issue otherwise is that we only keep the "most reduced" version of variables, so e.g. a condition on a variable may force its reduction and make the original expression disappear in the printed result.

@AltGr AltGr marked this pull request as ready for review April 17, 2023 09:36
@AltGr
Copy link
Contributor Author

AltGr commented Apr 17, 2023

I had marked this as draft, but while this correctly describes the state of the interpreter, it's a plugin so it may be OK to merge as-is so that everyone can experiment with it. The changes to the main tree are minimal:

  • small changes to the plugin API
  • added support for dcalc plugins (this is the first patch)
  • exposed a couple functions in .mlis

Copy link
Contributor

@denismerigoux denismerigoux left a comment

Choose a reason for hiding this comment

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

I'm okay with merging this! This is so cool, I hope you'll have time to get back to this and comment your code to show why it works from a more theoretical perspective.

module Env = struct
type 'm t =
| Env of
((dcalc, 'm mark) gexpr, ((dcalc, 'm mark) gexpr * 'm t) ref) Var.Map.t
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious, could you put a comment describing what you're putting in the Env.t ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will! Until then: an env maps a variable to an expression and the corresponding captured env.
I don't want to think about how much more complicated that would have been in an imperative setting :D

The ref part is only used for laziness: the raw expression is first added to the environment, then we can replace it once computed. We don't use lazy because we want more control (in particular, we can reduce the same expression several times with different levels)

let e, env1 = !v_env in
let r, env1 = lazy_eval ctx env1 llevel e in
if not (Expr.equal e r) then (
log "@[<hv 2>{{%a =@ [%a]@ ==> [%a]}}@]" Print.var_debug v
Copy link
Contributor

Choose a reason for hiding this comment

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

When will this log be shown? With the -d flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now you have to replace the ifprintf with fprintf in the definition above 😁
Of course it won't stay like that ; the issue with depending upon -d is that that would also change the value output, making it more verbose, we definitely need specific debug flags.

@AltGr AltGr merged commit bd870b0 into CatalaLang:master Apr 18, 2023
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.

2 participants