Pla i d: Pattern Language for Abstract Datatypes Austin Clements - - PowerPoint PPT Presentation

pla i d pattern language for abstract datatypes
SMART_READER_LITE
LIVE PREVIEW

Pla i d: Pattern Language for Abstract Datatypes Austin Clements - - PowerPoint PPT Presentation

Motivation Patternology Applications Implementation Pla i d: Pattern Language for Abstract Datatypes Austin Clements Irene Zhang Dan Ports Friday, May 11, 2007 Austin Clements, Irene Zhang, Dan Ports Pla i d: Pattern Language for Abstract


slide-1
SLIDE 1

Motivation Patternology Applications Implementation

Plaid: Pattern Language for Abstract Datatypes

Austin Clements Irene Zhang Dan Ports Friday, May 11, 2007

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-2
SLIDE 2

Motivation Patternology Applications Implementation Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-3
SLIDE 3

Motivation Patternology Applications Implementation

Syntax Patterns and ADT’s

( define ( make−computer model

  • s )

( l i s t ’ ∗computer∗

  • s

model ))

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-4
SLIDE 4

Motivation Patternology Applications Implementation

Syntax Patterns and ADT’s

( define ( make−computer model

  • s )

( l i s t ’ ∗computer∗

  • s

model )) ( define ( crash−computer comp) ( cond (( match comp ’( ∗computer∗ windows )) ’ done ) (( match comp ’( ∗computer∗ l i n u x )) ’ f a i l e d ) ) )

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-5
SLIDE 5

Motivation Patternology Applications Implementation

Syntax Patterns and ADT’s

( define ( make−computer model

  • s )

( l i s t ’ ∗computer∗

  • s

model )) ( define ( crash−computer comp) ( cond (( match comp ’( ∗computer∗ windows )) ’ done ) (( match comp ’( ∗computer∗ l i n u x )) ’ f a i l e d ) ) )

Abstraction Violation

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-6
SLIDE 6

Motivation Patternology Applications Implementation

Syntax Patterns and ADT’s

( define ( make−computer model

  • s )

( l i s t ’ ∗computer∗

  • s

model )) ( define ( crash−computer comp) ( cond (( match comp ’( ∗computer∗ windows )) ’ done ) (( match comp ’( ∗computer∗ l i n u x )) ’ f a i l e d ) ) )

Abstraction Violation

Syntax patterns are limited to built-in types.

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-7
SLIDE 7

Motivation Patternology Applications Implementation

Syntax Patterns and ADT’s

( define ( make−computer model

  • s )

( l i s t ’ ∗computer∗

  • s

model )) ( define ( crash−computer comp) ( cond (( match comp ’( make−computer ’ windows )) ’ done ) (( match comp ’( make−computer ’ l i n u x )) ’ f a i l e d ) ) )

Syntax patterns are limited to built-in types.

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-8
SLIDE 8

Motivation Patternology Applications Implementation

Semantic Patterns and ADT’s

( define ( make−computer model

  • s )

( l i s t ’ ∗computer∗

  • s

model ))

( d e f i n e ( computer? datum ) ( and ( p a i r ? datum ) ( eq? ( car datum ) ’ ∗computer∗ ) ) ) ( d e f i n e computer−model t h i r d ) ( d e f i n e computer−os second ) ( d e f i n e ( match : make−computer? p a t t e r n ) ( and ( p a i r ? p a t t e r n ) ( eq? ( car p a t t e r n ) ’ make−computer ) ) ) ( d e f i n e match : make−computer−model second ) ( d e f i n e match : make−computer−os t h i r d ) ( d e f i n e ( match : make−computer model−comb os−comb) ( d e f i n e ( match data d i c t i o n a r y succeed ) ( and ( computer? data ) (model−comb ( computer−model data ) d i c t i o n a r y ( lambda ( new−dict n ) (os−comb ( computer−os data ) new−dict succeed ) ) ) ) ) match ) ( d e f i n e ( compile−match−make−computer pat use−env loop ) ‘( match : make−computer ,( loop ( match : make−computer−model pat )) ,( loop ( match : make−computer−os pat ) ) ) ) ( eq−put! ’ make−computer ’ pattern−keyword compile−match−make−computer ) Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-9
SLIDE 9

