Computational Reflection and Context-Oriented Programming

34
Invited lecture at Advanced Development Techniques PhD School in Computer Science University of Milan 3–9 July 2012 Computational Reflection and Context-Oriented Programming Kim Mens Sebastián González

Transcript of Computational Reflection and Context-Oriented Programming

Page 1: Computational Reflection and Context-Oriented Programming

Invited lecture at

Advanced Development Techniques

PhD School in Computer ScienceUniversity of Milan

3–9 July 2012

Computational Reflection andContext-Oriented Programming

Kim MensSebastián González

Page 3: Computational Reflection and Context-Oriented Programming

Main Schedule

1.Reflectiona. Principlesb. In Smalltalk

2.Context-OrientedProgramming

a. Infrastructureb. Adaptationc. Compositiond. Resolution

day 1day 2

day 3

day 4

Page 4: Computational Reflection and Context-Oriented Programming

Today’s Schedule

A. Course OverviewB. Introduction to Reflection

and MetaprogrammingC. Introduction to SmalltalkD. Principles of Reflection

Page 5: Computational Reflection and Context-Oriented Programming

Course Overview

Page 6: Computational Reflection and Context-Oriented Programming

Course Objectives 6

‣ Introduction to the novel programming paradigm of context-oriented programming

‣ An introduction to reflection and meta-programming,the Smalltalk programming languageand context-oriented programming

‣ Hands-on experience on reflection in Smalltalk to build your own context-oriented framework

‣ Based on a larger course on programming paradigms

Page 7: Computational Reflection and Context-Oriented Programming

Course Schedule 7

Day Theory Practice

Tuesday July 3

Course introduction /Tuesday July 3

Basics of reflection /Tuesday July 3

Introduction to SmalltalkIntroduction to Smalltalk

Wednesday July 3

Reflection in SmalltalkReflection in Smalltalk

Thursday July 5

Context-oriented programming

Why? What? How?

InfrastructureThursday July 5

Context-oriented programming

Why? What? How? Adaptation

Monday July 9

/ CompositionMonday July 9 / Resolution

Page 8: Computational Reflection and Context-Oriented Programming

Regarding the Hands-on Sessions 8

‣Will be based on Pharo

‣an open source implementation of the Smalltalk language

‣http://pharo-project.org

‣ Read the free online book

‣Pharo by Example

‣http://pharobyexample.org

‣ If you haven’t done so yet, please try it out

‣ Ideally, before the first practical session, you should have

‣downloaded and installed Pharo

‣ read and tried out the first chapter of the Pharo book

Page 9: Computational Reflection and Context-Oriented Programming

Course Evaluation 9

‣ Two possibilities :

‣ Either a practical assignment that builds upon the hands-on sessions :

‣ develop an extension of the COP framework seen in the course

‣ Or a more theoretical evaluation :

‣ read scientific articles on COP and produce a report that compares and discusses existing COP approaches

‣Make your choice as soon as possible,so that we can prepare the assignment

Page 10: Computational Reflection and Context-Oriented Programming

Practical Assignment 10

‣ About the practical assignment...

‣ Assignment

‣During the practical sessions we start developing a COP framework in Smalltalk, making use of reflection

‣Afterwards you will need to extend this context-oriented framework with some additional features

‣Details will follow later

‣ Report

‣ In an accompanying report, discuss what/how reflective features of Smalltalk helped you in developing this assignment

‣ Illustrate with concrete code fragments from your assignment

Page 11: Computational Reflection and Context-Oriented Programming

Theoretical Evaluation 11

‣ to be determined ...

Page 12: Computational Reflection and Context-Oriented Programming

Introduction to Reflection

Page 13: Computational Reflection and Context-Oriented Programming

Reflection in language 13

‣ Meaning :

‣ In English, one of the meanings of reflection is “introspection” : contemplation of one-self

‣ In other words, reflection is the ability of a thing to talk about itself

‣ Reflection in natural language‣ “This sentence contains 37 characters.”‣ “This sentence contains 27 letters.”

‣ Reflection can easily introduce paradoxes‣ “This sentence is false.”‣Suppose true. Then claim is correct. So must be false.

Therefore contradiction.‣Suppose false. Then claim is wrong. So must be true.

Therefore contradiction.

Page 14: Computational Reflection and Context-Oriented Programming

Reflection in art 14

Page 15: Computational Reflection and Context-Oriented Programming

Reflection in art 15

Page 16: Computational Reflection and Context-Oriented Programming

Reflection in computing 16

‣ In computer science, computational reflection is the ability of a program to examine and control its own implementation

‣ Reflective programming‣ is the programming paradigm driven by reflection‣ is a special case of metaprogramming*

‣ Computational reflection is good for (amongst others) :

‣ extending a language / language design‣ building programming environments‣ advanced software development tools‣ building knowledge and learning systems‣ self-modifying / self-optimizing applications

* see later

Page 17: Computational Reflection and Context-Oriented Programming

Reflection in computing 17

‣ Reflection is a pretty advanced feature

‣ requires strong grasp of the fundamentals of the language

‣allows to solve problems elegantly, that were

