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

Numeric literal semantics #144

Merged
merged 14 commits into from
Sep 24, 2021
Merged

Numeric literal semantics #144

merged 14 commits into from
Sep 24, 2021

Conversation

zygoloid
Copy link
Contributor

@zygoloid zygoloid commented Aug 14, 2020

This proposal provides semantics for numeric literals.

  • Numeric literals have a type derived from their value, and can be converted to any type that can represent that value.

  • Simple operations such as arithmetic that involve only literals also produce values of literal types.

  • Literals implicitly convert to types that can represent them.

  • The Carbon prelude provides:

    • An arbitrary-precision integer type BigInt.
    • A rational number type Rational(T:! Type) with constraints on T not yet determined.
    • A family of integer literal types, IntLiteral(N:! BigInt).
    • A family of real literal types, RealLiteral(N:! Rational(BigInt)).

Examples:

42 has type IntLiteral(42 as BigInt).
5.2 has type RealLiteral(5.2 as Rational(BigInt)).

// This is OK: the initializer is of the integer literal type with value
// -2147483648 despite being written as a unary `-` applied to a literal.
var x: i32 = -2147483648;

// This initializes y to 2^60.
var y: i64 = 1 << 60;

// This forms a rational literal whose value is one third, and converts it to
// the nearest representable value of type `f64`.
var z: f64 = 1.0 / 3.0;

// This is an error: 300 cannot be represented in type `i8`.
var c: i8 = 300;

@zygoloid zygoloid added WIP proposal A proposal labels Aug 14, 2020
@googlebot googlebot added the cla: yes PR meets CLA requirements according to bot. label Aug 14, 2020
proposals/p0144.md Outdated Show resolved Hide resolved
proposals/p0144.md Outdated Show resolved Hide resolved
proposals/p0144.md Outdated Show resolved Hide resolved
proposals/p0144.md Show resolved Hide resolved
proposals/p0144.md Outdated Show resolved Hide resolved
proposals/p0144.md Outdated Show resolved Hide resolved
proposals/p0144.md Show resolved Hide resolved
proposals/p0144.md Outdated Show resolved Hide resolved
proposals/p0144.md Outdated Show resolved Hide resolved
proposals/p0144.md Outdated Show resolved Hide resolved
proposals/p0144.md Outdated Show resolved Hide resolved
Copy link

@davidstone davidstone left a comment

Choose a reason for hiding this comment

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

I have more general thoughts, but I need to gather them and figure out exactly what to say. I'll probably do some discussing on Discord first and then post more comments here. Here are my thoughts so far:

My general direction is that I would want to consider fitting integer literals into a broader plan for integers in general. Some of the decisions we can make with how to handle integers makes some of these choices a bit more "obvious".

I have written a C++ integer library: https://bitbucket.org/davidstone/bounded_integer/ where the general idea is that users specify min and max values that the integer type can use (bounded::integer<0, 10> holds values between 0 and 10 inclusive). If we decide to go in that direction for integers, the "obvious" answer (but not the only answer) to integer literals is that they would be of type Integer<N, N> (for which my library provides an alias template bounded::constant_t<N> for types and bounded::constant<N> for values). These are unit types with no storage, much as outlined in this proposal.

proposals/p0144.md Outdated Show resolved Hide resolved
@jonmeow jonmeow removed the WIP label Apr 20, 2021
@jonmeow jonmeow marked this pull request as draft April 20, 2021 16:19
@github-actions
Copy link

We triage inactive PRs and issues in order to make it easier to find active work. If this PR should remain active, please comment or remove the inactive label.
This PR is labeled inactive because the last activity was over 90 days ago. This PR will be closed and archived after 14 additional days without activity.

@github-actions github-actions bot added the inactive Issues and PRs which have been inactive for at least 90 days. label Jul 31, 2021
@github-actions
Copy link

We triage inactive PRs and issues in order to make it easier to find active work. If this PR should remain active or becomes active again, please reopen it.
This PR was closed and archived because there has been no new activity in the 14 days since the inactive label was added.

@github-actions github-actions bot closed this Aug 15, 2021
@zygoloid zygoloid reopened this Sep 17, 2021
@zygoloid zygoloid removed the inactive Issues and PRs which have been inactive for at least 90 days. label Sep 17, 2021
@zygoloid zygoloid marked this pull request as ready for review September 17, 2021 23:29
@zygoloid zygoloid requested a review from a team as a code owner September 17, 2021 23:29
proposals/p0144.md Outdated Show resolved Hide resolved
proposals/p0144.md Show resolved Hide resolved
@github-actions github-actions bot added the proposal rfc Proposal with request-for-comment sent out label Sep 18, 2021
represent all rationals, not only dyadic and decadic rationals.

For example, we need to be able to represent 1.0 / 3.0 exactly.
@zygoloid zygoloid requested a review from a team as a code owner September 22, 2021 00:28
Copy link
Contributor

@chandlerc chandlerc left a comment

Choose a reason for hiding this comment

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

I'm generally happy with this direction. Mostly waiting for folks who've commented to have a chance to resolve comments based on updates to the document.

proposals/p0144.md Outdated Show resolved Hide resolved
proposals/p0144.md Outdated Show resolved Hide resolved
proposals/p0144.md Outdated Show resolved Hide resolved
Copy link
Contributor

@jonmeow jonmeow left a comment

Choose a reason for hiding this comment

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

A couple more comments

(note, I think the - comment from before may be obsolete under the current doc)

proposals/p0144.md Outdated Show resolved Hide resolved
proposals/p0144.md Outdated Show resolved Hide resolved
zygoloid and others added 4 commits September 22, 2021 11:59
…of a Carbon.Rational.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
Copy link
Contributor Author

@zygoloid zygoloid left a comment

Choose a reason for hiding this comment

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

I believe all review comments have been addressed.

proposals/p0144.md Outdated Show resolved Hide resolved
proposals/p0144.md Show resolved Hide resolved
adding an AnyIntLiteral interface, clarify that two listed disadvantages
are covering two different possibilities.
Copy link
Contributor

@chandlerc chandlerc left a comment

Choose a reason for hiding this comment

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

LGTM!

@github-actions github-actions bot added proposal accepted Decision made, proposal accepted and removed proposal rfc Proposal with request-for-comment sent out labels Sep 23, 2021
@zygoloid zygoloid merged commit 87680f1 into carbon-language:trunk Sep 24, 2021
@zygoloid zygoloid deleted the proposal-numeric-literal-sema branch March 11, 2022 01:02
chandlerc added a commit that referenced this pull request Jun 28, 2022
This proposal provides semantics for numeric literals.

* Numeric literals have a type derived from their value, and can be converted to any type that can represent that value.

* Simple operations such as arithmetic that involve only literals also produce values of literal types.

* Literals implicitly convert to types that can represent them.

* The Carbon prelude provides:
    * An arbitrary-precision integer type `BigInt`.
    * A rational number type `Rational(T:! Type)` with constraints on `T` not yet determined.
    * A family of integer literal types, `IntLiteral(N:! BigInt)`.
    * A family of real literal types, `RealLiteral(N:! Rational(BigInt))`.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
josh11b pushed a commit that referenced this pull request Sep 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes PR meets CLA requirements according to bot. proposal accepted Decision made, proposal accepted proposal A proposal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants