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

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

Modern Programming Languages, 2nd ed.
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Tim Sheard Oregon Graduate Institute Lecture 8: Operational Semantics of MetaML CSE 510 Section FSC Winter 2005 Winter 2005.
Principles of programming languages 1: Introduction (with a simple language) Isao Sasano Department of Information Science and Engineering.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Tim Sheard Oregon Graduate Institute Lecture 4: Staging Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
Chapter 10.
A Type System for MetaML MetaML types as an Omega program Lecture 12.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter 2: Algorithm Discovery and Design
Tim Sheard Oregon Graduate Institute Lecture 5: Review of homework 2 CS510 Sect FSC Winter 2004 Winter 2004.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Tim Sheard Oregon Graduate Institute CS510 Sect FSC Winter 2004 Winter 2004 Lecture 1: Introduction.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005.
C++ for Engineers and Scientists Third Edition
Introduction to C Programming
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
1CMSC 345, Version 4/04 Verification and Validation Reference: Software Engineering, Ian Sommerville, 6th edition, Chapter 19.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Invitation to Computer Science, Java Version, Second Edition.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Chapter 9: Functional Programming in a Typed Language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
CS101 Computer Programming I Chapter 4 Extra Examples.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Advanced Functional Programming Tim Sheard 1 Lecture 18 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
Artificial Intelligence Lecture No. 26 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Introduction to Objective Caml. General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Lab 8 Overview Apache Web Server. SCRIPTS Linux Tricks.
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CS314 – Section 5 Recitation 9
Discussion 4 eecs 183 Hannah Westra.
User-Written Functions
Principles of programming languages 12: Functional programming
Information Science and Engineering
Introduction To Repetition The for loop
Type Systems Terms to learn about types: Related concepts: Type
Tim Sheard Oregon Graduate Institute
MATLAB: Structures and File I/O
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 12 Equivalence
Continuation Marks A john clements talk.
Type Systems Terms to learn about types: Related concepts: Type
Recursive Procedures and Scopes
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 12 Equivalence
Presentation transcript:

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

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.

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?

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...

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

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.

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

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

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 = :

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 = :

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>

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).! _

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)

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

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 “ ; ”

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 -|

17Cs510 FSC, Winter 2005 Using MetaOcaml 113 adara.cs.pdx.edu> pwd /u/sheard 114 adara.cs.pdx.edu> bin/bin/metaocaml MetaOCaml version 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

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 “#”

19Cs510 FSC, Winter 2005 Example of MetaOcaml Use MetaOCaml version 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

20Cs510 FSC, Winter 2005 Manuals and tutorials for Ocaml Just normal Ocaml manual Good for the basics of how Ocaml works Tutorial Lots of examples in MetaOcaml format Gentle Introduction All these links can be found on the metaocaml page

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

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 = \ x -> x + 1 even 0 = True even n = odd (n-1) odd 1 = True odd n = even n-1 code = [| |] -- 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 = 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 = 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

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

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' 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‘

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) ( ))>

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

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!

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

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 >

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

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> : -|

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”.