Motivation Patternology Applications Implementation

(match comp ’(make−computer ’windows))

We want to be able to write this

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-10
SLIDE 10

Motivation Patternology Applications Implementation ( d e f i n e ( computer? datum ) ( and ( p a i r ? datum ) ( eq? ( car datum ) ’ ∗computer∗ ) ) ) ( d e f i n e computer−model t h i r d ) ( d e f i n e computer−os second ) ( d e f i n e ( match : make−computer? p a t t e r n ) ( and ( p a i r ? p a t t e r n ) ( eq? ( car p a t t e r n ) ’ make−computer ) ) ) ( d e f i n e match : make−computer−model second ) ( d e f i n e match : make−computer−os t h i r d ) ( d e f i n e ( match : make−computer model−comb os−comb) ( d e f i n e ( match data d i c t i o n a r y succeed ) ( and ( computer? data ) (model−comb ( computer−model data ) d i c t i o n a r y ( lambda ( new−dict n ) (os−comb ( computer−os data ) new−dict succeed ) ) ) ) ) match ) ( d e f i n e ( compile−match−make−computer pat use−env loop ) ‘( match : make−computer ,( loop ( match : make−computer−model pat )) ,( loop ( match : make−computer−os pat ) ) ) ) ( eq−put! ’ make−computer ’ pattern−keyword compile−match−make−computer )

Without having to write this

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-11
SLIDE 11

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

1 Motivation 2 Patternology

Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

3 Applications 4 Implementation

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-12
SLIDE 12

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Constructor-Oriented Pattern Syntax

Data-oriented Constructor-oriented (cons 1 (cons x ())) (1 (? x)) Resembles data Resembles code

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-13
SLIDE 13

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Constructor-Oriented Pattern Syntax

Data-oriented Constructor-oriented (cons 1 (cons x ())) (1 . ((? x) . ())) Resembles data Resembles code

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-14
SLIDE 14

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Constructor-Oriented Pattern Syntax

Data-oriented Constructor-oriented (cons 1 (cons x ())) (1 . ((? x) . ())) Literals Resembles data Resembles code

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-15
SLIDE 15

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Constructor-Oriented Pattern Syntax

Data-oriented Constructor-oriented (cons 1 (cons x ())) (1 . ((? x) . ())) Pattern variables Literals Resembles data Resembles code

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-16
SLIDE 16

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Constructor-Oriented Pattern Syntax

Data-oriented Constructor-oriented (cons 1 (cons x ())) (1 . ((? x) . ())) Deconstructors Implicit Explicit Literals Pattern variables Resembles data Resembles code

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-17
SLIDE 17

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Constructor-Oriented Pattern Syntax

Data-oriented Constructor-oriented (cons 1 (cons x ())) (1 . ((? x) . ())) Deconstructors Implicit Explicit Literals Pattern variables Resembles data Resembles code

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-18
SLIDE 18

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Abstraction, Abstraction, Abstraction

( define ( make−computer model

  • s )

( l i s t ’ ∗computer∗

  • s

model ))

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-19
SLIDE 19

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Abstraction, Abstraction, Abstraction

( define ( make−computer model

  • s )

( l i s t ’ ∗computer∗

  • s

model ))

Pattern Datum

(make−computer ’linux) (∗computer∗ linux pc)

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-20
SLIDE 20

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Abstraction, Abstraction, Abstraction

( define ( make−computer model

  • s )

( l i s t ’ ∗computer∗

  • s

model ))

Pattern Datum

(make−computer ’linux) (∗computer∗ linux pc) ( list ’∗computer∗ ’linux ) (∗computer∗ linux pc)

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-21
SLIDE 21

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Abstraction, Abstraction, Abstraction

( define ( make−computer model

  • s )

( l i s t ’ ∗computer∗

  • s

model ))

Pattern Datum

(make−computer ’linux) (∗computer∗ linux pc) ( list ’∗computer∗ ’linux ) (∗computer∗ linux pc) (cons ’∗computer∗ ( list ’ linux )) (∗computer∗ linux pc)

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-22
SLIDE 22

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Abstraction, Abstraction, Abstraction

( define ( make−computer model

  • s )

( l i s t ’ ∗computer∗

  • s

model ))

