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

Turn bool into a numeric type? #264

Open
mark-koch opened this issue Jun 24, 2024 · 1 comment
Open

Turn bool into a numeric type? #264

mark-koch opened this issue Jun 24, 2024 · 1 comment
Labels
enhancement New feature or request question Further information is requested

Comments

@mark-koch
Copy link
Collaborator

Should we turn bool into a numeric type to match Python's behaviour? This means all of the following equalities would hold:

True == 1
False == 0
True + True == 2
True - False == 1
2 * False == 0
...

See #263 for a PR that implements this.

Personally, I'd be in favour to increase consistency with Python. It also allows some nice patterns like

results = [measure(q) for q in qs]
parity = sum(results) % 2

What do you think? @croyzor @qartik @ss2165

@mark-koch mark-koch added enhancement New feature or request question Further information is requested labels Jun 24, 2024
@qartik
Copy link
Member

qartik commented Jun 24, 2024

If we are considering guppy as a strongly typed language, I think bool should not be implicitly treated as a numeric type. We could certainly provide casting/conversion functions for explicit coercion, if they don't exist already.

Originally posted by @qartik in #225 (comment)

Type-theoretically speaking, I wouldn't want to allow the equalities in the OP.

The pattern shown is very easy and way more explicit with a coercion function:

- results = [measure(q) for q in qs]
+ results = [int(measure(q)) for q in qs]
  parity = sum(results) % 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants