Title: Integer generics · Issue #3345 · python/mypy · GitHub
Open Graph Title: Integer generics · Issue #3345 · python/mypy
X Title: Integer generics · Issue #3345 · python/mypy
Description: Dependent types have been brought up before in a few places: #366, this python-ideas thread, and most recently, #3062. The latter limits itself to one specific feature of a dependent type system, namely, support for simple literal types....
Open Graph Description: Dependent types have been brought up before in a few places: #366, this python-ideas thread, and most recently, #3062. The latter limits itself to one specific feature of a dependent type system, n...
X Description: Dependent types have been brought up before in a few places: #366, this python-ideas thread, and most recently, #3062. The latter limits itself to one specific feature of a dependent type system, n...
Opengraph URL: https://github.com/python/mypy/issues/3345
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Integer generics","articleBody":"Dependent types have been brought up before in a few places: #366, [this python-ideas thread](https://mail.python.org/pipermail/python-ideas/2014-August/028853.html), and most recently, #3062. The latter limits itself to one specific feature of a dependent type system, namely, support for simple literal types.\r\n\r\nAnother limited feature which could prove helpful is support for _integer generics_ —that is, the ability to parametrize types by integers as well as other types. For example, the length of a homogeneous tuple could be parametrized:\r\n\r\n```python\r\n# Currently supported:\r\nTuple[int, int, int] # A tuple of 3 ints\r\nTuple[int, ...] # A tuple of any number of ints\r\n\r\n# Proposed:\r\nTuple[int, 256] # A tuple of 256 ints\r\n```\r\n\r\nOne real-world use-case for this came up in python/typing#221:\r\n```python\r\n# Currently supported:\r\nSboxType = Tuple[\r\n Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],\r\n Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],\r\n Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],\r\n Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],\r\n Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],\r\n Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],\r\n Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],\r\n Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],\r\n]\r\n\r\n# Proposed:\r\nSboxType = Tuple[Tuple[int, 16], 8] # A tuple of 8 tuples of 16 ints each\r\n```\r\n\r\nInteger genericity could also be used for things like statically checking vector or matrix dimensions:\r\n```python\r\nN = TypeVar('N', numeric=True) # one idea for denoting type-level integers\r\n\r\nclass Vector(Generic[N]):\r\n def __init__(self, values: Iterable[float, N]) -> None:\r\n self.value = tuple(values)\r\n\r\n def __add__(self, other: Vector[N]) -> Vector[N]:\r\n return Vector(a + b for a, b in zip(self.values, other.values))\r\n\r\n# ...more code...\r\n\r\ndef matmul(a: Matrix[N, M], b: Matrix[M, D]) -> Matrix[N, D]: ...\r\n```\r\n\r\nNote that the Rust community has been discussing this for some time (as well as the more general _const generics_) and has a lot of material collected on the motivation, challenges, and details of such a feature:\r\n* [Pre-RFC: Integer Templating](https://internals.rust-lang.org/t/pre-rfc-integer-templating/2974)\r\n* rust-lang/rfcs#1038: Parameterize types over numerics / type level integers\r\n* rust-lang/rfcs#1931: RFC: Constant generics (restricted Π types) for Rust, core RFC (v2)\r\n* [[lang-team-minutes] Const generics](https://internals.rust-lang.org/t/lang-team-minutes-const-generics/5090)\r\n\r\nWhether or not it's a good fit for Python is an open question, of course, but I figured I should at least bring it up for discussion! (Especially since it could interact with #3062 in interesting ways.)\r\n\r\n---\r\n\r\n_(Even nicer would be to support simple type-level expressions so we could do things like this..._\r\n```python\r\n# Vector concatenation (relevant: http://stackoverflow.com/q/43292197/1974654)\r\ndef concat(a: Vector[N], b: Vector[M]) -> Vector[N+M]: ...\r\n\r\n# The `zip` builtin (based on the typeshed stub)\r\n@overload\r\ndef zip(iter1: Iterable[_T1, N1],\r\n iter2: Iterable[_T2, N2]) -> Iterator[Tuple[_T1, _T2], min(N1, N2)]: ...\r\n\r\n# HDL-like bit vector operations\r\ndef add(a: BitVec[N], b: BitVec[M]) -> BitVec[max(N, M) + 1]: ...\r\n```\r\n_...but that may be taking it too far. To avoid running arbitrary user code the expression language would have to be explicitly defined and simple enough to implement in languages other than Python; it may be feasible to support something like +, -, *, //, %, **, max(), min(), and abs(), though.)_","author":{"url":"https://github.com/msegado","@type":"Person","name":"msegado"},"datePublished":"2017-05-09T19:41:19Z","url":"https://github.com/python/mypy/issues/3345"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:5bd35a47-c0ae-cef5-f97f-b8ff2a98032a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D962:1FBEEF:140F490:1A27E54:6B000686 |
| html-safe-nonce | 3a03d9edf1d2e3a7f4f8ae95cbd1cec67416c3fe12790b8b7b077410ecc9c2e2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOTYyOjFGQkVFRjoxNDBGNDkwOjFBMjdFNTQ6NkIwMDA2ODYiLCJ2aXNpdG9yX2lkIjoiNjA5MjUzMTg3NzA2MTU5MjcxMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 47ef2cee7f6ac0c3506e24da77c7dd303aaffdc6a3dfd8236154bb2b587b6c01 |
| hovercard-subject-tag | issue:227475500 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/3345/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b590726c6cf2c2356209f47b1d1c50c991d5b11245cc3fdf9664be656afa15ee/python/mypy/issues/3345 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b590726c6cf2c2356209f47b1d1c50c991d5b11245cc3fdf9664be656afa15ee/python/mypy/issues/3345 |
| og:image:alt | Dependent types have been brought up before in a few places: #366, this python-ideas thread, and most recently, #3062. The latter limits itself to one specific feature of a dependent type system, n... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | msegado |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width