Tim Sheard Portland State University Lecture 11: C-Mix and partial evaluation CS510 Section FSC Winter 2005 Winter 2005.

Slides:



Advertisements
Similar presentations
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Tim Sheard Oregon Graduate Institute Lecture 8: Operational Semantics of MetaML CSE 510 Section FSC Winter 2005 Winter 2005.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
Tim Sheard Oregon Graduate Institute Lecture 4: Staging Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
1 Meta-Programming through Typeful Code Representation Chiyan Chen and Hongwei Xi Boston University.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
A Type System for MetaML MetaML types as an Omega program Lecture 12.
Tim Sheard Oregon Graduate Institute Lecture 6: Monads and Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
CS 312 Spring 2004 Lecture 18 Environment Model. Substitution Model Represents computation as doing substitutions for bound variables at reduction of.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Tim Sheard Oregon Graduate Institute Lecture 2: More on MetaML CS510 Sect FSC Winter 2004 Winter 2004.
CS 312 Spring 2002 Lecture 16 The Environment Model.
Cse536 Functional Programming 1 6/23/2015 Lecture #17, Dec. 1, 2004 Todays Topics – Higher Order types »Type constructors that take types as arguments.
Tim Sheard Oregon Graduate Institute Lecture 5: Review of homework 2 CS510 Sect FSC Winter 2004 Winter 2004.
A Methodology for Generating Verified Combinatorial Circuits CSE 510 Section FSC Winter 2004 Winter 2004 Kiselyov, Swadi, Taha.
Tim Sheard Oregon Graduate Institute CS510 Sect FSC Winter 2004 Winter 2004 Lecture 1: Introduction.
Closure and Environment Compiler Baojian Hua
Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Agile and Efficient Domain-Specific Languages using Multi-stage Programming in Java Mint Edwin Westbrook, Mathias Ricken, and Walid Taha.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
“Introduction to Programming With Java” Lecture - 6 U MESH P ATIL
Advanced Functional Programming Tim Sheard 1 Lecture 6 Functional Programming Tim Sheard & Mark Jones Monads & Interpreters.
Advanced Functional Programming Tim Sheard 1 Lecture 18 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
A Staged Semantics of Overloading (preliminary) Bill Harrison & Tim Sheard.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
Advanced Functional Programming Tim Sheard 1 Lecture 6 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
Catie Welsh February 14,  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Environments, Stores, and Interpreters. Overview As we study languages we will build small languages that illustrate language features We will use two.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Object Oriented Programming Lecture 2: BallWorld.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
User-Written Functions
6.001 SICP Compilation Context: special purpose vs. universal machines
ML Again ( Chapter 7) Patterns Local variable definitions
On the Structure of Interpreters
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
Tim Sheard Oregon Graduate Institute
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
Introduction to Computer Programming
Chapter 15 Pointers, Dynamic Data, and Reference Types
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2018.
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Zach Tatlock Winter 2018.
CSE 341 Section 5 Winter 2018.
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2016.
Programming Languages and Compilers (CS 421) #3: Closures, evaluation of function applications, order of evaluation #4: Evaluation and Application.
Environments, Stores, and Interpreters
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2017.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Assignments and Procs w/Params
Recursive Procedures and Scopes
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2019.
Python fundamental.
Presentation transcript:

Tim Sheard Portland State University Lecture 11: C-Mix and partial evaluation CS510 Section FSC Winter 2005 Winter 2005

2Cse583 Winterl 2002 Assignments Remember the ¾-term exam is 2 weeks from today on Tuesday March 8, 2005 Homework #7 is now assigned Due Thursday March 3. Reading Assignment Read the very short paper “C-Mix: Making Easily Maintainable C-Programs run Fast” Handed out in class today.

3Cse583 Winterl 2002 Discussion of Homework #6 Homework 6 Use a monad that encodes dynamic alpha renaming Stage the resulting interpreter

4Cse583 Winterl 2002 The Monad datatype 'a M = M of (int -> ('a * int)); fun return x = M(fn n => (x,n)); fun bind (M f) g = M(fn n => let val (a,n1) = f n val M h = g a in h n1 end); val m = Mon(return,bind); fun newvar s = M(fn n => (s^(toString n),n+1)); fun fmap f e = Do m { x <- e; Return m (f x) };

5Cse583 Winterl 2002 Changes to the Abstract Syntax datatype V = VI of int | VF of (V -> V M) | VC of exp and exp = EI of int (* integers *) | EA of exp * exp (* applications *) | EL of string * exp (* lambda-abstractions *) | EV of string (* variables *) | EB of exp (* brackets *) | ES of exp (* escape *) | ER of exp (* run *) | EC of string * V; (* cross-stage constants *) Note functions return V M and not V

6Cse583 Winterl 2002 Remember Covers fun coverE env e = case e of EI i => EI i | EA(e1,e2) => EA(coverE env e1,coverE env e2) | EL(x,e1) => EL(x,coverE env e1) | EV y => env y | EB e1 => EB(coverE env e1) | ES e1 => ES(coverE env e1) | ER e1 => ER(coverE env e1) | EC(s,v) => EC(s,coverV env v) and coverV env v = case v of VI i => VI i | VF f => VF(fmap (coverV env) o f) | VC e => VC(coverE env e);

7Cse583 Winterl 2002 Monadic Interpreter fun ev2 e env = case e of EI i => Return m (VI i) | EA(e1,e2) => Do m { VF f <- ev2 e1 env ; x <- ev2 e2 env ; f x } | EL(x,e1) => Return m (VF(fn v => ev2 e1 (ext env x (EC(x,v))))) | EV x => (case env x of EC(_,v) => Return m v | w => Return m (VC w)) | EB e1 => Do m { c <- eb2 1 e1 env ; Return m (VC c) } | ER e1 => Do m { VC c <- ev2 e1 env ; ev2 c env0 } | EC(s,v) => Return m (coverV env v) | ES e1 => error "escape at level 0"

