Functional Programming. Another representative from the Declarative paradigm

Similar documents
CPS 506 Comparative Programming Languages. Programming Language Paradigm

Chapter 15. Functional Programming Languages

Example Scheme Function: equal

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Functional Programming. Big Picture. Design of Programming Languages

Concepts of Programming Languages

CS 11 Haskell track: lecture 1

Imperative languages

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

15. Functional Programming

Haskell 98 in short! CPSC 449 Principles of Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Functional Programming Languages (FPL)

Programming Languages Third Edition

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Programming Language Pragmatics

Chapter 11 :: Functional Languages

Com S 541. Programming Languages I

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Lists. Michael P. Fourman. February 2, 2010

Handout 9: Imperative Programs and State

A Small Interpreted Language

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Data Types The ML Type System

Chapter 15. Functional Programming Languages

Lecture Notes on Induction and Recursion

Logic - CM0845 Introduction to Haskell

Introduction to Functional Programming

Functional Languages. Hwansoo Han

Some Advanced ML Features

Chapter 15. Functional Programming Languages ISBN

Homework 3 COSE212, Fall 2018

Thoughts on Assignment 4 Haskell: Flow of Control

3. Functional Programming. Oscar Nierstrasz

Functional Programming

Functional Programming

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

Programming Paradigms

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Functional Programming and Haskell

A Brief Introduction to Standard ML

Software Paradigms (Lesson 4) Functional Programming Paradigm

CS558 Programming Languages

CS 320: Concepts of Programming Languages

Lecture 3: Recursion; Structural Induction

MIDTERM EXAM (Solutions)

F28PL1 Programming Languages. Lecture 11: Standard ML 1

FreePascal changes: user documentation

4/19/2018. Chapter 11 :: Functional Languages

Lecture 1: Overview

CSE 3302 Programming Languages Lecture 8: Functional Programming

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Type Checking and Type Inference

SML A F unctional Functional Language Language Lecture 19

Combining Programming with Theorem Proving

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

CSE 12 Abstract Syntax Trees

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

CMSC 330: Organization of Programming Languages

Tree Equality: The Problem

CS 3410 Ch 7 Recursion

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Advanced features of Functional Programming (Haskell)

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Programming with Math and Logic

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Combining Static and Dynamic Contract Checking for Curry

(Refer Slide Time: 4:00)

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

The type checker will complain that the two branches have different types, one is string and the other is int

CSC 501 Semantics of Programming Languages

Logic Programming II & Revision

Programming Paradigms

(Refer Slide Time: 01:01)

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

7. Introduction to Denotational Semantics. Oscar Nierstrasz

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Harvard-MIT Division of Health Sciences and Technology HST.952: Computing for Biomedical Scientists HST 952. Computing for Biomedical Scientists

Part (04) Introduction to Programming

Functional Programming. Functional Programming

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

Polymorphism and Type Inference

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

Chapter 2 The Language PCF

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter 11 p. 1/38

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Introduction to SML Getting Started

Chapter 3. Describing Syntax and Semantics

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

Mini-ML. CS 502 Lecture 2 8/28/08

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

6.001 Notes: Section 4.1

Functional Programming

LECTURE 16. Functional Programming

CS457/557 Functional Languages

Transcription:

Functional Programming Another representative from the Declarative paradigm 1

Variables in Imperative Languages A variable in an imperative programming language can be regarded as an abstraction of the von Neumann computer store location. Assignment is the means by which values are changed. This influences the whole paradigm. In particular: 1. Issues around assignment. 2. Control of execution of sequences of instructions. 2

Assignment There are problems with assignment. These relate to Side-effects (covered earlier) Aliasing (covered earlier) Referential transparency 3

Referential Transparency Referential transparency means The meaning (or the effect, or the value) of an expression is the same wherever (and whenever) it occurs. Imperative languages do not support referential transparency. Trivial example: X := 0; Write(X); X := X + 1; Write(X); This problem becomes even worse when parallel execution is considered because the order of execution matters. 4

Sequences of Instructions Imperative programs are sequences of instructions because to be executed they must be stored in memory in a sequence of locations, and because of the way the standard fetch/execute cycle works. The control of such execution, involving loops of different kinds, is a common source of bugs in imperative programs. 5

Summary Imperative languages are based on standard computer architecture. The advantage of this is run-time efficiency. BUT 1. It facilitates errors. 2. It makes bug-tracing difficult. 3. It makes programs hard to read and understand. 4. (Backus, 1978) It limits the programmer's thinking and inhibits the problem-solving process. 6

