Summary
Tom is a new pattern-matching compiler for Java. It provides a match operator, similar to what some functional languages define for pattern matching.
Advertisement
Pattern matching—the ability to determine if a given pattern is present in some sequence or tree structure—is a basic feature of many programming languages. In some functional languages, it is used as a form of control structure, roughly corresponding to the switch/case construct. In contrast with switch, pattern matching can match not only on primitive types, but on more complex expressions and types.
A team at INRIA in France recently developed a pattern matching compiler for Java, Tom, that adds a match primitive to Java. A new video presentation about using Tom with the Java Persistence Architecture was recently made available on the project Web site. Tom is available under the GPL 2 license.
The project documentation describes Tom as:
Tom is a pattern matching compiler developed at INRIA. It is particularly well-suited for programming various transformations on trees/terms and XML based documents. Its design follows our research on rule based languages...
When observing its illuminated face, Tom can be seen as a Yacc-like compiler translating patterns into executable pattern matching automata. Similarly to Yacc, when a match is found, the corresponding semantic action (a sequence of instructions written in Java, C or Caml) is triggered and executed...
When observing its dark side, Tom is a complex compiler which adds non-trivial constructs to Java, C and Caml(ML family), and offers the possibility to analyse any kind of data-structure. Tom is a real programming language, with many programming and debugging facilities...
Tom is language extension which adds a new matching primitives to C and Java: %match. This construct is similar to the match primitive found in functional languages: given a term (called subject) and a list of pairs: pattern-action, the match primitive selects a pattern that matches the subject and performs the associated action. This construct may thus be seen as an extension of the classical switch/case construct.
The main difference is that the discrimination occurs on a term and not on atomic values like characters or integers: the patterns are used to discriminate and retrieve information from an algebraic data structure. Therefore, Tom is a good language for programming by pattern matching...
What do you think of pattern-matching as a language construct?
> If I want pattern matching I'd use Scala and compile it to > Java. I don't know Tom from Adam.
hm, i only know vaguely of these things from reading about them at a high level, but i wonder if Tom is more "powerful" than Scala's pattern matching? or at least different in some fashion so perhaps not apples to apples.
> > If I want pattern matching I'd use Scala and compile it > to > > Java. I don't know Tom from Adam. > > hm, i only know vaguely of these things from reading about > them at a high level, but i wonder if Tom is more > "powerful" than Scala's pattern matching? or at least > different in some fashion so perhaps not apples to apples.
OK even if it is, lets not pretend that Tom is just an add-on like an API. It introduces new syntax. Anyone who uses it will only be able to compile their code with a Tom compiler.
Dear James, you are right, Tom is not just an API. This is a language extension. Therefore, there is a compiler. The main difference with a completely new language is that any Java construct can be used: a Java program is also a Tom program. So, you can continue to use your favorite constructs and data-structures.
Dear Raoul, Tom and Scala are targeting the same kind of programmers. As far as I know, there are some differences between the two languages. Evan if both languages can access to Java classes, in Tom, the pattern matching is not restricted to simple "syntactic" data-structures. It supports list-matching. For example, list(_*,Car(...),_*) denotes the search of a car in a list (a Collection for example). It can be used to search for two identical characters in a String for example: (_*,x,_*,x,_*) Tom also supports anti-pattern matching (i.e. the negation of a fact). For instance list(_*,x,_*,!x,_*) looks for two different elements, whereas !list(_*,x,_*,x,_*) looks for a list which does not have two identical elements.
Besides that, a big difference with Scala is that the data-structure against which Tom can match is not fixed. This means that you can match against any data-structure (in particular your hierarchy of classes). If you don't care, Tom can also generate an efficient implementation for you.
The third main difference is the presence of strategies. This corresponds to functional programming and allows you to control how the transformations/rules are applied. For instance Repeat(Innermost(R)).visit(object) applies the rule R in an innermost-way, as many times as possible. The strategy library can be modified and extended easily.