Pattern matching vs. multiple dispatcher

412 views
Skip to first unread message

Francesco Bonazzi

unread,
Oct 10, 2013, 2:14:25 PM10/10/13
to juli...@googlegroups.com
Hi there!

I am new to this discussion, I was wondering whether there are any plans to add a pattern matching function definition besides multiple dispatcher.

Let me give an example, in Wolfram Mathematica it is possible to define a function such as:

f[a_. + b_ ] = a+b  /; FreeQ[a + b x, x]

That is, this function returns a+b if matched, 'a' is optional, 'b' is required but we have the condition that a + b x can't be an expression in x. This would match '1/x', 'z + 1/x', 'Unevaluated[z + 0]', '-1 + x' etc.

An other example:

f[Sin[ x_ ]] = x

matches only the sinus as the trunk of the expression tree, and the sinus' content is passed as a parameter to f.

The SymPy project has a pattern matching system (unfortunately, unlike Mathematica, it does not declare any functions in such a way), employing Wild objects, which can match any expression tree.

Are there any plans to introduce such an advanced feature into Julia? I suppose that a pattern matcher to define functions would require a non-constant-time search algorithm, decreasing performance if such patterns are used, but that would be really very useful in symbolic math.

John Myles White

unread,
Oct 10, 2013, 4:06:32 PM10/10/13
to juli...@googlegroups.com
Hi Francesco,

Welcome to the mailing list.

There are many Julia users interested in pattern matching. They've implemented some forms of it as add-on packages:

* https://github.com/toivoh/PatternDispatch.jl
* https://github.com/kmsquire/Match.jl

The trouble with getting pattern matching right is that it depends on looking at the values of inputs, not just their types, so it's distinct from Julia's multiple dispatch system. In particular, multiple dispatch can often be done (at least in principle) at compile-time, whereas pattern matching generally can't.

-- John
Reply all
Reply to author
Forward
0 new messages