JuliaServices / Match.jl

Advanced Pattern Matching for Julia
https://juliaservices.github.io/Match.jl/latest/
Other
240 stars 22 forks source link

Inverse functions #15

Closed mschauer closed 23 hours ago

mschauer commented 9 years ago

I am thinking since a while, that it would be nice and very julian to match general functional expressions if the inverse of the function is provided, for example

@match (a,b) g(x,(y,z))

would translate into

x, _yz = @inversefun(g, (typeof(a,b))(a,b)
y,z = _yz

where @inversefun can be extended.

kmsquire commented 9 years ago

@mschauer, thank you for noting this. I was pretty busy when you contributed this comment, and it fell off my radar--sorry for my slow response!

I think that the suggestion sounds reasonable. I'm not sure if or when I'll have a chance to look at this, but if you have time and want to explore contributing something like this to Match.jl, I'd be happy to consider it.

TotalVerb commented 8 years ago

Macros cannot be extended, but inversefun can easily be made a function instead of a macro. One potential problem is that functions can't be dispatched on. Here's a very inelegant possible solution:

immutable Inverse end

f(x) = x^2
f(::Inverse, x) = sqrt(x)

that is, using the inverse function as a method of the original. This probably has ambiguity problems, and it doesn't strike me as very Julian.

nystrom commented 1 year ago

This is very similar to https://github.com/JuliaServices/Match.jl/issues/92.

gafter commented 23 hours ago

Treating as a dup of #92, which is probably closer to how we would handle this.