Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.

Slides:



Advertisements
Similar presentations
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Advertisements

Higher-order functions in ML
Modern Programming Languages, 2nd ed.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4]
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
Introduction to OCaml Slides prepared by Matt Gruskin Some material borrowed from the CIS 500 lecture notes.
Higher-order functions in OCaml. Higher-order functions A first-order function is one whose parameters and result are all "data" A second-order function.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function F can return.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML – Part 1 Frances Spalding. Assignment 1 chive/fall05/cos441/assignments/a1.ht m
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
CSE341: Programming Languages Lecture 6 Tail Recursion, Accumulators, Exceptions Dan Grossman Fall 2011.
CS 312 Spring 2004 Lecture 18 Environment Model. Substitution Model Represents computation as doing substitutions for bound variables at reduction of.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
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.
Compiling Functional Programs Mooly Sagiv Chapter 7
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
COMP313A Functional Programming (1)
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
CMSC 330: Organization of Programming Languages Maps and Folds Anonymous Functions.
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:
CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
7/7/20161 Programming Languages and Compilers (CS 421) Dennis Griffith 0207 SC, UIUC Based in part on slides by Mattox.
Functional Programming
Programming Languages and Compilers (CS 421)
Programming Languages Dan Grossman 2013
Principles of programming languages 12: Functional programming
CSE341: Programming Languages Lecture 7 First-Class Functions
ML: a quasi-functional language with strong typing
ML Again ( Chapter 7) Patterns Local variable definitions
CMSC 330: Organization of Programming Languages
Functional Programming
CSE341: Programming Languages Lecture 7 First-Class Functions
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
CSE341: Programming Languages Lecture 7 First-Class Functions
Functions, Patterns and Datatypes
CSE 341 Section 5 Winter 2018.
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
CS 457/557: Functional Languages Folds
CSE 341 Section 3 Nick Mooney Spring 2017.
topics mutable data structures
341 midterm review Xinrong Zhao Autumn 2018.
Madhusudan Parthasarathy
CSE341: Programming Languages Lecture 7 First-Class Functions
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 7 First-Class Functions
Graduate Programming Languages: Caml Tutorial
CSE341: Programming Languages Lecture 7 First-Class Functions
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 7 First-Class Functions
Review Previously User-defined data types: records, variants
CSE341: Programming Languages Lecture 7 First-Class Functions
Presentation transcript:

objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002

topics for today familiar notions (from Scheme) let bindings, functions, closures, lists new notions (from ML) inferred types and parametric polymorphism side-effects and the unit type datatypes (variants) 2

functions applying an anonymous function # (fun x -> 2 * x) 3;; - : int = 6 declaring a function and applying it # let dbl = fun x -> 2 * x;; val dbl : int -> int = <fun> # dbl 3;; - : int = 6 functionals, or higher-order functions # let twice = fun f -> (fun x -> (f (f x)));; val twice : ('a -> 'a) -> 'a -> 'a = <fun> # (twice dbl) 3;; - : int = 12 3

let bindings a let expression binds a variable to a value # let x = 3 and y = 4 in x + y;; - : int = 7 read-eval-print-loop uses let instead of define # let x = 5;; val x : int = 5 # x;; - : int = 5 recursive let # let rec fact i = if i = 0 then 1 else i * fact (i - 1);; val fact : int -> int = <fun> # fact 4;; - : int = 24 4

let vs. define # let k = 5;; val k : int = 5 # let f = fun x -> x + k;; val f : int -> int = <fun> # f 3;; - : int = 8 # let k = 6;; val k : int = 6 # f 3;; - : int = 8 let is lexical no side-effecting top-level define built-in 5

tuples tuple constructor # let x = 1, 2;; val x : int * int = 1, 2 # fst x;; - : int = 1 # snd x;; - : int = 2 empty tuple, used instead of ‘void’ # ();; - : unit = () # print_string;; - : string -> unit = <fun> 6

function arguments tupled form: like in an imperative language # let diff (i,j) = if i < j then j-i else i-j;; val diff : int * int -> int = <fun> # diff (3, 4);; - : int = 1 # (diff 3 4);; This function is applied to too many arguments curried form: stages the computation # let diff i j = if i < j then j-i else i-j;; val diff : int -> int -> int = <fun> # (diff 3) 4;; - : int = 1 # (diff 3 4);; - : int = 1 7

