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

GlobalRef pattern is not defined for Expr match

Open Roger-luo opened this issue 4 years ago • 5 comments

e.g

@match stmt begin
            Expr(:call, GlobalRef(Base, :getfield), SlotNumber(2), QuoteNode(:variables)) => break
            Expr(:call, getfield, SlotNumber(2), QuoteNode(:variables)) => break
            _ => nothing
end

it would also be nice to have the following things supported for Expr match, e.g Core.GotoIfNot Core.GotoNode, Core.SSAValue, Core.SlotNumber since they are valid items in lowered AST.

Roger-luo avatar Nov 06 '20 00:11 Roger-luo

This can be achieved by defining active patterns:

julia> @active GlobalRef(x) begin
           (x.mod, x.name)
       end

julia> @match GlobalRef(Base, :getfield) begin
           GlobalRef(a, b) => (a, b)
       end
(Base, :getfield)

julia> SlotNumber = Core.SlotNumber
julia> @active SlotNumber(x) begin
           Some(x.id)  # must match
       end

julia> @match Core.SlotNumber(1) begin
           Core.SlotNumber(x) => x
       end
1

thautwarm avatar Nov 06 '20 00:11 thautwarm

I end up implementing the following

# MLStyle patches

@active GlobalRef(x) begin
    if x isa GlobalRef
        (x.mod, x.name)
    else
        nothing
    end
end

@active Argument(x) begin
    if x isa Argument
        Some(x.n)
    else
        nothing
    end
end

@active SSAValue(x) begin
    if x isa SSAValue
        Some(x.id)
    else
        nothing
    end
end

@active SlotNumber(x) begin
    if x isa SlotNumber
        Some(x.id)
    else
        nothing
    end
end

but I'm wondering if we can define these in MLStyle? If I define these in my package, I will end up pirating MLStyle and Julia Core. I'm not sure where to put them in MLStyle.

Roger-luo avatar Nov 12 '20 16:11 Roger-luo

Sorry for missing this, I'd like to add them into https://github.com/thautwarm/MLStyle.jl/blob/master/src/Pervasives.jl

thautwarm avatar Mar 13 '21 02:03 thautwarm

It seems I can't use @active in that file, I don't know how to implement them without @active, perhaps I'll just define them in Expronicon.jl instead.

Roger-luo avatar Mar 13 '21 05:03 Roger-luo

After considering it again I guess these patterns would be better to get separated from MLStyle base. MLStyle is to provide infrastructure, but implementing patterns for stdlib types might be a big task comparing to the pattern compilation itself.

thautwarm avatar Mar 13 '21 06:03 thautwarm