38
Meta-Matters in Squeak Andrew P. Black 1 CS410/510 Advanced Programming

CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Meta-Matters in SqueakAndrew P. Black

1

CS410/510 Advanced Programming

Page 2: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

What's Meta?

• Metaprogramming is the act of writing a program that writes or manipulates another program… or themselves

• Why not? After all programs are just data!

2

Page 3: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Simple Example• In the visitor pattern last week:

3

Page 4: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Simple Example• In the visitor pattern last week:

Solution (continued)

2. Every class Foo in the hierarchy gets a method accept: aVisitor defined as follows:

Note how the selector of the message tells

the visitor what kind of node it is visiting

7

Foo ›› accept: aVisitor

↑ aVisitor visitFoo: self

3

Page 5: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Simple Example• In the visitor pattern last week:

Solution (continued)

2. Every class Foo in the hierarchy gets a method accept: aVisitor defined as follows:

Note how the selector of the message tells

the visitor what kind of node it is visiting

7

Foo ›› accept: aVisitor

↑ aVisitor visitFoo: self

3

We wrote these methods with a metaprogram:

Expression allSubclassesDo: [ :each | each compile: 'accept: aVisitor ↑ aVisitor visit', (each name), ': self ' classified: 'visiting']

Page 6: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Alternative Solution• Instead of writing a separate program to

write our program, we could make the program write itself:• Put the following single method at the root of

the hierarchy:

4

Expression ›› accept: aVisitor ↑ aVisitor perform: ('visit', (self class name), ':') asSymbol

with: self

• This is a reflective program — one that writes itself dynamically

Page 7: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Another Example

• We know that we can write this:(1 to: 10) select: [ :x | x even]

• How about this?(1 to: 10) select even

• Can we make this work? What about other unary messages (odd, isPrime, …)?

5

Page 8: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Summary of Solution

• (1 to: 10) select must answer an object that "remembers" the collection and the fact that we plan to do a select: operation

• This object is called a Trampoline

• How can we make the trampoline understand even, odd, isPrime, factorial …

• Reflection!

6

Page 9: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Another Example• Remember ParserFun ?

7

Page 10: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Another Example• Remember ParserFun ?

7

Page 11: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Another Example• Remember ParserFun ?

7

Page 12: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Another Example• Remember ParserFun ?

7

Page 13: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Another Example• Remember ParserFun ?

7

Page 14: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Naming Parsers• The name given to the parser is often

(but not always) the same as the selector of the method that builds it.

8

Page 15: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Naming Parsers• The name given to the parser is often

(but not always) the same as the selector of the method that builds it.

8

Page 16: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Naming Parsers• The name given to the parser is often

(but not always) the same as the selector of the method that builds it.

8

Page 17: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Naming Parsers• The name given to the parser is often

(but not always) the same as the selector of the method that builds it.

8

Page 18: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Naming Parsers• The name given to the parser is often

(but not always) the same as the selector of the method that builds it.

8

Page 19: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Obtain the name reflexively

9

Page 20: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Obtain the name reflexively

9

Page 21: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Obtain the name reflexively

9

Page 22: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Obtain the name reflexively

9

Page 23: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Obtain the name reflexively

9

Page 24: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Obtain the name reflexively

9

Page 25: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Obtain the name reflexively

9

• There are only a few combinators that build parsers• Each combinator can extract the name from the context

Page 26: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Structural Equality

• We say how to build a recursive equality operation in Haskell that reaches down into the structure of a data type

• Can we do the same in Squeak?• How is equality defined in Object?

10

Page 27: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Try a new Equality Operation

11

Page 28: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

How does === work out?

12

Page 29: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

What about zipAllWith: ?

13

• We would like to be able to write{ $a to: $z . $A to: $Z } zipAllWith: [ :lo :up|String with: lo with: up ]

for n collections and any n argument block

• Can we do it?

Page 30: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Smalltalk Browsers

• There are lots of different browsers in the Smalltalk environment• system browser, hierarchy browser, protocol browser,

inheritance browser, … inspector, explorer, change set browser, file system browser