Pattern Datum

(make−computer ’linux) (∗computer∗ linux pc) ( list ’∗computer∗ ’linux ) (∗computer∗ linux pc) (cons ’∗computer∗ ( list ’ linux )) (∗computer∗ linux pc) ( list ’ linux ) ( linux pc)

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-23
SLIDE 23

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Abstraction, Abstraction, Abstraction

( define ( make−computer model

  • s )

( l i s t ’ ∗computer∗

  • s

model ))

Pattern Datum

(make−computer ’linux) (∗computer∗ linux pc) ( list ’∗computer∗ ’linux ) (∗computer∗ linux pc) (cons ’∗computer∗ ( list ’ linux )) (∗computer∗ linux pc) ( list ’ linux ) ( linux pc) (cons ’ linux ( list )) ( linux pc) ( list ) (pc) (cons ( list )) (pc) ( list ) () () ()

Success

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-24
SLIDE 24

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Plambda the Ultimate Deconstructor

( define make−computer ( plambda ( model

  • s )

( l i s t ’ ∗computer∗

  • s

model ) ) )

Bridges Scheme abstraction and pattern abstraction

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-25
SLIDE 25

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Plambda the Ultimate Deconstructor

( define make−computer ( plambda ( model

  • s )

( l i s t ’ ∗computer∗

  • s

model ) ) )

Constructor Code

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-26
SLIDE 26

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Plambda the Ultimate Deconstructor

( define make−computer ( plambda ( model

  • s )

( l i s t ’ ∗computer∗

  • s

model ) ) )

Constructor Code Deconstructor Pattern

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-27
SLIDE 27

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Syntactic Sugar

( define−constructor ( make−computer model

  • s )

( l i s t ’ ∗computer∗

  • s

model ))

desugars into

( define make−computer ( plambda ( model

  • s )

( l i s t ’ computer

  • s

model ) ) )

Can often simply change define to define−constructor in existing code!

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-28
SLIDE 28

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Pattern Matching in Scheme

( define ( crash−computer comp) ( pcase comp (( make−computer ’ windows ) ’ done ) (( make−computer ’ l i n u x ) ’ f a i l e d ) ) ) pcase combines pattern matching, failure

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-29
SLIDE 29

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Pattern Matching in Scheme

( define ( crash−computer comp) ( pcase comp (( make−computer model ’ windows ) ( i f ( e x p e n s i v e ? model ) ’ this−is−not−the−crash−you−are−looking−for ’ done )) (( make−computer ’ l i n u x ) ’ f a i l e d ) ) ) pcase combines pattern matching, failure, and binding

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-30
SLIDE 30

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Pro-Choice Patterns

pcase allows us to add decision to the pattern language

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-31
SLIDE 31

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Pro-Choice Patterns

pcase allows us to add decision to the pattern language ( define−constructor ( append a b ) ( pcase a ( ( ) b ) (( cons t h i s r e s t ) ( cons t h i s ( append r e s t b ) ) ) ) )

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-32
SLIDE 32

Motivation Patternology Applications Implementation Constructor-Oriented Pattern Syntax Abstraction, Abstraction, Abstraction Embedding Patterns in Scheme Pro-Choice Patterns

Pro-Choice Patterns

pcase allows us to add decision to the pattern language ( define−constructor ( append a b ) ( pcase a ( ( ) b ) (( cons t h i s r e s t ) ( cons t h i s ( append r e s t b ) ) ) ) ) ( pcase ’(1 2 3 4) (( append x ( l i s t 3 4)) ( pp x ) ) )

= ⇒ x ← (1 2)

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-33
SLIDE 33

Motivation Patternology Applications Implementation Constructors in Plaid Deconstructors in Plaid Imagination in Plaid

1 Motivation 2 Patternology 3 Applications

Constructors in Plaid Deconstructors in Plaid Imagination in Plaid

4 Implementation

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-34
SLIDE 34

Motivation Patternology Applications Implementation Constructors in Plaid Deconstructors in Plaid Imagination in Plaid

Constructors in Plaid

