CS%202104%20 - PowerPoint PPT Presentation

About This Presentation
Title:

CS%202104%20

Description:

Examples of Polymorphic Functions. fun compose f g = (fn x = f (g x)) t1 ... compose: (t(gx)- t(f(gx)))- (tx- t(gx))- (tx- t(f(gx))) Rename the variables: ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 43
Provided by: khoosia
Category:
Tags: compose

less

Transcript and Presenter's Notes

Title: CS%202104%20


1
CS 2104 Prog. Lang. ConceptsFunctional
Programming II
  • Lecturer Dr. Abhik Roychoudhury
  • School of Computing
  • From Dr. Khoo Siau Chengs lecture notes

2
reduce (op ) 2,4,6 1 gt 2 (4 (6
1)) gt 48
reduce (fn (x,y)gt1y) 2,4,6 0 gt 1 (1
(1 0)) gt 3
3
Types Classification of Values and Their
Operators
Basic Types
Type Values Operations bool true,false ,
ltgt, int ,1,0,1,2, ,ltgt,lt,,div, real ..
,0.0,.,3.14,.. ,ltgt,lt,,/, string foo,\q\
, ,ltgt,
Boolean Operations e1 andalso e2 e1
orelse e2
4
Types in ML
  • Every expression used in a program must be
    well-typed.
  • It is typable by the ML Type system.
  • Declaring a type
  • 3 int
  • 1,2 int list
  • Usually, there is no need to declare the type in
    your program ML infers it for you.

5
Structured Types
Structured Types consist of structured values.
  • Structured values are built up through
  • expressions. Eg (23, square 3)
  • Structured types are denoted by type
  • expressions.

lttype-exprgt lttype-namegt lttype-constantgt
lttype-exprgt lttype-exprgt
lttype-exprgt ? lttype-exprgt lttype-exprgt list

6
Type of a Tuple
(1,2) int int (3.14159, x3,true) real
int bool
A B set of ordered pairs (a,b)
Data Constructor ( , ) as in (a,b) Type
Constructor as in A B
In general, (a1,a2,,an) belongs to A1A2An.
7
Type of A List
Type Constructor list
1,2,3 int list 3.14, 2.414 real
list 1, true, 3.14 ??
Not well-typed!!
A list set of all lists of A -typed values.
A in A-list refers to any types (intint)
list , (1,3), (3,3),(2,1), int list
list , 1,2, 1,0,1,2,2,3,
8
Function TypesDeclaring domain co-domain
fac int -gt int
A -gt B set of all functions from A to B.
Type Constructor -gt Data Construction via
1. Function declaration fun f x x 1 2.
Lambda abstraction fn x gt x 1
Value Selection via function application f 3
? 4 (fn x gt x 1) 3 ? 4
9
Sum of Types Enumerated Types
datatype Days Mo Tu We Th Fr Sa
Su
New Type
data / data constructors
Selecting a summand via pattern matching case d
of Sa gt Go to cinema Su gt Extra
Curriculum _ gt Life goes on
10
Combining Sum and Product of Types Algebraic
Data Types
Defining an integer binary tree datatype IntTree
Leaf int Node of (IntTree, int, IntTree)
fun height (Leaf x) 0 height
(Node(t1,n,t2)) 1 max(height(t1),height(t2))

11
Some remarks
  • A functional program consists of an expression,
    not a sequence of statements.
  • Higher-order functions are first-class citizen in
    the language.
  • It can be nameless
  • List processing is convenient and expressive
  • In ML, every expression must be well-typed.
  • Algebraic data types empowers the language.

12
Outline
  • More about Higher-order Function
  • Type inference and Polymorphism
  • Evaluation Strategies
  • Exception Handling

13
Function with Multiple Arguments
  • Curried functions accept multiple arguments
  • fun twice f x f (f x)

Take 2 arguments
Curried function enables partial application.
let val inc2 twice (fn x gt x x) in (inc2 1)
(inc2 2) end val it 12
Apply 1st argument
Apply 2nd argument
14
Curried vs. Uncurried
  • Curried functions
  • fun twice f x f (f x)
  • twice (fn x gt xx) 3 ? 12

Uncurried functions fun twice (f, x) f (f
x) twice (fn x gt xx, 3) ? 12
15
Curried Functions
  • Curried functions provide extra flexibility
  • to the language.

compose f g fn x gt f (g x) ? compose f g
x f (g x) ? compose f fn g gt fn x gt f
(g x) ? compose fn f gt fn g gt fn x gt f (g
x)
16
Types of Multi-Argument Funs
fun f(x,y) x y f intint -gt int
fun g x y x y g int -gt int -gt int
(g 3) int -gt int ((g 3) 4) int
Function application is left associative -gt is
right associative
17
Outline
  • More about Higher-order Function
  • Type inference and Polymorphism
  • Evaluation Strategies
  • Exception Handling

18
Type Inference
  • ML expressions seldom need type declaration.
  • ML cleverly infers types without much help from
  • the user.

2 2 val it 4 int fun succ n n 1
val succ fn int -gt int
19
Helping the Type Inference
  • Explicit types are needed when type coercion is
    needed.

fun add(x,y real) x y
fun add(x,y) (xreal) y
val add fn realreal -gt real
20
Every Expression has only One Type
fun f x if x gt 0 then x else 1,2,3 val f
fn Int -gt ???
This is not type-able in ML.
  • Conditional expression has the same type at
  • both branch.

fun abs(x) if xgt0 then x else 0-x val abs
fn int -gt int
21
Example of Type Inference
fun f g g (g 1)
type(g) t int?t2 t2?t3 int t2, t2
t3 type(g) t int ? int
type(f) t ? t1 t ? t3 (int ? int) ? int
22
Three Type Inference Rules
(Application rule) If f x t, then x t
and f t -gt t for some new type t.
(Equality rule) If both the types x t and
x t can be deduced for a variable
x, then t t.
(Function rule) If t ? u t ? u, then t
t and u u.
23
Example of Type Inference
fun f g g (g 1)
Let g tg (g (g 1)) trhs So, by function
declaration, we have f tg -gt trhs By
application rule, let (g 1) t(g 1) g (g 1)
trhs ? g t(g 1) -gt trhs. By application rule,
(g 1) t(g 1) ? g int -gt t(g 1). By equality
rule t(g 1) int trhs. By equality rule tg
int -gt int Hence, f (int -gt int) -gt int
24
Parametric Polymorphism
Type parameter
fun I x x val I fn a -gt a
  • A Polymorphic function is one whose type
    contains
  • type parameters.
  • A poymorphic function can be applied to
    arguments
  • of more than one type.

(I 3) (I 1,2) (I square)
  • Interpretation of val I fn a -gt a
  • for all type a, function I takes an input of
    type a
  • and returns a result of the same type a.

25
Polymorphic Functions
  • A polymorphic function is one whose type contains
    type parameter.
  • fun map f
  • map f (xxs) (f x) (map f xs)
  • Type of map (a-gtb) -gt a -gt b
  • map (fn x gt x1) 1,2,3 gt 2,3,4
  • map (fn x gt x) 1,2,3 gt 1,2,3
  • map (fn x gt x) y,n gt y, n

26
Examples of Polymorphic Functions
fun compose f g (fn x gt f (g x))
type(f) t1 t5?t6 type(g) t2
t4?t5 range(compose) t t4?t6 type(compose)
t1?t2?t (t5?t6) ? (t4?t5) ? (t4?t6)
27
Examples of Polymorphic Functions
fun compose f g (fn x gt f (g x))
Let xtx ftf gtg so compose tf -gt tg -gttrhs
(fn xgtf (g x))trhs gt trhs tx-gtt(f(gx)) (g
x)t(gx) gt g tx-gtt(gx) and tg tx-gtt(g x)
(f (g x))t(f(gx)) gt ft(gx)-gtt(f(gx))
and tf t(gx)-gtt(f(gx)) compose
(t(gx)-gtt(f(gx)))-gt(tx-gtt(gx))-gt(tx-gtt(f(gx))) Ren
ame the variables compose (a-gtb)-gt(c-gta)-gt(
c-gtb)
28
Outline
  • More about Higher-order Function
  • Type inference and Polymorphism
  • Evaluation Strategies
  • Exception Handling

