Functional Programming Element of Functional Programming.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Modern Programming Languages, 2nd ed.
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Principles of programming languages 1: Introduction (with a simple language) Isao Sasano Department of Information Science and Engineering.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Foundations of Programming Languages: Introduction to Lambda Calculus
CS 312 Spring 2004 Lecture 18 Environment Model. Substitution Model Represents computation as doing substitutions for bound variables at reduction of.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CS 312 Spring 2002 Lecture 16 The Environment Model.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Introduction and Chapter 8 of Programming Languages by Ravi Sethi Elements of Functional Programming.
Chapter 15: Functional Programming Languages
ISBN Chapter 15 Functional Programming Languages.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
1 Programming Languages and Paradigms Functional Programming.
Chapter 9: Functional Programming in a Typed Language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CSE 341 : Programming Languages Lecture 2 Functions, Pairs, Lists Zach Tatlock Spring 2014.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Procedure Activations Programming Language. Exploration Name ocurrenceDeclarationLocationValue scopeactivationstate a.From names to their declarations.
Little Quilt Programming Language. 2 A Little language of expressions Little Quilt The little language ----Little Quilt: quilts: Constructs in Little.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Functional Programming
Functional Programming
Functional Programming Languages
Functional Programming
Principles of programming languages 12: Functional programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
ML: a quasi-functional language with strong typing
Information Science and Engineering
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Principles of programming languages 4: Parameter passing, Scope rules
A lightening tour in 45 minutes
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
The Metacircular Evaluator
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSCE 314: Programming Languages Dr. Dylan Shell
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
What is a programming language?
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Presentation transcript:

Functional Programming Element of Functional Programming

Functional Programming  Functional Programming began as computing with expressions.  The following are examples: 2 An integer constant X A variable log n Function log applied to n 2+3 Function + applied to 2 and 3

Functional Programming  Expressions can also include conditionals and function definitions. Example: The value of following condition expression is the maximum of x and y: if (x > y) then x else y

Functional Programming  For concreteness, specific ML examples will be written in a typewriter-like font;  For example:  Computing with expressions will be introduced by designing a little language of expressions ; val it = 4 : int

A LITTLE LANGUAGE OF EXPRESSIONS  Basic Values  Constants: Names for Basic Values  Operations  Expressions  Convenient Extensions  Local Declarations

A LITTLE LANGUAGE OF EXPRESSIONS  Expressions are formed by giving names to values and to operations on them  The little language manipulates just one type of value: geometric objects called quilts.

Example : Quilt What is Quilt?

Little Quilt  Little Quilt manipulates geometric objects with  Height  Width  Texture

Basic Values  Given two primitive objects in the language are the square pieces:  Each quilt has A fixed direction or orientation A height A width A texture

Operations  Quilt can be turned and can be sewn together turn sew

Rules 1.A quilt is one of the primitive pieces, or 2.It is formed by turning a guilt clockwise 90, or 3.It is formed by sewing a quilt to the right of another quilt of equal height 4.Nothing else is a quilt Quilts and the operations on them are specified by the following rules:

Constants: Names for Basic Values  The first step in construction a language to specify quilts is to give names to the primitive pieces and to the operations on quilts Name “a” Name “b” Operation called turn sew

Expressions  The syntax of expressions mirrors the definition of quilts:  Complex expressions are built up from simpler ones, ::= a | b | turn( ) | sew(, )

Expressions Sew(turn(turn(b)),a) No expression quilt 1 b 2 turn(b) 3 turn(turn(b)) 4 a 5 sew(turn(turn(b)),a)

Convenient Extensions  Expressions will now be extended by allowing functions from quilts to quilts.  It would be convenient to give names to the operations.  Once defined, functions like unturn and pile can used as if they were built in Fun unturn(x) = turn(turn(turn(x))) Fun pile(x,y) = unturn(sew(turn(y),turn(x)))

Local Declarations  Let-expressions or let-bindings allow declarations to appear within expressions.  Let-expression Form Let in end For example let fun unturn(x) = turn(turn(turn(x))) fun pile(x,y) = unturn(sew(turn(y),turn(x))) in pile(unturn(b),turn(b)) end

User-Defined Names for Values  The final extension is convenient for writing large expressions in terms of simpler ones.  A value declaration form:  Gives a name to a value val =

 Value declarations are used together with let- bindings.  An expression of the form let val x=E1 in E2 end let val bnw = unturn(b) in pile(bnw,turn(b)) end Rewrite Pile(unturn(b),turn(b))

Review: Design of Little Quilt  The language Little Quilt was defined by starting with values and operations.  The values are quilts, built up from two square pieces;  The operations are for turning and sewing quilts.  The language began with the name a and b for the square pieces and  The names turn and sew for the operations

Specification of a quilt Let fun unturn(x) = turn(turn(turn(x))) fun pile(x,y) = unturn(sew(turn(y),turn(x))) val aa = pile(a,turn(turn(a))) val bb = pile(unturn(b),turn(b)) val p = sew(bb,aa) val q = sew(aa,bb) In pile(p,q) end

Summary of Little Quilt ::= a | b ::= turn( ) | sew(, ) ::= let in end ::= | ::= fun ( ) = ::= |, ::= ( ) ::= |, ::= val = ::=

TYPEs:Values and Operations  A types consists of a set of elements called values together with a set of functions called operations.  We will consider methods for defining structured values such as products, lists, and functions.

