Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tim Sheard Oregon Graduate Institute Lecture 2: More on MetaML CS510 Sect FSC Winter 2004 Winter 2004.

Similar presentations


Presentation on theme: "Tim Sheard Oregon Graduate Institute Lecture 2: More on MetaML CS510 Sect FSC Winter 2004 Winter 2004."— Presentation transcript:

1 Tim Sheard Oregon Graduate Institute Lecture 2: More on MetaML CS510 Sect FSC Winter 2004 Winter 2004

2 2Cs510 FSC, Winter 2005 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.

3 3Cs510 FSC, Winter 2005 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?

4 4Cs510 FSC, Winter 2005 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...

5 5Cs510 FSC, Winter 2005 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

6 6Cs510 FSC, Winter 2005 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.

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

8 8Cs510 FSC, Winter 2005 Cross-stage Persistence Sometimes called lexical-capture The source of the mysterious % -| let val x = 6 in end; val it = : 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

9 9Cs510 FSC, Winter 2005 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 end val z = let val y = true in end in end; val it = :

10 10Cs510 FSC, Winter 2005 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 = ; val f = Fn : ['b^].'b -> -| f 3; val it = : Lexical capture is more efficient -| lift [1+3,4,8-4]; val it = :

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

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

13 13Cs510 FSC, Winter 2005 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)

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

15 15Cs510 FSC, Winter 2005 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 “ ; ”

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

17 17Cs510 FSC, Winter 2005 Using MetaOcaml 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 #..;; - : ('a, int) code =.. # ^D 115 adara.cs.pdx.edu> Path, different on different computers Control “D” exits metaocal /pkgs/metaocaml/current/bin/metaocaml

18 18Cs510 FSC, Winter 2005 MetaOcaml Command line arguments #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 All commands start with a “#”

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

20 20Cs510 FSC, Winter 2005 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/

21 21Cs510 FSC, Winter 2005 MetaML “features” 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 Note the use of quotes (‘) around the less than, and greater than operators. Avoids ambiguity with staging annotations

22 22Cs510 FSC, Winter 2005 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 = ; val ans = run code Differences between 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 =.. ;; Let ans =.! code Template Haskell,MetaMlMetaOcaml

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

24 24Cs510 FSC, Winter 2005 Correct use of staged variables Each variable is declared at some level -| fn x => z + x + y>>; val it = fn: int -> int>> ~((fn 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. -| ~(copies x )>; 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 -| ~(run )>; Error: In the dynamic runtime environment: Variable Not Found: 'x'617‘

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

26 26Cs510 FSC, Winter 2005 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

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

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

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

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

31 31Cs510 FSC, Winter 2005 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> : -| 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> : -|

32 32Cs510 FSC, Winter 2005 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”.


Download ppt "Tim Sheard Oregon Graduate Institute Lecture 2: More on MetaML CS510 Sect FSC Winter 2004 Winter 2004."

Similar presentations


Ads by Google