• Each one “knows” about the structure that it is browsing• e.g., the system browser has hardwired into its code the

facts that Categories contain Classes and Classes contain Protocols and Protocols contain methods

14

Page 31: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

The OmniBrowser• The OmniBrowser is a browser for everything

and nothing in particular• it doesn’t “know” about any system structure

• instead, it is driven by metadata that describes the thing that it is browsing

• The metadata takes the form of a graph of objects — the metagraph

• The domain that the browser navigates is also a graph of objects — the subject graph

15

Page 32: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

A File System Browser• We will build an instance of the OmniBrowser

that examines the file system

• The file system is not a graph of objects

• That’s OK: we build OBNodes to represent the entities that we are browsing

• We define two subclasses of OBNode: OBDirectoryNode and OBFileNode

• What do these OBNodes have to do?

• that is defined by the metagraph

16

Page 33: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

File System: Graph & Metagraph

17

File

Directory

#files

N metanode

is a child of

#directories

N object node

/

/temp pic1.jpg

pic2.jpg pic3.jpg

(a) (b)

Figure 2: A graph describing a filesystem (a) and its corresponding metagraph(b).

FileNode>>setPath: newPathpath := newPath

FileNode>>path^ path

FileNode class>>on: path^ self new setPath: path

FileNode>>name^ (FileDirectory directoryEntryFor: self path) name.

FileNode subclass: #DirectoryNodeinstanceVariableNames: ’’ ...

The abstract relationships between di!erent types of nodes within the browserare defined using another type of graph, a metagraph. In a metagraph each typeof node of an object graph is represented by a metanode. As a whole, the meta-graph describes the structure of the object graph navigated by the browser.Edges between metanodes define which kinds of nodes may be connected towhich other kinds of nodes.“Kind of node” is a notion that is formalized byobject node classes; typically, there is a metanode for each node class, as is thecase in our file browser.

The metagraph for the file browser is shown in Figure 2 (b). There are twometanodes for the two types of nodes in the object graph. A directory maycontain other directories and files, the two edges in the metagraph representthese two relationships.

OmniBrowser opts to store metagraphs in a central place. New metagraphsshould be returned from class methods of the class OBMetagraph. Thus weconstruct the file browser metagraph in the method #fileBrowser:

5

Page 34: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

File System: Graph & Metagraph

17

File

Directory

#files

N metanode

is a child of

#directories

N object node

/

/temp pic1.jpg

pic2.jpg pic3.jpg

(a) (b)

Figure 2: A graph describing a filesystem (a) and its corresponding metagraph(b).

FileNode>>setPath: newPathpath := newPath

FileNode>>path^ path

FileNode class>>on: path^ self new setPath: path

FileNode>>name^ (FileDirectory directoryEntryFor: self path) name.

FileNode subclass: #DirectoryNodeinstanceVariableNames: ’’ ...

The abstract relationships between di!erent types of nodes within the browserare defined using another type of graph, a metagraph. In a metagraph each typeof node of an object graph is represented by a metanode. As a whole, the meta-graph describes the structure of the object graph navigated by the browser.Edges between metanodes define which kinds of nodes may be connected towhich other kinds of nodes.“Kind of node” is a notion that is formalized byobject node classes; typically, there is a metanode for each node class, as is thecase in our file browser.

The metagraph for the file browser is shown in Figure 2 (b). There are twometanodes for the two types of nodes in the object graph. A directory maycontain other directories and files, the two edges in the metagraph representthese two relationships.

OmniBrowser opts to store metagraphs in a central place. New metagraphsshould be returned from class methods of the class OBMetagraph. Thus weconstruct the file browser metagraph in the method #fileBrowser:

5

directory

Page 35: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

File System: Graph & Metagraph

17

File

Directory

#files

N metanode

is a child of

#directories

N object node

/

/temp pic1.jpg

pic2.jpg pic3.jpg

(a) (b)

Figure 2: A graph describing a filesystem (a) and its corresponding metagraph(b).

FileNode>>setPath: newPathpath := newPath

FileNode>>path^ path

FileNode class>>on: path^ self new setPath: path

