RelationalAI-oss / Rematch.jl

Pattern matching
Other
52 stars 6 forks source link

Support parametric types #26

Open MasonProtter opened 4 years ago

MasonProtter commented 4 years ago

Rematch can't handle pattern matching on parametric types:

julia> @match Val(2) begin
           Val{2} => "hi"
           Val{x} => "bye"
       end
ERROR: LoadError: Unrecognized pattern syntax: Val{x}
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] handle_destruct(::Symbol, ::Expr, ::Set{Symbol}, ::Array{Expr,1}) at /Users/mason/.julia/packages/Rematch/Dgg7p/src/Rematch.jl:187
 [3] handle_match_case(::Symbol, ::Expr, ::Expr, ::Array{Expr,1}) at /Users/mason/.julia/packages/Rematch/Dgg7p/src/Rematch.jl:213
 [4] handle_match_cases(::Expr, ::Expr) at /Users/mason/.julia/packages/Rematch/Dgg7p/src/Rematch.jl:237
 [5] @match(::LineNumberNode, ::Module, ::Any, ::Any) at /Users/mason/.julia/packages/Rematch/Dgg7p/src/Rematch.jl:268
in expression starting at REPL[13]:1
ghost commented 4 years ago

I think you've structured your pattern incorrectly.

Val(2) doesn't produce Val{2}, it produces Val{2}(). It works (sort of) with the parens:

julia> @match Val(2) begin
           Val{2}() => "hi"
           Val{3}() => "bye"
       end
"hi"

julia> @match Val(3) begin
           Val{2}() => "hi"
           Val{3}() => "bye"
       end
"bye"

But you're right that it still doesn't seem to work with variables in the type parameter:

julia> @match Val(2) begin
           Val{2}() => "hi"
           Val{x}() => "bye"
       end
ERROR: UndefVarError: x not defined

EDIT: Okay never mind, I didn't realize, apparently you can also just put the type there, and it will match on that? In which case, you're right the example you posted is broken.

julia> @match 2 begin
           Int => "hi"
           x => "bye"
       end
"hi"

But i'm not sure if @match intended to support that syntax? Reading the help string, there's a x::Int or _::Int syntax which i assume was intended to support this... I'm assuming this is a generalization of the Foo(x,y) struct destructuring matching... Weird.

Sooo anyway, you're right that type params aren't supported, but i'm still not sure whether we intend to support the syntax as you've written it?

ghost commented 4 years ago

More brokenness:

julia> @match Val(2) begin
           _::Val{2} => "hi"
           _::Val{x} => "bye"
       end
"hi"

julia> @match Val(3) begin
           _::Val{2} => "hi"
           _::Val{x} => "bye"
       end
ERROR: UndefVarError: x not defined
nystrom commented 4 years ago

@rai-nhdaly FYI, you cannot use a type as a pattern:

@match 2 begin
   Int => "hi"
   x => 'bye"
end

binds 2 to the variable Int, it doesn't check that type at all. The pattern you want is _::Int.

That said, Rematch should allow type variables to be bound in where clauses, mirroring the function syntax. Something like:

@match Val(3) begin
   _::Val{x} where x => @assert x == 3
end

and

@match [1,2,3] begin
   _::Vector{T} where T => @assert T == Int
end

If you write this now you get:

julia> @match [1,2,3] begin
          _::Vector{T} where T => @assert T == Int
       end
ERROR: UndefVarError: T not defined