Tim Sheard Oregon Graduate Institute

Slides:



Advertisements
Similar presentations
Closures & Environments CS153: Compilers Greg Morrisett.
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
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.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
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.
Tim Sheard Portland State University Lecture 11: C-Mix and partial evaluation CS510 Section FSC Winter 2005 Winter 2005.
Tim Sheard Oregon Graduate Institute Lecture 6: Monads and Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
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.
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.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005.
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.
Staging in Haskell What is Staging What does it Mean Using Template Haskell.
Agile and Efficient Domain-Specific Languages using Multi-stage Programming in Java Mint Edwin Westbrook, Mathias Ricken, and Walid Taha.
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:
Advanced Functional Programming Tim Sheard 1 Lecture 6 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
Environments, Stores, and Interpreters. Overview As we study languages we will build small languages that illustrate language features We will use two.
Operational Semantics of Scheme
CSE341: Programming Languages Lecture 7 First-Class Functions
ML: a quasi-functional language with strong typing
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
Nondeterministic Evaluation
Advanced Functional Programming
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
Advanced Functional Programming
CSE341: Programming Languages Lecture 7 First-Class Functions
Nicholas Shahan Spring 2016
Advanced Functional Programming
CSE341: Programming Languages Lecture 11 Type Inference
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Advanced Functional Programming
6.001 SICP Variations on a Scheme
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 7 First-Class Functions
Environments, Stores, and Interpreters
Functional Programming
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 7 First-Class Functions
Recursive Procedures and Scopes
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Advanced Functional Programming
Presentation transcript:

Tim Sheard Oregon Graduate Institute Fundamentals of Staged Computation Lecture 13: Automatic Binding Time Analysis & Binding-time improvements Tim Sheard Oregon Graduate Institute CSE 510 Section FSC Winter 2004

Assignments Correction: the ¾-term exam is not next week. Instead it will be on Tuesday March 8, 2005 Homework #7 is now assigned and is still Due Thursday March 3. Cse583 Winter 2002

Automatic BTA Take an unstaged function (* power : int -> int -> int *) fun power n x = if n=0 then 1 else x * (power (n-1) x); A specification of its Binding – spec n is static, x is dynamic Add <_>, ~_, and lift _ to meet the spec fun pow1 n x = if n=0 then <1> else < ~x * ~(pow1 (n-1) x) >; Cse583 Winter 2002

Questions How do we specify the binding-times in a clear and non-ambiguous way, that is still precise? Use higher-order types with code types How do we automate the placing of the staging annotations? Use abstract interpretation Use constraint solving Use search Use type-information to prune the search tree Cse583 Winter 2002

Vision Add a new kind of declaration to MetaML Automatically generates bta pow1 = power at int -> <int> -> <int>; Automatically generates fun pow1 n x = if n=0 then <1> else < x * ~(pow1 (n-1) x) >; Cse583 Winter 2002

Types are more precise Generates (* map : ('b -> 'a ) -> 'b list -> 'a list *) fun map f [] = [] | map f (x::xs) = (f x) :: (map f xs); bta map' = map at (a -> <b>) -> [a] -> <[b]> Generates fun map' f [] = <[]> | map' f (x::xs) = < ~(f x) :: ~(map' f xs) > Cse583 Winter 2002

Can be multi-stage (n>2) (* iprod : int -> Vector -> Vector -> int *) fun iprod n v w = if n '>' 0 then ((nth v n) * (nth w n)) + (iprod (n-1) v w) else 0; bta p3 = iprod at int -> <Vector> -> <<Vector>> -> <<int>> Generates fun p3 n v w = then << (~(lift (nth ~v n)) * (nth ~ ~w n)) + ~ ~(p3 (n-1) v w) >> else <<0>>; Cse583 Winter 2002

Desirable properties Automatic BTA is part of the language, has semantic meaning Not a source-to-source transformation that works at the file level The annotations are precise Capable of partially static data Capable of higher-order partially static More than 2 stages Output understandable to programmers Cse583 Winter 2002

Terms Base terms Annotated terms E = v | E E | fn v => E | i | if E E E Annotated terms E = … | < E > | ~ E | lift E Cse583 Winter 2002

Types Base Types T = I | T -> T Annotated Types T = . . . Cse583 Winter 2002

Well typed terms Note without the level information, collapses to the usual rules for the typed lambda calculus Cse583 Winter 2002

Well annotated terms Rules for well typed terms plus . . . Cse583 Winter 2002

Relating actual types to needed types f :: t bta f’ = f at t’ Cse583 Winter 2002

Relating terms to annotated terms Cse583 Winter 2002

Define search Find e2 Given e1, t1, and t2 such that Define Cse583 Winter 2002

Search Judgments Cse583 Winter 2002

Search = Failure + successes Consider Search for f’ and x’ may Fail 1 or more successes Cse583 Winter 2002

Haskell Program data T = I -- int | B -- bool | Arr T T -- T -> T | Code T -- < T > | L T -- [ T ] data E = EA E E -- f x | EL String T E -- (l x.E) | EV String -- x | EI Int -- 5 | Eif E E E -- if x then y else z | EB E -- < E > | ES E -- ~ E Cse583 Winter 2002