The Functional Approach Here are two examples of function definitions: Double(n) = n+n; Square(n) = n*n; A `functional program' is simply a collection of function definitions. A functional programming system allows us to store such function definitions, and then ask for specific expressions to be evaluated. For example, if we had stored the definitions above, we could give Double(7); to the system, and it would respond by giving the value of the expression, namely 14. 7

Functional Programming Note that a functional language is inherently declarative. Such function definitions are simply statements of what our functions are. There is an analogy with Prolog. But remember that in Prolog we dealt with predicates, not functions, and we achieve goals rather than evaluate expressions. 8

The Functional Approach A program consists of a collection of function definitions, and a `run' amounts to an evaluation of an expression involving these functions. The intention is to focus on the computations which are to be done, not on how to fit them to the structure of the machine. Assignment is not a part of this paradigm, nor is the idea of a sequence of instructions. Under this paradigm: 1. Side-effects can be completely avoided. 2. Aliasing is not possible. 3. Referential transparency can be supported. Examples of functional languages: LISP, ML, Miranda, Haskell, Erlang 9

FP in the Real World from homepages.inf.ed.ac.uk/wadler/realworld/ Industrial Erlang, an e-commerce database, Partial evaluator for airline scheduling, Combinators for financial derivatives Compilers, Interpreters and Partial Evaluators, Syntax and Analysis Tools Theorem Provers and Reasoning Assistants Network Toolkits and Applications, Web, HTML, XML, XSLT Natural Language Processing and Speech Recognition Numerically Based Applications, Computer Algebra MC-SYM - computes 3D shape of nucleic acid, FFTW - Fastest Fourier Transform in the West, BlurFit - model focal plane imaging Database Systems Operating Systems Light and sound Lula: a system for theater lighting, Scheme Score 10

1. Programs A program consists of a collection of function definitions: For Example fun Double(n) = n+n; fun Square(n) = n*n; fun Avg(x,y) = (x+y)/2; fun SqAvg(x,y) = Avg(Square(x),Square(y)); The the following can be evaluated: Square(Double(6)); SqAvg(5,7); In each case the system would respond with the value (respectively 144 and 37). 11

2. Data Structures Lists are the main basic data structure. Notations (ML): [2,6,4,5] h :: t nil hd(s) tl(s) Example function involving lists (more later): fun Length(nil) = 0 Length(h :: t) = 1 + Length(t); 12

