Skip to content

Desugar loops#191

Merged
nomeata merged 30 commits intomasterfrom
desugar-loops
Mar 11, 2019
Merged

Desugar loops#191
nomeata merged 30 commits intomasterfrom
desugar-loops

Conversation

@matthewhammer
Copy link
Contributor

In the Desugar module, transform each for loop, while loop and loop ... while loop into an equivalent unconditional loop form, with compensating labels and breaks.

@matthewhammer
Copy link
Contributor Author

I could use another pair of eyes on this now, or two: I can't see the cause of the current issue.

All the (non-dfinity) tests pass for me, save for this one (part of control.as):

// Triggers IR type error
  testLoopWhile() {
    label l loop {
      if true break l
      else continue l;
    } while (condition());
  };

In particular, I get an IR type error saying that () is not a subtype of Non. I'm a bit stumped. Other variations of this loop do not trigger this error; for example, these are each fine:

// Does not trigger IR type error
  testLoopWhile2() {
    loop { } while (false);
  };

  testLoopWhile3() {
    label l {
      loop { }
      while (false and true)
    };
  };

Strangely, when I remove the "extra block" in testLoopWhile3 above, I get a similar IR type error after my transformation, as in this example:

// Triggers IR type error
  testLoopWhile4() {
    label l loop {
    } while (true and false);
  };

I'm not exactly sure why yet.

I noticed that the parser does some pattern-matching on the case where a label immediately contains a loop; that seems related to me, but I can't yet see how. Maybe it's a red herring; I'm not yet sure.

@nomeata
Copy link
Contributor

nomeata commented Feb 27, 2019

None of the variations use break. Might that be the problem?

@nomeata
Copy link
Contributor

nomeata commented Feb 27, 2019

According to the stack trace Check_IR, fails while checking the type of the expression in LabelE:

    check_exp (add_lab env id.it t0) exp1;

The IR (dumped by asc) has three labels, but likely it is the inner one, the one that was already in the source:

                      (LabelE
                        continue l
                        ()
                        (IfE
                          (LitE (BoolLit true))
                          (BreakE l (TupE))
                          (BreakE continue l (TupE))
                        )
                      )

And indeed: It has a type () when the body contains two breaks, so never terminates, so returns Non.

Now I regret having asked for this to be merged into desugar (at least for debugging) ;-)

Just checking …The bug is not yet in master.

Hmm, Construct.labelE is wrong (didn’t we fix this before?); it should annotate the label with the type typ. But that does not fix this issue.

Construct.breakE and Construct.returnE as well, they annotate with type Non, as they don’t return.

Ok, there are a bunch of bugs in Construct. And one in Typing. That did id. Will create a separate PR for the fix to typing and check_ir, and push my other fixes to this branch.

@nomeata
Copy link
Contributor

nomeata commented Feb 27, 2019

Pushed to #193 (please review, so that we can merge this first) and also merged into this PR, so that you can continue your work.

@matthewhammer
Copy link
Contributor Author

Thank you @nomeata!

So, I'm a bit confused about the next steps to get this totally sorted. Are my transformations incorrect in some way, or are they merely using some (still) buggy Construct helpers, or both? (If you're not sure, that's okay, I'll investigate; just let me know what you suspect now).

Aside: I know that we want the OCaml source to stand in for a LaTeX document with inference rules (for stepping and typing judgements); I had a difficult time imagining the typing rules from looking at these cases. I submit that it may have been easier to have these bugs here because they were obscured by the (mostly minor) OCaml syntactical noise. Perhaps they would have been more apparent if we had some inference rules to stare at and compare against? Would it be worth writing these out sometime soon? The IR is fairly small, and it shouldn't take more than part of an afternoon. I think it would help me a lot in the future, and cement the IR's static semantics more in my mind.
(cc @crusso @rossberg).

@nomeata
Copy link
Contributor

nomeata commented Feb 27, 2019

