The Artima Developer Community
Sponsored Link

Java Community News
Tom: A Pattern-Matching Compiler for Java

4 replies on 1 page. Most recent reply: Nov 1, 2007 5:33 AM by Pierre-Etienne Moreau

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Tom: A Pattern-Matching Compiler for Java Posted: Oct 25, 2007 1:59 PM
Reply to this message Reply
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?


James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Tom: A Pattern-Matching Compiler for Java Posted: Oct 26, 2007 11:26 AM
Reply to this message Reply
If I want pattern matching I'd use Scala and compile it to Java. I don't know Tom from Adam.

Tom for Java? It's a new language called Tom. If you are create a new language, be honest about it.

Raoul Duke

Posts: 127
Nickname: raoulduke
Registered: Apr, 2006

Re: Tom: A Pattern-Matching Compiler for Java Posted: Oct 30, 2007 2:55 PM
Reply to this message Reply
> 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.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Tom: A Pattern-Matching Compiler for Java Posted: Oct 31, 2007 7:17 AM
Reply to this message Reply
> > 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.

Pierre-Etienne Moreau

Posts: 1
Nickname: pem
Registered: Oct, 2007

Re: Tom: A Pattern-Matching Compiler for Java Posted: Nov 1, 2007 5:33 AM
Reply to this message Reply
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.

Hope this helps

Flat View: This topic has 4 replies on 1 page
Topic: The Anatomy of a Call Site in the JVM Previous Topic   Next Topic Topic: Microsoft Releases New JDBC Driver for SQLServer

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use