Skip to content

Set-theoretic tuple types #13576

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

Closed
wants to merge 2 commits into from
Closed

Set-theoretic tuple types #13576

wants to merge 2 commits into from

Conversation

gldubc
Copy link
Member

@gldubc gldubc commented May 20, 2024

Adds to the Descr tuple types {:ok, binary(), integer()} and open tuple types {atom(), boolean(), ...} which represent every tuple of at least two elements that are an atom and a boolean.

Tuples are encoded as map records, where the keys are the indices. E.g., type {integer(), atom()} is encoded as type %{0 => integer(), 1 => atom()} and {atom(), boolean(), ...} is encoded as the open map %{..., 0 => atom(), 1 => boolean()}.

Emptiness checking is slightly modified to account for the fact that tuple types do not admit explicit optional indices.

catch
:discard_negative -> tuple_empty?(tag, fields, negs)
end
end
Copy link
Member

Choose a reason for hiding this comment

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

Can we rewrite it to a non-raising versaion like the map one is currently implemented? :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

@gldubc gldubc May 21, 2024

Choose a reason for hiding this comment

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

Yes! I see this now thank you :)

@@ -672,6 +686,19 @@ defmodule Module.Types.Descr do
end
end

defp remove_optional(type) do
Copy link
Member

Choose a reason for hiding this comment

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

Let's use the existing pop_optional instead!

defp tuple_union(left, right), do: map_union(left, right)

# Same as map_fetch, only the tuple descr field is accessed
def tuple_fetch(%{} = descr, key) do
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def tuple_fetch(%{} = descr, key) do
def tuple_fetch(%{} = descr, key) when is_integer(key) do

for {neg_index, neg_type} <- neg_fields, not is_map_key(fields, neg_index) do
cond do
# This index is not in the closed positive tuple, so there is no value in common
# Tthere are no explicit optional types in tuples
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Tthere are no explicit optional types in tuples
# There are no explicit optional types in tuples

@josevalim
Copy link
Member

Closing in favor of #13752.

@josevalim josevalim closed this Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants