-
Notifications
You must be signed in to change notification settings - Fork 123
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
Typing differences for decimal and other literals is unintuitive #744
Comments
There is a shortcut (not that it gets at your bigger issue). Try `0x10 : [16] |
Yeah, literals are quite tricky. This might not be terribly well explained in the book, and we should probably correct it, but your statement about the types of the literals is not quite accurate. In particular, the rule is:
So decimal literals are not restricted to Also the type of the other literals is determined by the number of digits in them, not the least number of bits you'd need to represent them. So |
People have been arguing about this design for a long time (see #30). Another related awkwardness about our fixed-size typing rule for hex/octal/binary literals is that Cryptol's pretty printer doesn't respect it: For example, On the other hand, sometimes you do want your literals to have a fixed size, and it's nice not to have to put type annotations on them. At one point I suggested that we could adopt verilog's syntax for literals, which include an optional explicit bit-width, but nobody else seemed to be enthusiastic about the idea. |
I wonder if we could thread this needle with a notion of "preferred defaulting" for literals. The idea is, all literals are polymorphic (like decimal literals are now); if the type is forced by context, we just unify and try to solve the corresponding |
@Launchbury Would you be willing to share your thoughts on the history of this? |
The history of this is like this: In the beginning, all literals were overloaded. As a result people would end up writing much more polymorphic programs than they intended, which would result in the various defaulting warnings and spurious type annotations. To solve this, we decided to make literals monomorphic, and for sizes that are powers of 2 there is a the natural literal width, which is pretty standard when you work with bits (e.g, Decimal literals, OTOH, remained overloaded because there was no natural type for them to use. This was in the pre |
Hey, this is my first bit of feedback on Cryptol and the Cryptol book as I've been working through it. Expect more tickets to stream in over the next week or two. I'm not necessarily suggesting these things should change, just pointing out little snags I came across.
It is unintuitive that decimal literals have type
Integer
while hex literals are of a fixed width numeric type based on the smallest number of bits they could fit into. This also applies to octal and binary literals.This breaks an assumption I have that for all numerals
n1
,n2
, and basesb1
,b2
, ifn1
is a representation of a numberx
in baseb1
, andn2
is a representation ofx
in baseb2
, thenn1 == n2
.This is not itself terribly strange, but it leads to some confusing situations. For example,
16 : [16]
is legal, but0x10 : [16]
is not. In order to get a hexidecimal representation of 16 stored as a 16-bit number, I must instead write0x0010
. Most other languages would implicitly pad out0x10
to0x0010
.The text was updated successfully, but these errors were encountered: