Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

First class patterns for object matching

254 views

Published on

Presentation of IWST16, Prag

Video: https://youtu.be/JAl1NET4Eik

Published in: Software
  • Video: https://youtu.be/JAl1NET4Eik
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

First class patterns for object matching

  1. 1. Phorms: Pattern Matching Library for Pharo Markiyan Rizun Camille Teruel, Gustavo Santos, Stéphane Ducasse1 Markiyan Rizun e-mail: mrizun@gmail.com download: http://smalltalkhub.com/#!/~CamilleTeruel/Patterns/
  2. 2. Pattern matching Introduced in functional programming. Mainly used for inductive definition of functions. 2
  3. 3. Pattern matching. Example 3 fib n | n == 0 = 1 | n == 1 = 1 | n >= 2 = fib (n-1) + fib (n-2)
  4. 4. Pattern matching4 Integrated in Newspeak, Scala. Mainly used to decompose object data.
  5. 5. Pattern matching in Pharo5 • Source code rewriting The Rewrite Engine
  6. 6. 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
  7. 7. The Rewrite Engine7 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.
  8. 8. Solution8 • Patterns are objects Phorms
  9. 9. Phorms. Patterns 9 1 equals “equality” p1 & p2 & … & pN “and” p1 | p2 | … | pN “or”
  10. 10. Phorms. Patterns 10 p1, p2, … , pN “list” p1, (p2, p3) inList “nested list” p1, p2 star, p3 “star list”
  11. 11. Phorms. Patterns 11 pattern ==> [ :it :context |… ] “rewriting” 1 equals named: #aName “named” #var any “any”
  12. 12. Phorms. Matching 12 pattern match: anObject
  13. 13. Phorms. Matching Example 1 13 1) pattern := 2 equals, 3 equals. 2) pattern match: 2@3.
  14. 14. Phorms. Matching Example 1 14 2@3 2 3 2 equals, 3 equals
  15. 15. Phorms. Matching Example 2 15 1) pattern := #var any, 5 equals. 2) ast := RBParser parseExpression: 3) ‘variable := 5’. 4) pattern match: ast.
  16. 16. Phorms. Matching Example 2 16 variable := 5 variable 5 #var any, 5 equals
  17. 17. Phorms. Rewriting 17 rewritingPattern rewrite: anObject Rewriting pattern = match + transform.
  18. 18. 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)
  19. 19. 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.
  20. 20. Markiyan Rizun e-mail: mrizun@gmail.com download: http://smalltalkhub.com/#!/~CamilleTeruel/Patterns/ 20

×