OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students.

Slides:



Advertisements
Similar presentations
Modern Programming Languages, 2nd ed.
Advertisements

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.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Writing functions in OCaml. Defining a simple function # let add (x, y) = x + y;; val add : int * int -> int = Notice what this says: –add is a value.
ML Declarations.1 Standard ML Declarations. ML Declarations.2 Declarations o Re-declaration of names in ML o The reserved words "val rec" o Pattern Matching.
Peter Fritzson 1 MetaModelica for Meta-Modeling and Model Transformations Peter Fritzson, Adrian Pop, Peter Aronsson OpenModelica Course at INRIA, 2006.
ML Declarations.1 Standard ML Declarations. ML Declarations.2  Area of a circle: - val pi = ; val pi = : real - fun area (r) = pi*r*r;
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Programovací jazyky F# a OCaml Chapter 3. Composing primitive types into data.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
Recursive Data Types Koen Lindström Claessen. Modelling Arithmetic Expressions Imagine a program to help school-children learn arithmetic, which presents.
Map and Fold Building Powerful Abstractions. Hello. I’m Zach, one of Sorin’s students.
CSE341: Programming Languages Lecture 4 Records (“each of”), Datatypes (“one of”), Case Expressions Dan Grossman Fall 2011.
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.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture: Datatypes and Recursion.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Chapter 9: Functional Programming in a Typed Language.
Functional Programming Language OCaml Tutorial 科大 - 耶鲁联合研究中心
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
CMSC 330: Organization of Programming Languages
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 1 Extracting Sequence.
Chapter SevenModern Programming Languages1 A Second Look At ML.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
CSE 341 : Programming Languages Lecture 4 Records, Datatypes, Case Expressions Zach Tatlock Spring 2014.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2013.
7/7/20161 Programming Languages and Compilers (CS 421) Dennis Griffith 0207 SC, UIUC Based in part on slides by Mattox.
Koen Lindström Claessen
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2013.
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2018.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2016.
Programming Languages and Compilers (CS 421) #3: Closures, evaluation of function applications, order of evaluation #4: Evaluation and Application.
Madhusudan Parthasarathy (madhu) 3112 Siebel Center
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Doug Woos Winter 2018.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

OCaml The PL for the discerning hacker.

Hello. I’m Zach, one of Sorin’s students.

ML Anatomy 101 ML Program = One Giant, Complex Expression Controlling complexity is the essence of computer programming. B. Kerninghan A complex system that works is invariably found to have evolved from a simple system that worked. J. Gall ML Program = ? ? ?

Building ML Programs ML provides tools to control complexity Build complex exprs from simple exprs Build complex types from simple types PREV NOW

Building Types 1.basic (recap) 2.function 3.record 4.variant 5.demo M.C. Escher’s Relativity in LEGO

basic Who cares about types anyway? Every good programmer! (not just old timers) Types provide: 1.Documentation 2.Early bug warning system 3.Performance!

basic Who cares about types anyway? Even programmers without a type system! I think it may just be a property of large systems in dynamic languages, that eventually you end up rewriting your own type system, and you sort of do it badly. -- Twitter

basic ExpressionType Kind of Type 5intbasic “hello”stringbasic (5, “hello”)int * stringtuple [1; 2; 3; 4]int listlist [ (1, 1) ; (2, 2) ; (3, 3) ](int * int) listtuple+list

more on tuples ExpressionType (5, (10, 15))int * (int * int) ((5, 10), 15)(int * int) * int

more on tuples ExpressionType (5, (10, 15))int * (int * int) ((5, 10), 15)(int * int) * int ( [ (1, 2); (3, 4); (5, 6) ], (“hello”, “india”), (5, “int”, (1, 2, 3)) )

more on tuples ExpressionType (5, (10, 15))int * (int * int) ((5, 10), 15)(int * int) * int ( [ (1, 2); (3, 4); (5, 6) ], (“hello”, “india”), (5, “int”, (1, 2, 3)) ) (int * int) list * (string * string) * (int * string * (int * int * int))

basic Don’t know how it works ? Try it in the toplevel !

Building Types 1.basic (recap) 2.function 3.record 4.variant 5.demo M.C. Escher’s Relativity in LEGO

function A -> B Type of a function which: expects parameter of type A produces a value of type B Contract: caller satisfies A callee satisfies B precondition postcondition

function let f x = x + 5 f: int -> int let f x = “hello “ ^ x f: string -> string let f x = “number “ ^ (string_of_int x) f: int -> string let f x y = x * x + y * y f: int -> int -> int

function let f x y = 1 :: x :: y f: int -> int list -> int list let f x y z = [y + z] f: int list -> int -> int -> int list let rec f = f f ERROR

