stan-dev / stanc3

The Stan transpiler (from Stan to C++ and beyond).
BSD 3-Clause "New" or "Revised" License
138 stars 44 forks source link

Experimental support for unicode identifiers. #1407

Open WardBrian opened 4 months ago

WardBrian commented 4 months ago

I know for a fact that this requires a few changes in stan-dev/stan's json data handler to recognize unicode names, which is just one of several reasons this is a draft.

The basic overview: OCaml strings should be treated mostly like arrays of bytes, and ocamllex handles inputs as sets of bytes. We can define rules that recognize UTF-8-compatible bytes, and then do validation on them after the fact based on the the Unicode Annex 31: Unicode Identifiers standard.

We then pretend for most of the compiler like it's just bytes, which is fine, because we never do things like subslice variable names.

Finally, at output time, we already had string escaping (since #952), so most of the code-gen works fine. Recent C++ standards require that compilers support UTF-8 names based on the same UAX31 rules linked above, but older ones may not. For now I've got it generating "Universal character names" which seem like the legacy version of this, which hopefully means older compilers will be happy with it.

Submission Checklist

Release notes

stanc3 can now accept a flag --allow-unicode which enables the use of non-ascii characters in Stan files. All files are expected to be encoded in UTF-8. This is experimental and may not work with older C++ compilers.

Copyright and Licensing

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the BSD 3-clause license (https://opensource.org/licenses/BSD-3-Clause)

codecov[bot] commented 4 months ago

Codecov Report

Attention: Patch coverage is 73.75000% with 21 lines in your changes are missing coverage. Please review.

Project coverage is 89.76%. Comparing base (8bc6ba0) to head (49381b7). Report is 3 commits behind head on master.

:exclamation: Current head 49381b7 differs from pull request most recent head 450cad4. Consider uploading reports for the commit 450cad4 to get more accurate results

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1407 +/- ## ========================================== - Coverage 89.87% 89.76% -0.12% ========================================== Files 63 65 +2 Lines 10525 10597 +72 ========================================== + Hits 9459 9512 +53 - Misses 1066 1085 +19 ``` | [Files](https://app.codecov.io/gh/stan-dev/stanc3/pull/1407?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev) | Coverage Δ | | |---|---|---| | [src/frontend/Errors.ml](https://app.codecov.io/gh/stan-dev/stanc3/pull/1407?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev#diff-c3JjL2Zyb250ZW5kL0Vycm9ycy5tbA==) | `88.00% <100.00%> (ø)` | | | [src/stan\_math\_backend/Cpp.ml](https://app.codecov.io/gh/stan-dev/stanc3/pull/1407?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev#diff-c3JjL3N0YW5fbWF0aF9iYWNrZW5kL0NwcC5tbA==) | `88.91% <100.00%> (+0.11%)` | :arrow_up: | | [src/stan\_math\_backend/Cpp\_Json.ml](https://app.codecov.io/gh/stan-dev/stanc3/pull/1407?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev#diff-c3JjL3N0YW5fbWF0aF9iYWNrZW5kL0NwcF9Kc29uLm1s) | `100.00% <100.00%> (ø)` | | | [src/stanc/stanc.ml](https://app.codecov.io/gh/stan-dev/stanc3/pull/1407?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev#diff-c3JjL3N0YW5jL3N0YW5jLm1s) | `82.14% <ø> (ø)` | | | [src/frontend/Identifiers.ml](https://app.codecov.io/gh/stan-dev/stanc3/pull/1407?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev#diff-c3JjL2Zyb250ZW5kL0lkZW50aWZpZXJzLm1s) | `91.66% <91.66%> (ø)` | | | [src/common/Unicode.ml](https://app.codecov.io/gh/stan-dev/stanc3/pull/1407?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev#diff-c3JjL2NvbW1vbi9Vbmljb2RlLm1s) | `55.81% <55.81%> (ø)` | | ... and [1 file with indirect coverage changes](https://app.codecov.io/gh/stan-dev/stanc3/pull/1407/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=stan-dev)
bob-carpenter commented 4 months ago

Isn't there sub slicing when we peel off _lpdf suffixes in sampling statements?

WardBrian commented 4 months ago

Isn't there sub slicing when we peel off _lpdf suffixes in sampling statements?

  1. Not quite, since we generally go from something without a suffix (Y ~ foo(...)) to the thing with the suffix (target += foo_lpdf(Y | ...)), so even there it is a concatenation problem more than a subslicing.
  2. Even in situations where we do need to do it, I think that should be fine, since the cut point is happening somewhere we require ASCII characters to appear. So even a θ_lpdf, the cut point in the string of bytes would still just be 5 bytes from the end

But, that is definitely one area of this that would need much more testing before it could be merged.