Tim Sheard - PowerPoint PPT Presentation

About This Presentation
Title:

Tim Sheard

Description:

feature 0; 1 Safe-beta is off. 2 Safe-eta is on. 3 Let-hoisting is on. ... feature 3; Let-hoisting is off. val it = false : bool ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 33
Provided by: markp77
Learn more at: http://web.cecs.pdx.edu
Category:
Tags: featurelet | sheard | tim

less

Transcript and Presenter's Notes

Title: Tim Sheard


1
Fundamentals of
Staged Computation
Lecture 2 More on MetaML
  • Tim Sheard
  • Oregon Graduate Institute

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. Ill help you if you have
    trouble.

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?

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 hasnt 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
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
Summary
  • The summary lets you describe what the paper is
    about. A retelling of the paper isnt 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.
  • Whats New. What new knowledge does the paper
    provide, if any. It there isnt anything new,
    then what else makes the paper interesting.
  • Whats 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
The lift operator
  • - lt41gt
  • val it lt4 1gt ltintgt
  • - lift (41)
  • val it lt5gt ltintgt
  • - val z ltfn n gt lt (lift n) 1gtgt
  • val z lt(fn a gt lt(lift a) 1gt)gt ltint -gt
    ltintgtgt
  • - run z
  • val it Fn int -gt ltintgt
  • - it 5
  • val it lt5 1gt ltintgt
  • - run it
  • val it 6 int

8
Cross-stage Persistence
  • Sometimes called lexical-capture
  • The source of the mysterious
  • - let val x 6 in ltx 5gt end
  • val it ltx 5gt ltintgt
  • 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
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 ltygt end
  • val z let val y true in ltygt end
  • in lt (x , z) gt end
  • val it lt(y,y)gt lt(int bool)gt

10
Lift v.s. lexical capture
  • Lift cannot be used on functions
  • - lift id
  • Error The term id
  • Non Qualified type, not liftable ? -gt ?
  • Lift makes code readable
  • - fun f x lt(x, (lift x))gt
  • val f Fn 'b.'b -gt lt('b 'b )gt
  • - f 3
  • val it lt(x,3)gt lt(int int)gt
  • Lexical capture is more efficient
  • - lift 13,4,8-4
  • val it lt4 (4 (4 )))gt ltint list gt

11
Alpha Renaming of bound variables
  • - fun f y ltfn x gt x ygt
  • val f fn ltintgt -gt ltint -gt intgt
  • - ltfn x gt 4 - (f ltxgt) 3gt
  • val it
  • lt(fn a gt 4 - ((fn b gt b a)) 3)gt
  • ltint -gt intgt

12
Synopsis Annotations
  • notation pronounced purpose
    metaOCaml
  • lt _ gt brackets (build code) .lt _ gt.
  • _ escape (splice in code) . _
  • lift _ lift (turn values into code) ??
  • run _ run (execute runtime code) .! _

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)

14
Example staged program
  • - fun copies 0 x ltgt
  • copies n x
  • lt x (copies (n-1) x)gt
  • val copies
  • Fn 'a.int -gt lt'a gt -gt lt'a listgt
  • - copies 3 lt3gt
  • val it lt3,3,3gt ltint listgt

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

