Some Advanced ML Features

Similar documents
Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Modular Programming. Prof. Clarkson Fall Today s music: "Giorgio By Moroder" by Daft Punk

CS3110 Spring 2016 Lecture 6: Modules

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Types, Type Inference and Unification

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

OCaml Language Choices CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Lists. Michael P. Fourman. February 2, 2010

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

CMSC 330: Organization of Programming Languages

G Programming Languages - Fall 2012

Types and Type Inference

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

Mini-ML. CS 502 Lecture 2 8/28/08

Type Checking and Type Inference

Functional Programming and Haskell

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

CS 11 Haskell track: lecture 1

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CSE 341 : Programming Languages Midterm, Spring 2015

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Types and Type Inference

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Data Types

Introduction to OCaml

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

CSCI-GA Scripting Languages

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Mutation. COS 326 David Walker Princeton University

Introduction to Objective Caml

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

Metaprogramming assignment 3

Introduction to OCaml

FUNCTIONAL PROGRAMMING

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CMSC330. Objects, Functional Programming, and lambda calculus

CMSC 330: Organization of Programming Languages

Types in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9)

CMSC 330, Fall 2013, Practice Problems 3

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML)

CS Lecture 7: The dynamic environment. Prof. Clarkson Spring Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Two approaches to writing interfaces

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Reference Cells. Example

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

An introduction introduction to functional functional programming programming using usin Haskell

Programming language seminar The F# language. Peter Sestoft Wednesday Course plan

Programming Languages and Techniques (CIS120)

Principles of Functional Programming

CS 345. Functions. Vitaly Shmatikov. slide 1

Parametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!

The Typed Racket Guide

"Object Oriented" Programming using ML

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Monads. Prof. Clarkson Fall Today s music: Vámanos Pal Monte by Eddie Palmieri

CSE 307: Principles of Programming Languages

CS 321 Programming Languages

CS 11 Ocaml track: lecture 2

CSE 307: Principles of Programming Languages

Programming Languages and Techniques (CIS120)

Functional Programming. Introduction To Cool

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Today s lecture. CS 314 fall 01 C++ 1, page 1

G Programming Languages Spring 2010 Lecture 8. Robert Grimm, New York University

Software System Design and Implementation

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Programming Languages and Techniques (CIS120)

CS 11 Ocaml track: lecture 4. Today: modules

CS 312. Lecture April Lazy Evaluation, Thunks, and Streams. Evaluation

Recap: Functions as first-class values

Programming Languages and Techniques (CIS120)

Transcription:

Some Advanced ML Features Mooly Sagiv Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming University of Washington: Dan Grossman

ML is small Small number of powerful constructs Easy to learn

What is the difference between Statement and Expression?

Class Grammar/Meta-Variable Examples Identifiers x, y a, x, y, x_y, foo1000 Datatype Type constructors X,Y Nil, List Constants c 2, 4.0, ff, [] Expressions e e::= x c e 1 e 2 (e 1, e 2,.., e n ) if e 1 then e 2 else e 3 let [rec] d 1 and and d n in e match e with p 1 -> e 1 p n ->e n fun x => e Patterns p p ::= c x [:t] (p 1,, p n ) X X(p) a:int, (x: int, y: int) Declarations Types d::= p = e y p [: t] = e datatype Y = X 1 [of t 1 ]... X n [of t n ] t ::= int float string char t 1 -> t 2 t 1 * * t n Y let one = 1 let sq(x: int): int Values v ::= c (v 1,, v n ) X(v) fun x => e

Factorial in ML let rec fac n = if n = 0 then 1 else n * fac (n - 1) let rec fac n : int = if n = 0 then 1 else n * fac (n - 1) let rec fac n = match n with 0 -> 1 n -> n * fac(n - 1) val fac : int -> int = <fun> let fac n = let rec ifac n acc = if n=0 then acc else ifac n-1, n * acc in ifac n, 1

Benefits of Functional Programming No side-effects Referential Transparency The value of expression e depends only on its arguments Conceptual Commutativity Easier to show that the code is correct Easier to generate efficient implementation

