Generic Programming, Partial Evaluation, and a New Programming - - PowerPoint PPT Presentation

generic programming partial evaluation and a new
SMART_READER_LITE
LIVE PREVIEW

Generic Programming, Partial Evaluation, and a New Programming - - PowerPoint PPT Presentation

vg-gppe.txt Sun Jan 3 20:59:22 1999 1 Generic Programming, Partial Evaluation, and a New Programming Paradigm Dr. Christopher Landauer Aerospace Integration Science Center The Aerospace Corporation cal@aero.org Dr. Kirstie L.


slide-1
SLIDE 1

vg-gppe.txt Sun Jan 3 20:59:22 1999 1

Generic Programming, Partial Evaluation, and a New Programming Paradigm

  • Dr. Christopher Landauer

Aerospace Integration Science Center The Aerospace Corporation cal@aero.org

  • Dr. Kirstie L. Bellman, Principal Director

Aerospace Integration Science Center The Aerospace Corporation bellman@aero.org

slide-2
SLIDE 2

vg-gppe.txt Sun Jan 3 20:59:22 1999 2

Outline Introduction

  • Generic Programming

. Goals and Progress, Examples

  • Partial Evaluation

. Basic Approach, Futamura Projections, Performance Problem Posing Programming Paradigm Underlying Technology

  • Wrapping: Knowledge Based Integration Infrastructure

. Coordination Manager, Study Manager, Meta-Recursion . Wrapping Knowledge Base Semantics

  • Wrex (Wrapping Expression notation)

Applications

  • Coordination Languages
  • Reuse without Modification

Conclusions

  • Partial Evaluation
  • Generic Programming
slide-3
SLIDE 3

vg-gppe.txt Sun Jan 3 20:59:22 1999 3

Generic Programming Goals and Progress A goal of programming since the beginning

  • Get the right level of abstraction in algorithms

Result would be

  • All and only the essential parts of the algorithm are displayed
  • All of the important characteristics of the execution environment

can be made explicit and deferred to pre-compile time

  • Algorithm is then instantiated or adapted to fit the environment

Generic Programming is about making programs more flexible

  • More interesting kinds of Polymorphism
  • More interesting kinds of parameters

(e.g., programs, types, constructors, ...) Gradual improvements over the last 50 years: Machine language -> assemblers -> compilers -> interpreters Still looking for more effective techniques

  • Partial Evaluation looks like a good one
slide-4
SLIDE 4

vg-gppe.txt Sun Jan 3 20:59:22 1999 4

Examples Algorithms are often written descriptively for explanation

  • We want those descriptions to be the actual generic programs

Generic genetic programming algorithm: generate initial population loop until done compute offspring retain only the ones with best fitness Generic simulated annealing algorithm: set initial temperature and position loop until done generate potential new position check movement criterion reduce temperature Generic k-means clustering algorithm: generate initial cluster centers loop until done partition data into nearest-center clusters recompute cluster centers

slide-5
SLIDE 5

vg-gppe.txt Sun Jan 3 20:59:22 1999 5

Partial Evaluation: Basic Approach Simple idea

  • If you know a program and some of the parameters,

then you can specialize the program

  • The resulting specialized program should be (and often is) faster

Difficult to make it work

  • Want completely automatic techniques
  • Requires compiler optimization methods

. Symbolic execution . Partly unrolling iterations and recursions . Program point specialization: Replicate parts of program with different specializations, Definition Creation, Folding, Memoization

  • Need care in replication to avoid exponential blowup

Imagine a program ‘‘mix’’ that can do partial evaluation: for all programs p, and all inputs in1 and in2, [[mix] (p, in1)] in2 = [p] (in1, in2), so mix specializes p to the inputs in1 (such programs do exist for some languages) Partial evaluation provides an adaptation mechanism for generic programs

slide-6
SLIDE 6

vg-gppe.txt Sun Jan 3 20:59:22 1999 6

Futamura Projections We can get compilers and compiler generators out of partial evaluators (1970) Definition: An interpreter is a program interp for which: for all inputs in, and all source programs s, [interp] (s, in) = [s] in We claim that a kind of ‘‘compilation target’’ t for source program s can be computed: t = [mix] (interp, s), since for any input in,

  • ut

= [s] in = [interp] (s, in) = [[mix] (interp, s)] in = [t] in, so mix specializes the interpreter via part of its input (the source program s) Now comp = [mix] (mix, interp) turns out to be a compiler for the language interpreted by interp: for any source program s, the compilation target t has t = [mix] (interp, s) = [[mix] (mix, interp) ] s = [comp] s

slide-7
SLIDE 7

vg-gppe.txt Sun Jan 3 20:59:22 1999 7

Futamura Projections (continued) So far, t = [mix] (interp, s) is a compiled form of s, comp = [mix] (mix, interp) is a compiler for the language interp interprets Finally, cogen = [mix] (mix, mix) is a compiler generator for the language interp is written in: for any interpreter interp, source program s, and input in, [s] in = [interp] (s, in) = [[mix] (interp, s)] in = [[[mix] (mix, interp)] s ] in = [[[[mix] (mix, mix)] interp] s] in, = [[[cogen] interp] s] in so [cogen] interp = [comp] is a compiler for the language of s

slide-8
SLIDE 8

vg-gppe.txt Sun Jan 3 20:59:22 1999 8

Performance of Partial Evaluation The use of these formal projections looks nice

  • The performance of the programs is important

Two ways to produce output with these notions:

  • ut

= [interp] (s, in) = [s] in t = [mix] (interp, s) = [comp] s comp = [mix] (mix, interp) = [cogen] interp cogen = [mix] (mix, mix) = [cogen] mix The second way is often something like 10 times faster The first way is usually much easier to write (once the program mix is written) We want to use this fact to write faster programs more easily

slide-9
SLIDE 9

vg-gppe.txt Sun Jan 3 20:59:22 1999 9

Problem Posing Programming Paradigm All programming languages have a notion of information service

  • Information service requests
  • Information service providers

We connect them now by using the same names ‘‘Problem Posing’’ is a new declarative programming style

  • Unifies all major classes of programming

Programs interpreted in this style ‘‘pose problems’’; they do not

  • ‘‘call functions’’,
  • ‘‘issue commands’’,
  • ‘‘assert constraints’’, or
  • ‘‘send messages’’

(these are information service requests) Program fragments are ‘‘resources’’ that can be ‘‘applied’’ to problems; they are not

  • ‘‘functions’’,
  • ‘‘modules’’,
  • ‘‘clauses’’, or
  • ‘‘objects’’ that do things

(these are information service providers)

slide-10
SLIDE 10

vg-gppe.txt Sun Jan 3 20:59:22 1999 10

Problem Posing Programming Paradigm (continued) The Problem Posing interpretation separates the requests from the providers

  • Easy: compilers and interpreters always know the difference anyway
  • So do we when we write the programs

We call the service requests ‘‘posed problems’’ We call the service providers ‘‘resources’’ We can connect them through knowledge bases (or other means)

  • ‘‘Knowledge-Based’’ Polymorphism maps problems to resources

. From the problem specification in its context . To the computational resources that will organize the solution Any programming language can be interpreted in this new way (well, almost any) Problem Posing changes the semantics of programming languages, not the syntax

  • It makes a program an organized collection of posed problems

. Instead of an organized collection of solutions without problems

  • That should make programs easier to understand

. Because the problems at all levels of detail remain in it

slide-11
SLIDE 11

vg-gppe.txt Sun Jan 3 20:59:22 1999 11

Examples C

  • Functions are resources, so are data structures
  • Function calls and Data accesses are posed problems
  • Each function relates a posed problem to an algorithm that poses other problems
  • Data structures are used by other resources to retain and provide data

Lisp

  • S-expressions are resources (some are applicable, others not)
  • Applications are posed problems
  • Each applicable S-expression relates a posed problem to an algorithm

that poses other problems

  • Other S-expressions are used by other resources to retain and provide data

Smalltalk

  • Methods are resources, so are other object components
  • Messages are posed problems
  • Each method relates a posed problem, with some constraints on parameters

(including which object receives the message), to other posed problems

  • Other object components are used by other resources to retain and provide data

Prolog

  • Clauses are resources, so are tuples
  • Terms are posed problems
  • Each clause relates a posed problem, with some constraints on parameters,

to other posed problems

  • Tuples are used by other resources to retain and provide data
slide-12
SLIDE 12

vg-gppe.txt Sun Jan 3 20:59:22 1999 12

Wrapping: Knowledge Based Integration Infrastructure Problem Posing is particularly effective in combination with ‘‘wrappings’’ Computationally reflective knowledge-based integration infrastructure

  • Explicit, machine-processable information about resource uses

AND

  • Active processes that use the information

Wrapping has four main features

  • Everything is a ‘‘resource’’ that provides an information service

. Tools, components, interfaces, even architectures

  • Every activity is ‘‘problem study’’:

. A resource is ‘‘applied’’ to a ‘‘posed problem’’ . Separate problem to be studied from the resources that might study it

  • Wrappings are meta-knowledge about the uses of resources:

. Not just ‘‘how’’, but ‘‘when’’, ‘‘why’’ and ‘‘whether’’ . Wrapping Knowledge Bases (WKBs) contain all the wrappings

  • Problem Managers (PMs) organize the use of resources

. Use implicit invocation, both context and problem dependent . Knowledge-Based Polymorphism . These wrapping processes are also resources; they are also wrapped (Computational Reflection) . Coordination Manager (CM) and Study Manager (SM) are two PMs

slide-13
SLIDE 13

vg-gppe.txt Sun Jan 3 20:59:22 1999 13

Coordination Manager (CM) The CM is a simple analog of ‘‘read-eval-print’’ in LISP Find Context loop Pose Problem Study Problem Present Results The CM is a kind of ‘‘heartbeat’’ for a wrapping-based system CM steps

  • Find Context determines the current context of problem study

(e.g., menu of choices, interpretation of invocation parameters, etc.)

  • Pose Problem converts the user input into a posed problem from it

(e.g., by parsing)

  • Study Problem finds resources to apply to the problem and does it
  • Present Results tells the user the result (if any)

Context is very important in this approach

  • Context provides and organizes _all_ interpretation
  • Context determines what each symbolic expression means or does

. Semantics _is_ interpretation of syntax

  • Context determines what representations are important or useful

. ‘‘Sanity Checks’’ are constraints determined by context

slide-14
SLIDE 14

vg-gppe.txt Sun Jan 3 20:59:22 1999 14

Study Manager (SM) The SM is our central approach to problem study

  • It assumes a context, problem poser, problem and associated data (from the CM)

The SM is a (VERY) simple planning algorithm: Interpret Problem: Match Resources Resolve Resources Select Resource Adapt Resource Advise Poser Apply Resources Assess Result SM steps

  • Match looks for resources that seem to fit the context and the problem

(it uses a quick and simple filter through the WKBs)

  • Resolve looks for resources whose requirements fit the problem also

(it negotiates between the resource and the posed problem)

  • Select picks one
  • Advise tells the problem poser what resource is about to be applied
  • Apply does the doing - applies the selected resource to the problem
  • Assess determines what happened and if it is acceptable
slide-15
SLIDE 15

vg-gppe.txt Sun Jan 3 20:59:22 1999 15

Meta-Recursion The SM (by itself) is a simple-minded planner that considers only one step at a time Recursion in the meta-direction

  • Every CM and SM step is also a posed problem, studied with the same algorithms

. Independence from wrapping syntax and semantics The system therefore has ‘‘knowledge’’ of its own resources

  • It can tell if it is ‘‘clueless’’ about a problem
  • It can tell if it has something that applies to the problem, without doing it

Bottoming out of the recursion

  • Simplest form of each step resource is as described above

. No simplest form for adaptation, because that is hard Not necessarily immediate execution of resource functions

  • The SM above is a one-step-at-a-time planner
  • Applying a resource might be placing it into a configuration script for later

. Or analyzing it and placing the result in a structure (e.g., V&V)

  • Other SMs can plan more complex combinations (e.g., planning parser)

We make essential use of this reflection in our partial evaluations

  • The system has access to its own computational resources
slide-16
SLIDE 16

vg-gppe.txt Sun Jan 3 20:59:22 1999 16

Wrapping Knowledge Base Semantics Wrapping Knowledge Base (WKB) information

  • Not just about how to use the resource, also why, when and whether

. Assumptions, limitations, applicability, scope, styles of use

  • Since the SM steps are selected, the syntax and semantics can be heterogeneous

. There are some common entries because the WKB supports the SM steps For each resource, a sequence of entries

  • Problem Identification
  • Context under which this resource might be appropriate for this problem
  • Requirements (information or services) for using this resource
  • Products (information or services)

. Relevant specializations, methods of application Use of wrappings (Intelligent User Support Functions)

  • Selection (which resource to use)
  • Assembly (how to use it: data formats and protocols, mainly syntactic)
  • Integration (whether and when to use it: more semantics, also context)

. The key to Use

  • Adaptation (how to set up resource for problem)

. The key to ReUse (hard!)

  • Explanation (why the resource was used)

. The key to Understanding (harder!)

slide-17
SLIDE 17

vg-gppe.txt Sun Jan 3 20:59:22 1999 17

Wrex (Wrapping Expression Notation) Syntax reflects the central role of the posed problem in Problem Posing

  • Just as the syntax of Smalltalk reflects the central role of message passing

Syntax definition for posed problem (general): <posed problem> ::= <prob> ‘[’ <data> ‘]’ [‘in’ <context>] [‘by’ <study manager>] Default context (when ‘in’ phrase is omitted) is current context at problem pose time Study Manager (when ‘by’ clause is present) is the one to use to _find_ an SM

  • Instead of the one to use to interpret the problem

Syntax items

  • Variable: multiple words, delimited by ‘<>’
  • Constant: words, other symbols, numbers, or strings
  • Constructors for grouping structures

. List: multiple items, separated by commas, delimited by ‘[]’ . Set: multiple items, separated by commas, delimited by ‘{}’ . Group: multiple items, separated by commas, delimited by ‘()’ (Groups not in groups can omit parentheses)

  • Separated problem names

. E.g., ‘find [ ... ] in [ ... ] using [ ... ]’

  • Programs are sequences of these items (i.e., groups without parentheses)

. The simple posed problem is a group whose last element is a list

slide-18
SLIDE 18

vg-gppe.txt Sun Jan 3 20:59:22 1999 18

Example of wrex Syntax: Coordination Manager define CM [ <user> ]: [ Find context [ <user> ], for ever : [ <problem> = Pose problem [ <user> ], <result> = Study problem [ <user>, <problem>, <context> ], Present results [ <user>, <result> ] ] ], Discussion of syntax features

  • The entire resource is one group (with a comma at the end):

. ‘define’ ‘CM’ ‘[ ... ]’ ‘:’ ‘[ ... ]’

  • ‘Find context’ is a separated problem name

. So is ‘define’ ... ‘:’ ‘[ ... ]’

  • ‘for ever : [ ... ]’ is a posed problem with wrex statements as data
  • ‘<problem> = Pose problem [ <user> ]’ is also a group without parentheses

. It is also a posed problem with problem name ‘=’

  • ‘if’ and ‘for’ expressions are defined concepts, not primitive

. So is ‘=’

slide-19
SLIDE 19

vg-gppe.txt Sun Jan 3 20:59:22 1999 19

Example of wrex Syntax: Study Manager define SM-simple [ <poser>, <prob>, <data>, <context> ]: [ <candidates> = Match resources [ <prob>, <data>, <context> ], if (<candidates> == empty): then return FAIL_NO_CANDS, <candidate applications> = Resolve resources [ <candidates>, <prob>, <data>, <context> ], if (<candidate applications> == empty): then return FAIL_NO_APPLS, <selection> = Select resource [ <candidate applications> ], <resource application> = Adapt resource [ <selection>, <prob>, <data>, <context> ], <resp> = Advise poser [ <poser>, <resource application>, <prob>, <data>, <context> ], if (not <resp>): then return FAIL_NO_APPLS, <result> = Apply resource [ <poser>, <resource application>, <context> ], <success flag> = Assess results [ <result>, <prob>, <data>, <context> ], return <result> ],

slide-20
SLIDE 20

vg-gppe.txt Sun Jan 3 20:59:22 1999 20

Applications Coordination Languages in distributed systems

  • We can use wrex as a coordination language

. It is independent of particular language assumptions . We need an interpreter within each program in the system (this is not too hard)

  • We gain interoperability among (almost) any programming languages this way

. The coordination language can remain as the components change, also Reuse Without Modification

  • Change the compilers

. They always know the difference between definition and use of symbols

  • Identify and separate posed problems from computational resources

. For example, intercept the function calls from the called functions

  • Can then insert complete wrapping infrastructure, for

. Instrumentation . Monitoring . Different functions under different conditions . Completely new functions for previously difficult situations

  • Gradual weaning from old code and increased use of new code

. Old code is kept only as long as it is still useful

slide-21
SLIDE 21

vg-gppe.txt Sun Jan 3 20:59:22 1999 21

Decision Times and Performance Decision times

  • Language Design time, Compiler Generation time
  • Program Generation time, Compile time, Link time
  • Run time

Too many decisions at run-time

  • Is as rigid as restricting them to any other times
  • Also costs greatly in performance

We can use our knowledge of the execution environment with partial evaluation

  • Context and collection of available resources (when known)

. If we know that a posed problem is only addressed by one resource, we can avoid the full search and the SM recursion Partial evaluation of wrex

  • Many more kinds of input than usual programs

. Program and input data, as usual . Context data . Collection of available resources

  • Much greater specialization will result
  • We hope much better programs; both faster AND more easily changed
slide-22
SLIDE 22

vg-gppe.txt Sun Jan 3 20:59:22 1999 22

Conclusions and other Claims Generic Programming

  • The perennial goal of programming is easier this way than others (sez me)
  • Can defer many decisions from Program Generation, Compile, and even Link time

Partial Evaluation

  • The Problem Posing interpretation can produce regular compiler code

. We don’t know how fast it will be, but we’re very hopeful . We want programs that are BOTH easier to write AND faster to run

  • We have to write some partial evaluators like ‘‘mix’’ for wrex (probly hard)
  • Can defer many decisions from Language Design and Compiler Generation time

Problem Posing

  • Knowledge-Based Polymorphism with wrappings
  • Has interesting reuse and system re-engineering implications

. Reuse without modification

  • Provides a kind of interoperability for (almost) all programming languages

. Coordination language . Needs language to accept information from outside (hard for some) None of this solves the hard problems in any particular domain (they are still hard)

  • It does allow them to be solved more separately
  • And the integration of even partial solutions to be more automatic