Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tim Sheard Oregon Graduate Institute CS510 Sect FSC Winter 2004 Winter 2004 Lecture 1: Introduction.

Similar presentations


Presentation on theme: "Tim Sheard Oregon Graduate Institute CS510 Sect FSC Winter 2004 Winter 2004 Lecture 1: Introduction."— Presentation transcript:

1 Tim Sheard Oregon Graduate Institute CS510 Sect FSC Winter 2004 Winter 2004 Lecture 1: Introduction

2 2Cse583 Winter 2002 Acknowledgements: 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

3 3Cse583 Winter 2002 Contact Details: Tim Sheard: Office: FAB115 Telephone: (503) 725 2410 Email: sheard@cs.pdx.edu CS510 Section FSC Home PAGE: http://www.cs.pdx.edu/~sheard/course/stagedcomp/index.html

4 4Cse583 Winter 2002 Time and Location: Currently scheduled: Neuberger 220 Tues & Thurs, 4:00 – 5:15 pm;

5 5Cse583 Winter 2002 Methods of assessment: ElementWeight 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% TOTAL100%

6 6Cse583 Winter 2002 Policies: 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.

7 7Cse583 Winter 2002 Academic 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.

8 8Cse583 Winter 2002 Prerequisites: Programming knowledge. Knowledge of ML is a plus but not necessary if you’re willing to pick it up as we proceed Curiosity about how programs can be made more efficient.

9 9Cse583 Winter 2002 Course 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.

10 10Cse583 Winter 2002 Today’s 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.

11 11Cse583 Winter 2002 Proposed 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

12 12Cse583 Winter 2002 What 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

13 13Cse583 Winter 2002 What 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.

14 14Cse583 Winter 2002 What 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

15 15Cse583 Winter 2002 What is staging used for? Performance generate a family of specialized solutions Partial Evaluation Translation Reasoning Mobile – extensional code, proof carrying code Visualization and pedagogy

16 16Cse583 Winter 2002 How 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

17 17Cse583 Winter 2002 Controlling Evaluation Order We want more than just the correct result! We want to control resources without resorting to tricks or unnatural programming styles.

18 18Cse583 Winter 2002 Traditional 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 -> 'a lazylist); fun count n = lazyCons(n, fn () => count (n+1))

19 19Cse583 Winter 2002 Limitations 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 => if n=0 then 1 else x * (power (n-1) x)) power 2; A local unnamed function

20 20Cse583 Winter 2002 Reduction rules Function Definition (unfolding) fun f x y = body f act1 act2  body[act1/x, act2/y] E.g. fun f x = (x,x+1) f 34  (34,34+1) Beta-rule (fn x => body) value  body[value/x] E.g. (fn x => 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

21 21Cse583 Winter 2002 Example reduction (power 2) unfold the definition (fn x => if 2=0 then 1 else x * (power (2-1) x)) perform the if, under the lambda (fn x => x * (power (2-1) x)) unfold power again (fn x => x * ((fn x => if 1=0 then 1 else x * (power (1-1) x)) x)) use the beta rule to apply the explicit lambda to x

22 22Cse583 Winter 2002 Example (cont.) (fn x => x * (if 1=0 then 1 else x * (power (1-1) x))) perform the if (fn x => x * (x * (power (1-1) x))) unfold power again (fn x => x * (x * ((fn x => if 0=0 then 1 else x * (power (0-1) x))) x)) use the beta rule to apply the explicit lambda to x (fn x => x * (x * (if 0=0 then 1 else x * (power (0-1) x)))) perform the if (fn x => x * (x * 1))

23 23Cse583 Winter 2002 Solution - Use rich staging annotations Brackets:  no reductions allowed in e  delay computation  if e:t then : (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 24Cse583 Winter 2002 Rules for code Introduction rule for code 1st elimination rule for code (escape-bracket elim) … > --->  ~ must appear inside enclosing brackets  e must be escape free 2nd elimination rule for code (run-bracket elim) run ---> e  provided e has no escapes

25 25Cse583 Winter 2002 Power example revisited (* power : int -> -> *) fun power n = fn x => if n=0 then else (* ans : int > *) val ans = ~(power 2 ) >;

26 26Cse583 Winter 2002 ~ (power 2 ) > ~(if 2=0 then else * ~(power (2-1) ) >)> ~ * ~(power (2-1) ) >>) ~ ) >>) ~< z * ~(if 1=0 then else * ~(power (1-1) ) >) >>)

27 27Cse583 Winter 2002 ~ * ~(power (1-1) ) >>> ~ ) >>> ~ >>> ~ >> ~ > z * z * 1>

28 28Cse583 Winter 2002 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 Annotations

29 29Cse583 Winter 2002 Why 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...)

30 30Cse583 Winter 2002 Introduction 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

31 31Cse583 Winter 2002 Building pieces of code -| ; val it = : -| ; val it = : -| ; val it = : -| x + 5>; val it = a %+ 5)> : int> -| >; val it = )> : >

32 32Cse583 Winter 2002 Let’s try it ! Run MetaML

33 33Cse583 Winter 2002 Composing pieces of code -| val x = ; val x = : -| val y = ; val y = : -| fun pair x = ; val pair = Fn : ['b]. -> -| pair ; val it = :

34 34Cse583 Winter 2002 Executing constructed code -| val x = ; val x = : -| run x; val it = 36 : int -| fun id x = x; val id = Fn : ['a].'a -> 'a -| val q = ) >; val q = : -| run q; val it = 6 : int

35 35Cse583 Winter 2002 Multi-stage code -| val z = >; val z = )> : > -| val f = run z; val f = Fn : int -> -| f 12; val it = : -| run it; val it = 13 : int

36 36Cse583 Winter 2002 Next time More about MetaML Insight into the problems that staged programs run into Some significant examples


Download ppt "Tim Sheard Oregon Graduate Institute CS510 Sect FSC Winter 2004 Winter 2004 Lecture 1: Introduction."

Similar presentations


Ads by Google