let expressions Introduce scope rules w/o side effects let x = e1 in e2 Introduce a new name x Binds x to e1 Every occurrence of x in e2 is replaced by e1 let x = e1 in e2 = ( x.e2) e1

Understanding let expressions let x = f(y, z) in g(x, x) let x = 1 and y = 2 in x + y C code { int x= 1; int y = 2; return x + y ; }

Let-expressions Syntax: Each d i is any binding and e is any expression let d 1 and and d n in e Type-checking: Type-check each d i and e in a static environment that includes the previous bindings. Type of whole let-expression is the type of e Evaluation: Evaluate each d i and e in a dynamic environment that includes the previous bindings. Result of whole let-expression is result of evaluating e.

Silly Examples let silly1 (z : int) = let x = if z > 0 then z else 34 and y = x+z+9 in if x > y then x*2 else y*y val silly1 : int -> int = <fun> let silly2 (z : int) = let x = 1 in (let x = 2 in x+1) + (let y= x+2 in y+1) val silly2 : int -> int = <fun> silly2 is poor style but shows let-expressions are expressions Can also use them in function-call arguments, if branches, etc. Also notice shadowing

List Example let rec append l1 l2 = match l1 with [] -> l2 hd :: tl -> hd :: append tl l2 val append : 'a list -> 'a list -> 'a list = <fun> let x = [2;4] //val x : int list = [2; 4] let y = [5;3;0] //val y : int list = [5; 3; 0] let z = append x y //val z : int list = [2; 4; 5; 3; 0] x y z or x y z 2 4 5 3 0 2 4 5 3 0 2 4 5 3 0 2 4 (can t tell, but it s the second one)

Exceptions Captures abnormal behavior Error handling Integrated into the type system exception Error exception Error let sqrt1 (x : float) : float = if x < 0.0 then raise Error else sqrt x val sqrt1 : float -> float = <fun> exception FailWith of string raise (FailWith "Some error message")

Question? What s the largest program you ve ever worked on by yourself or as part of a team? A. 10-100 LoC B. 100-1,000 LoC C. 1,000-10,000 LoC D. 10,000-100,000 LoC E. >= 100,000 LoC

http://www.informationisbeautiful.net/visualizations/million-lines-of-code/

Modularity Modular programming: code comprises independent modules developed separately understand behavior of module in isolation reason locally, not globally

Java features for modularity classes, packages organize identifiers (classes, methods, fields, etc.) into namespaces Interfaces describe related classes public, protected, private control what is visible outside a namespace

Ocaml Features for modularity modules organize identifiers (functions, values, etc.) into namespaces signatures describe related modules abstract types control what is visible outside a namespace

Ocaml modules Syntax: module ModuleName = struct definitions end The name must be capitalized The definitions can be any top-level definitions let, type, exception Create a new namespace Every file myfile.ml with contents D is essentially wrapped in a module definition module MyFile = struct D end Modules can be opened locally to save writing module M = struct let x = 42 end module M : sig val x : int end let fourtytwo = M.x val fourtytwo : int = 42

module Stack = struct let empty = [ ] let is_empty s = s = [ ] let push x s = x :: s let pop s = match s with [ ] -> failwith Empty x::xs -> (x,xs) end module Stack : sig val empty : 'a list val is_empty : 'a list -> bool val push : 'a -> 'a list -> 'a list val pop : 'a list -> 'a * 'a list end fst (Stack.pop (Stack.push 1 Stack.empty)) - : int = 1 Stack Module

Might Seem Backwards Java: s = new Stack(); s.push(1); s.pop() OCaml: let s = Stack.empty in let s = Stack.push 1 s in let (one, _) = Stack.pop s

Abstraction Forgetting Information Treating different things as identical

Abstraction Programming language predefined abstractions Data structures like list Library functions like map and fold Programming languages enable to define new abstractions Procedural abstractions Data abstraction Iterator abstraction

Procedural Abstraction Abstract implementation details sqrt : float -> float List.sort : ( a -> a -> int) -> a list -> a list Abstracts how the functions are implemented Both the implementation and the usage should obey the type contract The implementation can assume the right type Important for composing functions

