1 / 32

Tim Sheard Oregon Graduate Institute

Fundamentals of. Staged Computation. Tim Sheard Oregon Graduate Institute. Lecture 2: More on MetaML. CS510 Sect FSC Winter 2004. Assignment #1. The first assignment is now posted. See the class home page for details. Assignment due Thursday Jan 13, 2005

Download Presentation

Tim Sheard Oregon Graduate Institute

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fundamentals of Staged Computation Tim Sheard Oregon Graduate Institute Lecture 2: More on MetaML CS510 Sect FSC Winter 2004

  2. Assignment #1 • The first assignment is now posted. • See the class home page for details. • Assignment due Thursday Jan 13, 2005 • Talk to me or call me immediately if you have trouble installing MetaML or MetaOcaml. Try it today, not being able to install is not an acceptable excuse. I’ll help you if you have trouble. Cs510 FSC, Winter 2005

  3. Notes on how to give a paper presentation • Presentation should have several parts • What kind of paper is it? • What previous work is necessary to understand this paper? • What is it about (a summary) • What is the problem • Structure of paper • What examples are used? • What is new? • What is important? Cs510 FSC, Winter 2005

  4. What kind of paper is it? • There are lots of kinds of papers, try and pin down what kind is it. • New idea – the paper introduces a new concept that hasn’t been studied before. Often opens up a new area of research. Such papers are uncommon. • Synthesis – the paper synthesizes several old ideas into a new coherent whole. Might use the old ideas in completely new contexts. • Extension – the paper extends previous work in an incremental manner, that strengthens it, or makes it applicable in new contexts. Very common. • Unifying – the paper takes ideas from often very different domains and shows that they are really the same idea in different guises. Rare but very useful as it increases understanding. • Systems Engineering – the paper describes a large complex system. Deep thought usually went into designing the system to meet conflicting goals. The usefulness of the paper is in how it makes tradeoffs between the conflicting goals. • Analysis or Measurement – the paper measures the performance or other aspect of a system in order to evaluate the system. Measurement papers often measure competing systems as a way of comparison. • This list is not complete . . . Cs510 FSC, Winter 2005

  5. Previous work necessary • What ideas do you have to already know to appreciate and understand the paper. • Often useful in putting the paper in context of larger surrounding work Cs510 FSC, Winter 2005

  6. Summary • The summary lets you describe what the paper is about. A retelling of the paper isn’t necessary. Instead recap • The problem. What problem is the paper trying to solve? Why is it important. • Structure. How was the story told. If there was an intro, what points are made, are there measurements, tables, or proofs. • Examples. Examples often provide the greatest insight for unknowledgable readers. Which ones are used, recap them if they are good ones. • What’s New. What new knowledge does the paper provide, if any. It there isn’t anything new, then what else makes the paper interesting. • What’s Important? A good paper makes this obvious, the author spells it out, but sometimes the importance of a paper grows with time. Try and convince the class why they should care about this paper. Cs510 FSC, Winter 2005

  7. The lift operator -| <4+1>; val it = <4 %+ 1> : <int> -| lift (4+1); val it = <5> : <int> -| val z = <fn n => < ~(lift n) + 1>>; val z = <(fn a => <~(lift a) %+ 1>)> :<int -> <int>> -| run z; val it = Fn : int -> <int> -| it 5; val it = <5 %+ 1>: <int> -| run it; val it = 6 : int Cs510 FSC, Winter 2005

  8. Cross-stage Persistence • Sometimes called lexical-capture • The source of the mysterious % -| let val x = 6 in <x + 5> end; val it = <%x %+ 5> : <int> • Free variables in code evaluate to constants in the code constructed. The %X indicates that the object is a constant and originated from a lexically bound variable called X Cs510 FSC, Winter 2005

  9. Cross-stage Persistence cont. • Two cross-stage constants can look the same, but be two different values: -| let val x = let val y = 4 in <y> end val z = let val y = true in <y> end in < (~x , ~ z) > end; val it = <(%y,%y)> : <(int * bool)> Cs510 FSC, Winter 2005

  10. Lift v.s. lexical capture • Lift cannot be used on functions -| lift id; Error: The term: id Non Qualified type, not liftable: ? -> ? • Lift makes code readable -| fun f x = <(x, ~(lift x))>; val f = Fn : ['b^].'b -> <('b * 'b )> -| f 3; val it = <(%x,3)> : <(int * int)> • Lexical capture is more efficient -| lift [1+3,4,8-4]; val it = <4 :: (4 :: (4 :: [])))> : <int list > Cs510 FSC, Winter 2005

  11. Alpha Renaming of bound variables -| fun f y = <fn x => x + ~y>; val f = fn : <int> -> <int -> int> -| <fn x => 4 - ~(f <x>) 3>; val it = <(fn a => 4 %- ((fn b => b %+ a)) 3)> : <int -> int> Cs510 FSC, Winter 2005

  12. Synopsis Annotations notationpronouncedpurpose metaOCaml • < _ > brackets (build code) .< _ >. • ~ _ escape (splice in code) .~ _ • lift _ lift (turn values into code) ?? • run _ run (execute runtime code) .! _ Cs510 FSC, Winter 2005

  13. Synopsis MetaML features • Pattern based object code templates • templates “look like” the object language • Object-code has a type. • The type of code is embedded in the meta-lang type system • Object code has structure. • Possible to analyze it, take it apart, etc. (future lecture) • Automatic alpha-renaming of bound variables • No name clashes • Object-code can be run or executed (runtime code-gen.) • Object-code can be observed (pretty-printed) Cs510 FSC, Winter 2005

  14. Example staged program -| fun copies 0 x = <[]> | copies n x = < ~x :: ~(copies (n-1) x)>; val copies = Fn : ['a].int -> <'a > -> <'a list> -| copies 3 <3>; val it = <[3,3,3]> : <int list> Cs510 FSC, Winter 2005

  15. Using Metaml • Finding the installed image • /usr/local/bin/metaml ?? • If you installed it on your machine look there • You should be able to type metaml and it should work • Caution - it may take a few seconds or so to start up • You may type functions and declarations at top level, or use the use function to load functions from a file. Keep a copy of your functions in a file and edit them separately from the command loop. • In the command loop end a command with “;” Cs510 FSC, Winter 2005

  16. Usual Mode MetaML Interaction Window Edit window “XX.mml” -| use "XX.mml"; val y = <5> : <int> val f = fn : int -> <int> val ans = 8 : int val it = () : unit -| val y = <5>; fun f x = <x + ~y>; val ans = run (f 3); Cs510 FSC, Winter 2005

  17. Using MetaOcaml Path, different on different computers 113 adara.cs.pdx.edu> pwd /u/sheard 114 adara.cs.pdx.edu> bin/bin/metaocaml MetaOCaml version 3.08.0 alpha 015 # 3;; - : int = 3 # .< 3 >.;; - : ('a, int) code = .<3>. # ^D 115 adara.cs.pdx.edu> Control “D” exits metaocal /pkgs/metaocaml/current/bin/metaocaml Cs510 FSC, Winter 2005

  18. MetaOcaml Command line arguments All commands start with a “#” • #quit ;; quit from the toplevel interaction • #directory directory ;; add the directory to the search path • #cd directory ;; change the working directory • #load object_file ;; load an object file (.cmo) • #use source_file ;; compile and load a source file • #print_depth depth ;; modify the depth of printing • #print_length width ;; modify the length of printing • #install_printer function ;; specify a printing function • #remove_printer function ;; remove a printing function • #trace function ;; trace the arguments of the function • #untrace function ;; stop tracing the functio • #untrace_all ;; stop all tracing Cs510 FSC, Winter 2005

  19. Example of MetaOcaml Use File xx.ml MetaOCaml version 3.08.0 alpha 015 # #cd "D:/work/sheard/Courses/StagedComp/web/notes" ;; # #use "xx.ml" ;; val y : ('a, int) code = .<5>. val f : int -> ('a, int) code = <fun> val ans : int = 8 # let y = .< 5 >. ;; let f x = .< x + .~ y >. ;; let ans = .! (f 3) ;; Toplevel interaction Cs510 FSC, Winter 2005

  20. Manuals and tutorials for Ocaml • Just normal Ocaml manual • http://caml.inria.fr/oreilly-book • Good for the basics of how Ocaml works • Tutorial • http://www.metaocaml.org/doc/Tutorial%202004.pdf • Lots of examples in MetaOcaml format • Gentle Introduction • http://www.cs.rice.edu/~taha/publications/journal/dspg04a.pdf • All these links can be found on the metaocaml page • http://www.metaocaml.org/ Cs510 FSC, Winter 2005

  21. Useful functions -| pwd; val it = fn :unit -> string -| cd; val it = fn :string -> unit -| use; val it = fn :string -> unit -| feature; val it = fn :int -> bool Other features -| if 4 '<' 5 then 1 else 2; val it = 1 : int -| if 4'>' 5 then 1 else 2; val it = 2 : int -| #"a"; val it = #"a" : char MetaML “features” Note the use of quotes (‘) around the less than, and greater than operators. Avoids ambiguity with staging annotations Cs510 FSC, Winter 2005

  22. data List a = Nil | Cons a (list a) map f Nil = Nil map f (Cons x xs)= (f x):(map f xs) pi = 3.14159 \ x -> x + 1 even 0 = True even n = odd (n-1) odd 1 = True odd n = even n-1 code = [| 3 + 4 |] -- No run in -- Template Haskell datatype ‘a list = Nil | Cons of ‘a*(‘a list) fun map f Nil = Nil | map f (Cons(x,xs)) = (f x)::(map f xs) val pi = 3.14159 fn x => x + 1 fun even 0 = true | even n = odd (n-1) and odd 1 = true | odd n = even (n-1) val code = < 3 + 4 >; val ans = run code Differences between Template Haskell, MetaMl MetaOcaml type ‘a list = Nil | Cons of ‘a*(‘a list) let rec map f x = match x with Nil -> Nil | Cons(x,xs) -> (f x)::(map f xs) let pi = 3.14159 function x -> x + 1 let rec even x = match x with 0 -> true | n -> odd (n-1) and odd x = match x with 1 -> true | n = even (n-1) let code = .< 3 + 4 >. ;; Let ans = .! code Cs510 FSC, Winter 2005

  23. Staging Anomalies • Correct use of variables • Run of open code • Dynamically typed programs Cs510 FSC, Winter 2005

  24. Correct use of staged variables • Each variable is declared at some level -| fn x => <fn y => <fn z => z + x + y>>; val it = fn: int -> <int -> <int -> int>> <fn x => ~((fn y => <y>) 3)> • What level is each variable bound at? • A variable can legally be accessed at a level greater or equal to the level of its declaration. -| <fn x => ~(copies x <1>)>; Error: The term: x in file 'top level' 20 - 21 variable bound in phase 1 used too early in phase 0 • Run of open code -| <fn x => ~(run <x>)>; Error: In the dynamic runtime environment: Variable Not Found: 'x'617‘ Cs510 FSC, Winter 2005

  25. Dynamic typing fun k 0 x = x | k n x = <fn x => ~(k (n-1) <x>)> K 0 <1>  <1> K 1 <1>  <fn x => 1> K 2 <1> -> <fn x => fn y => 1> K 3 <1> -> <fn x => fn y => fn z => 1> But look what happens -| fun k 0 x = x | k n x = <fn x => ~(k (n-1) <x>)>; Error: The term: Cannot unify the types: occurs check a'618 and a'618 -> a'618 in expression: <(fn x => ~k (n - 1) (<x>))> Cs510 FSC, Winter 2005

  26. Code level optimizations • MetaML performs optimizations on code when the code is performed. It performs only those optimizations that are guaranteed not to make the code larger, and guaranteed not to change the termination of the code. -| feature 0; 1 Safe-beta is off. 2 Safe-eta is on. 3 Let-hoisting is on. 4 Monad-law-normalization is on. val it = false : bool Cs510 FSC, Winter 2005

  27. Safe Beta -| <(fn x => (fn y => x+y) 5)>; val it = <(fn a => ((fn b => a %+ b)) 5)> : <int -> int> -| feature 1; Safe-beta is on. val it = true : bool -| <(fn x => (fn y => x+y) 5)>; val it = <(fn a => a %+ 5)> : <int -> int> Compare! Cs510 FSC, Winter 2005

  28. When Beta is not safe -| feature 1; Safe-beta is on. val it = true : bool -| <(fn x => x + x) (3+4)>; val it = <((fn a => a %+ a)) (3 %+ 4)> : <int> -| <(fn x => x + x) 6>; val it = <6 %+ 6> : <int> Duplication could make code bigger, or duplicate effects Constants never make code grow, or have effects Cs510 FSC, Winter 2005

  29. Safe eta -| <fn f => fn x => f x>; val it = <(fn a => (fn b => a b))> : ['a,'b].<('b -> 'a ) -> 'b -> 'a > -| feature 2; Safe-eta is on. val it = true : bool -| <fn f => fn x => f x>; val it = <(fn a => a)> : ['a,'b].<('b -> 'a ) -> 'b -> 'a > Cs510 FSC, Winter 2005

  30. When eta is not safe You might expect <append [2]> But this could have side effect, and <fn x => append [2] x> Won’t have side effect until called. -| <fn x => append [2] x>; val it = <(fn a => %append ([2]) a)> : <int list -> int list> -| <fn x => append x x>; val it = <(fn a => %append a a)> : ['a].<'a list -> 'a list> You might expect <append a> But then a escapes it’s binding site Cs510 FSC, Winter 2005

  31. Let normalization -| <let val x = (let val y = 5 in y + 2 end) in x - 7 end>; val it = <let val a = 5 val b = a %+ 2 in b %- 7 end> : <int> -| feature 3; Let-hoisting is off. val it = false : bool -| <let val x = (let val y = 5 in y + 2 end) in x - 7 end>; val it = <let val a = (let val b = 5 in b %+ 2 end) in a %- 7 end> : <int> -| Cs510 FSC, Winter 2005

  32. Object Level Optimizations • Optimizations may or may not apply depending upon whether they might change the semantics of the object program. • The “feature” control allows user to turn them on and off. • If they do apply, the optimized object program will always behave the same. • The optimized object program will be smaller, or look “prettier”. Cs510 FSC, Winter 2005

More Related