8Cse583 Winterl 2002 Monadic Rebuilding Fun eb2 n e env = case e of EI i => Return m (EI i) | EA(e1,e2) => Do m { f <- eb2 n e1 env ; x <- eb2 n e2 env ; Return m (EA(f,x)) } | EL(x,e1) => Do m { x' <- newvar x ; body <- eb2 n e1 (ext env x (EV x')) ; Return m (EL(x',body)) } | EV y => Return m (env y) | EB e1 => Do m { c <- eb2 (n+1) e1 env ; Return m (EB c) } | ES e1 => if n=1 then Do m { VC c <- ev2 e1 env; Return m c } else Do m { c <- eb2 (n-1) e1 env; Return m (ES c) } | ER e1 => Do m { c <- eb2 n e1 env; Return m (ER c) } | EC(s,v) => Return m (EC(s,coverV env v));

9Cse583 Winterl 2002 Helper Functions fun apply f x = (case (f,x) of (VF f,v) => f v); fun F f = Return m (VF f);

10Cse583 Winterl 2002 Staged Interpreter fun ev3 e env = case e of EI i => | EA(e1,e2) => <Do m { f <- ~(ev3 e1 env) ; x <- ~(ev3 e2 env) ; apply f x }> | EL(x,e1) => ~(ev3 e1 (ext env x )))> | EV x => (case env x of => | w => ) | EB e1 => <Do m { c <- ~(eb3 1 e1 env) ; Return m (VC c) }> | ER e1 => <Do m { VC c <- ~(ev3 e1 env) ; run (ev3 c env0) }> | EC(s,v) => run (env x)) v)> | ES e1 => error "escape at level 0"

11Cse583 Winterl 2002 ~( (fn x => ) (fn w => ) ) 5> <Do %m { a <- %newvar "a" ; d Do %m { c <- Return %m EC("x",b) ; Return %m VC c }) ; g Do %m { f <- Return %m EV a ; Return %m VC f }) ; VC h <- %apply d g ; i <- Return %m EI 5 ; j <- Return %m EA(h,i) ; k <- Return %m EL(a,j) ; VC l <- Return %m VC k ; m <- run %ev3 l (%env0) ; n <- Return %m VI 3 ; VC o <- %apply m n ; run %ev3 o (%env0) }>

12Cse583 Winterl 2002 After Monad Laws <Do %m { a <- %newvar "a" ; c Return %m (VC (EC("x",b)))) ; e Return %m (VC (EV a))) ; VC f <- %apply c e ; VC g <- Return %m (VC (EL(a,EA(f,EI 5)))) ; h <- run %ev3 g %env0 ; VC i <- %apply h (VI 3) ; run %ev3 i %env0 }>

13Cse583 Winterl 2002 The H function %F (fn b => %F (fn c => Do %m { d <- %apply (VI 7) b ; f Return %m c) ; g <- %apply d f ; o <- %F (fn h => Do %m { i <- %newvar "x" ; j <- %apply (VI 7) h ; k <- %apply j (VI 1) ; l <- %apply a k ; VC m <- Return %m c ; VC n <- %apply l (VC (EA(EA(EC("+",VI 7),EV i),m))) ; Return %m (VC (EA(EL(i,n),EC("n",h)))) }) ; %apply g o }))) ; q <- %apply (VI 6) p ; r <- %apply q (VI 3) ; %apply r (VC (EI 4)) }>

14Cse583 Winterl 2002 The Cmix partial Evaluator Partial evaluation Specialization of a program because some of the arguments are known to be constant Source to Source transformation Can dramatically speed up program Analogy to compilation vs intepretation

15Cse583 Winterl 2002 Xdup.c interpreter 123abc abcccc Xdup.c Xdup.exe 123abc abcccc C-compiler

16Cse583 Winterl 2002 Xdup.c Xdup.exe 123abc abcccc C-compiler Xdup.c Xdup123.exe abc abcccc C-compiler 123 specializer Xdup123.c

17Cse583 Winterl 2002 Original Code #include void xdupfun(char counts[], char data[]) { int i, t ; for( i=0; counts[i]; i++ ) { t = counts[i]­'0' ; while( t­­ ) putchar(data[i]) ; }

18Cse583 Winterl 2002 Specialized Code #include void xdupfun(char *residual_arg) { char *data; data = residual_arg; putchar((int )data[0]); putchar((int )data[1]); putchar((int )data[2]); return ; }

19Cse583 Winterl 2002 Xdup.c abc abbccc C-compiler 123 Xdup-gen.c C-Mix C-compiler Xdup-gen.exe Xdup-123.c Xdup-123.exe Xdupfun(St,Dy)

20Cse583 Winterl 2002 cmix xdup.c ­e 'goal: xdupfun($1,?)' source Next arg is a specializer directive Function to specialize 1 st arg static, 2 nd arg dynamic

21Cse583 Winterl 2002

22Cse583 Winterl 2002

23Cse583 Winterl 2002 /cmix/xdup $ cc xdup-gen.c -o xdup-gen -lcmix /cmix/xdup $ ls xdup-gen.c xdup-gen.exe xdup.ann xdup.c /cmix/xdup $$./xdup-gen 123 /* This program was generated by C-Mix/II * THIS IS AUTOMATICALLY GENERATED CODE */ #include void xdupfun(char *residual_arg) { char *data; data = residual_arg; putchar((int )data[0]); putchar((int )data[1]); putchar((int )data[2]); return ; }

24Cse583 Winterl 2002