( define−constructor ( make−computer model

  • s )

( l i s t ’ ∗computer∗

  • s

model )) ( define bills−computer ( make−computer ’ desktop ’ windows )) ( define ( crash−computer comp) ( pcase comp (( make−computer ’ windows ) ’ done ) (( make−computer ’ l i n u x ) ’ f a i l e d ) ) )

(crash-computer bills-computer) = ⇒ done With the power of Plaid, patterns are no longer limited to built-in types.

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-35
SLIDE 35

Motivation Patternology Applications Implementation Constructors in Plaid Deconstructors in Plaid Imagination in Plaid

Deconstructors in Plaid

Pattern matching can be used to pick data out of an abstract datatype...

( define ( computer : os comp) ( pcase comp (( make−computer

  • s )
  • s )

( ( e r r o r ‘ ‘ Object must be

  • f

type computer ’ ’ ) ) ) ) ( define ( computer : model comp) ( pcase comp (( make−computer model ) model ) ( ( e r r o r ‘ ‘ Object must be

  • f

type computer ’ ’ ) ) ) )

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-36
SLIDE 36

Motivation Patternology Applications Implementation Constructors in Plaid Deconstructors in Plaid Imagination in Plaid

Deconstructors in Plaid

... or to imagine different views of a data structure.

( define−constructor ( lambda∗ args body ) ( cons ’ lambda ( cons args body ) ) ) ( define−constructor ( d e f i n e ∗ name expr ) ( pcase expr (( lambda∗ args . body ) ( cons ’ define ( cons ( cons name args ) body ) ) ) ( ( l i s t ’ define name expr ) ) ) )

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-37
SLIDE 37

Motivation Patternology Applications Implementation Constructors in Plaid Deconstructors in Plaid Imagination in Plaid

Imagination in Plaid

( define−constructor ( ∗∗ x y ) ( pcase x (1 y ) ( ( pcase y (1 x ) ( ( l i s t ’ ∗ x y ) ) ) ) ) ) ( pcase ’( ∗ 2 3) (( ∗∗ a b ) ( pp ( l i s t a b )) ( i f ( or ( eqv? a 1) ( eqv? b 1)) ( next ) ) ) )

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-38
SLIDE 38

Motivation Patternology Applications Implementation Building Patterns Unification

1 Motivation 2 Patternology 3 Applications 4 Implementation

Building Patterns Unification

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-39
SLIDE 39

Motivation Patternology Applications Implementation Building Patterns Unification

Pattern Compiler

Pattern syntax is represented by an abstract structure A compiler translates the syntax

( cons 1 ( cons a ( ) ) )

to

( p a t t e r n : d e c o n s t r u c t o r ( lambda () ( constructor−>d e c o n s t r u c t o r cons )) ( l i s t ( p a t t e r n : l i t e r a l 1) ( p a t t e r n : d e c o n s t r u c t o r ( lambda () ( constructor−>d e c o n s t r u c t o r cons )) ( l i s t ( p a t t e r n : v a r i a b l e a ) ( p a t t e r n : l i t e r a l ( ) ) ) ) ) ) Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-40
SLIDE 40

Motivation Patternology Applications Implementation Building Patterns Unification

Pattern Compiler

( d e f i n e ( compile−pattern expr env ) ( cond (( n u l l a r y ? expr ) ‘( p a t t e r n : l i t e r a l , expr )) (( quoted? expr ) ‘( p a t t e r n : l i t e r a l ’ , ( cadr expr ) ) ) (( v a r i a b l e ? expr ) ‘( p a t t e r n : v a r i a b l e ’ , expr )) (( w i l d c a r d ? expr ) ‘( p a t t e r n : w i l d c a r d )) (( a p p l i c a t i o n ? expr ) ‘( p a t t e r n : d e c o n s t r u c t o r ( lambda () ( constructor−>d e c o n s t r u c t o r ,( close−syntax ( car expr ) env ) ) ) ( l i s t . ,(map ( lambda ( arg ) ( compile−pattern arg env )) ( cdr expr ) ) ) ) ) (( pcase? expr ) ‘( p a t t e r n : pcase ,( compile−pattern ( cadr expr ) env ) ( l i s t ,@(map ( lambda ( c l a u s e ) ‘( p a t t e r n : pcase : c l a u s e ,( compile−pattern ( car c l a u s e ) env ) ,( compile−pattern ( cadr c l a u s e ) env ) ) ) ( cddr expr ) ) ) ) ) ( e l s e ( e r r o r : invalid−pattern−syntax ” p a t t e r n ” expr ) ) ) ) Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-41
SLIDE 41

