Summary of polymorphism Back to OCaml

• Subtype • Polymorphic types allow us to reuse code

• Parametric • However, not always obvious from staring at code • Bounded • But... Types never entered w/ program! • F-bounded

1 2

Example 1

Type inference let x = 2 + 3;;

let y = string_of_int x;; aka: how in the world does Ocaml figure out all the types for me ???

3 4

Example 2 Example 2

let x = 2 + 3;; let x = 2 + 3;; let inc y = x + y;; let inc y = x + y;;

5 6

1 Example 3 Example 3

let foo x = let foo x = let (y,z) = x in let (y,z) = x in z-y;; z-y;;

7 8

ML doesn’t know what the function does, or even that it terminates. Example 4 Example 4 ML only knows its type!

let rec cat l = let rec cat l = match l with match l with [] -> “” [] -> “” | h::t -> h^(cat t) | h::t -> h^(cat t)

9 10

Example 5 Example 5

let rec map f l = let rec map f l = match l with match l with [] -> [] [] -> [] | h::t ->(f h)::(map f t) | h::t ->(f h)::(map f t)

11 12

2 Inferring types with ‘a Example 6

• Introduce unknown type vars let compose (f,g) x = f (g x)

• Figure out equalities that must hold, and solve these equalities

• Remaining types vars get a forall and thus become the ‘a, ‘b, etc.

13 14

Example 6 Example 7

let compose (f,g) x = f (g x) let rec fold f cur l = match l with [] -> cur | h::t -> fold f (f h cur) t

15 16

Example 7

let rec fold f cur l = match l with Deconstructing OCaml [] -> cur | h::t -> fold f (f h cur) t What makes up a language

17

3 Key components of a lang

• Units of computation Units of computation • Types

• Memory model

In OCaml In OCaml

• Expressions that evaluate to values • Everything is an expression – int, bool, real – if-then-else – let-in – match – fn x -> x+1 – e1 e2

In /Python In Java/Python

• Store and update commands

• Message sends

4 In Prolog In Prolog

• Logical facts • Inference rules

Mexican(CARNITAS) “Fact” Food(CARNITAS) “Fact” Mexican(X)  Food(X) Delicious(X) “Rule”

Delicious(CARNITAS) “Fact”

Types

• Used to classify things created by the Types programmer

• Classification used to check what can be done with/to those things

In OCaml: Static typing In OCaml: Static typing

• Types are assigned statically at compile • How can one reuse code for different time types? – parametric types: ‘a * ‘b -> ‘b * ‘a • Without computing values – implicit forall

• Rules state when expressions are type- • Type “discovered” (inferred) correct automatically from code e1:T1→ T2 e2: T1 – less burden on the programmer e1 e2 : T2

5 In Python: Dynamic typing In Python: Dynamic typing

• Types assigned to values/objects as they • More programs are accepted by compiler are computed, ie: dynamically • More flexible, but find errors late

• Before an operation is performed, check [1, “abc”, 1.8, [ “efg”, 20]] that operands are compatible with operation let x = if b then 1 else “abc” let y = if b then x + 1 else x ^ “efg”

Dynamic vs. Static, OO vs. Func Dynamic vs. Static, OO vs. Func

Dynamically Dynamically Statically typed Statically typed typed typed

Python, OO OO Java Smalltalk

Functional Functional Ocaml, Haskell Lisp/Scheme

Polymorphism

• Can a language be dynamically typed, but not polymorphic? Memory/Data model • Every dynamically typed language is polymorphic – functions just simply work on any datatype that can be operated on at runtime aka: what do variables refer to?

• Only need explicit polymorphism in statically typed languages to assign at a suitably general polymorphic type

6 Data model in functional langs Data model in functional langs

• Environment of bindings (phonebook) • Variables are names that refer into the phonebook • Most recent entry looked up during evaluation

• Never change a binding • Environment “frozen” inside function – add new bindings at the end of the value so that the behavior of the function phonebook cannot be changed later on (easier reasoning)

Data model in OO langs Data model in Prolog

• Variables are cells in memory • Variables in Prolog are unknowns to solve • Can change them by assigning into them for

Mexican(CARNITAS) • Variables point to objects on the heap Food(CARNITAS)

 X Mexican(X)  Food(X) Delicious(X) • x = x + 10 Delicious(Y)?

Q: What is delicious? A: CARNITAS!

What’s the point of all this?

Final words on

7 Advantages of functional progs Don’t be fooled

• Functional programming more concise • Some of the programming assignments “one line of lisp can replace 20 lines of ” made you do certain things using fold in (quote from http://www.ddj.com/dept/architect/184414500?pgno=3) order to force you to think about it, even though using fold was not the easiest way • Recall reverse function in OCaml: to do it.

let reverse = fold (::) [];; • But there are many cases where map and • How many lines in C, C++? fold make life A LOT EASIER.

Can better reason about progs So what?

• No side effects. Call a function twice • From the authors: “Inspired by similar with same params, produces same value primitives in LISP and other languages” http://labs.google.com/papers/mapreduce-osdi04-slides/index-auto-0003.html

• As a result, computations can be • The point is this: programmers who only reordered more easily know Java/C/C++ would probably not • They can also be parallelized more easily have come up with this idea

• Many other similar examples in industry

This stuff is for real: F# Remember

F# = Microsoft’s Ocaml-on-steroids • The next time you use google, think of how functional programming has inspired http://channel9.msdn.com/pdc2008/TL11/ some of the technical ideas behind their engine

• Why FP is way cool • And of course: • How FP works with Objects (C#) “Free your mind” • How FP allows you to write parallel code -Morpheus … all with an extremely engaging speaker

8