polymorphic functions Some functions work on many types: let id x = x id: ‘a -> ‘a Takes a value of any type, call it ‘a Returns a value of that type ‘a

polymorphic functions let f a b = a f: ‘a -> ‘b -> ‘a let f a b = b f: ‘a -> ‘b -> ‘b let pipe x f = f x f: ‘a -> (‘a -> ‘b) -> ‘b let (|>) = pipe “hello” |> id |> print_string (print_string (id (“hello”))) binary infix operator

polymorphic functions let f g x = g x f: (‘a -> ‘b) -> ‘a -> ‘b let f g h x = g (h x) f: (‘a -> ‘b) -> (‘c -> ‘a) -> ‘c -> ‘b

Building Types 1.basic (recap) 2.function 3.record 4.variant 5.demo M.C. Escher’s Relativity in LEGO

record type RT = { NM1 : T1 ;... ; NMN : TN } Like a tuple, but refer to members by name.

record type person = { name : string ; age : int ; hair : string ; job : string }

making a record value let pres = { name = “Obama” ; age = 49 ; hair = “black” ; job = “president” }

updating a record value let pres = { name = “Obama” ; age = 49 ; hair = “black” ; job = “president” } let pres’ = {pres with hair = “gray” }

updating a record value let year_older p = if p.age > 45 then { p with age = p.age + 1, hair = “gray” } else { p with age = p.age + 1 } year_older: person -> person

Building Types 1.basic (recap) 2.function 3.record 4.variant 5.demo M.C. Escher’s Relativity in LEGO

variant type VT = | C1 of T1 |... | CN of TN C1 to CN are “constructors” Ci like function from Ti to VT value of type VT can only be constructed with one of these

variant type pet = | Dog of string | Cat of string | Fish of string Value of type pet constructed with one of: Dog, Cat, Fish Each takes a string and returns a pet

variant values type pet = | Dog of string | Cat of string | Fish of string let d = Dog “spot” let c = Cat “whiskers” let f = Fish “nemo”

matching variant values let name pet = match pet with | Dog nm -> nm | Cat nm -> nm | Fish nm -> nm let d = Dog “sparky” in name d

matching variant values let says pet = match pet with | Dog _ -> “woof” | Cat _ -> “meow” | Fish _ -> “bubble bubble” let c = Cat “walter” in says c

variant type fuel_level = | Empty | Middle | Full type vehicle = | Car of int * fuel_level | Tank of int * fuel_level | Boat of int * fuel_level

matching variant values let miles v = match v with | Car (m, _) -> m | Tank (m, _) -> m | Boat (m, -) -> m

updating variant values let reduce f = match f with | Empty -> Empty | Middle -> Empty | Full -> Middle let drive v = match v with | Car (m, f) -> Car (m, reduce f) | Tank (m, f) -> Tank (m, reduce f) | Boat (m, f) -> Boat (m, reduce f)

updating variant values let refill v = match v with | Car (m, f) -> Car (m, Full) | Tank (m, f) -> Tank (m, Full) | Boat (m, f) -> Boat (m, Full)

recursive variant type expr = | Val of int | Add of expr * expr | Sub of expr * expr | Mul of expr * expr let e1 = Val 5 let e2 = Add (e1, e1) let e3 = Mul (e2, e1)

recursive variant let rec eval e =

recursive variant let rec eval e = match e with | Val i -> i | Add (l, r) -> (eval l) + (eval r) | Sub (l, r) -> (eval l) - (eval r) | Mul (l, r) -> (eval l) * (eval r)

mutual recursion let rec expr_dot e = match e with | Val i -> string_of_int i | Add (l, r) -> (aux "add" l) ^ (aux "add" r) | Sub (l, r) -> (aux "sub" l) ^ (aux "sub" r) | Mul (l, r) -> (aux "mul" l) ^ (aux "mul" r) and aux p e = match e with | Val i -> p ^ " -> " ^ string_of_int i ^ ";\n" | Add _ -> p ^ " -> add;\n" ^ (expr_dot e) | Sub _ -> p ^ " -> sub;\n" ^ (expr_dot e) | Mul _ -> p ^ " -> mul;\n" ^ (expr_dot e)

Pattern Matching: a PL Masterpiece match is one of ML’s very best features simultaneous test / extract / bind auto checks any missed cases leads to compact, readable code

Building Types 1.basic (recap) 2.function 3.record 4.variant 5.demo M.C. Escher’s Relativity in LEGO

demo Conway’s Game of Life

demo Conway’s Game of Life code at:

demo OCaml in the “real world” at JaneStreet Check out their leader, Yaron Minsky

Building Types 1.basic (recap) 2.function 3.record 4.variant 5.demo M.C. Escher’s Relativity in LEGO