Lecture 2 :

Basics of Reflection
LSINF 2335
Programming Paradigms
Prof. Kim Mens (UCL)

with Prof. Roel Wuyts (IMEC)
Disclaimer: any pictures used in this presentation
remain with the copyright of their original owner and
were included here for didactical purposes only. In case
of any suspected violation of copyright please contact
me so that I can remove or replace those pictures.
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Remember these?
■ Last session
– Metaprogramming
– Computational reflection
■ This session
– Closer look at some essential properties of
• metaprogramming languages
• reflective languages
2
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
3
Metaprogramming:
definitions
■ To define the notion of metaprogramming more
clearly, let us define some important concepts:
– computational system
– programming language and program
– meta system
– meta programming language and meta program
– meta language and base language
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
4
Computational system
■ A computational system is a system that reasons
about and acts upon some part of the world, called
the domain of that system.
executor
program
data
manipulates
executes
system
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Domain of a computational system
■ The domain of a computational system or its universe
of discourse is the collection of elements (entities or
concepts) it can reason about.
■ Example: an address book application is a system that
reasons about a universe of discourse which contains
(real) persons, names, phone numbers, days, addresses,
etc.
■ The computational system manipulates representations
of these elements.
5
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
6
Computational system (example)
■ Domain : numbers and lists
■ Computation : sorting
executor
program
data
manipulates
executes
system
Java
quicksort
program
Java VMarray, int, ...
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
7
Program and language
■ A program is a formal, executable specification of
a computational system.
– for example, a Java quicksort program
■ A programming language is a formalism that can
be interpreted in an automatic manner in order to
obtain the computational system specified by the
program written in it.
– for example, the Java programming language
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
8
Meta system
■ A meta system is a system that has as its domain
another computational system, called its base
system.
executor
program
data
manipulates
executes
base
system
executor
program
data
manipulates
executes
meta
system
represents
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
9
Meta system
■ Somehow the data of the meta system needs to
represent programs of the base system.
executor
program
data
manipulates
executes
base
system
executor
program
data
manipulates
executes
meta
system
represents
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
10
Meta system (example)
CheckStyle
Java program
representation
executor
program
data
manipulates
executes
base
system
executor
program
data
manipulates
executes
meta
system
represents
Java
quicksort
program
Java VM
array, int, ...
CheckStyle
expression
CheckStyle
tool
“Checkstyle is a development
tool to help programmers
write Java code that adheres
to a coding standard. It
automates the process of
checking Java code to spare
humans of this boring (but
important) task. This makes it
ideal for projects that want to
enforce a coding standard.”
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Meta system (example)
CheckStyle
■ CheckStyle represents Java programs as parse
trees
■ Choosing a good representation is important
– alternative: represent Java programs as source code text
• pro: easy; no work (source is there)
• con: difficult to write advanced rules (lack of structure)
– parse tree structure
• pro: parse tree provides good structure (scoping!)
• con: need to have parser and visitor
11
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
12
Meta program
■ A meta program is the program specifying the
meta system of a computational system.
– The meta system does not directly manipulate its base-
system
– It is the meta program that manipulates programs of the
base system.
– The meta program needs some kind of explicit
representation of the base program
■ Example
– A Checkstyle expression to check for a particular code
smell like “duplicate code”
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
13
Meta program
■ A meta program is the program specifying the
meta system of a computational system.
executor
program
data
manipulates
executes
base
system
executor
program
data
manipulates
executes
meta
system
represents
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
14
Meta language and base language
■ Metaprogramming involves two languages
– Meta language in which the meta program is written
• can be a general-purpose programming language
• or specifically tuned for specifying meta programs
– example: CheckStyle
– Base language in which the base programs about which the
meta programs reason are written
• typically a meta language is specifically tuned towards a
particular base language
– Both languages have to be integrated somehow
• metalanguage needs explicit representation of (concepts
relevant to meta programs in) the base language
• for example a “meta-object protocol” (MOP)
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
15
Meta language and base language
■ Examples
– CheckStyle tool
• Goal: meta “program” checks conventions in Java programs
• Meta language = configuration files accepted by CheckStyle
• Base language = Java
– Aspect-oriented programming (example)
• Goal: meta-program “weaves” aspects in base program
• Meta language = AspectJ
• Base language = Java
– Reflection
• Goal: meta-program manipulates and modifies base program
• Meta program = Base program
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
16
Reflection: definitions
■ To define the notion of reflection more clearly, let
us define some important concepts:
– reflection
– reification
– causally connected
– reflective system
– introspection
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
17
Reflection
■ Reflection is the ability of a program to examine or
change its own implementation at runtime
– A reflective program is a meta program
• it is a program that manipulates a program
– A reflective program is its own meta program
• it is a program that manipulates itself
■ Reflection is an instance of metaprogramming where
metaprogram and base program are the same
– This is a necessary but not sufficient condition
– Something more is needed to obtain a fully reflective language
• a well-designed reflective architecture
• that is causally connected with the base program
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
18
Reflective system
■ A computational system is causally connected to
its domain if the computational system is linked
with its domain in such way that, if one of the two
changes, this leads to an effect on the other.
■ Example: robot arm
– Domain: numbers indicating position of the arm.
– Updating coordinates: robot arm moves.
– Moving the robot arm: updates coordinates
■ A reflective system is a causally connected meta
system that has as base system itself.
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Reflective system
19
executor
program
data
manipulates
executes
system
reifies
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
20
Reification (definition)
■ Definition (Merriam-Webster’s)
Re-ify
[verb] to regard (something abstract) as a material or concrete thing
■ Definition (Wikipedia)
Re-ify
[verb] to make an abstract concept or low-level implementation detail
of a programming language accessible to the programmer
■ In reflective languages, reification is the act of
making the language concepts available for
manipulation by the programmer
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
21
Reification
■ Every aspect of the internal workings of a computational
system that has an explicit representation in the data of
that system is said to be reified.
■ Examples
– the C programming language reifies the low-level detail of memory
addresses
• the abstract notion of memory addresses becomes
manipulatable by the programmer
– the Scheme programming language reifies continuations (the call
stack)
• the abstract concept of continuations becomes manipulatable
by the programmer
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
22
Reification in Java
■ Most aspects of the internal workings of the Java
virtual machine are absorbed (i.e. not reified)
■ But you can explicitly import a reflection API to open
up (some of) the inner workings of the VM
– java.lang
– java.lang.reflect
■ java.lang.Class reifies the notion of a Java class
– The abstract notion of a Java class becomes manipulatable
from within normal Java programs
• provides methods to examine the runtime properties of the object,
including its members and type information
• provides the ability to create new classes and objects
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
23
Reification in Java (example)
java.lang.Class
...
static Class           forName(String className)
Returns the Class object associated with the class or interface with the given string name.
...
Constructor[]         getConstructors()
Returns an array containing Constructor objects reflecting all the public constructors of the
class represented by this Class object.
...
Field[]                   getDeclaredFields()
Returns an array of Field objects reflecting all the fields declared by the class or interface
represented by this Class object.
...
Method                    getDeclaredMethod(String name, Class[] parameterTypes)
Returns a Method object that reflects the specified declared method of the class or interface
represented by this Class object.
...
Object                     newInstance()
Creates a new instance of the class represented by this Class object.
...
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Reflective system
24
executor
program
data
manipulates
executes
system
causal
connection
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Reflection, reification,
introspection & intercession
■ Reflective languages require language concepts
(meta-entities) to be reified (at base level) so
that they can be manipulated by programs as if
they were ordinary data.
■ Two parts of reflection:
– Introspection: only looking at these reified entities
• ex: inspecting the details of a class
– Intercession: you can change the program behaviour by
manipulating those entities
• ex: adding a method to a class
25
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
26
Introspective system
■ Reflection as defined means that the system can
inspect and change itself
– Sometimes only inspection is possible
■ An introspective system is a meta system that has
as base-system itself.
– So not necessarily causally connected
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
27
Types of Reflection
■ What may be reflected
– introspection
• you can access the representation, but not modify it
• ex.: java.lang.reflect
– structural reflection
• ex. : adding instance variables
– computational (behavourial) reflection
• ex.: modify method dispatching mechanism
– Can you think of other examples of each of these types
of reflection?
!
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
28
Types of Reflection
■ When does reflection take place
– Compile-time Reflection
• Customization takes place at compile-time.
• Pro: runtime performance and the ability to adapt its own
language (i.e., linguistic reflection).
– Runtime Reflection
• The system may be adapted at runtime, once it has been
created and run.
• Pro: greater adaptability
• Con: performance penalties.
• Typically used for computational reflection
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Implementing reflection
■ Use towers of interpreters
■ Analogy:
29
language = physics
language = electronics
language = ANSI C
language = Pic%
C
Smalltalk
a Smalltalk program
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Reflective tower of interpreters
■ Original model of reflection defined by Smith in 1982 for 3-LISP is
based on meta-level interpretation.
■ The program (level 0) is interpreted by an interpreter (level 1)
■ The interpreter (level 1) is interpreted by a metainterpreter (level 2)
■ Leading to a (potentially infinite) tower of interpreters each defining
the semantics of the program (the interpreter) it interprets
30
language = physics
language = electronics
language = ANSI C
language = Pic%3-LISP
a 3-LISP program
3-LISP
3-LISP
3-LISP
......
level 1
level 2
level 0
level 3
level 4
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Reflective architecture
■ A tower of interpreters is too slow in practice.
■ To enable reflection in mainstream languages like
CLOS, Smalltalk, Java or Ruby, the tower of
interpreters is replaced with a reflective
architecture consisting of meta-objects that model
and control the different aspects of reflection
offered by the language.
■ These languages are said to have a meta-object
protocol or MOP (see later).
31
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
32
Remember
■ With meta-programming, one system reasons
about another
– even if both systems are implemented in a same language
– one system is meta for the other
■ With reflection, the system reasons about itself
– only one system is involved
– and consequently only one language is involved
• the language is said to be reflective or not
• or exhibits some reflective aspects...
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Things to remember
■ Meta vs. base language/program
■ Reflection vs. metaprogramming
■ Reification & causal connection
■ Different kinds of reflection
■ Towers of reflective interpreters vs. reflective
architecture
33
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Suggested Reading
■ B. C. Smith. Reflection and semantics in a procedural language. Technical
Report 272, Massachusetts Institute of Technology, Laboratory for
Computer Science, 1982.
■ P. Maes. Concepts and experiments in computational reflection. Proceedings
of OOPSLA 1987, ACM SIGPLAN Notices, vol. 22, pp. 147–155, 1987.
■ G. Kiczales, J.d. Rivières, D.G. Bobrow. The Art of the Metaobject
Protocol. MIT Press, 1991.
■ J. Malenfant, M. Jacques, F.-N. Demers. A tutorial on behavioral reflection
and its implementation. Proceedings of 1st International Conference on
Computational Reflection (G. Kiczales, ed.), pp. 1–20, Apr. 1996.
■ M. Denker, M. Suen, S. Ducasse. The Meta in Meta-object Architectures.
Proceedings of TOOLS EUROPE 20081, pp. 218–237, 2008.
■ C. Herzeel, P. Costanza, T. D’Hondt. Reflection for the Masses. In
Proceedings of S3 2008, Springer LNCS 5146, pp. 87–122, 2008.