‣previously handled on an ad-hoc basis

‣or hard / impossible to achieve

‣ Be careful not to introduce paradoxes

‣ It is possible to write programs that modify themselves while being executed

‣ If you’re not careful this can lead to weird situations

‣Here’s an example you all know:

‣Modifying a data-structure while iterating over it

‣And that’s not even reflection...

* see later

Page 18: Computational Reflection and Context-Oriented Programming

Reflective languages 18

‣Many programmers (and some programming languages) do not fully recognise the importance of reflection

‣ for example, debugging a program by manually adding debugging statements all over

‣ Some languages provide some types of reflection

‣ for debugging or error handling only

‣or only introspection but no real modification of existing programs

‣ Aspect-oriented languages take it a step further

‣but still limited by the set of constructs they offer

Page 19: Computational Reflection and Context-Oriented Programming

Reflective languages 19

‣ Fully reflective languages offer the full power of reflection

‣allow to reason about and modify nearly any aspect of a running program

‣ Today, many programming languages offer interesting reflective capabilities

‣Common in high-level virtual machine programming languages like Smalltalk

‣Ruby has quite powerful reflective features too

‣Note that Java is not fully reflective (mostly introspection)

‣Reflection is less common in lower-level programming languages like C

Page 20: Computational Reflection and Context-Oriented Programming

Reflective languages 20

‣ Fully reflective languages offer the full power of reflection

‣allow to reason about and modify nearly any aspect of a running program

‣ Have you ever thought of...

‣ writing a program to generate a visitor class, given a class hierarchy ?

‣ adding methods on a class while your code is running ?

‣ finding out all the instances of a particular class at runtime, or

‣ all messages understood by an object ?

‣ adding new keywords to your language ?

‣ In a fully reflective language like Smalltalk you can do all that...

‣ ... and more.

Page 21: Computational Reflection and Context-Oriented Programming

Metaprogramming 21

‣ In computer science, computational reflection is the ability of a program to examine and control its own implementation

‣ Reflective programming‣ is the programming paradigm driven by reflection‣ is a special case of metaprogramming

‣ Computational reflection is good for (amongst others) :

‣ extending a language / language design‣ building programming environments‣ advanced software development tools‣ building knowledge and learning systems‣ self-modifying / self-optimizing applications

Page 22: Computational Reflection and Context-Oriented Programming

What is “meta” ? 22

‣ In Greek‣ The preposition μετά means “after”, “beside” or “with”

‣ In English‣ the prefix “meta-” means “about (its own category)”

‣ for example, metadata are data about data ‣ who has produced it, when, what format are the data in, ...

‣ Douglas Hofstadter popularised the meaning of the term

‣ For example, “going meta”

‣ rethorical trick to take a debate or analysis to a higher abstraction level‣ “This discussion isn’t really going anywhere.”

‣His books “Gödel, Escher, Bach” and “Metamagical Themas” are a must-read (for any self-reflecting computer scientist)

Page 23: Computational Reflection and Context-Oriented Programming

What is “meta” ? 23

‣ In computer science‣metaprogramming is the writing of computer programs

that write or manipulate other programs as their data

‣ Some examples of metaprogramming

‣ a compiler

‣ an interpreter

‣ a code analysis tool

‣ a code generator

‣ Reflective programming is a case of metaprogramming

‣where a program is its own metaprogram

‣ i.e., when a program manipulates itself

Page 24: Computational Reflection and Context-Oriented Programming

Quine (the philosopher) 24

‣ Willard Van Orman Quine (1908–2000)

‣ Philosopher who made an extensive study of indirect self-reference

‣ Quine’s paradox‣ ‘Yields falsehood when preceded by its quotation’‣ Can you see the paradox?

Page 25: Computational Reflection and Context-Oriented Programming

Quine (the philosopher) 25

‣ Willard Van Orman Quine (1908–2000)

‣ Philosopher who made an extensive study of indirect self-reference

‣ Quine’s paradox‣ ‘Yields falsehood when preceded by its quotation’‣ Can you see the paradox?‣ “ ‘Yields falsehood when preceded by its quotation’ yields

falsehood when preceded by its quotation ”‣ Suppose true. Then sentence states falsehood.

Contradiction.‣ Suppose false. Then sentence is wrong. So it does yield

falsehood. So sentence must be true. Contradiction.

Page 26: Computational Reflection and Context-Oriented Programming

Quine (programming) 26

‣ In computing a quine is a program that produces its complete source code as its only output

‣ A quine is a metaprogram‣ can be a reflective program

‣ Some restrictions‣ program cannot take input‣ empty programs are not allowed‣The empty quine once won the “worst abuse of the

rules” price in the Obfuscated C contest

‣ The Quine page‣ http://www.nyx.net/~gthompso/quine.htm‣ http://en.wikipedia.org/wiki/Quine_(computing)

Page 27: Computational Reflection and Context-Oriented Programming

Bootstrapping 27

‣ An allusion to lift yourself up by your own bootstraps‣ Bootstrapping means using a special process to perform a

