Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Matchete Paths through the Pattern Matching Jungle Martin Hirzel Nate Nystrom Bard Bloom Jan Vitek 7+8 January 2008 PADL.

Similar presentations


Presentation on theme: "1 Matchete Paths through the Pattern Matching Jungle Martin Hirzel Nate Nystrom Bard Bloom Jan Vitek 7+8 January 2008 PADL."— Presentation transcript:

1 1 Matchete Paths through the Pattern Matching Jungle Martin Hirzel Nate Nystrom Bard Bloom Jan Vitek 7+8 January 2008 PADL

2 2 What is Pattern Matching? Examples: –Switch in C/Java –Exception handlers –ML-style patterns –Regular expressions –XPath patterns –Bit masks Selection –If match, then execute handler –E.g. is this a float? Bindings –Give names to parts –E.g. integral part: 22, fractional part: 341

3 3 Example: Lists -- list multiplication mult( ) = 3 * mult( ) = 3 * -1 * mult( ) = 3 * -1 * 0 * mult( ) = 3 * -1 * 0 * 4 * mult(nil) = 3 * -1 * 0 * 4 * 1 = 0 -- list construction cons(3, cons(-1, cons(0, cons(4, null)))) =

4 4 Matching Structured Terms int mult(List ls) { match(ls) { cons~(0, _): return 0; cons~(int h, List t): return h * mult(t); null: return 1; } return 1; } Selection Bindings Central feature of ML, Haskell Hardly a jungle!

5 5 Less Structured Data DataPatternLanguage StringsRegular expressionPerl XMLXPathXSLT Raw bitsBinary patternErlang Major factor in success of practical languages!

6 6 Why Unify? Given list of strings: Given String variable: name Find name, extract int age Match list deconstructor pattern cons~(…, …) Match string nested RegExp /([a-z]+) ([0-9]+)/(name, int age) sue 10bob 15ann 11

7 7 Matchete (Java Extension) Integrates pattern sublanguages Common set of primitive patterns Nesting composite patterns Simple uniform semantics

8 8 Primitive Patterns NameExamples Wildcard _ Value x tiger.stripes + spider.legs Binder int x ScaryAnimal python

9 9 Composite Patterns [[(0x2cf9:16) 01 (int x:14)]] BitLevel /([a-z]) ([0-9]+)/(chr,int f) RegExp (NodeList n) XPath int[]{1, x, int y} Array re("([0-9]+)")~(int i) Parameterized cons~(0, _) Deconstructor ExamplesName

10 10 Deconstructor Definition class List { private int head; private List tail; public List(int h, List t) { head = h; tail = t; } public cons~(int h, List t) { h = head; t = tail; } } Fields Constructor Deconstructor Match on receiver object Out parameters = subjects for nested patterns

11 11 Nesting cons~(/([a-z]+) ([0-9]+)/(name, int age), _) Wildcard _ Value name Binder int age Deconstructor cons RegExp ([a-z]+) ([0-9]+) sue 10bob 15ann 11

12 12 Subjects flow to children RegExp ([a-z]+) ([0-9]+) Wildcard _ Value name Binder int age Deconstructor cons sue 10bob 15ann 11 bob 15ann 11 sue 10 sue10

13 13 Decisions and bindings flow to textual successor RegExp ([a-z]+) ([0-9]+) Wildcard _ Value name Binder int age Deconstructor cons Handler print(age)

14 14 Compilation Matchete source code Built on Rats! parser generator Generated Java source Debugging information Runtime library Other Java source Matchete compiler Java class files Java compilerPostprocessor

15 15 Implemented Examples Balance red-black tree Process TCP/IP network packet Pretty-print XML bibliography … + smaller regression tests

16 16 Discussion: Typing Matchete uses strong dynamic typing –No runtime errors, just failed matches –If Matchete compiler gives no error, then Java compiler gives no error either Why not (more) static typing? –Data formats mismatch –Test bed for a new scripting language

17 17 Discussion: Integration Simpler language re("a(b)c(d)") ~(p,q) No integration No need to count /a(p:b)c(q:d)/ Tight integration Sublanguage reuse /a(b)c(d)/ (p,q) Loose integration AdvantageExampleChoice Matchete choses tight integration for BitLevel, loose integration for RegExp and XPath, no integration for XML as terms

18 18 Related Work Structured terms –Algebraic types: ML, Haskell, … –Objects: Tom, OOMatch, JMatch, … –Letting users define patterns: F#, Scala Strings: Perl; SNOBOL Bit-level data: Erlang; DataScript; PADS XML: –As trees: XSLT, XJ (XPath) –As terms: XDuce, HydroJ, …

19 19 Conclusions Pattern matching applies to terms, strings, XML, and raw bits Matchete offers path to unification


Download ppt "1 Matchete Paths through the Pattern Matching Jungle Martin Hirzel Nate Nystrom Bard Bloom Jan Vitek 7+8 January 2008 PADL."

Similar presentations


Ads by Google