http://www.p-cos.net/documents/s32008.pdf
34
LSINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Homework
■ Explain, in your own words, what the key difference is between
“metaprogramming” and “reflection”. Illustrate with examples
taken from your own experience.
■ Can we consider a program like CheckStyle as a reflective
program since it could be used to check its own style?
■ What does “reification” mean? Illustrate with an example.
■ Explain, in your own words, the key difference between
“introspection” and “intercession” and give an example of each,
taken from your own experience.
■ Read the article “Reflection for the Masses” and write a 2 page
summary
35

Basics of reflection

  • 1.
    Lecture 2 :
 Basicsof Reflection LSINF 2335 Programming Paradigms Prof. Kim Mens (UCL)
 with Prof. Roel Wuyts (IMEC) Disclaimer: any pictures used in this presentation remain with the copyright of their original owner and were included here for didactical purposes only. In case of any suspected violation of copyright please contact me so that I can remove or replace those pictures.
  • 2.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Remember these? ■ Lastsession – Metaprogramming – Computational reflection ■ This session – Closer look at some essential properties of • metaprogramming languages • reflective languages 2
  • 3.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 3 Metaprogramming: definitions ■ To definethe notion of metaprogramming more clearly, let us define some important concepts: – computational system – programming language and program – meta system – meta programming language and meta program – meta language and base language
  • 4.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 4 Computational system ■ Acomputational system is a system that reasons about and acts upon some part of the world, called the domain of that system. executor program data manipulates executes system
  • 5.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Domain of acomputational system ■ The domain of a computational system or its universe of discourse is the collection of elements (entities or concepts) it can reason about. ■ Example: an address book application is a system that reasons about a universe of discourse which contains (real) persons, names, phone numbers, days, addresses, etc. ■ The computational system manipulates representations of these elements. 5
  • 6.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 6 Computational system (example) ■Domain : numbers and lists ■ Computation : sorting executor program data manipulates executes system Java quicksort program Java VMarray, int, ...
  • 7.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 7 Program and language ■A program is a formal, executable specification of a computational system. – for example, a Java quicksort program ■ A programming language is a formalism that can be interpreted in an automatic manner in order to obtain the computational system specified by the program written in it. – for example, the Java programming language
  • 8.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 8 Meta system ■ Ameta system is a system that has as its domain another computational system, called its base system. executor program data manipulates executes base system executor program data manipulates executes meta system represents
  • 9.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 9 Meta system ■ Somehowthe data of the meta system needs to represent programs of the base system. executor program data manipulates executes base system executor program data manipulates executes meta system represents
  • 10.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 10 Meta system (example) CheckStyle Javaprogram representation executor program data manipulates executes base system executor program data manipulates executes meta system represents Java quicksort program Java VM array, int, ... CheckStyle expression CheckStyle tool “Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.”
  • 11.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Meta system(example) CheckStyle ■ CheckStyle represents Java programs as parse trees ■ Choosing a good representation is important – alternative: represent Java programs as source code text • pro: easy; no work (source is there) • con: difficult to write advanced rules (lack of structure) – parse tree structure • pro: parse tree provides good structure (scoping!) • con: need to have parser and visitor 11
  • 12.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 12 Meta program ■ Ameta program is the program specifying the meta system of a computational system. – The meta system does not directly manipulate its base- system – It is the meta program that manipulates programs of the base system. – The meta program needs some kind of explicit representation of the base program ■ Example – A Checkstyle expression to check for a particular code smell like “duplicate code”
  • 13.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 13 Meta program ■ Ameta program is the program specifying the meta system of a computational system. executor program data manipulates executes base system executor program data manipulates executes meta system represents
  • 14.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 14 Meta language andbase language ■ Metaprogramming involves two languages – Meta language in which the meta program is written • can be a general-purpose programming language • or specifically tuned for specifying meta programs – example: CheckStyle – Base language in which the base programs about which the meta programs reason are written • typically a meta language is specifically tuned towards a particular base language – Both languages have to be integrated somehow • metalanguage needs explicit representation of (concepts relevant to meta programs in) the base language • for example a “meta-object protocol” (MOP)
  • 15.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 15 Meta language andbase language ■ Examples – CheckStyle tool • Goal: meta “program” checks conventions in Java programs • Meta language = configuration files accepted by CheckStyle • Base language = Java – Aspect-oriented programming (example) • Goal: meta-program “weaves” aspects in base program • Meta language = AspectJ • Base language = Java – Reflection • Goal: meta-program manipulates and modifies base program • Meta program = Base program
  • 16.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 16 Reflection: definitions ■ Todefine the notion of reflection more clearly, let us define some important concepts: – reflection – reification – causally connected – reflective system – introspection
  • 17.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 17 Reflection ■ Reflection isthe ability of a program to examine or change its own implementation at runtime – A reflective program is a meta program • it is a program that manipulates a program – A reflective program is its own meta program • it is a program that manipulates itself ■ Reflection is an instance of metaprogramming where metaprogram and base program are the same – This is a necessary but not sufficient condition – Something more is needed to obtain a fully reflective language • a well-designed reflective architecture • that is causally connected with the base program
  • 18.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 18 Reflective system ■ Acomputational system is causally connected to its domain if the computational system is linked with its domain in such way that, if one of the two changes, this leads to an effect on the other. ■ Example: robot arm – Domain: numbers indicating position of the arm. – Updating coordinates: robot arm moves. – Moving the robot arm: updates coordinates ■ A reflective system is a causally connected meta system that has as base system itself.
  • 19.
  • 20.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 20 Reification (definition) ■ Definition(Merriam-Webster’s) Re-ify [verb] to regard (something abstract) as a material or concrete thing ■ Definition (Wikipedia) Re-ify [verb] to make an abstract concept or low-level implementation detail of a programming language accessible to the programmer ■ In reflective languages, reification is the act of making the language concepts available for manipulation by the programmer
  • 21.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 21 Reification ■ Every aspectof the internal workings of a computational system that has an explicit representation in the data of that system is said to be reified. ■ Examples – the C programming language reifies the low-level detail of memory addresses • the abstract notion of memory addresses becomes manipulatable by the programmer – the Scheme programming language reifies continuations (the call stack) • the abstract concept of continuations becomes manipulatable by the programmer
  • 22.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 22 Reification in Java ■Most aspects of the internal workings of the Java virtual machine are absorbed (i.e. not reified) ■ But you can explicitly import a reflection API to open up (some of) the inner workings of the VM – java.lang – java.lang.reflect ■ java.lang.Class reifies the notion of a Java class – The abstract notion of a Java class becomes manipulatable from within normal Java programs • provides methods to examine the runtime properties of the object, including its members and type information • provides the ability to create new classes and objects
  • 23.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 23 Reification in Java(example) java.lang.Class ... static Class           forName(String className) Returns the Class object associated with the class or interface with the given string name. ... Constructor[]         getConstructors() Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object. ... Field[]                   getDeclaredFields() Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. ... Method                    getDeclaredMethod(String name, Class[] parameterTypes) Returns a Method object that reflects the specified declared method of the class or interface represented by this Class object. ... Object                     newInstance() Creates a new instance of the class represented by this Class object. ...
  • 24.
  • 25.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Reflection, reification, introspection& intercession ■ Reflective languages require language concepts (meta-entities) to be reified (at base level) so that they can be manipulated by programs as if they were ordinary data. ■ Two parts of reflection: – Introspection: only looking at these reified entities • ex: inspecting the details of a class – Intercession: you can change the program behaviour by manipulating those entities • ex: adding a method to a class 25
  • 26.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 26 Introspective system ■ Reflectionas defined means that the system can inspect and change itself – Sometimes only inspection is possible ■ An introspective system is a meta system that has as base-system itself. – So not necessarily causally connected
  • 27.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 27 Types of Reflection ■What may be reflected – introspection • you can access the representation, but not modify it • ex.: java.lang.reflect – structural reflection • ex. : adding instance variables – computational (behavourial) reflection • ex.: modify method dispatching mechanism – Can you think of other examples of each of these types of reflection? !
  • 28.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 28 Types of Reflection ■When does reflection take place – Compile-time Reflection • Customization takes place at compile-time. • Pro: runtime performance and the ability to adapt its own language (i.e., linguistic reflection). – Runtime Reflection • The system may be adapted at runtime, once it has been created and run. • Pro: greater adaptability • Con: performance penalties. • Typically used for computational reflection
  • 29.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Implementing reflection ■ Usetowers of interpreters ■ Analogy: 29 language = physics language = electronics language = ANSI C language = Pic% C Smalltalk a Smalltalk program
  • 30.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Reflective tower ofinterpreters ■ Original model of reflection defined by Smith in 1982 for 3-LISP is based on meta-level interpretation. ■ The program (level 0) is interpreted by an interpreter (level 1) ■ The interpreter (level 1) is interpreted by a metainterpreter (level 2) ■ Leading to a (potentially infinite) tower of interpreters each defining the semantics of the program (the interpreter) it interprets 30 language = physics language = electronics language = ANSI C language = Pic%3-LISP a 3-LISP program 3-LISP 3-LISP 3-LISP ...... level 1 level 2 level 0 level 3 level 4
  • 31.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Reflective architecture ■ Atower of interpreters is too slow in practice. ■ To enable reflection in mainstream languages like CLOS, Smalltalk, Java or Ruby, the tower of interpreters is replaced with a reflective architecture consisting of meta-objects that model and control the different aspects of reflection offered by the language. ■ These languages are said to have a meta-object protocol or MOP (see later). 31
  • 32.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. 32 Remember ■ With meta-programming,one system reasons about another – even if both systems are implemented in a same language – one system is meta for the other ■ With reflection, the system reasons about itself – only one system is involved – and consequently only one language is involved • the language is said to be reflective or not • or exhibits some reflective aspects...
  • 33.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Things to remember ■Meta vs. base language/program ■ Reflection vs. metaprogramming ■ Reification & causal connection ■ Different kinds of reflection ■ Towers of reflective interpreters vs. reflective architecture 33
  • 34.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Suggested Reading ■ B.C. Smith. Reflection and semantics in a procedural language. Technical Report 272, Massachusetts Institute of Technology, Laboratory for Computer Science, 1982. ■ P. Maes. Concepts and experiments in computational reflection. Proceedings of OOPSLA 1987, ACM SIGPLAN Notices, vol. 22, pp. 147–155, 1987. ■ G. Kiczales, J.d. Rivières, D.G. Bobrow. The Art of the Metaobject Protocol. MIT Press, 1991. ■ J. Malenfant, M. Jacques, F.-N. Demers. A tutorial on behavioral reflection and its implementation. Proceedings of 1st International Conference on Computational Reflection (G. Kiczales, ed.), pp. 1–20, Apr. 1996. ■ M. Denker, M. Suen, S. Ducasse. The Meta in Meta-object Architectures. Proceedings of TOOLS EUROPE 20081, pp. 218–237, 2008. ■ C. Herzeel, P. Costanza, T. D’Hondt. Reflection for the Masses. In Proceedings of S3 2008, Springer LNCS 5146, pp. 87–122, 2008.
 http://www.p-cos.net/documents/s32008.pdf 34
  • 35.
    LSINF2335:Prog.Paradigms: Theory,Pract.andApplic. Homework ■ Explain, inyour own words, what the key difference is between “metaprogramming” and “reflection”. Illustrate with examples taken from your own experience. ■ Can we consider a program like CheckStyle as a reflective program since it could be used to check its own style? ■ What does “reification” mean? Illustrate with an example. ■ Explain, in your own words, the key difference between “introspection” and “intercession” and give an example of each, taken from your own experience. ■ Read the article “Reflection for the Masses” and write a 2 page summary 35