Data Abstraction Abstract from details of organizing data stacks, symbol tables, environments, bank accounts, polynomials, matrices, dictionaries,... Abstract from implementation of organization: Actual code used to add elements (e.g.) isn t Important But types of operations and assumptions about what they do and what they require are important

Ocaml Advanced Modularity Features Functors and Signatures Functions from Modules to Modules Permit Dependency injection Swap implementations Advanced testing

Stack Abstract Data Type module type STACK = sig val empty : 'a list val is_empty : 'a list -> bool val push : 'a -> 'a list -> 'a list val pop : 'a list -> 'a * 'a list end module Stack : STACK = struct... (* as before *) end

Stack with Abstract Data Types module type STACK = sig type t val empty : t val is_empty : t -> bool val push : int -> t -> t val pop : t -> int * t end module Stack : STACK = struct type t = int list let empty = [ ] let is_empty s = s = [ ] let push x s = x :: s let pop s = match s with [ ] -> failwith "Empty x::xs -> (x,xs) end

Summary Modularity ML provides flexible mechanisms for modularity Guarantees type safety

Side-Effects But sometimes side-effects are necessary The whole purpose of programming is to conduct side-effects Input/Output Sometimes sharing is essential for functionality ML provides mechanisms to capture side-effects Enable efficient handling of code with little side effects

Input/Output print_string: String -> Unit let x = 3 in let () = print_string ("Value of x is " ^ (string_of_int x)) in x + 1 value of x is 3- : int = 4 e ::=... ( e 1 ;... ; e n ) let x = 3 in (print_string ("Value of x is " ^ (string_of_int x)); x + 1) Iterative loops are supported too

Refs and Arrays Two built-in data-structures for implementing shared objects module type REF = sig type 'a ref (* ref(x) creates a new ref containing x *) val ref : 'a -> 'a ref (*!x is the contents of the ref cell x *) val (!) : 'a ref -> a (* Effects: x := y updates the contents of x * so it contains y. *) val (:=) : 'a ref -> 'a -> unit end

Simple Ref Examples let x : int ref = ref 3 in let y : int =!x in (x :=!x + 1); y +!x - : int = 7

More Examples of Imperative Programming Create cell and change contents val x = ref Bob ; x := Bill ; Create cell and increment val y = ref 0; y :=!y + 1; While loop val i = ref 0; while!i < 10 do i :=!i +1; i; x y Bob Bill 01

Summary References Provide an escape for imperative programming But insures type safety No dangling references No (double) free No null dereferences Relies on automatic memory management

Functional Programming Languages PL types evaluation Side-effect scheme Weakly typed Eager yes ML OCAML F# Haskell Polymorphic strongly typed Polymorphic strongly typed Eager Lazy References None

Things to Notice Pure functions are easy to test prop_revrev l = reverse(reverse l) == l In an imperative or OO language, you have to set up the state of the object and the external state it reads or writes make the call inspect the state of the object and the external state perhaps copy part of the object or global state, so that you can use it in the post condition

Things to Notice Types are everywhere. reverse:: [w] -> [w] Usual static-typing panegyric omitted... In ML, types express high-level design, in the same way that UML diagrams do, with the advantage that the type signatures are machine-checked Types are (almost always) optional: type inference fills them in if you leave them out

Information from Type Inference Consider this function let reverse ls = match ls with [] -> [] x :: xs -> reverse xs and its most general type: val reverse :: list t_1 -> list t_2 = function What does this type mean? Reversing a list should not change its type, so there must be an error in the definition of reverse!

Recommended ML Textbooks L. C. PAULSON: ML for the Working Programmer J. Ullman: Elements of ML Programming R. Harper: Programming in Standard ML

Recommended Ocaml Textbooks Xavier Leroy: The OCaml system release 4.02 Part I: Introduction Jason Hickey: Introduction to Objective Caml Yaron Minsky, Anil Madhavapeddy, Jason Hickey: Real World Ocaml

Summary Functional programs provide concise coding Compiled code compares with C code Successfully used in some commercial applications F#, ERLANG, Jane Street Ideas used in imperative programs Good conceptual tool Less popular than imperative programs