lists # [1;2];; - : int list = [1; 2] # 1::2::[];; - : int list = [1; 2] lists are homogeneous # [[1]];; - : int list list = [[1]] # [1;[2]];; This expression has type 'a list but is here used with type int empty list is polymorphic # [];; - : 'a list = [] 8

polymorphic functions the simplest polymorphic function # fun x -> x;; - : 'a -> 'a = <fun> a polymorphic function over lists # let cons e l = e :: l;; val cons : 'a -> 'a list -> 'a list = <fun> # cons 1 2;; This expression has type int but is here used with type 'a list # cons 1 [];; - : int list = [1] 9

datatypes a simple datatype # type color = Red | Green | Blue;; type color = Red | Green | Blue # Red;; - : color = Red # [Red ; Green];; - : color list = [Red; Green] constructors can take arguments # type numtree = Empty | Tree of numtree * int * numtree;; type numtree = Empty | Tree of numtree * int * numtree # Empty;; - : numtree = Empty # Tree (Empty, 3, Empty);; - : numtree = Tree (Empty, 3, Empty) 10

patterns a function on number trees # type numtree = Empty | Tree of numtree * int * numtree;; # let rec treesum t = match t with Empty -> 0 | Tree (t1, i, t2) -> i + treesum t1 + treesum t2;; val treesum : numtree -> int = <fun> # let tt = Tree (Tree (Empty, 1, Empty), 3, Tree (Empty, 2,Empty));; …# treesum tt;; - : int = 6 a function on lists # let rec sum l = match l with [] -> 0 | e :: rest -> e + sum rest;; val sum : int list -> int = <fun> # sum [1;2;3;4];; - : int = 10 11

puzzle: poly functional over lists write the function map val map : ('a -> 'b) -> 'a list -> 'b list solution # let rec map f l = match l with [] -> [] | x :: xs -> (f x) :: (map f xs);; val map : ('a -> 'b) -> 'a list -> 'b list = <fun> # map dbl [1;2];; - : int list = [2; 4] 12

puzzle: user-defined poly datatypes a polymorphic tree # type 'a tree = Empty | Tree of ('a tree) * 'a * ('a tree);; type 'a tree = Empty | Tree of 'a tree * 'a * 'a tree what is the type of treefold? # let rec treefold f b t = match t with Empty -> b | Tree (left, v, right) -> f (treefold f b left, v, treefold f b right);; val treefold : ('a * 'b * 'a -> 'a) -> 'a -> 'b tree -> 'a = <fun> 13

side-effects mutable cells # let seed = ref 0;; val seed : int ref = {contents=0} dereference # !seed;; - : int = 0 assignment # seed := 1;; - : unit = () # !seed;; - : int = 1 14

puzzle write a function next which produces 0, 1, 2, etc takes no arguments 15

closures and cells # let next = (let seed = ref 0 in function () -> seed := !seed+1; !seed);; val next : unit -> int = <fun> # (next);; - : unit -> int = <fun> # next ();; - : int = 1 # next ();; - : int = 2 16

lazy lists or ‘streams’ define a datatype for streams # type 'a stream = Nil | Cons of 'a * (unit -> 'a stream);; type 'a stream = Nil | Cons of 'a * (unit -> 'a stream) # let cons x s = Cons (x, fun () -> s);; val cons : 'a -> 'a stream -> 'a stream = <fun> # let hd s = match s with Cons (x,f) -> x;; Warning: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: Nil val hd : 'a stream -> 'a = <fun> # let tl s = match s with Cons (x, f) -> f ();; Warning: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: Nil val tl : 'a stream -> 'a stream = <fun> 17

using streams # let rec from k = Cons (k, fun () -> from (k+1));; val from : int -> int stream = <fun> # (from 3);; - : int stream = Cons (3, <fun>) # hd (tl (from 3));; - : int = 4 18

puzzle given # type 'a tree = Empty | Tree of 'a * 'a tree list;; type 'a tree = Empty | Tree of 'a * 'a tree list write a function that performs a depth-first traversal of a tree gives result as a stream you can assume an infix function @ for appending streams 19