Well, I sorted out one problem (and the fix is both in #193 and this branch is in master). I did not investigate the remaining problem.

Would it be worth writing these out sometime soon?

Not sure. There is too much risk of it going stale if they cannot be executed in some way. Check_ir isn’t too bad to read; if you want to learn the rules better by working with them, maybe invest time to make Check_ir even more declarative?

@matthewhammer
Copy link
Contributor Author

Sounds good. In a more ideal world, Check_ir would optionally produce a typing derivation, even if it's written in the current imperative style.

@nomeata
Copy link
Contributor

nomeata commented Feb 27, 2019

Sounds good. In a more ideal world, Check_ir would optionally produce a typing derivation, even if it's written in the current imperative style.

The AST is typed, so there it is a typing derivation of sorts. You can write a pretty-printer for it that includes all the annotations. Also see https://dfinity.atlassian.net/browse/AST-61

@matthewhammer
Copy link
Contributor Author

Cool. I added a comment there (on Jira) with my thoughts; I wish I could give you a direct link, but that's either missing as a Jira feature, or I haven't yet found it.

@nomeata
Copy link
Contributor

nomeata commented Feb 27, 2019

Just looked at the Jenkins output again. It seems there are merely IR dumps in the expected test output that need to be updated. Let me do that for you.

@nomeata
Copy link
Contributor

nomeata commented Feb 27, 2019

Done. I guess what is left to do now is to reap the benefit of the exercise and remove all those loop forms from ir.ml that are no longer needed, leaving only LoopE of exp. I’ll leave that enjoyable part to you :-)

src/construct.ml Outdated
expD (ifE exp2
(tupE [])
(breakE lab (tupE []) ty1)
ty1
Copy link
Contributor

Choose a reason for hiding this comment

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

Very minor nit: Can you just write Type.unit here? I find it easier to read if I don’t have to see what ty1 is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, will do.

@matthewhammer
Copy link
Contributor Author

Awesome; thanks again, @nomeata!

Shall we do that refactoring/simplification of Ir to another PR? I'd prefer that, if possible.

Assuming you agree, can we merge this now? (I think you have the ability to approve, right?)

@matthewhammer
Copy link
Contributor Author

I mean now, after fixing that nit. :)

Copy link
Contributor

@nomeata nomeata left a comment

Choose a reason for hiding this comment

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

Let’s remove these comments and if true statements, and then merge. The IR cleanup may happen separately, if you want.

(Also, I am experimenting with GithHub’s “suggested change” feature.)

nomeata and others added 4 commits February 28, 2019 08:09
Co-Authored-By: matthewhammer <matthew.hammer@gmail.com>
Co-Authored-By: matthewhammer <matthew.hammer@gmail.com>
Co-Authored-By: matthewhammer <matthew.hammer@gmail.com>
Co-Authored-By: matthewhammer <matthew.hammer@gmail.com>
Co-Authored-By: matthewhammer <matthew.hammer@gmail.com>
@matthewhammer
Copy link
Contributor Author

matthewhammer commented Feb 28, 2019

Oops: I forgot about that temp stuff; thanks!

Aside: That new feature (suggested changes) was somewhat nice. I clicked a button, and the change was made for me.

How hard was it to propose these changes? You didn't need to do the edits in your browser, did you?

@nomeata
Copy link
Contributor

nomeata commented Feb 28, 2019

How hard was it to propose these changes? You didn't need to do the edits in your browser, did you?

See https://help.github.com/en/articles/commenting-on-a-pull-request#adding-line-comments-to-a-pull-request point 6.

You can also apply the changes in a batch commit: https://help.github.com/en/articles/incorporating-feedback-in-your-pull-request (but less important if we squash before merging, or if we simply don’t care too much about the tidynes of master)

@nomeata
Copy link
Contributor

nomeata commented Feb 28, 2019

Look like master has changed, and you’ll have to merge master into this branch before you can merge the PR.