FileNode>>name^ (FileDirectory directoryEntryFor: self path) name.

FileNode subclass: #DirectoryNodeinstanceVariableNames: ’’ ...

The abstract relationships between di!erent types of nodes within the browserare defined using another type of graph, a metagraph. In a metagraph each typeof node of an object graph is represented by a metanode. As a whole, the meta-graph describes the structure of the object graph navigated by the browser.Edges between metanodes define which kinds of nodes may be connected towhich other kinds of nodes.“Kind of node” is a notion that is formalized byobject node classes; typically, there is a metanode for each node class, as is thecase in our file browser.

The metagraph for the file browser is shown in Figure 2 (b). There are twometanodes for the two types of nodes in the object graph. A directory maycontain other directories and files, the two edges in the metagraph representthese two relationships.

OmniBrowser opts to store metagraphs in a central place. New metagraphsshould be returned from class methods of the class OBMetagraph. Thus weconstruct the file browser metagraph in the method #fileBrowser:

5

directory

directory

Page 36: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

File System: Graph & Metagraph

17

File

Directory

#files

N metanode

is a child of

#directories

N object node

/

/temp pic1.jpg

pic2.jpg pic3.jpg

(a) (b)

Figure 2: A graph describing a filesystem (a) and its corresponding metagraph(b).

FileNode>>setPath: newPathpath := newPath

FileNode>>path^ path

FileNode class>>on: path^ self new setPath: path

FileNode>>name^ (FileDirectory directoryEntryFor: self path) name.

FileNode subclass: #DirectoryNodeinstanceVariableNames: ’’ ...

The abstract relationships between di!erent types of nodes within the browserare defined using another type of graph, a metagraph. In a metagraph each typeof node of an object graph is represented by a metanode. As a whole, the meta-graph describes the structure of the object graph navigated by the browser.Edges between metanodes define which kinds of nodes may be connected towhich other kinds of nodes.“Kind of node” is a notion that is formalized byobject node classes; typically, there is a metanode for each node class, as is thecase in our file browser.

The metagraph for the file browser is shown in Figure 2 (b). There are twometanodes for the two types of nodes in the object graph. A directory maycontain other directories and files, the two edges in the metagraph representthese two relationships.

OmniBrowser opts to store metagraphs in a central place. New metagraphsshould be returned from class methods of the class OBMetagraph. Thus weconstruct the file browser metagraph in the method #fileBrowser:

5

directory

directory

Page 37: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Metagraph as data

18

Page 38: CS410/510 Advanced Programming Meta-Matters in Squeakweb.cecs.pdx.edu/~cs410aph/Lectures/Meta Matters/Meta.pdf · 2006-06-08 · • Metaprogramming is the act of writing a program

Metagraph as data

18

File

Directory

#files

N metanode

is a child of

#directories

N object node

/

/temp pic1.jpg

pic2.jpg pic3.jpg

(a) (b)

Figure 2: A graph describing a filesystem (a) and its corresponding metagraph(b).

FileNode>>setPath: newPathpath := newPath

FileNode>>path^ path

FileNode class>>on: path^ self new setPath: path

FileNode>>name^ (FileDirectory directoryEntryFor: self path) name.

FileNode subclass: #DirectoryNodeinstanceVariableNames: ’’ ...

The abstract relationships between di!erent types of nodes within the browserare defined using another type of graph, a metagraph. In a metagraph each typeof node of an object graph is represented by a metanode. As a whole, the meta-graph describes the structure of the object graph navigated by the browser.Edges between metanodes define which kinds of nodes may be connected towhich other kinds of nodes.“Kind of node” is a notion that is formalized byobject node classes; typically, there is a metanode for each node class, as is thecase in our file browser.

The metagraph for the file browser is shown in Figure 2 (b). There are twometanodes for the two types of nodes in the object graph. A directory maycontain other directories and files, the two edges in the metagraph representthese two relationships.

OmniBrowser opts to store metagraphs in a central place. New metagraphsshould be returned from class methods of the class OBMetagraph. Thus weconstruct the file browser metagraph in the method #fileBrowser:

5