Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/elixir/lib/module/types/apply.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,19 @@ defmodule Module.Types.Apply do
end
end

defp remote_apply(:erlang, :--, _info, [left, right], stack) do
# TODO: remove once we add parametric types, this will just be:
# list(a), list(term()) -> list(a)
case {list_of(left), list_of(right)} do
{{_, list_of}, {_, _}} ->
result = if list_of, do: list(list_of), else: empty_list()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Isn't this the same as left?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No. Trivial counterexample [:foo] -- [:foo] === []. If left is nonempty the result may be empty list. But even if we returned union(left, empty_list()) it would be unsound with negations. Given left as non_empty_list(atom()) and not list(atom([:a])), in [:b, :a] -- [:b] the result [:a] would fall outside the union type (credit Fable for this one counterexample).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fascinating example where list(a), list(term()) -> list(a) when a: term() gives a different result than a, list(term()) -> a when a: list(term()), which I would expect them to be equivalent. Does this make sense to you @gldubc?

@lukaszsamson lukaszsamson Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fascinating sure but currently purely synthetic. I don't think elixir can produce such type negation via guards/patterns. Guards do not allow expressing whole list properties and patterns only match on empty or list head. It may not be synthetic when type definition syntax lands.
Are they equivalent - it boils down to what -- actually preserves. Contains some type or is empty: yes, Is the list proper: yes, is the list not empty: no, contains at least one sth: no, contains at least one not sth: no

{:ok, return(result, [left, right], stack)}

_ ->
{:error, badremote(:erlang, :--, [left, right])}
end
end

defp remote_apply(Kernel, :elem, info, [tuple, _index] = args_types, stack) do
remote_apply_tuple_element(info, args_types, tuple, stack)
end
Expand Down
9 changes: 9 additions & 0 deletions lib/elixir/test/elixir/module/types/expr_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ defmodule Module.Types.ExprTest do
non_empty_list(term()), term()
"""
end

test "--" do
assert typecheck!([x], [1, 2, 3] -- x) == list(integer())
assert typecheck!([1] -- [1]) == list(integer())
assert typecheck!([x], [] -- x) == empty_list()

assert typeerror!([x], [1, 2, 3] -- String.to_integer(x)) |> strip_ansi() =~
"incompatible types given to Kernel.--/2"
end
end

describe "funs" do
Expand Down