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

Implement Set #91

Merged
merged 8 commits into from
Feb 1, 2022
Merged

Implement Set #91

merged 8 commits into from
Feb 1, 2022

Conversation

namannimmo10
Copy link
Collaborator

No description provided.

Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

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

We should start with extending our type system with Set and Dict, and then go from there. See above.

grammar/ASR.asdl Outdated Show resolved Hide resolved
src/lpython/semantics/python_ast_to_asr.cpp Outdated Show resolved Hide resolved
@namannimmo10
Copy link
Collaborator Author

ASR:

               test_Set [] [
               (=
                  (Var 2 a)
                  (ConstantSet [(ConstantInteger 1
                     (Integer 4 [])) (ConstantInteger 2
                     (Integer 4 [])) (ConstantInteger 3
                     (Integer 4 []))]
                     (Set
                        (Integer 4 []))) ())
               (=
                  (Var 2 a)
                  (ConstantSet [(ConstantInteger 2
                     (Integer 4 [])) (ConstantInteger 3
                     (Integer 4 [])) (ConstantInteger 4
                     (Integer 4 [])) (ConstantInteger 5
                     (Integer 4 [])) (ConstantInteger 5
                     (Integer 4 []))]
                     (Set
                        (Integer 4 []))) ())
               (=
                  (Var 2 a)
                  (ConstantSet [
                     (ConstantString "a"
                        (Character 1 1 () []))
                     (ConstantString "b"
                        (Character 1 1 () []))
                     (ConstantString "c"
                        (Character 1 1 () []))]
                     (Set
                        (Character 1 1 () []))) ())
               (=
                  (Var 2 b)
                  (ArrayRef 2 a [
                     (() (ConstantInteger 0
                        (Integer 4 [])) ())]
                     (Integer 4 []) ()) ())]

@namannimmo10
Copy link
Collaborator Author

However, the type of ConstantSet is not the type of the element (say i32), but rather a new type Set<i32>

Seems like this is true in the above-produced ASR.


a = {1, 2, 3}
a = {2, 3, 4, 5, 5}
a = {"a", "b", "c"}
Copy link
Contributor

Choose a reason for hiding this comment

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

All these should give an error message that "Set[i32] (or character) cannot be assigned to i32".

Then let's add tests like:

a: Set[i32]
a = {1, 2, 3}

And we need to improve the assignment to do this check.

@certik
Copy link
Contributor

certik commented Jan 31, 2022

Yes, this looks very good. The RHS seems correct in ASR. The Assignment is incorrect, as you cannot assign a set to an integer, see my comment above. You can only assign a set to a set.

@namannimmo10
Copy link
Collaborator Author

namannimmo10 commented Jan 31, 2022

The Assignment is incorrect, as you cannot assign a set to an integer, see my comment above. You can only assign a set to a set.

I know, just I was planning to add the set annotation.

@certik
Copy link
Contributor

certik commented Jan 31, 2022

Here are examples of typing: https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html#built-in-types, let's use the same syntax

@namannimmo10
Copy link
Collaborator Author

Let's move the improvement of the assignment to a separate PR?

I know many tests will fail once I apply this diff:

diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp
index bc7bb209..72fe972d 100644
--- a/src/lpython/semantics/python_ast_to_asr.cpp
+++ b/src/lpython/semantics/python_ast_to_asr.cpp
@@ -515,7 +515,11 @@ public:
         }
         this->visit_expr(*x.m_value);
         ASR::expr_t *value = ASRUtils::EXPR(tmp);
-        ASR::stmt_t *overloaded=nullptr;
+        ASR::stmt_t *overloaded = nullptr;
+        if (!ASRUtils::check_equal_type(ASRUtils::expr_type(target), ASRUtils::expr_type(value))) {
+            throw SemanticError("Assignment types do not match",
+                x.base.base.loc);
+        }
         tmp = ASR::make_Assignment_t(al, x.base.base.loc, target, value,
                                 overloaded);
     }

@certik
Copy link
Contributor

certik commented Jan 31, 2022

We can't merge this PR anyway until we run all tests in LFortran, so we have to do that.

@namannimmo10
Copy link
Collaborator Author

until we run all tests in LFortran

I did so -- no test failures.

@certik
Copy link
Contributor

certik commented Feb 1, 2022

Awesome. Would you mind please sending a Merge Request against LFortran with this change in ASR? Let's get it merged there first. Then we'll come back and finish this PR.

@namannimmo10
Copy link
Collaborator Author

MR opened: !1650.

Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

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

The same comment as for the dictionary. I think this is ok to merge.

The ConstantSet creation is correct. But declaring a set and assignment to it does not generate a correct ASR yet, but we can fix that later.

@namannimmo10 namannimmo10 merged commit d6d69bb into lcompilers:main Feb 1, 2022
@namannimmo10 namannimmo10 deleted the set branch February 1, 2022 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants