20
Phorms: Pattern Matching Library for Pharo Markiyan Rizun Camille Teruel, Gustavo Santos, Stéphane Ducasse 1 Markiyan Rizun e-mail: [email protected] download: http://smalltalkhub.com/#!/~CamilleTeruel/Patterns/

First class patterns for object matching

  • Upload
    esug

  • View
    169

  • Download
    3

Embed Size (px)

Citation preview

Page 1: First class patterns for object matching

Phorms: Pattern Matching Library for Pharo

Markiyan Rizun Camille Teruel, Gustavo Santos, Stéphane Ducasse 1

Markiyan Rizun e-mail: [email protected] download: http://smalltalkhub.com/#!/~CamilleTeruel/Patterns/

Page 2: First class patterns for object matching

Pattern matching

Introduced in functional programming.

Mainly used for inductive definition of functions.

2

Page 3: First class patterns for object matching

Pattern matching. Example

3

fib n

| n == 0 = 1

| n == 1 = 1

| n >= 2 = fib (n-1) + fib (n-2)

Page 4: First class patterns for object matching

Pattern matching 4

Integrated in Newspeak, Scala.

Mainly used to decompose object data.

Page 5: First class patterns for object matching

Pattern matching in Pharo 5

• Source code rewriting

The Rewrite Engine

Page 6: First class patterns for object matching

Pattern matching in Pharo. Example

6

“matching part” ``@a > ``@b

ifTrue: [ ``@a ] ifFalse: [ ``@b ]

“transforming part”

``@a max: ``@b

“input” a > b

ifTrue: [ a ] ifFalse: [ b ]

“result”

a max: b

Page 7: First class patterns for object matching

The Rewrite Engine 7

1. AST focused.

2. Sometimes confusing.

3. Patterns are not composable. 4. Debug and inspect are not user-friendly.

5. Not extendable.

1. Efficient source code rewriting.

2. Smalltalk style syntax.

Page 8: First class patterns for object matching

Solution 8

• Patterns are objects

Phorms

Page 9: First class patterns for object matching

Phorms. Patterns

9

1 equals “equality”

p1 & p2 & … & pN “and”

p1 | p2 | … | pN “or”

Page 10: First class patterns for object matching

Phorms. Patterns

10

p1, p2, … , pN “list”

p1, (p2, p3) inList “nested list”

p1, p2 star, p3 “star list”

Page 11: First class patterns for object matching

Phorms. Patterns

11

pattern ==> [ :it :context |… ] “rewriting”

1 equals named: #aName “named”

#var any “any”

Page 12: First class patterns for object matching

Phorms. Matching

12

pattern match: anObject

Page 13: First class patterns for object matching

Phorms. Matching Example 1

13

1) pattern := 2 equals, 3 equals. 2) pattern match: 2@3.

Page 14: First class patterns for object matching

Phorms. Matching Example 1

14

2@3

2 3

2 equals, 3 equals

Page 15: First class patterns for object matching

Phorms. Matching Example 2

15

1) pattern := #var any, 5 equals. 2) ast := RBParser parseExpression:

3) ‘variable := 5’.

4) pattern match: ast.

Page 16: First class patterns for object matching

Phorms. Matching Example 2

16

variable := 5

variable 5

#var any, 5 equals

Page 17: First class patterns for object matching

Phorms. Rewriting

17

rewritingPattern rewrite: anObject

Rewriting pattern = match + transform.

Page 18: First class patterns for object matching

Phorms. Rewriting Example

18

1) match := #x any, #y any.

2) transform := [ :it :context| it y @ it x ]. 4) pattern := match ==> transform .

5) pattern rewrite: 1@2.

Result: (2@1)

Page 19: First class patterns for object matching

Phorms. Conclusion

19

1. Rewrite anything! 2. Patterns are objects (compose & convert). 3. User-friendly debug & inspect. 4. Easily extendable

1. Not (yet!) AST focused.

2. Sometimes bulky code.

Page 20: First class patterns for object matching

Markiyan Rizun e-mail: [email protected] download: http://smalltalkhub.com/#!/~CamilleTeruel/Patterns/

20