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

Require types to start with an uppercase letter #1583

Merged
merged 28 commits into from
May 21, 2024
Merged

Conversation

byorgey
Copy link
Member

@byorgey byorgey commented Oct 17, 2023

Closes #1547 , which was caused by misspelled/nonexistent types being interpreted as type variables. In #1550 I proposed one solution to the problem, namely, to stop implicitly quantifying types and require explicit forall on any polymorphic type. This PR represents an alternative solution: to keep implicit quantification but require all types to start with an uppercase letter, as in Haskell. I think I like this one better but I'm open to feedback.

Specifically, with this PR:

  • All built-in type constructors (Int, Cmd, Unit, etc.) must start with a capital letter
  • Type variables:
    • Must start with a lowercase letter or underscore
    • May not be named a lowercase version of a type constructor, e.g. int
      • This is important so that old code with lowercase types is not silently accepted by parsing the former types as implicitly quantified type variables; even in cases where old code no longer typechecks, this will give better error messages.
  • Term variables:
    • May start with upper- or lowercase
    • May be named lowercase versions of type names, e.g. let f : Int -> Int = \int. int + 1 is fine

This PR obviously represents a bigger breaking change. Once we merge this we might consider adding an option to swarm format to be able to parse old code with lowercase types and then reformat it with uppercase, to aid in porting code.

Once this is merged I will also regenerate the wiki pages that mention types.

Closes #1550.

@byorgey byorgey requested review from kostmo and xsebek October 17, 2023 14:44
Copy link
Member

@xsebek xsebek left a comment

Choose a reason for hiding this comment

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

The types look more readable to me 👍

Just to check, uppercase is parsed as a concrete type or fails if it does not exist, right? Apologies if I overlooked a test for it.

@byorgey
Copy link
Member Author

byorgey commented Oct 18, 2023

Just to check, uppercase is parsed as a concrete type or fails if it does not exist, right? Apologies if I overlooked a test for it.

That is correct. Wait, I think it doesn't actually do that (but it should)! I will add some more tests, and fix that behavior if I got it wrong.

@xsebek
Copy link
Member

xsebek commented Oct 18, 2023

Btw. I think that if you showed data from Swarm 0.4 and read and printed them by 0.5, that would work as that tool.

It could look like:

swarm-0.4 format --out=Haskell old_code.sw | swarm-0.5 format --in=Haskell --stdin

@byorgey
Copy link
Member Author

byorgey commented Oct 24, 2023

Btw. I think that if you showed data from Swarm 0.4 and read and printed them by 0.5, that would work as that tool.

That's true, but (1) that would require adding --out and --in flags, (2) it would be kind of annoying to tell users that in order to upgrade they needed to keep around an old binary instead of just replacing that old binary with the newer one, as usual.

Edit: actually, the big problem right now is that currently none of these approaches would preserve comments; see #1467 .

Edit 2: since I personally would really like a tool to do the type case conversion automatically on all my .sw files, I have started working on #1467, and from there I can make format work as the upgrade tool I envision, with some kind of --compat flag perhaps. Hopefully it won't be terribly hard to get something reasonable working. My initial attempts are in the branch feature/preserve-comments.

@byorgey
Copy link
Member Author

byorgey commented May 15, 2024

@kostmo I've resurrected this PR. When you get a chance, take a look and let me know what you think.

I was going to give swarm format the ability to convert code from the old format to the new. I may still do that but I don't think it has to be in this PR.

src/swarm-lang/Swarm/Language/Parser/Lex.hs Outdated Show resolved Hide resolved
Co-authored-by: Restyled.io <commits@restyled.io>
@byorgey byorgey added the merge me Trigger the merge process of the Pull request. label May 21, 2024
@mergify mergify bot merged commit 1a4dcd8 into main May 21, 2024
13 checks passed
@mergify mergify bot deleted the feature/uppercase-types branch May 21, 2024 04:17
mergify bot pushed a commit that referenced this pull request May 22, 2024
This is a followup on top of #1583 which turns `swarm format` into a tool for porting existing Swarm code into the newest syntax, via an extra `--v0.5` argument.  In particular, this PR:

- Generalizes the parser to take a configuration record, which among other things contains the language version being parsed.
- Adds code to allow the parser to parse either the current syntax or one version ago (when types did not start with capital letter) depending on the version in the configuration.
    - The idea is to have the parser always support the current version and one older version, so we can always upgrade version n to version n+1.
- Adds a new flag `--v0.5` to the `format` subcommand which causes the input to be parsed in v0.5 mode.  However, the output of `format` will always use the latest syntax.  Thus, `swarm format --v0.5` reads code in v0.5 format and prints it in the latest format, so this can be used to automatically port existing `.sw` files.

This PR also makes a few minor improvements to pretty-printing.
@byorgey byorgey added the CHANGELOG Once merged, this PR should be highlighted in the changelog for the next release. label Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CHANGELOG Once merged, this PR should be highlighted in the changelog for the next release. merge me Trigger the merge process of the Pull request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

path type mentions entity which is not a real type
3 participants