Motivation Patternology Applications Implementation Building Patterns Unification

Constructorification

We can do the same for data... The constructorifier picks apart pairs, converting the data

’(1 a )

to

( p a t t e r n : d e c o n s t r u c t o r ( lambda () ( constructor−>d e c o n s t r u c t o r cons )) ( l i s t ( p a t t e r n : l i t e r a l 1) ( p a t t e r n : d e c o n s t r u c t o r ( lambda () ( constructor−>d e c o n s t r u c t o r cons )) ( l i s t ( p a t t e r n : v a r i a b l e a ) ( p a t t e r n : l i t e r a l ( ) ) ) ) ) )

Patterns and data have the same representation!

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-42
SLIDE 42

Motivation Patternology Applications Implementation Building Patterns Unification

plambda

plambda defines both a constructor and a deconstructor plambda body is a pattern =

⇒ also valid Scheme code Returns a real Scheme procedure, and tags the procedure with a deconstructor in a eq hash table Lexical scoping of deconstructor bindings is automatic

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-43
SLIDE 43

Motivation Patternology Applications Implementation Building Patterns Unification

Matching is Unification

This pattern matching actually requires unification: generalization of matching with variables on both sides Takes two patterns and initial set of bindings (“environment”); returns new bindings necessary for both sides to match

( u n i f y ( l i s t 1 2 b ) ( l i s t 1 a 3) empty−environment )

= ⇒ a ← 2;

b ← 3

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-44
SLIDE 44

Motivation Patternology Applications Implementation Building Patterns Unification

Basic Unification

( define ( u n i f y p1 p2 env ) ( cond (( both? p a t t e r n : l i t e r a l ? ) ( i f ( equal? ( p a t t e r n : l i t e r a l / value p1 ) ( p a t t e r n : l i t e r a l / value p2 )) env (( unifier−environment / f a i l env ) ) ) ) (( e i t h e r ? p a t t e r n : w i l d c a rd ? ) env ) (( e i t h e r ? p a t t e r n : v a r i a b l e ? ) = > (w/ l ( lambda ( var

  • ther )

( extend−if−possible var

  • ther

env ) ) ) ) (( e i t h e r ? p a t t e r n : l i t e r a l ? ) (( unifier−environment / f a i l env ) ) ) ( e l s e ( e r r o r ”Don ’ t know how to u n i f y ” p1 p2 ) ) ) )

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-45
SLIDE 45

Motivation Patternology Applications Implementation Building Patterns Unification

Unifying with Fundamental Deconstructors

Two fundamental deconstructors unify if they have the same type, and their arguments are unifiable

( define ( unify−two−fundamental−deconstructors p1 p1−dtor p2 p2−dtor env ) ( and ( eq? ( d e c o n s t r u c t o r : fundamental / symbol p1−dtor ) ( d e c o n s t r u c t o r : fundamental / symbol p2−dtor )) (= ( l e n g th ( p a t t e r n : d e c o n s t r u c t o r / args p1 )) ( l e n g th ( p a t t e r n : d e c o n s t r u c t o r / args p2 ) ) ) ( l e t l p (( args1 ( p a t t e r n : d e c o n s t r u c t o r / args p1 )) ( args2 ( p a t t e r n : d e c o n s t r u c t o r / args p2 )) ( env env )) ( i f ( n u l l ? args1 ) env ( l p ( cdr args1 ) ( cdr args2 ) ( u n i f y ( car args1 ) ( car args2 ) env ) ) ) ) ) )

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-46
SLIDE 46

Motivation Patternology Applications Implementation Building Patterns Unification

Unifying with Compound Deconstructors

Algorithm First, unify the deconstructor body with the other pattern Then, unify the formal arguments with their values Need alpha-renaming to ensure scoping

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-47
SLIDE 47

Motivation Patternology Applications Implementation Building Patterns Unification

Unifying with Compound Deconstructors

