-
Notifications
You must be signed in to change notification settings - Fork 146
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
BSV: Pretty-print module return types when possible #666
base: main
Are you sure you want to change the base?
Conversation
The BSV pretty-printer would print a function like this: ``` module [Module] helloWorld#(Module#(Empty) m)(Empty); let e <- m; endmodule ``` Without the `[Module]` bit, resulting in a function declaration that wouldn't typecheck. This patch makes a best-effort attempt to pretty-print this bit whenever possible. More specifically: * We check if the return type of a function is equal to `M ty`, where `M` is a type constructor like `Module`. If so, pretty-print `[M]`. * Otherwise, check if the return type is equal to `m ty`, where `m` is a type variable with a corresponding `IsModule#(m, c)` constraint. If so, pretty-print `[m]`. The `findModId` function is responsible for finding type variables like `m`. While investigating this issue, I noticed a bug in which `findModId` would drop the `IsModule#(m, c)` constraint in which `m` appears, which would cause the constraint not to be pretty-printed. I've fixed this bug as part of this patch. Fixes B-Lang-org#663.
Thanks! The
is equivalent to this BH code:
The Also, if the body of the module doesn't use
and it's equivalent to the above with explicit This code:
should probably be this:
Because the module type could be a constructor (or variable) partially applied to its other arguments, leaving just the interface argument. For example, see I also think there needs to be better coordination between the Anyway, I would restructure
Note that Does that seem reasonable? |
Is it? I tried this: module [m] mkMod (Empty);
endmodule But that fails to compile:
Granted, you don't need
This gives me pause, since I'm imagining a program like this:
If you dropped the
|
In BSV, you can use the
Someone could write it explicitly, as you've shown, and it would be good if the pretty-printer displayed that as correct code. But I'm suggesting that it's not your responsibility to get it perfect, if you want to just get working the part you need. If we have to cut a corner, I think it's ok for the example as you wrote to not parse back in. My original suggestion was to just not print bare variables (like However, I can think of a simpler idea: Detect when the module type variable is exactly the one that the BSV parser inserts implicitly, and only don't print in that case. IDs can carry properties (see However, this would only improve the printing of BSV code; pretty-printing BH code as BSV would still insert unnecessary variables and provisos. So I would probably favor one of the above approaches, such as to look for whether There's a package
Hm. I will have to think about this. Would it hurt to change the BSV parser to insert an Anyway, the rest of my statement still stands, which is that most users write no brackets and no |
Sorry for taking so long to respond to this.
Fair enough. My use case only concerns code that uses capital-
This is an interesting suggestion. Changing the type of |
The BSV pretty-printer would print a function like this:
Without the
[Module]
bit, resulting in a function declaration that wouldn't typecheck. This patch makes a best-effort attempt to pretty-print this bit whenever possible. More specifically:We check if the return type of a function is equal to
M ty
, whereM
is a type constructor likeModule
. If so, pretty-print[M]
.Otherwise, check if the return type is equal to
m ty
, wherem
is a type variable with a correspondingIsModule#(m, c)
constraint. If so, pretty-print[m]
.The
findModId
function is responsible for finding type variables likem
. While investigating this issue, I noticed a bug in whichfindModId
would drop theIsModule#(m, c)
constraint in whichm
appears, which would cause the constraint not to be pretty-printed. I've fixed this bug as part of this patch.Fixes #663.