nomeata and others added 17 commits March 6, 2019 19:38
to make life easier for arithmetic on Word8/Word16 (#216)

(This is with GC disabled.)
Co-Authored-By: nomeata <mail@joachim-breitner.de>
* remove tab
* Update compile.ml
[FileCheck](https://llvm.org/docs/CommandGuide/FileCheck.html) is a tool
created by the LLVM folks to embed textual assertions about the output
code in test cases. This comment sets up our test suite so that we can
put `CHECK` directives into the test files, and `run.sh` will use
`FileCheck` to see if the resulting `.wat` file matchs the specs.

I am using this in one example to assert that the mutually recursive
functions in `mutrec2.as` are compiled to direct calls (and not
closures).

This requires you to have `FileCheck` in your PATH. The easiest way of
doing that is to run

    nix-env -i -f . -A filecheck
add newline for unix police
Extend testsuite to allow for FileCheck assertions
* clean result of `shift_right`s
* testcase
* In the makefile there is now 'parallel' which runs all the tests in parallel,
* and 'quick' which does the same, but without the `dvm` tests.
* regression tests (via `nix`) are now running the `quick` target (with unlimited parallelism)
* for testing also `dvm`, we run in `run-dfinity/` parallel with at most 8 jobs, as unlimited jobs cause problems

It should be checked why `-j` for the latter causes failure (some traps appear duplicated in the logs).
@nomeata nomeata merged commit f2f213c into master Mar 11, 2019
@nomeata nomeata deleted the desugar-loops branch March 11, 2019 14:02
dfinity-bot added a commit that referenced this pull request Jun 23, 2023
## Changelog for ic-hs:
Branch: master
Commits: [dfinity/ic-hs@52369fa1...f092a2d5](dfinity/ic-hs@52369fa...f092a2d)

* [`12590345`](dfinity/ic-hs@1259034) bump cachix/install-nix-action ([dfinity/ic-hs⁠#201](https://github.com/dfinity/ic-hs/issues/201))
* [`2ef5690c`](dfinity/ic-hs@2ef5690) Bump openssl from 0.10.48 to 0.10.55 in /httpbin-rs ([dfinity/ic-hs⁠#202](https://github.com/dfinity/ic-hs/issues/202))
* [`46af8266`](dfinity/ic-hs@46af826) sync httpbin implementation with ic repo ([dfinity/ic-hs⁠#193](https://github.com/dfinity/ic-hs/issues/193))
* [`1fd276eb`](dfinity/ic-hs@1fd276e) implement Call context removal transition ([dfinity/ic-hs⁠#191](https://github.com/dfinity/ic-hs/issues/191))
* [`11c00d2a`](dfinity/ic-hs@11c00d2) catch HTTP exception from HTTPS outcalls ([dfinity/ic-hs⁠#195](https://github.com/dfinity/ic-hs/issues/195))
* [`9eb5bf48`](dfinity/ic-hs@9eb5bf4) deleted call contexts prevent canister from stopping (again) ([dfinity/ic-hs⁠#190](https://github.com/dfinity/ic-hs/issues/190))
* [`f092a2d5`](dfinity/ic-hs@f092a2d) implement and test canister history ([dfinity/ic-hs⁠#198](https://github.com/dfinity/ic-hs/issues/198))
mergify bot pushed a commit that referenced this pull request Jun 23, 2023
## Changelog for ic-hs:
Branch: master
Commits: [dfinity/ic-hs@52369fa1...f092a2d5](dfinity/ic-hs@52369fa...f092a2d)

* [`12590345`](dfinity/ic-hs@1259034) bump cachix/install-nix-action ([dfinity/ic-hs⁠#201](https://github.com/dfinity/ic-hs/issues/201))
* [`2ef5690c`](dfinity/ic-hs@2ef5690) Bump openssl from 0.10.48 to 0.10.55 in /httpbin-rs ([dfinity/ic-hs⁠#202](https://github.com/dfinity/ic-hs/issues/202))
* [`46af8266`](dfinity/ic-hs@46af826) sync httpbin implementation with ic repo ([dfinity/ic-hs⁠#193](https://github.com/dfinity/ic-hs/issues/193))
* [`1fd276eb`](dfinity/ic-hs@1fd276e) implement Call context removal transition ([dfinity/ic-hs⁠#191](https://github.com/dfinity/ic-hs/issues/191))
* [`11c00d2a`](dfinity/ic-hs@11c00d2) catch HTTP exception from HTTPS outcalls ([dfinity/ic-hs⁠#195](https://github.com/dfinity/ic-hs/issues/195))
* [`9eb5bf48`](dfinity/ic-hs@9eb5bf4) deleted call contexts prevent canister from stopping (again) ([dfinity/ic-hs⁠#190](https://github.com/dfinity/ic-hs/issues/190))
* [`f092a2d5`](dfinity/ic-hs@f092a2d) implement and test canister history ([dfinity/ic-hs⁠#198](https://github.com/dfinity/ic-hs/issues/198))
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