au-ts / cogent

Cogent Project
https://trustworthy.systems/projects/TS/cogent.pml
Other
158 stars 26 forks source link

Preserve Type Synonyms in Shallow Embedding #407

Closed gteege closed 2 years ago

gteege commented 2 years ago

Up to now the type synonyms defined in Cogent have been unfolded at the end of the typecheck stage. When generating the shallow embedding, monomorphic type synonyms have been re-introduced by matching the replacements. Pull requests #390 and #395 extended this to polymorphic types. However, this may yield surprising results, since several type synonyms may match a type expression, and an arbitrary one is selected.

Therefore, this pull request replaces the re-introduction of type synonyms by preserving the original type synonyms for the shallow embedding generation. The advantages are:

The implementation works as follows: The type synonyms are not unfolded at the end of the typecheck stage, instead, they are on-demand unfolded in the desugaring stage, whenever necessary. At the beginning of the normalisation stage they are permanently unfolded, so the remaining stages are not affected. The shallow embedding generated in the desugaring stage uses the preserved type synonyms, the shallow embedding generated in later stages has type synonyms unfolded. As a drawback, developers have to keep in mind to on-demand unfold type synonyms in the desugaring stage.

Some remarks about details:

All compiler regression tests have been passed. For BilbyFS the shallow embedding is successfully created and the generated C code is identical.

gteege commented 2 years ago

I am aware of the failed checks and will fix them, so please be patient...

gteege commented 2 years ago

Ok, now it works (the failed checks now are due to a misconfiguration in examples/buffer and are not related to this pull-request, all other tests were successful). Two further remarks:

gteege commented 2 years ago

I just added a small improvement. Now in the shallow embedding record and variant types with name nnn are matched back to using the type synonym nnn (which is printed as nnn\<^sub>T). This makes inferred type annotations more readable because it is closer to the original Cogent code.

zilinc commented 2 years ago

Merged, with some minor fixes. Thanks for the PR.