The syntax of type expressions ::= |  | * | list

Structured value  Structured values such as lists can be used as freely in functional languages as basic values like integers and strings  Value in a function language take advantage of the underlying machine, but are not tied to it.

Operations for Constructing and Inspecting Values  The structuring methods will be presented by specifying a set of values together with a set of operations.  Concentrate on operations for constructing and inspecting the elements of the set.  For example To Extend a list by adding a new first element To test whether a list is empty

Operations for Constructing and Inspecting Values  Basic Types  Operations on Basic Values  Products of Types  Operations on Pairs  List of Elements  Operations on Lists

Basic Types  A type is basic if its values are atomic  If the values are treated as whole elements, with no internal structure. For example The boolean values in the set {true,false}

Operations on Basic Values  Basic values have no internal structure, so the only operation defined for all basic types is a comparison for equality; For example The equality 2 = 2 is true, The inequality 2!=2 is false

Operations on Basic Values  Technically, an operation is a function  The equality test on integers is a function from pairs of integers to boolean. The type int*int  bool

Products of Types  The product A*B of two types A and B consists of ordered pairs written as For Example (1,“one”) is a pair consisting of int:1 and string: “one” (a,b) ; where a is a value of type A and b is a value of type B

Products of Types  A product of n types A*A*A*…*A consists of tuples written as (a 1, a 2, a 3,…, a n ) ; where a i is a value of type A 1

Operations on Pairs  A pair is constructed from a and b by writing (a,b)  Associated with pairs are operations called projection functions to extract the first and second elements from a pair. The first element of the pair (a,b) is a The second element is b  Projection functions can be readily defined: fun first(x,y) = x fun second(x,y) = y

Lists of Elements  A list is a finite-length sequence of elements. For Example int list ; consists of all lists of integers. The type A list consists of all lists of elements, where each element belongs to type A

Lists of Elements  List elements will be written between brackets [ and ]  List elements will be separated by commas  The empty list is written equivalently as [ ] For Example [1, 2, 3] is a list of three integers [“red”, “white”, “blue”] is a list of three strings

Operations on Lists  Lists-manipulation programs must be prepared to construct and inspect lists of any length.  The following operations on list are from ML: null(x)True is x is the empty list hd(x)The first or head element of list x. tl(x)The tail or rest of the list. a::xConstruct a list with head a and tail x

Operations on Lists For Example Given x is [1, 2, 3] Null(x)false Hd(x)1 Tl(x)[2, 3] From the following equalities: [1, 2, 3] = 1::[2, 3] = 1::2::[3] = 1::2::3::[]

Functions from a Domain to a Range  The type A  B is the set of all functions from A to B For Example If Q is the set of quilt then function turn from quilts to quilts is Q  Q function sew form pairs of quits to quilts is Q*Q  Q

Functions from a Domain to a Range  A function f in A  B is total  A function f in A  B is partial If it associates an element of B with each element of A A is called the domain and B is called the range of f. It is possible for there to be no element of B associated with an element of A

Function Application  The set A  B is application which takes a function f in A  B and an element a in A and yields and element b of B. For Example  A function is applied in ML; f a is the application of f to a.

APPOACHES TO EXPRESSION EVALUATION  The rules for expression are base on the structure of expressions E1 + E2 1.Evaluate the subexpressions E1 and 2.Evaluate the subexpressions E2 and 3.Add two values

Innermost Evaluation  Evaluate the expression represented by  Substitute the result for the formal in the function body  Evaluate the body  Return its value as the answer

Selection Evaluate  is an expression that evaluates to either true or false.  True : is evaluated  False : is evaluated If then else

Evaluation of Recursive Functions  The actual parameters are evaluated and substituted into the function body. Length(X) ((cond (null X) 0 (+ 1 (length(cdrX))))) Length([“hello”, “world”]) = 1 + length([“world”]) = length([]) = = 2

Othermost Evaluation  Substitute the actual for the formal in the function body  Evaluate the body  Return its value as the answer.

Example Function Fun f(x) = if x > 100 then x-10 else f(f(x+11))

Innermost Evaluate F(100) = if 100>100 then else f(f(100+11)) = f(f(100+11)) = f(f(111)) = f( if 111> 100 then else f(f(111+11)) ) = f(111-10) = f(101) = if 101>100 then else f(f(101+11)) = 101 – 10 = 91

Outermost Evaluate F(100) = if 100>100 then else f(f(100+11)) = f(f(100+11)) = if f(111> 100) then f(111+11)-10 else f(f(f(100+11)+11)) ) F(100+11) = if > 100 then else f(f( )) = if 111>100 then else f(f( )) = = = 101

LEXICAL SCOPE  Renaming is made precide by introducing a notion of local or “bound” variables;bound occurrences of variables can be renamed without changing the meaning of a program fun successor(x) = x + 1 fun successor(n) = n + 1

Val Bindings  Binding occurrence or simply binding of x  All occurrences of x in E2 are said to be within the scope of this binding  Occurrences of x in E1 are not in the scope of this binding of x let val x=E1 in E2 end let val x=2 in x+x end

Fun Bindings  This binding of the formal parameter x is visible only to the occurrences of x in E1  This binding of the function name f is visible to all occurrences of f in both E1 and E2 let fun f(x)=E1 in E2 end let fun f(x)=x+1 in 2*f(x) end