Monad of multiple results app1 n sig phi (a,Code z,EA f x) w = do f' <- a1 (n+1) sig phi (Arr w a) (Arr w z) f x' <- a1 (n+1) sig phi w w v return (EB (EA f' x')) app1 n sig phi _ _ = fail "" Cse583 Winter 2002

Using to perform search do f' <- comp1 x' <- comp2 return (EB (EA f' x')) f’ x’ (EB (EA f' x')) [] [w,z] [a,b] [(EB (EA a w)) ,(EB (EA a z)) ,(EB (EA b w)) ,(EB (EA b z)) ] Cse583 Winter 2002

The list monad instance Monad [ ] where (x:xs) >>= f = f x ++ (xs >>= f) [] >>= f = [] return x = [x] fail s = [] instance MonadPlus [ ] where mzero = [] mplus = (++) Cse583 Winter 2002

Search Combinators type M a = [ a ] ifFail [] ys = ys ifFail xs ys = xs first [] = fail "first done\n" first [x] = x first (x:xs) = ifFail x (first xs) many [] = fail "many done\n" many [c] = c many (x:xs) = mplus x (many xs) succeed s x = return x Cse583 Winter 2002

Search Algorithm a2 n sig phi x = first [ int1 n sig phi x -- i , var1 n sig phi x -- x , var2 n sig phi x -- <x> , lam1 n sig phi x -- (fn x => e) , lam2 n sig phi x -- <fn x => e> , first [ if1 n sig phi x -- (if b <x> <y>) , if2 n sig phi x -- <if b ~x ~y> , if3 n sig phi x -- <if b x y> ] , first [ if4 n sig phi x -- ~(if b <x> <y>) , if5 n sig phi x -- (if b ~x ~y) , if6 n sig phi x -- (if b x y) ] , . . . ] Cse583 Winter 2002

Search Algorithm cont. a2 n sig phi x = first [. . . ,isEA sig x (\ w -> many [first[ app1 n sig phi x w -- <f v> , many[ app2 n sig phi x w -- <~f v> , app3 n sig phi x w -- <f ~x> ] , app4 n sig phi x w -- <~f ~v>] ,first[app6 n sig phi x w -- (f <x>) ,app11 n sig phi x w -- (f:(<a>->b) x:<a>)] ,first[app5 n sig phi x w -- (f:(a-><b>) x:a) ,app7 n sig phi x w -- (f:(a->b) x:b) ,app8 n sig phi x w -- (~f v) ,app9 n sig phi x w -- (f ~v) ,app10 n sig phi x w -- (~f ~v)] ]) Cse583 Winter 2002

Algorithm Components int1 n sig phi (I,I,EI i) = do trace "int1 " n (EI i) I succeed "int1" (EI i) int1 n sig phi _ = fail ""--"Case 1" int2 n sig phi (I,Code z,EI i) = do trace "int2 " n (EI i) (Code z) e' <- a1 (n+1) sig phi I z (EI i) succeed "int2" (EB e') int2 n sig phi _ = fail ""--"CASE 2" var1 n sig phi (a,b,EV s) = do trace "var1 " n (EV s) b phi s b var1 n sig phi _ = fail ""--"Case 3A" Cse583 Winter 2002

More Components lam1 n sig phi (Arr x y,Arr a b,EL s t e) = do trace "lam1 " n (EL s t e) (Arr a b) e' <- a1 n (ext sig s x) (ext phi s (varTest s a)) y b e succeed "lam1" (EL s a e') lam1 n sig phi _ = fail ""--"CASE 4" if1 n sig phi (a,Code z,Eif i j k) = do trace "if1 " n (Eif i j k) (Code z) b1 <- a1 n sig phi B B i j1 <- a1 n sig phi a (Code z) j k1 <- a1 n sig phi a (Code z) k succeed "if1"(Eif b1 j1 k1) if1 n sig phi _ = fail ""--"CASE 6A" Cse583 Winter 2002

lam1 {0} \ x -> f x : I -> <I> app1 {0} f x : <I> lam1 {0} \ f -> \ x -> f x : (I -> <I>) -> I -> <I> lam1 {0} \ x -> f x : I -> <I> app1 {0} f x : <I> var1 {1} f : I -> I Fails. Actual type: I -> <I> app2 {0} f x : <I> var1 {1} f : <I -> I> Fails. Actual type: I -> <I> var2 {1} f : <I -> I> var1 {2} f : I -> I Fails. Actual type: I -> <I> app3 {0} f x : <I> app4 {0} f x : <I> app6 {0} f x : <I> Cse583 Winter 2002

var1 {0} f : I -> <I> var1 {0} x : I var1 {0} f : <I> -> <I> Fails. Actual type: I -> <I> app11 {0} f x : <I> app5 {0} f x : <I> var1 {0} f : I -> <I> var1 {0} x : I app5 succeeded. returning value f x lam1 succeeded. returning value \ x -> f x lam1 succeeded. returning value \ f -> \ x -> f x 24 [\ f -> \ x -> f x] Cse583 Winter 2002