3. Program Control We have composition of functions: apply one function to some value(s), and then apply another function to the result. (Like SqAvg above). There is no notion of program loop, so recursion is essential (see the Length function above). Functional programming also has an `if' construct, to distinguish cases. Example: fun Max(x,y) = if x >= y then x else y; fun IsEmpty(l) = if l = nil then true else false; 13

4. Pattern-Matching Two equivalent definitions: fun IsEmpty(l) = if l = nil then true else false; fun IsEmpty(nil) = true IsEmpty(h :: t) = false; When we ask to evaluate (say) IsEmpty([4,1,7]), a matching process takes place, and as a result the second line of the definition is used. (See also the Length function above.) 14

More Pattern Matching fun IsMember(x,l) = if l = nil then false else if x = hd(l) then true is equivalent to else IsMember(x,tl(l)); fun IsMember(x,nil) = false IsMember(x,x :: t) = true IsMember(x,h :: t) = IsMember(x,t); The word pattern here refers to the formal structure of an expression (nil and h :: t are patterns). Patternmatching is an essential part of functional languages. (As it is also with Prolog.) 15

Further Pattern Matching Examples fun AddUpTo(0) = 0 AddUpTo(n) = n + AddUpTo(n - 1); fun Factorial(0) = 1 Factorial(n) = n * Factorial(n - 1); fun ListAsFarAs(x,nil) = nil ListAsFarAs(x,x :: t) = [x] ListAsFarAs(x,h :: t) = h :: ListAsFarAs(x,t); fun DoubleList(nil) = nil DoubleList(h :: t) = Double(h) :: DoubleList(t); fun SumList([x]) = x SumList(h :: t) = h + SumList(t); fun ListAvg(l) = SumList(l)/Length(l); 16

5. Evaluation A functional programming system simply allows evaluation of expressions. An actual evaluation is done by a process called reduction or rewriting. Here is how it works. Say we wish to evaluate ListAvg([3,7,2]). ListAvg([3,7,2]) SumList([3,7,2]) / Length([3,7,2]) (3 + SumList([7,2])) / (1 + Length([7,2])) (3 + (7 + SumList([2]))) / (1 + (1 + Length([2]))) (3 + (7 + 2)) / (1 + (1 + (1 + Length(nil)))) (3 + 9) / (1 + (1 + (1 + 0))) 12 / (1 + (1 + 1)) 12 / (1 + 2) 12 / 3 4 17

6. Higher-order Functions In functional languages there is the possibility to define functions which have functions as parameters, or which return functions as results. This is what is meant by higher-order functions. Avoids repetition of code. E.g. We have a function to traverse a list and add all the elements up. We also have a function to traverse a list and multiply all the elements together. Why not have a function to traverse a list and apply some action, and make the action (function) a parameter? This is a natural feature of functional languages. Functions can have functions as parameters. 18

6. Higher-Order Functions (continued) Example fun DoubleList(nil) = nil DoubleList(h :: t) = Double(h) :: DoubleList(t); fun SquareList(nil) = nil SquareList(h :: t) = Square(h) :: SquareList(t); Similarities can be drawn out: fun ListApply(f,nil) = nil ListApply(f,h :: t) = f(h) :: ListApply(f,t); Examples become: ListApply(Double,[3,1,4]) ListApply(Square,[3,1,4]) 19

(The simplistic story) 7. Types Static Types: All variables and parameters must have their types declared in the program, so that they can be checked by the compiler. Dynamic Types: The types of program entities are not constrained at all in advance of run-time information. 20

Best of Both? Security of compile-time type checking Flexibility of dynamic typing / freedom from type declarations Make the system infer types itself! 21

8. Strong Typing and Type Inference Strong typing: all expressions have a well defined type. Most functional languages other than LISP have strong typing. They have a type-checking system which infers types for all objects for which a type is not specified, and checks for inconsistencies. It seems to give the best of both worlds: security against errors, provided by type-checking, freedom from the necessity to specify types. 22

Type Inference Example: fun Sum1To(n) = n*(n+1) div 2; n must be such that +1 and * and div are appropriate operations Sum1To must return a corresponding numerical result The function Sum1To thus must have type int -> int 23

Type Inference (continued) Here is another example: fun AddToList(a,nil) = nil AddToList(a,h :: t) = if (h < 0) then (a + h) :: AddToList(a,t) else h :: AddToList(a,t); The type of AddToList is int * (int list) -> (int list) 24

Type Inference (continued) Another example fun IsEmpty(l) = if l=nil then true else false; It is clear that this function acts on a list and returns a Boolean result. (why?) What kind of list?...any kind of list. It is polymorphic... 25

9. Polymorphism fun Length(nil) = 0 Length(h :: t) = 1 + Length(t); The type of h may be anything. We could use this function to find the length of a list of numbers, a list of Booleans, a list of strings, a list of lists,... And the value returned will be an integer (because?). The type of Length would be inferred as ('a list) -> int ML uses the notation 'a to represent types which are unconstrained in this way. The type of IsEmpty above is ('a list) -> bool 26

10. Lazy Evaluation Simply, this means that parameters are not evaluated until they are required. The opposite of lazy is strict. Of functional languages, LISP and ML are strict, whereas Miranda and Haskell are lazy. Laziness has an efficiency advantage: parameter values which are not in fact needed will not be evaluated. 27

Lazy Evaluation But laziness also brings other possibilities: fun CountFrom(n) = n :: CountFrom(n+1) An infinite list? Consider SumList(ListAsFarAs(10,CountFrom(1))) What is used in the evaluation? 28

11. Abstract Data Types The only means of building data structures so far has been the list. Most functional programming languages include a facility for the user to build tailor-made data types, specific to current needs, using abstract data types. Example (ML): datatype 'a tree = empty node of ('a * ('a tree) * ('a tree)); This is a definition of a binary tree type. Here, 'a is a type variable (and the * separates types). 29

Abstract Data Types (continued) Values of the type (int tree) would be expressions such as empty node(4,empty,empty) node(4,empty,node(6,empty,empty)) A binary tree either is empty, or consists of a root (at which is stored a data item) and two subtrees. That is exactly what we are constructing here. Note that the only mechanism used to build this structure is the (formal) application of functions. 30

Abstract Data Types (continued) Example (ML): datatype 'a stack = new_stack push of ('a * ('a stack)); Values are expressions like new_stack push(3,new_stack) push(7,push(3,new_stack)) Standard operations include: exception top_err: fun top(new_stack) = raise top_err; top(push(a,b)) = a; (Note the mechanism for dealing with error situations.) 31

Summary Here are the aspects of functional languages that we have considered: 1. Composition of functions. 2. Data structures (lists). 3. Distinguishing cases using if. 4. Pattern matching. 5. Evaluation by reduction/rewriting. 6. Static/dynamic typing. 7. Higher-order functions. 8. Strong typing and type inference. 9. Polymorphism. 10. Lazy evaluation. 11. Data structures (abstract data types). And finally... 32

Declarative Programming Both functional languages and logic languages are said to be declarative. Declarative programs consist of assertions and definitions, not instructions or commands. A consequence of this is the meaning of a declarative program should be independent of whatever run-time system is used to execute it. 33

Declarative Programming Specifically: 1. A functional program has meaning in a mathematical sense. The forms and symbols used have precise mathematical meanings. 2. A logic program has meaning in terms of the logical interpretation of the symbols. (Although in the case of Prolog this is somewhat compromised.) There are perhaps two significant benefits which derive from this: 1. Mathematically precise meanings facilitate reasoning about programs (for example in relation to correctness). 2. The independence from the machine makes such languages closer to specification languages, and so they lessen the gap (which is a cause of bugs) between specification and implementation. 34