16
Usual Mode
MetaML Interaction Window
Edit window XX.mml
- use "XX.mml" val y lt5gt ltintgt val f fn
int -gt ltintgt val ans 8 int val it ()
unit -
val y lt5gt fun f x ltx ygt val ans run (f
3)
17
Using MetaOcaml
Path, different on different computers
  • 113 adara.cs.pdx.edugt pwd
  • /u/sheard
  • 114 adara.cs.pdx.edugt bin/bin/metaocaml
  • MetaOCaml version 3.08.0 alpha 015
  • 3
  • - int 3
  • .lt 3 gt.
  • - ('a, int) code .lt3gt.
  • D
  • 115 adara.cs.pdx.edugt

Control D exits metaocal
/pkgs/metaocaml/current/bin/metaocaml
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

19
Example of MetaOcaml Use
File xx.ml
let y .lt 5 gt. let f x .lt x . y gt.
let ans .! (f 3)
Toplevel interaction
  • MetaOCaml version 3.08.0 alpha 015
  • cd "D/work/sheard/Courses/StagedComp/web/notes
    "
  • use "xx.ml"
  • val y ('a, int) code .lt5gt.
  • val f int -gt ('a, int) code ltfungt
  • val ans int 8

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/Tutorial202004.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
MetaML features
  • Useful functions
  • - pwd
  • val it fn unit -gt string
  • - cd
  • val it fn string -gt unit
  • - use
  • val it fn string -gt unit
  • - feature
  • val it fn int -gt bool
  • Other features
  • - if 4 'lt' 5 then 1 else 2
  • val it 1 int
  • - if 4'gt' 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
Differences between
Template Haskell,
MetaMl
MetaOcaml
  • 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 -gt x 1
  • even 0 True
  • even n odd (n-1)
  • odd 1 True
  • odd n even n-1
  • code 3 4
  • 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 gt x 1
  • fun even 0 true
  • even n odd (n-1)
  • and odd 1 true
  • odd n even (n-1)
  • val code lt 3 4 gt

type a list Nil Cons of a(a list) let
rec map f x match x with Nil -gt Nil
Cons(x,xs) -gt (f x)(map f xs) let pi
3.14159 function x -gt x 1 let rec even x
match x with 0 -gt true n -gt odd
(n-1) and odd x match x with 1 -gt
true n even (n-1) let code .lt 3 4
gt. Let ans .! code
23
Staging Anomalies
  • Correct use of variables
  • Run of open code
  • Dynamically typed programs

24
Correct use of staged variables
  • Each variable is declared at some level
  • - fn x gt ltfn y gt ltfn z gt z x ygtgt
  • val it fn int -gt ltint -gt ltint -gt intgtgt
  • ltfn x gt ((fn y gt ltygt) 3)gt
  • 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.
  • - ltfn x gt (copies x lt1gt)gt
  • 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
  • - ltfn x gt (run ltxgt)gt
  • Error In the dynamic runtime environment
    Variable Not Found 'x'617

25
Dynamic typing
  • fun k 0 x x
  • k n x ltfn x gt (k (n-1) ltxgt)gt
  • K 0 lt1gt ? lt1gt
  • K 1 lt1gt ? ltfn x gt 1gt
  • K 2 lt1gt -gt ltfn x gt fn y gt 1gt
  • K 3 lt1gt -gt ltfn x gt fn y gt fn z gt 1gt
  • But look what happens
  • - fun k 0 x x
  • k n x ltfn x gt (k (n-1) ltxgt)gt
  • Error The term
  • Cannot unify the types occurs check
  • a'618
  • and
  • a'618 -gt a'618
  • in expression lt(fn x gt k (n - 1) (ltxgt))gt

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

27
Safe Beta
  • - lt(fn x gt (fn y gt xy) 5)gt
  • val it
  • lt(fn a gt ((fn b gt a b)) 5)gt
  • ltint -gt intgt
  • - feature 1
  • Safe-beta is on.
  • val it true bool
  • - lt(fn x gt (fn y gt xy) 5)gt
  • val it
  • lt(fn a gt a 5)gt
  • ltint -gt intgt

Compare!
28
When Beta is not safe
  • - feature 1
  • Safe-beta is on.
  • val it true bool
  • - lt(fn x gt x x) (34)gt
  • val it
  • lt((fn a gt a a)) (3 4)gt ltintgt
  • - lt(fn x gt x x) 6gt
  • val it lt6 6gt ltintgt

Duplication could make code bigger, or duplicate
effects
Constants never make code grow, or have effects
29
Safe eta
  • - ltfn f gt fn x gt f xgt
  • val it lt(fn a gt (fn b gt a b))gt
  • 'a,'b.lt('b -gt 'a ) -gt 'b -gt 'a gt
  • - feature 2
  • Safe-eta is on.
  • val it true bool
  • - ltfn f gt fn x gt f xgt
  • val it lt(fn a gt a)gt
  • 'a,'b.lt('b -gt 'a ) -gt 'b -gt 'a gt

30
When eta is not safe
  • - ltfn x gt append 2 xgt
  • val it
  • lt(fn a gt append (2) a)gt
  • ltint list -gt int listgt
  • - ltfn x gt append x xgt
  • val it lt(fn a gt append a a)gt
  • 'a.lt'a list -gt 'a listgt

You might expect ltappend 2gt But this could
have side effect, and ltfn x gt append 2
xgt Wont have side effect until called.
You might expect ltappend agt But then a escapes
its binding site
31
Let normalization
  • - ltlet val x (let val y 5 in y 2 end)
  • in x - 7 endgt
  • val it
  • ltlet val a 5
  • val b a 2
  • in b - 7 endgt ltintgt
  • - feature 3
  • Let-hoisting is off.
  • val it false bool
  • - ltlet val x (let val y 5 in y 2 end)
  • in x - 7 endgt
  • val it
  • ltlet val a (let val b 5 in b 2 end)
  • in a - 7 endgt ltintgt
  • -

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.
Write a Comment
User Comments (0)
About PowerShow.com