dot : I -> [I] -> <[I]> -> <I> \ n -> \ xs -> \ ys -> if eq n 0 then <0> else <add (mult (hd xs) (hd ~ys)) ~(dot (sub n 1) (tl xs) <tl ~ys>)> dot : I -> <[I]> -> <[I]> -> <I> else <add (mult (hd ~xs) (hd ~ys)) ~(dot (sub n 1) <tl ~xs> <tl ~ys>)> Cse583 Winter 2002

dot : I -> <[I]> -> [I] -> <I> \ n -> \ xs -> \ ys -> if eq n 0 then <0> else <add (mult (hd ~xs) (hd ys)) ~(dot (sub n 1) <tl ~xs> (tl ys))> dot : I -> [I] -> <[I] -> I> \ n -> \ xs -> <\ ys -> ~(if eq n 0 else <add (mult (hd xs) (hd ys)) ( ~(dot (sub n 1) (tl xs)) (tl ys))>)> Cse583 Winter 2002

Binding-time improvements Some programs don’t have a well formed annotation at some type. Or if they do, the annotated program always goes into an infinite loop. Or they generate large, or “ugly” code But if we make small changes to the structure of the program, we can make it work. Cse583 Winter 2002

#1 Splitting Arguments type env = (string * int) list ; Lookup: string -> env -> int Set: string -> int -> env -> env Ext: string -> int -> env -> env Remove: env -> env Interp0 : Com -> ((string * int) list) -> ((string * int) list) fun interpret0 stmt env = case stmt of Assign(name,e) => let val v = eval0 e env in set name v env end | Seq(s1,s2) => let val env1 = interpret0 s1 env val env2 = interpret0 s2 env1 in env2 end Cse583 Winter 2002

The env has two parts The string is known at generation time The int is only known at the time the code will be run. Split: (string * int) list into String list Int list Interp1:Com ->(string list)->(int list )->(int list) Cse583 Winter 2002

fun interp1 stmt index stack = case stmt of Assign(name,e) => let val v = eval1 e index stack val loc = pos name index in put loc v stack end | Seq(s1,s2) => let val stack1 = interp1 s1 index stack val stack2 = interp1 s2 index stack1 in stack2 end Cse583 Winter 2002

Naturally Stages fun interp2 stmt index stack = case stmt of Interp2::Stmt-> string list-> <int list> -> <int list> fun interp2 stmt index stack = case stmt of Assign(name,e) => <let val v = ~(eval2 e index stack) in put ~(lift (pos name index)) v ~stack end> | Seq(s1,s2) => <let val stack1 = ~(interp2 s1 index stack) val stack2 = ~(interp2 s2 index <stack1>) in stack2 end> Cse583 Winter 2002

#2 Recursion v.s. Generated Loops Consider Interp0 : Com -> env -> env fun interpret0 stmt env = case stmt of | While(e,body) => let val v = eval0 e env in if v=0 then env else interpret0 (While(e,body))(interpret0 body env) end Cse583 Winter 2002

Diverges at generation time fun interp2 stmt index stack = case stmt of . . . | While(e,body) => <let val v = ~(eval2 e index stack) in if v=0 then ~stack else ~(interp2 (While(e,body)) index (interp2 body index stack)) end> Cse583 Winter 2002

Generate loop | While(e,body) => <let fun loop stk0 = let val v = ~(eval2 e index <stk0>) in if v=0 then stk0 else let val stk1 = ~(interp2 body index <stk0>) in loop stk1 end end in loop ~stack end> Cse583 Winter 2002

#3 Partially static structures Instead of splitting a structure into two structures, make it a structure that has some values and some code. Datatype Exp = Var of string | App of (Exp*Exp) | Lam of (string*Exp) eval::Exp->(String*Value) list -> Value eval::Exp >(String*<Value>) list -> <Value> Cse583 Winter 2002

#4 Using continuations fun match pat msigma (term as (Wrap t)) = case (msigma) of NONE => NONE | SOME (sigma) => (case pat of Var u => (case find u sigma of NONE => SOME ((u,term) :: sigma) | SOME w => if termeq w term then SOME sigma else NONE) Cse583 Winter 2002

fun match pat k msigma term = case (msigma) of NONE => k NONE | SOME (sigma) => (case pat of Var u => (case find u sigma of NONE => k (SOME ((u,term) :: sigma)) | SOME w => <if termeq ~w ~term then ~(k (SOME sigma)) else ~(k NONE)>) Cse583 Winter 2002

#5 Let–introduction fun filter p [] = <[]> | filter p (x::xs) = <if ~p ~(lift x) then ~(filter p xs) else ~(lift x) :: ~(filter p xs)>; Cse583 Winter 2002

fun filter p [] = <[]> | filter p (x::xs) = <let val ys = ~(filter p xs) in if ~p ~(lift x) then ys else ~(lift x) :: ys end>; Cse583 Winter 2002

#6 special cases fun power 0 x = <1> | power n = <~x * ~(power (n-1))> <fn y => ~(power 3 <y>)>  <fn a => a * a * a * 1> | power 1 x = x  <fn a => a * a * a> Cse583 Winter 2002