Docsity

ML Programming: History, Types, Polymorphism, and Function Matching at UNC Chapel Hill - P, Study notes of Programming Languages

Partial preview of the text

Download ML Programming: History, Types, Polymorphism, and Function Matching at UNC Chapel Hill - P and more Study notes Programming Languages in PDF only on Docsity!

Lecture 14: ML, Deep/Shallow Access, Referential Transparency

COMP 524 Programming Language Concepts Aaron Block March 6, 2007 Based on notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

ML History

  • • ML Stands for “Meta-language”Developed in 1970s by Robert Milner at the University of Edinburgh
  • Characteristics • Functional control structures
    • • strict, formal semantics (provable correctness)Strict polymorphic type system
    • Still subject of active industry research^ •^ Coercion not allowed
      • Microsoft is working on variant called F# for their .NET framework 2

Types in ML • Built-in Types:

• • • IntegerRealString

• • CharBoolean

• From these we can construct • Tuples • (#“a”, 5, 3.0, “hello”,: Heterogeneous element types with finite fixed length true): char int realstringbool

• Lists: • • [5.0, 3.2, 6.7] : real[(# “a”, 7), (# “b”, 8)]: list (char *int)list

• • FunctionsRecords

4

Types inference in ML

• Everything is inferred; ML complains if anything is ambiguous.

• • What is the inferred type ofHow about the function? r? Why?

• • r must be of type real Type of function • Can be explicit by defining fun circum.: circum(r:real)...

• real->real 5

fun circum(r) circum(7.0); = r * 2.0 * 3.14159;

Polymorphism in ML

• Consider the following function in ML:

7

fun compare(x,p,q) = if x=p then if x = q then “both” else^ else “first” if x=q then “second” either “neither”

What is type of compare? x? p? q?

Polymorphism in ML

• Consider the following function in ML:

8

fun compare(x,p,q) = if x=p then if x = q then “both” else^ else “first” if x=q then “second” either “neither”

What is type of compare? x? p? q? a*a*`a->string

Type Checking

• • ML verifiesSet of constraints type consistency

• • All occurrences of same identifier (in same scope) have the same type.In an if...then..else... construct, if condition must have

• typeProgrammer defines functions have type type of function parameters and^ bool, and^ then^ and^ else^ must have same type. b is type of function return.a->b wherea is

• When function is called, the arguments passed and value returned must have same type as definition.

• Process of checking if two types are the same is called unification.

10

Lists in ML

• Heterogeneous & Homogeneous Lists operator: • Appending (joining) two lists:

• Prefixing a list with an item:

11

[1,4]@[3,5] => [1,4,3,5] (“hi”, 3.0)@(4, “bye”) => (“hi”, 3.0, 4, “bye”) 1::[2,7,9] => [1,2,7,9]; NOTE: [2,7,9]::1 is illegal (use [2,7,9]@[1] instead)

Conditional Expressions & Pattern Matching in ML

• Case Statement:

13

fun append(l1,l2) = case l1 of nil =>l | h :: t => h::append(t,l2);

if L1 has form “if^ L1 = nilh::t ” then return “^ then return h::append(t,L2)^ L2 ”

Function Pattern Matching in ML

• Function definition as series of alternatives:

• Becomes

14

fun appends(l1, l2) = if l1 = nil then l2 else hd(l1) :: append (tl(l1), l2); fun append(nil, l2) = l2 | append (h::t, l2) = h :: append(t, l2);

Higher-Order Functions

• Higher-order functions are functions that take functions as arguments

16

fun map(F, nil) = nil | map(F, x::cs) = F(x)::map(F,xs);

fun add5(x) = map(add5, [3,24,7,9]); => [8,29,12,14] x+5; map(fn x=> x+5, [3,24,7,9]); => [8,29,12,14]

To Add 5 to every integer we could...

“Currying” in ML

• Currying function is replaced by ais a method in which a single argument function multiple argument

that returns a function with the remaining arguments.

17

fun add(x,y) = x + y : int; >> val add = fn int * int -> int fun add x = fn y=> x+y; >> val add = fn int -> int ->int fun add x y = x+y; >> val add = fn int -> int ->int

Scope in ML is Lexical

• • Top level environment has allEvery “ val x = false; ” binding adds another row to the pre-defined bindings

•^ symbol table when compiling/interpretingEach row hides earlier bindings of the same name

•^ (does not destroy them)Local bindings can be made in functions definitions

• Locals are removed from the symbol stack function definition is complete when the

19

Name/Value Bindings

20

sq^ x^ varfn \z:z*26.3[1,2,3]^ real->realint list

k2 x varvar 26.3false boolreal

name^ r1^ varcat (7,3.14)value int * realtype

val r1 = (7,3.14); val x = false; val k2 = 26.3; fun sq z:real = z*k2; val x = [1,2,3]

new bindings