( define ( make−computer model

  • s )

( cons ’ ∗computer∗ ( cons

  • s

( cons model ( ) ) ) ) ) ( u n i f y ( make−computer x ’ l i n u x ) ( cons ’ ∗computer∗ ( cons ’ l i n u x ( cons ’ pc ( ) ) ) ) ) ( u n i f y ( cons ’ ∗computer∗ ( cons

  • s

( cons model ( ) ) ) ) ( cons ’ ∗computer∗ ( cons ’ l i n u x ( cons ’ pc ( ) ) ) ) )

= ⇒ os ← linux; model ← pc

( u n i f y model x )

= ⇒ x ← pc

( u n i f y

  • s

’ l i n u x )

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-48
SLIDE 48

Motivation Patternology Applications Implementation Building Patterns Unification

Unifying with Compound Deconstructors

( d e f i n e ( unify−compound−deconstructor var dtor

  • ther

env ) ( r e c e i v e ( rename−table renamed−body ) ( alpha−rename−pattern ( d e c o n s t r u c t o r : compound/body dtor ) ( improper−list−to−proper−list ( d e c o n s t r u c t o r : compound/ arguments dtor ) ) ) ( l e t (( env ( u n i f y renamed−body

  • ther

env ) ) ) ( l e t l p (( env env ) ( f o r m a l s ( d e c o n s t r u c t o r : compound/ arguments dtor )) ( args ( p a t t e r n : d e c o n s t r u c t o r / args var ) ) ) ( cond (( and ( n u l l ? f o r m a l s ) ( n u l l ? args )) env ) (( n u l l ? f o r m a l s ) ( e r r o r ”Wrong number

  • f

arguments ” )) (( n u l l ? args ) ( e r r o r ”Wrong number

  • f

arguments ” )) (( p a i r ? f o r m a l s ) ( l p ( u n i f y ( car args ) ( p a t t e r n : v a r i a b l e ( cdr ( assq ( car f o r m a l s ) rename−table ) ) ) env ) ( cdr f o r m a l s ) ( cdr args ) ) ) ( e l s e ( u n i f y ( p a t t e r n : v a r i a b l e ( cdr ( assq f o r m a l s rename−table ) ) ) ( list −to−patt er n−list args ) env ) ) ) ) ) ) ) Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-49
SLIDE 49

Motivation Patternology Applications Implementation Building Patterns Unification

Unification and the Power of Choice

pcase introduces nondeterminism — multiple possible results: ( u n i f y x ( pcase ’ foo ( ’ foo 1) ( 2 ) ) )

= ⇒ x can be either 1 or 2 Return failure continuation to try next possibility if unification fails down the line Environments now contain bindings and a failure continuation Unification becomes a search problem!

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-50
SLIDE 50

Motivation Patternology Applications Implementation Building Patterns Unification

Unification and the Power of Choice

Algorithm Try first clause

Install new failure continuation in environment Unify consequent with argument Unify condition with key

If failure, try next clause

Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes

slide-51
SLIDE 51

Motivation Patternology Applications Implementation Building Patterns Unification

Unification and the Power of Choice

( d e f i n e ( unify−pcase var

  • ther

env ) ( l e t (( var ( alpha−rename−pcase var ) ) ) ( l e t l p (( c l a u s e s ( p a t t e r n : pcase / c l a u s e s var ) ) ) ( i f ( n u l l ? c l a u s e s ) (( unifier−environment / f a i l env )) ( l e t (( env1 ( call−with−current−continuation ( lambda ( k ) ( unifier−environment / new−failure−continuation env ( lambda () ( k #f ) ) ) ) ) ) ) ( i f ( f a l s e ? env1 ) ( l p ( cdr c l a u s e s )) ( let∗ (( c l a u s e ( car c l a u s e s )) ( env2 ( u n i f y ( p a t t e r n : pcase : c l a u s e / consequent c l a u s e )

  • ther

env1 )) ( env3 ( u n i f y ( p a t t e r n : pcase : c l a u s e / c o n d i t i o n c l a u s e ) ( p a t t e r n : pcase / v a r i a b l e var ) env2 ) ) ) env3 ) ) ) ) ) ) ) Austin Clements, Irene Zhang, Dan Ports Plaid: Pattern Language for Abstract Datatypes