240 likes | 355 Views
Getting started with ML. ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a program are calculated (inferred) at compile time . Scheme, on the other hand, is a dynamically typed language.
E N D
Getting started with ML • ML is a functional programming language. • ML is statically typed: The types of literals, values, expressions and functions in a program are calculated (inferred) at compile time. Scheme, on the other hand, is a dynamically typed language. • ML has a general mechanismfor parameter passing: Patten Matching. • ML enables the definition of new types.
Recursive Functions: Example (1) • Recursive functions can be defined in the global scope using standard naming (using the global environment for binding). • ML introduces a keyword 'rec' for the declarations of recursive functions. • Given b≠0 and a natural number, n, calculate bn according to the following formula: ML: Would this work? Good old Scheme: (define exp (lambda (b n) (cond((= n 1) b) ((even? n) (exp (* b b) (/ n 2))) (else (* b (exp b (- n 1))))))) • val exp = fn(b, n)=> • if n = 1 • then b • elseif n mod 2 = 0 • then exp (b*b, n div 2) • else b * exp (b, n-1);
Recursive Functions • val exp = fn(b, n)=> • if n = 1 • then b • elseif n mod 2 = 0 • then exp (b*b, n div 2) • else b * exp (b, n-1); unbound variable or constructor: exp Error: Why is it yelling? ML’s compiler checks the function body at static (compile) time. When it reaches the recursive call to exp, exp is not yet unbound! So how come Dr.scheme was so nice not to yell? Because it does not read the function’s body at static time, and at run-time, the function is already defined!
Recursive Functions That’s how you comment (* Signature: exp(b,n) Purpose: Calculates the of a natural number Type: Int*Int-> Int*) valrecexp = fn(b, n) => if n=1 then b else if n mod 2 = 0 then exp (b*b, n div 2) else b * exp (b, n-1); - valexp = fn : int * int -> int The expression fn=> is the procedure value constructor in ML. It is equivalent to scheme’s lambda. Use the keyword rec for the declerations of recursive functions
Pattern Matching: Example (2) • The exp procedure was previously definedwith a single pattern (b, n). valrecexp = fn(b, n) => if n=1 … • We redefine exp function using two patterns: valrecexp = fn (b, 1) => b | (b, n) => if n mod 2 = 0 then exp (b*b, n div 2) else b * exp (b, n-1); - valexp = fn : int * int -> int • A call to exp with (2,8) may be regarded as a call with one argument. • The argument (2,8) matches the 2nd pattern: (b,n) and its “body” is executed. • The first clause whose pattern matches the argument is the one to execute. • The rest are ignored (like 'cond' evaluation in Scheme).
Pattern Matching: • Pattern => Variable | Value Constructor | Wildcard Character • Wildcard Character => _ • Value Constructor => Atomic | Tuple | List | User defined value • Atomic => Numeric | Character | Boolean | String constants • Tuple => ‘(‘ Pattern ‘,’,…’,’ Pattern ‘)’ • List => nil | Pattern::List • User defined value constructors=> • Constraints: • A variable may occur in a pattern only once: valfoo=fn (a,a)=> • The function constructor ‘fn' cannot appear in a pattern (a function is not an equality type). • Notevery ML expression is a pattern: In the exp procedure defined earlier, +, =>, (if 1 = a then true else false), are all ML expressions but are not patterns.
Data Types: • Problems that require data beyond numbers or Booleans require the extension of the type system with new datatypes (datatype: a type and its associated operations). • The introduction of a new data type consists of: • Type constructors: introduce a name(s) for the new type, possibly with parameters. • Value constructors: introduce labeled values. • In scheme we use tagged values to define new types. However, these tagged values have no special positions in the language, which is why they are not really new types. • In contrast to scheme, ML enables the extension of the typing system with new types.
Data Types: Atomic Types An atomic type is a set of atomic values, which are also its (parameter-less) value constructors. They are also called Enumeration Types: (* Type Constructor: direction Value Constructor: North, South, East, West Purpose: A datatype declaration that creates a new type to represent the compass directions. *) datatypedirection = North | South | East | West; • - valmove = fn((x,y),North) => (x,y+1) • |((x,y),South) => (x,y-1) • |((x,y),East) => (x+1,y) • |((x,y),West) => (x-1,y); • valmove = fn : (int * int) * Direction -> int * int West is a value constructor of type direction and it has no parameters – it is a constant. • move((4,5), North); • valit = (4,6) : int * int
Data Types: Composite Types: Example – complex numbers A composite type is a (user defined) type whose values are created by value constructors that take as parameters values of other types. A concrete type is a non-polymorphic type. Example: A system that performs arithmetic operations on complex numbers: Rectangular representation: Convenient for addition and subtraction Polar representation: Convenient for division and multiplication. r
Data Types: Composite Types: Example – complex numbers In ML, using the type constructors and value constructors (that act like type tags in Scheme), the problem is simple to solve: (* Type constructor: complex Value constructors: Rec, Polar. Data values of this type have the form: Rec(3.0, 4.5), Polar(-3.5, 40.0) *) datatypecomplex = Rec of real * real | Polar of real * real; (* auxiliary function: square*) - val square = fn x : real => x * x; val square = fn : real -> real
Data Types: Composite Types: Example – complex numbers In ML, using the type constructors and value constructors (that act like type tags in Scheme), the problem is simple to solve: - val real = fn (Rec(x,y) ) => x | (Polar(r,a)) => r * Math.cos(a); val real = fn : complex -> real - val imaginary = fn (Rec(x,y) ) => y | (Polar(r,a)) => r * Math.sin(a); val imaginary = fn : complex -> real - val radius = fn (Rec(x,y) ) => Math.sqrt( square(x) + square(y) ) | (Polar(r,a)) => r; val radius = fn : complex -> real - val angle = fn (Rec(x,y) ) => Math.atan( y / x ) | (Polar(r,a)) => a; val angle = fn : complex -> real
Data Types: Composite Types: Example – complex numbers • - valadd_complex= fn (Rec(x, y), Rec(x', y')) => ( Rec( x + x', y + y') ) • | (Rec(x,y), z) => ( Rec( x + real(z), y + imaginary(z) ) ) • | (z, Rec(x, y)) => ( Rec( real(z) + x, imaginary(z) + y) ) • | (z,z') => (Rec( real(z) + real(z'), imaginary(z) + imaginary(z') ) ); • valadd_complex = fn : complex * complex -> complex valsub_complex = fn (Rec(x, y), Rec(x', y')) => ( Rec( x - x', y - y') ) | (Rec(x,y), z) => ( Rec( x - real(z), y + imaginary(z) ) ) | (z, Rec(x, y)) => ( Rec( real(z) - x, imaginary(z) - y) ) | (z,z') => (Rec( real(z) - real(z'), imaginary(z) - imaginary(z') ) ); valsub_complex = fn : complex * complex -> complex valmul_complex = fn (Polar(r, a), Polar(r', a')) => (Polar(r * r', a + a')) | (Polar(r,a), z) => (Polar( r * radius(z), a + angle(z) ) ) | (z, Polar(r,a)) => (Polar( radius(z) * r, angle(z) + a ) ) | (z, z') => (Polar( radius(z) * radius(z'), angle(z) + angle(z') ) ); valmul_complex = fn : complex * complex -> complex
Data Types: Composite Types: Example – complex numbers (* Pre -condition: r' != 0 *) valdiv_complex= fn (Polar(r, a), Polar(r', a')) => (Polar(r / r', a - a')) | (Polar(r, a), z) => (Polar(r / radius(z), a - angle(z))) | (z, Polar(r, a)) => (Polar(radius(z) / r, angle(z) - a)) | (z, z') => (Polar(radius(z) / radius(z'), angle(z) - angle(z'))); valdiv_complex = fn : complex * complex -> complex
Data Types: Polymorphic and Recursive Types: Example – ‘a tree Recursive types create infinite sets of values. The value constructors of a recursive datatypes accept parameters of the defined datatype. A recursive type definition needs a base case, i.e., a value constructor whose parameters are not the defined type. - Datatype ‘a tree = Null | Leaf of ‘a | Node of ‘a * ‘a tree * ‘a tree; datatype ‘a tree = Null | Leaf of ‘a | Node of ‘a * ‘a tree * ‘a tree - Leaf(3); val it = Leaf 3 : int tree - Node(1, Leaf(2), Null); val it = Node (1,Leaf 2,Null) : int tree val binnum = Node (5, Leaf(4), Node(2, Leaf(1), Node(8, Null, Null))); valbinnum = Node (5,Leaf 4,Node (2,Leaf 1,Node (8,Null,Null))) : int tree 5 2 4 1 8 N N
Data Types: Recursive Polymorphic Types: Example – tree Example: the function treefold (analogous to accumulate). valrectreefold = fn (f, e, Null) => e | (f, e, Leaf(n)) => n | (f, e, Node(node, left, right)) => f(node, treefold(f , e, left), treefold(f, e, right)); valtreefold = fn : ('a * 'b * 'b -> 'b) * 'b * 'a tree -> ‘b 5 • treefold((fn(a,b,c) => a + b + c), 0, binnum) => 20 • val it = 20 : int 2 4 1 8 N N
Lazy lists: Introduction • Lazy lists are lists whose elements are not explicitly computed. We will use such lists to represent infinite sequences. • In eager operational semantics, the "regular" list implementation computes all list elements before constructing the list: due to applicative order, arguments are evaluated before calling a list constructing function (e.g. the function 'list' in Scheme). therefore, lazy lists must bedefined as a new datatype. • Lazy lists are a special feature of functional programming, since their implementation is typically based upon creating procedures at run time.
Lazy lists: Introduction • Although lazy lists are possibly infinite, we take care to construct lazy lists such that it is possible to reach every finite location in the list in finite time. • An important advantage of lazy lists is that we only compute the part of the sequence the we require, without producing the entire sequence.
Basic Definitions: Lazy list definition We define the type constructor seq for creating lazy-lists : - datatype 'a seq = Nil | Cons of 'a * (unit -> 'a seq); - Nil; (* The empty lazy-list *) val it = Nil : 'a seq; • - Cons(1, Nil); • ERROR! Why? The 2nd argument should be a function which takes no arguments and returns an ‘a seq. - val seq1 = Cons(1, fn()=>Nil); (* this sequence contains Nil at it's tail *) val seq1 = Cons (1,fn) : intseq 1::Nil • - val seq2 = Cons(2, fn()=>seq1); (* this sequence contains seq1 at it's tail *) val seq2 = Cons (2,fn) : intseq 2::1::Nil
Basic Infinite Sequences: An Infinite sequence of ones (* Signature: ones() Purpose: produce a lazy sequence in which each element is the number 1. Type: unit -> intseq *) -valrec ones = fn () => Cons (1, ones); val ones = fn : unit -> intseq • We use take to produce a finite list of elements of the sequence ones: • (* Signature: take(seq,n) … *) • … • valrec take = fn (seq, 0) => [ ] • | (Nil, n) => raise Subscript • | (Cons(h,t), n) => h::take( t(), n-1); • val take = fn : 'a seq * int -> 'a list take ( ones () , 10 ); val it = [1,1,1,1,1,1,1,1,1,1] : int list
Infinite sequence operations: Fibonacci • We create the infinite sequence of Fibonacci numbers. • (* Signature: fibs() • Purpose: produce a seq of fib numbers. • Type: unit -> intseq *) • val fibs = let valrec fibs_help = fn(n, next) => Cons(n, (fn()=>fibs_help(next, n+next)) ) • in fibs_help(0, 1) end; take ( fibs(), 4 ) take ( Cons(0,fn()=>fibs_help(1,1)), 4 ) 0::take( fn()=>fibs_help(1,1) (), 3) 0::take( Cons(1,fn()=>fibs_help(1,2)), 3) 0::1::take(fn()=>fibs_help(1,2) (), 2) 0::1::take(Cons (1,fn()=>fibs_help(2,3)), 2) 0::1::1::take(fn()=>fibs_help(2,3) (), 1) 0::1::1::take(Cons(2,fn()=>fibs_help(3,5) ), 1) 0::1::1::2::take(fn()=>fibs_help(3,5) (), 0) 0::1::1::2::[] [0,1,1,2]
More examples: The function repeated (* Signature: repeated_seq(f,x) Purpose: produce the seqx,f(x),f(f(x)),…fn(x),… Type: ('a -> 'a) * 'a -> 'a seq *) - valrecrepeated_seq = fn (f, x) => ?; valrepeated_seq = fn : ('a -> 'a) * 'a -> 'a seq (* Signature: repeated_seq(f,x) Purpose: produce the seqx,f(x),f(f(x)),…fn(x),… Type: ('a -> 'a) * 'a -> 'a seq *) - valrecrepeated_seq = fn (f, x) => Cons(x, fn()=>repeated_seq(f, f(x))); valrepeated_seq = fn : ('a -> 'a) * 'a -> 'a seq The geometric series a0, a0q, a0q^2, …, a0q^n, … • valgeom_series = • fn (a0, q) => ? • valgeom_series = fn : int * int -> intseq • valgeom_series = • fn (a0, q) => repeated_seq (fn(x)=>q*x, a0); • valgeom_series = fn : int * int -> intseq - take(geom_series(10, 2), 5); val it = [10,20,40,80,160] : int list
Infinite sequence operations: Interleaving sequences • Joining 2 infinite sequences may be done by interleaving them together. • 1,1,1,…. with 2,2,2,… = 1,2,1,2,1,2,…. • (* Signature: interleave(seq1,seq2) • Purpose: produce a seq that combines elements from seq1 and seq2. • Type: 'a seq* 'a seq -> 'a seq *) • valrec interleave = fn (Nil, seq) => seq • | (Cons(h, tl), seq) => Cons(h, (fn()=>interleave(seq, tl() ) ) ); • val interleave = fn : 'a seq * 'a seq -> 'a seq take (interleave(ones,twos), 3) take (Cons (1,fn()=>interleave(twos, ones)), 3) 1::take(interleave(twos, ones), 2) 1:: take(Cons (2,fn()=>interleave(ones, twos)), 2) 1::2::take(interleave(ones, twos), 1) 1::2::take(Cons (1,fn()=>interleave(twos, ones)), 1) 1::2::1::take(interleave(twos, ones), 0) 1::2::1::[] [1,2,1]
Infinite sequence operations: Nested sequence • We can use nested_seq to produce a sequence in which each element is a sequence. (* Signature: nested_seq(seq) Purpose: produce a seq in which each element is seq… Type: intseq -> intseqseq Example: take(nested_seq(ints_from(1)),3) => [Cons (1,fn),Cons (2,fn),Cons (3,fn)] : intseq list *) - val nested_seq = fn(seq) => map_seq (fn(x)=>ints_from(x), seq); val nested_seq = fn : 'a seq -> 'a seqseq Mapping [1,2,3,4,…] [[1,2,3,4,…],[2,3,4,5,…],[3,4,5,6,…],…] nested_seq (ints_from(1)) map_seq ( fn(x)=>ints_from(x) ,Cons(1, fn()=>ints_from(2))) Each int is mapped to the infinite sequence of ints starting with it. Cons(fn(x)=>ints_from(x) (1), fn()=>map_seq(fn()=>ints_from(2)()))
Infinite sequence operations: Nested sequence • Another example (using the function list_ref): • (*TYPE: 'a list * int --> 'a *)valreclist_ref = fn ([], _) => raise Empty • | ( a::li, 0) => a • | ( a::li, n) => list_ref( li, n-1); val nest1 = nested_seq(ints_from(1)); val list2 = take(nest1 ,2); • Note:We have the concrete list ( [1,2,3,…] , [2,3,4,…] ) since we used take! valsecond_element = list_ref( list2, 1); • Note: We refer to elements starting with 0. list_ref( list2, 1) refers to the 2nd element. take(second_element, 5);val it = [2,3,4,5,6] : int list