29
Approaches to Expression Evaluation
  • Different approaches to evaluating an expression
    may change the expressiveness of a programming
    language.
  • Two basic approaches
  • Innermost (Strict) Evaluation Strategy
  • SML, Scheme
  • Outermost (Lazy) Evaluation Strategy
  • Haskell, Miranda, Lazy ML

30
Innermost Evaluation Strategy
  • To Evaluate the call ltnamegtltactualsgt
  • (1) Evaluate ltactualsgt
  • (2) Substitute the result of (1) for the formals
    in the body
  • (3) Evaluate the body of ltnamegt
  • (4) Return the result of (3) as the answer.

let fun f x x 1 x in f (2 3) end
31
fun f x x 2 x
f (23) gt f (5) gt 5 2 5 gt 12
fun g x y if (x lt 3) then y else x
g 3 (4/0) gt g 3 ? gt ?
  • Also referred to as call-by-value evaluation.
  • Occasionally, arguments are evaluated
    unnecessarily.

32
Outermost Evaluation Strategy
  • To Evaluate ltnamegtltactualsgt
  • (1) Substitute actuals for the formals in the
    body
  • (2) Evaluate the body
  • (3) Return the result of (2) as the answer.

fun f x x 2 x f (23) gt (23) 2
(23) gt 12 fun g x y if x lt 3 then y
else x g 3 (4/0) gt if 3 lt 3 then (4/0) else
3 gt 3
33
It is possible to eliminate redundant computation
in outermost evaluation strategy.
fun f x x 2 x f (23) gt x 2 x
gt 5 2 x gt 7 x gt
7 5 gt 12
x(23)
5
Note Arguments are evaluated only when they are
needed.
34
Why Use Outermost Strategy?
  • Closer to the meaning of mathematical functions

fun k x y x val const1 k 1 val const2 k
2
  • Better modeling of real mathematical objects

val naturalNos let fun inf n n inf
(n1) in inf 1 end
35
Hamming Number
  • List, in ascending order with no repetition, all
    positive integers with no prime factors other
    than 2, 3, or 5.
  • 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
    15,...

36
n as a prime factor
fun scale n scale n (xxs)
(nx) (scale n xs)
scale 2 1,2,3,4,5 2,4,6,8,10 scale 3
1,2,3,4,5 3,6,9,12,15
scale 3 (scale 2 1,2,3,4,5) 6,12,18,24,30
37
Merging two Streams
fun merge merge (xxs)
(yys) if x lt y then x merge xs
(yys) else if x gt y then y merge (xxs)
ys else x merge xs ys
merge 2,4,6 3,6,9 2,3,4,6,9
38
Hamming numbers
val hamming 1 merge (scale 2 hamming)
(merge (scale 3 hamming) (scale 5
hamming))
39
Outline
  • More about Higher-order Function
  • Type inference and Polymorphism
  • Evaluation Strategies
  • Exception Handling

40
Exception Handling
  • Handle special cases or failure (the
    exceptions)
  • occurred during program execution.
  • hd
  • uncaught exception hd
  • Exception can be raised and handled in the
    program.
  • exception Nomatch
  • exception Nomatch exn

fun member(a,x) if null(x) then raise
Nomatch else if a hd(x) then x else
member(a,tl(x))
41
fun member(a,x) if null(x) then raise
Nomatch else if a hd(x) then x else
member(a,tl(x))
member(3,1,2,3,1,2,3) val it 3,1,2,3
int list member(4,) uncaught exception
Nomatch member(5,1,2,3) handle Nomatchgt
val it int list
42
Conclusion
  • More about Higher-order Function
  • Curried vs Uncurried functions
  • Full vs Partial Application
  • Type inference and Polymorphism
  • Basic Type inference rules
  • Polymorphic functions
  • Evaluation Strategies
  • Innermost
  • Outermost
  • Exception Handling is available in ML
Write a Comment
User Comments (0)
About PowerShow.com