task that one would be unable to do in general‣ for example, writing a compiler in terms of itself‣ start with a simple compiler, compile a more complex

version in that one ‣When write a Quine‣ The program has a code part and a data part‣ The data part represents the entire code‣ But the code contains the data part ‣ Paradox: how can the data part contain itself?‣ Bootstrapping needed:‣ put special character in data part‣ replace that character by the data part itself

Page 28: Computational Reflection and Context-Oriented Programming

Quine (C) 28

/* A simple quine (self-printing program), in standard C. *//* Note: in designing this quine, we have tried to make the code clear * and readable, not concise and obscure as many quines are, so that * the general principle can be made clear at the expense of length. * In a nutshell: use the same data structure (called "progdata" * below) to output the program code (which it represents) and its own * textual representation. */

#include <stdio.h>

void quote(const char *s)

/* This function takes a character string s and prints the * textual representation of s as it might appear formatted * in C code. */

{

int i;

printf(" \"");

...

Page 29: Computational Reflection and Context-Oriented Programming

Quine (C) 29

for (i=0; s[i]; ++i) { /* Certain characters are quoted. */ if (s[i] == '\\') printf("\\\\"); else if (s[i] == '"') printf("\\\""); else if (s[i] == '\n') printf("\\n"); /* Others are just printed as such. */ else printf("%c", s[i]); /* Also insert occasional line breaks. */ if (i % 48 == 47) printf("\"\n \""); } printf("\"");} ...

Page 30: Computational Reflection and Context-Oriented Programming

Quine (C) 30

/* What follows is a string representation of the program code, * from beginning to end (formatted as per the quote() function * above), except that the string _itself_ is coded as two * consecutive '@' characters. */const char progdata[] = "/* A simple quine (self-printing program), in st" "andard C. */\n\n/* Note: in designing this quine, " "we have tried to make the code clear\n * and read" "able, not concise and obscure as many quines are" ", so that\n * the general principle can be made c" "lear at the expense of length.\n * In a nutshell:" " use the same data structure (called \"progdata\"\n" " * below) to output the program code (which it r" "epresents) and its own\n * textual representation" ". */\n\n#include <stdio.h>\n\nvoid quote(const char " "*s)\n /* This function takes a character stri" "ng s and prints the\n * textual representati" "on of s as it might appear formatted\n * in " "C code. */\n{\n int i;\n\n printf(\" \\\"\");\n " for (i=0; s[i]; ++i) {\n /* Certain cha" "racters are quoted. */\n if (s[i] == '\\\\')" "\n printf(\"\\\\\\\\\");\n else if (s[" ...

Page 31: Computational Reflection and Context-Oriented Programming

Quine (C) 31

"i] == '\"')\n printf(\"\\\\\\\"\");\n " "else if (s[i] == '\\n')\n printf(\"\\\\n\");" "\n /* Others are just printed as such. */\n" " else\n printf(\"%c\", s[i]);\n " " /* Also insert occasional line breaks. */\n " " if (i % 48 == 47)\n printf(\"\\\"\\" "n \\\"\");\n }\n printf(\"\\\"\");\n}\n\n/* What" " follows is a string representation of the program " "code,\n * from beginning to end (formatted as per" " the quote() function\n * above), except that the" " string _itself_ is coded as two\n * consecutive " "'@' characters. */\nconst char progdata[] =\n@@;\n\n" "int main(void)\n /* The program itself... */\n" "{\n int i;\n\n /* Print the program code, cha" "racter by character. */\n for (i=0; progdata[i" "]; ++i) {\n if (progdata[i] == '@' && prog" "data[i+1] == '@')\n /* We encounter tw" "o '@' signs, so we must print the quoted\n " " * form of the program code. */\n {\n " " quote(progdata); /* Quote all. */\n" " i++; /* Skip second '" "@'. */\n } else\n printf(\"%c\", p" "rogdata[i]); /* Print character. */\n }\n r" "eturn 0;\n}\n";

Page 32: Computational Reflection and Context-Oriented Programming

Quine (C) 32

int main(void) /* The program itself... */{ int i; /* Print the program code, character by character. */ for (i=0; progdata[i]; ++i) { if (progdata[i] == '@' && progdata[i+1] == '@') /* We encounter two '@' signs, so we must print the quoted * form of the program code. */ ...

{ quote(progdata); /* Quote all. */ i++; /* Skip second '@'. */ } else printf("%c", progdata[i]); /* Print character. */ } return 0;}

look for two consecutive @ characters and replace them by program string itself

Page 33: Computational Reflection and Context-Oriented Programming

Some reflective questions* to wrap-up 33

‣ Write a paradoxical reflective sentence.

‣ Explain reflection, metaprogramming and bootstrapping in your own words.

‣ Explain the word meta without “going meta”.

‣ Write a quine in your favourite programming language.

‣ Is the quine we wrote in C (or the quine you wrote)‣ a metaprogram?

‣ a reflective program?

‣ self-modifying?

‣ Can you write a quine that makes use of reflection?

‣ Are you programming reflectively when you use the keyword “this” in Java?

‣ Suggested reading: “Gödel, Escher, Bach”.

* pun intended