Docsity

Tree Pattern Matching in CS 502: Minimizing the Number of Tests - Prof. Suresh Jagannathan, Study notes of Computer Science

Partial preview of the text

Download Tree Pattern Matching in CS 502: Minimizing the Number of Tests - Prof. Suresh Jagannathan and more Study notes Computer Science in PDF only on Docsity!

Pattern Matching

CS 502

Lecture 3

Tree Pattern Matching

 ML functions can be defined by a

sequence of pattern expression pairs

called rules that are matched to a

subject pattern.

 The first rule that matches the subject

has its expression evaluated and

returned as the result of the pattern

match.

Classical Approach

 Build a decision tree:

 The tree encodes the pattern and defines the order in
which subterms of a term have to be evaluated.
 It also injects runtime tests to determine if a pattern
matches a value.

 Two classifications:

 Exhaustive: there exists at least one pattern that
matches the subject.
 Redundant: there is more than one pattern that matches
the subject.
 Desire exhaustive non-redundant pattern expressions.

Example

fun diff(nil,y) = y (1) | diff(x,nil) = x (2) | diff(hdx::tlx,hdy::tly) = diff(tlx,tly)  (^) The set of patterns is exhaustive  (^) Patterns (1) and (2) are non-disjoint or overlap because there is at least one value for which both patterns match (i.e., (nil,nil))  (^) Ordering of rules impose a priority ordering among patterns.  (^) Patterns must be linear: no variable in a pattern can occur more than once:  (^) fun equal(x,x) = true (* this is illegal *)  (^) Patterns should not be redundant: they should match some value, not matched by earlier patterns.

Value Terms

Patterns match values which are also tree-shaped structures formed by the application of constructors and tupling:

t ::= atom (atomic values: values of primitive
types, functional types, and abstract types)
| c (constants)
| (t1,…tn) (tuple of terms)
| C t’ (constructor C applied to term t’)

Example

Value term t = (true, Cons(1,Nil)) Following sequence of patterns of type (bool * intlist)): p1 = (true, _) p2 = (false, nil) p3 = (false, Cons(x,Nil)) p4 = (false, Cons(y,z)) p1 agrees with t on its first component. p2, p3, p4 do not. p1 also agrees on its second term. p3 and p4 have a pattern of the form Cons(p’) and agree with t on this component. p3 and p4 also agree with t on both arguments to Cons.

Matching

A pattern p matches a term t iff p agrees with t on all subterms of t. The matching problem: Given a finite sequence of patterns p1,…,pn and value term t, find the first pattern in the sequence which matches t, if one exists. (We elide issue of typing here.)

Efficiency

 (^) Naïve approach:

 Match each argument with each pattern in turn

(top-down), starting over after every failure.

 Inefficient because information gleaned about the

argument in each partially successful match is

ignored on subsequent matches.

 (^) Better strategy: analyze pattern sequence at compile-time, and try to minimize number of case discrimination tests.

Example (revisited)

 (^) Two choices: test either on first component or second component.

 First component: discover that p1 is the only

pattern that matches. Wildcard in second

component establishes p1 as a matching pattern.

 Second component: can discard p2. Must compare

against p1, p3, and p4:

 (^) Three additional tests required:  (^) Test on first component of t. (leads to desired result)  (^) Test on first component of Cons.  (^) Test on second component of Cons.

Decision Tree

1 {p1,p2,p3,p4} {p1} true false 2 {p2,p3,p4} {p2} Nil Cons 2,2 {p3,p4} {p3} {p4] Minimal decision tree for example

Relevance Heuristic

 If two patterns overlap, the one with the lowest index is
preferred.
 Consider (p1) (nil, x) and (p2) (y,nil)

16 1 {p1,p2} {p1} nil cons 2 {p2} {p2} nil cons {} 2 {p1,p2} {p1,p2} nil cons 1 {p1} {p1} nil cons {}

{p1} {p2} nil (^) cons

Branching Factor Heuristic

 (^) Favoring choice of tests with low branching factor first.  (^) type color = red | blue | green  (^) (p1) (true, green), (p2) (false, green) 17 2 {p1,p2} {} red,blue green 1 {p1,p2} {p1} true false {p2} 1 {p1,p2} {p1} true false 2 {p2} {} red,blue (^) green {p2}

{} {p1} red,blue (^) green