MLStyle.jl icon indicating copy to clipboard operation
MLStyle.jl copied to clipboard

Speculative idea: 'static' checks for testing if all cases are handled

Open MasonProtter opened this issue 4 years ago • 4 comments

Most functional languages with pattern matching have a mechanism to check if a pattern matching expression is 'exhaustive'. I'm imagining something like if I wrote

@match x :: Union{Int, Tuple{Int, Int} } begin
    (x, y) => x + y
end

throwing an error at compile time, because here we told @match that x could be an Int, or it could be a Tuple, but our pattern only matches a Tuple.

Maybe there's a way we could have a pluggable system where users can write 'static completeness checkers' for patterns.

MasonProtter avatar Feb 10 '21 21:02 MasonProtter

I do agree with the idea to bring exhaustiveness check..

However, this proposal approach can have many problems with respect to its completeness:

function (...)
    U = Union{Int, Tuple{Int, Int}}
    @match x :: U begin
         ...
    end
end

Should we check?

Besides, if there is a static type parameter T, what is the expected behaviour of the following one?

@match x :: Union{T, Int} begin
     (x, y) => ...
end

thautwarm avatar Feb 11 '21 03:02 thautwarm

If users write x :: U where U only contains global references, we can appropriately check the code statically.

thautwarm avatar Feb 11 '21 03:02 thautwarm

why not just write @match inside a function, it seems simple enough to provide such checks.

Roger-luo avatar Jun 01 '21 19:06 Roger-luo

This could maybe be a cool use case for JET.jl (Not suggesting adding that as a dependency to MLSyle yet, but maybe I’ll play around in a separate package and try making a @checked_match or something.

MasonProtter avatar Jun 01 '21 20:06 MasonProtter