Skip to content

Test failed - added support for int and float#1086

Draft
ronnuriel wants to merge 7 commits intolcompilers:mainfrom
ronnuriel:ron/support_float_int
Draft

Test failed - added support for int and float#1086
ronnuriel wants to merge 7 commits intolcompilers:mainfrom
ronnuriel:ron/support_float_int

Conversation

@ronnuriel
Copy link
Copy Markdown
Collaborator

Signed-off-by: Ron Nuriel rnuriel@gsitechnology.com

@czgdp1807 czgdp1807 force-pushed the ron/support_float_int branch from bab8f5e to 7d5fc04 Compare September 7, 2022 04:14
Comment on lines +12 to +15
def accept_int_array(xint: int[:]) -> i64:
xint[0] = 64
return xint[0]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This works with LPython but not with CPython. CPython throws the following error,

Traceback (most recent call last):
  File "/Users/czgdp1807/lpython_project/lpython/integration_tests/array_01_decl.py", line 12, in <module>
    def accept_int_array(xint: int[:]) -> i64:
TypeError: 'type' object is not subscriptable

So I pushed 8336f96. Using py_float, py_int seems a bit hackish but then the alternative is to not support x: float[:], x: int[:], which makes reduces the significance of x: float use case. Like if users can only declare normal variables and not arrays using floating point annotations then its not worth it to support float annotation. What do you say @certik ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is there any other way to achieve this (i.e., make the float[:] work with CPython)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not something like Array[int]? int[:] might not be in the spirit of PEP 483.

I general, we should never have something that works in LPython and not in CPython. That's an extension of Python and is a road to perdition! The reverse is ok IMO.

Copy link
Copy Markdown
Collaborator

@czgdp1807 czgdp1807 Sep 7, 2022

Choose a reason for hiding this comment

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

Array[int] makes sense. For multi-dimensional arrays, should it look like, Array[Array[int]]? Plus then we should also support Array[Array[f32]] (similarly for all the ltypes).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a more complex issue, so I created #1107 where we can discuss it more. In short: Array[int, :, :] can probably be done, but it's quite verbose, so we should first decide if we want to go this route, in addition (or even instead!) of the short i64[:,:] syntax. Also note that int in CPython means arbitrary precision integer, so we should not use it interchangeably with i64. However, float in CPython means f64, so that could be done, for scalars. For arrays of f64, the same issue happens. See #1107 for more discussion.

@ronnuriel ronnuriel self-assigned this Sep 7, 2022
type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc,
4, dims.p, dims.size()));
} else if (var_annotation == "i64") {
} else if (var_annotation == "i64" || var_annotation == "int") {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This we should not do, since int means arbitrary precision integer, which we will eventually support in LPython as well, but that is not the same as f64.

Comment thread tests/dict_expr_01.py
@@ -0,0 +1,4 @@
def test_dict():
mydictionarty3: dict[int, float] = {}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is currently not supported in LPython, as we do not allow arbitrary precision integers.

@certik
Copy link
Copy Markdown
Contributor

certik commented Sep 9, 2022

To move forward, I would not allow int to mean i64, since we will eventually support arbitrary precision integers, and int can be reserved for that, since that is what it means in CPython.

We could allow float to mean f64, since that is what it means in CPython, but as discussed in #1107, we use the nice syntax f64[:,:] to mean multidimensional arrays, but that is not possible with float. So given this, I would lean towards not allowing float at all, to reduce any kind of confusion why float[:,:] does not work. In #1107 I list a few workarounds that we could do, but none of these are ideal.

Instead, why don't we do the following: if a user writes float, LPython will give a compiler error and suggest to use f64 instead. For int, the compiler should say that arbitrary precision ints are not implemented yet, but you can use i64 or i32 instead.

@czgdp1807 czgdp1807 added the could close Issues/PRs which can be closed. label Oct 5, 2022
@certik certik marked this pull request as draft October 5, 2022 11:39
@certik
Copy link
Copy Markdown
Contributor

certik commented Oct 5, 2022

I am marking this as Draft for now. If this becomes ready, just click on "Ready for review".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

could close Issues/PRs which can be closed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants