Title: Tim%20Sheard
1Fundamentals of
Staged Computation
Lecture 1 Introduction
- Tim Sheard
- Oregon Graduate Institute
CS510 Sect FSC Winter 2004
2Acknowledgements
- I would like to thank all the people who have
collaborated with me over the years on staged
computation - Jim Hook
- Walid Taha
- Zino Benaissa
- Eugenio Moggi
- Emir Pasalic
- Bill Harrison
- Nathan Linger
3Contact Details
- Tim Sheard
- Office FAB115
- Telephone (503) 725 2410
- Email sheard_at_cs.pdx.edu
- CS510 Section FSC Home PAGE
- http//www.cs.pdx.edu/sheard/course/stagedcomp/in
dex.html
4Time and Location
- Currently scheduled
- Neuberger 220
- Tues Thurs, 400 515 pm
5Methods of assessment
Element Weight
Homework (8 homeworks, mostly small programming assignments, and one or more paper presentations. Programming assignments can be done in MetaML, Template Haskell, or MetaOcaml) 40
Midterm (week 6) 30
Final project (including a write up and a presentation) 30
TOTAL 100
6Policies
- By default, all deadlines are firm.
- We will be as flexible as possible in
accommodating special circumstances but advance
notice will make this a lot easier. - We follow the standard PSU guidelines for
academic integrity. - Discussion is good
- Items turned in should be your own, individual
work.
7Academic Integrity
- Students are expected to be honest in their
academic dealings. Dishonesty is dealt with
severely. - Homework. Pass in only your own work.
- Program assignments. Program independently.
- Examinations. Notes and such,
- only as the instructor allows.
- Homework. OK to discuss how to solve
- problems with other students, but each
- student should write up, debug, and
- turn in his own solution.
8Prerequisites
- Programming knowledge.
- Knowledge of ML is a plus but not necessary if
youre willing to pick it up as we proceed - Curiosity about how programs can be made more
efficient.
9Course Text
- None. Readings from primary sources
(conferences, journals, manuals) instead - I will update the readings list on the class home
page as the course proceeds.
10Todays Assignments
- Reading assignment
- Read the paper
- Using MetaML a staged Programming language.
- From the Lecture notes of the Summer School on
Advanced Functional Programming. September 1998.
Braga, Portugal. (see home page for link) - Programming assignment
- Find or install a copy of MetaML, Template
Haskell, or MetaOCaml. A MetaML package will be
added to the PSU PCAT systems soon. - On Thursday you will be given an assignment to
write a staged program (however small) and hand
it in one week from today. Never too early to get
started.
11Proposed Syllabus
- Intro to MetaML
- Using Staging Annotations
- Staging as fine control of evaluation order
- Example Staging an Interpreter
- Uses of staging
- Partial Evaluation
- Monads - staging as monadic computation
- Semantics of staging
- Implementing staging
- Other approaches to staging
- Type systems for staging
12What is Staging?
- Staging programs that proceed in stages. Each
stage writes a program to run in the next
stage. - Examples
- Compilers
- Partial evaluators
- Run-time code generation.
- Staging is a kind of Meta Programming
13What is Meta-programming?
- (at least) Two languages
- meta-language
- object-language(s)
- Meta-programs manipulate object-programs
- An object-program is a data-structure
representing a sentence in a formal language.
14What can they do?
- Meta-programs
- create new program fragments
- compose program fragments
- observe the structure of program fragments
- Examples
- compilers and interpreters
- type checkers
- theorem provers
- program generators
- transformation systems
- program analyzers
15What is staging used for?
- Performance
- generate a family of specialized solutions
- Partial Evaluation
- Translation
- Reasoning
- Mobile extensional code, proof carrying code
- Visualization and pedagogy
16How does it help?
- Resource aware programming
- Underlying cost model
- Programmer has fine control over cost of programs
- Cost might be space, time or other aspects
- Staging allows the programmer to control
evaluation order
17Controlling Evaluation Order
- We want more than just the correct result!
- We want to control resources without resorting to
tricks or unnatural programming styles. -
18Traditional Approaches
- Fixed evaluation order with language extensions
- Lazy - Outermost
- add strictness annotations
- Strict - Innermost
- add annotations like force and delay
- Encode laziness using lambda in a strict setting
- datatype 'a lazylist
- lazyNil
- lazyCons of 'a (unit -gt 'a lazylist)
- fun count n lazyCons(n, fn () gt count (n1))
19Limitations
- None of these approaches allow computation in the
definition of a function before it is called!
This is sometimes just what is needed. For
example - fun power n
- (fn x gt if n0
- then 1
- else x (power (n-1) x))
- power 2
A local unnamed function
20Reduction rules
- Function Definition (unfolding)
- fun f x y body
- f act1 act2 ? bodyact1/x, act2/y
- E.g. fun f x (x,x1)
- f 34 ? (34,341)
- Beta-rule
- (fn x gt body) value ? bodyvalue/x
- E.g. (fn x gt x 9) 32 ? 32 -9
- If-rule
- if True then x else y ? x
- if False then x else y ? y
- Primitive operators
- 4 5 ? 9
21Example reduction
- (power 2)
- unfold the definition
- (fn x gt if 20 then 1 else x (power (2-1) x))
- perform the if, under the lambda
- (fn x gt x (power (2-1) x))
- unfold power again
- (fn x gt x ((fn x gt if 10
- then 1
- else x (power (1-1)
x)) - x))
- use the beta rule to apply the explicit lambda to
x
22Example (cont.)
- (fn x gt x (if 10 then 1 else x (power (1-1)
x))) - perform the if
- (fn x gt x (x (power (1-1) x)))
- unfold power again
-
- (fn x gt x (x ((fn x gt if 00
- then 1
- else x (power
(0-1) x))) - x))
- use the beta rule to apply the explicit lambda to
x - (fn x gt x (x (if 00 then 1
- else x (power (0-1)
x)))) - perform the if
- (fn x gt x (x 1))
23Solution - Use rich staging annotations
- Brackets lt e gt
- no reductions allowed in e
- delay computation
- if et then ltegt lttgt (pronounced code of t)
- Escape e
- relax the no reduction rule of brackets above
- Escape may only occur inside Brackets
- splice code together to build larger code
- Run run e
- remove outermost brackets
- force computations which have been delayed
24 Rules for code
- Introduction rule for code
- lt e gt
- 1st elimination rule for code (escape-bracket
elim) - lt ltegt gt ---gt lt e gt
- ltegt must appear inside enclosing brackets
- e must be escape free
- 2nd elimination rule for code (run-bracket elim)
- run ltegt ---gt e
- provided e has no escapes
25Power example revisited
- ( power int -gt ltintgt -gt ltintgt )
- fun power n
- fn x gt if n0
- then lt1gt
- else lt x (power (n-1) x) gt
- ( ans lt int -gt int gt )
- val ans lt fn z gt (power 2 ltzgt) gt
26ltfn z gt (power 2 ltzgt) gt
ltfn z gt (if 20 then lt1gt
else lt ltzgt (power (2-1) ltzgt) gt)gt
ltfn z gt lt ltzgt (power (2-1) ltzgt) gtgt)
ltfn z gt lt z (power (2-1) ltzgt) gtgt)
ltfn z gt lt z (if 10 then
lt1gt else lt ltzgt (power (1-1)
ltzgt) gt) gtgt)
27ltfn z gt lt z lt ltzgt (power (1-1) ltzgt) gtgtgt
ltfn z gt lt z lt z (power (1-1) ltzgt) gtgtgt
ltfn z gt lt z lt z (power 0 ltzgt) gtgtgt
ltfn z gt lt z lt z lt1gt gtgtgt
ltfn z gt lt z lt z 1 gtgtgt
ltfn z gt lt z z 1 gtgt
ltfn z gt z z 1gt
28Annotations
- Annotations separate the meta-program from the
object-program fragments - Can be placed manually. MetaML, MetaOCaml, and
generating extensions - Can be placed automatically. Offline partial
evaluation. - Must have semantic meaning.
- Examples Quasi-quote notation, monadic
boundaries, abstract data-structures
29Why is Staging hard?
- Programs are complex
- Languages have features to manage complexity
- type systems (catch bad programs)
- scoping mechanisms (hide superfluous names)
- abstraction mechanisms (hide details)
- Staged programs deal with the complexity twice.
- Staged programming languages know about the
features of the object-language. Provide reusable
solutions to common problems (typing, binding,
name generation . . .)
30Introduction to MetaML
- MetaML a language for writing staged programs
- What Infrastructure is possible in a language
designed to help support the algorithmic
construction of other programs? - Advantages of MetaML
- MetaML knows about staging and its inherent
problems (see lecture 2) - capture knowledge
- efficient solutions
- design ideas can be communicated and shared
31Building pieces of code
- - lt23gt
- val it lt23gt ltintgt
- - ltlength 1,2,3gt
- val it ltlength 1,2,3gt ltintgt
- - lt23 5gt
- val it lt23 5gt ltintgt
- - ltfn x gt x 5gt
- val it lt(fn a gt a 5)gt ltint -gt intgt
- - ltfn x gt ltx 1gtgt
- val it lt(fn a gt lta 1gt)gt ltint -gt ltintgtgt
32Lets try it !
Run MetaML
33Composing pieces of code
- - val x lt2gt
- val x lt2gt ltintgt
- - val y lt44 xgt
- val y lt44 2gt ltintgt
- - fun pair x lt( x, x )gt
- val pair Fn 'b.lt'b gt -gt lt('b 'b )gt
- - pair lt11gt
- val it lt(11,11)gt lt(int int)gt
34Executing constructed code
- - val x lt34 2gt
- val x lt34 2gt ltintgt
- - run x
- val it 36 int
- - fun id x x
- val id Fn 'a.'a -gt 'a
- - val q lt1 (id lt23gt) gt
- val q lt1 2 3gt ltintgt
- - run q
- val it 6 int
35Multi-stage code
- - val z ltfn n gt ltn 1gtgt
- val z lt(fn a gt lta 1gt)gt ltint -gt ltintgtgt
- - val f run z
- val f Fn int -gt ltintgt
- - f 12
- val it ltn 1gt ltintgt
- - run it
- val it 13 int
36Next time
- More about MetaML
- Insight into the problems that staged programs
run into - Some significant examples