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

Similar documents
Functional Programming. Big Picture. Design of Programming Languages

15. Functional Programming

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Chapter 6 Control Flow. June 9, 2015

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CS 242. Fundamentals. Reading: See last slide

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

Imperative languages

Functional Programming Languages (FPL)

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

Programming Language Pragmatics

Functional Languages. Hwansoo Han

Concepts of Programming Languages

Com S 541. Programming Languages I

Chapter 11 :: Functional Languages

CS 11 Haskell track: lecture 1

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

CS 314 Principles of Programming Languages

Chapter 15. Functional Programming Languages

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

Concepts of Programming Languages

Functional Programming

Topic 1: Introduction

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

LECTURE 18. Control Flow

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

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

Programming Languages 2nd edition Tucker and Noonan"

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Logic Programming II & Revision

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Principles of Programming Languages. Lecture Outline

Programming Paradigms

More Lambda Calculus and Intro to Type Systems

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

Functional Programming. Another representative from the Declarative paradigm

3. Functional Programming. Oscar Nierstrasz

THE CONCEPTION AND APPLICATION OF PFL : A PROCESS FUNCTIONAL PROGRAMMING LANGUAGE

Introduction. A. Bellaachia Page: 1

Introduction to Functional Programming in Haskell 1 / 56

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

Introduction. chapter Functions

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University

Programming Systems in Artificial Intelligence Functional Programming

Continuations provide a novel way to suspend and reexecute

Chapter 1. Preliminaries

Programming with Math and Logic

CSc 372 Comparative Programming Languages

Functional Programming Lecture 1: Introduction

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

An introduction introduction to functional functional programming programming using usin Haskell

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

CSc 520. Principles of Programming Languages 11: Haskell Basics

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

CSc 372 Comparative Programming Languages

LECTURE 16. Functional Programming

CSc 372. Comparative Programming Languages. 1 : Introduction. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

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

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Presented By Andrew Butt

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Functional Programming and Haskell

Fall Lecture 3 September 4. Stephen Brookes

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

A general introduction to Functional Programming using Haskell

G Programming Languages - Fall 2012

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

CSc 372. Comparative Programming Languages. 13 : Haskell Lazy Evaluation. Department of Computer Science University of Arizona

Lecture 15: Iteration and Recursion

Example Scheme Function: equal

Functional programming in C#

CS558 Programming Languages

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

Functional Programming Language Haskell

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

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

Functional Programming

More Lambda Calculus and Intro to Type Systems

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Functional Programming

6.001 Notes: Section 4.1

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence

CIS 194: Homework 4. Due Wednesday, February 18, What is a Number?

CS457/557 Functional Languages

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

G Programming Languages - Fall 2012

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

So what does studying PL buy me?

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

Control Flow February 9, Lecture 7

Pace University. Fundamental Concepts of CS121 1

Transcription:

1/37 CSc 372 Comparative Programming Languages 2 : Functional Programming Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg

2/37 Programming Paradigms During the next few weeks we are going to work with functional programming. Before I can explain to you what FP is, I thought I d better put things into perspective by talking about other programming paradigms. Over the last 40 or so years, a number of programming paradigms (a programming paradigm is a way to think about programs and programming) have emerged.

3/37 Programming Paradigms... A programming paradigm is a way to think about programs, programming, and problem solving, is supported by one or more programming languages. Being familiar with several paradigms makes you a better programmer and problem solver. The most popular paradigms: 1 Imperative programming. 2 Functional programming. 3 Object-oriented programming. 4 Logic Programming. When all you have is a hammer, everything looks like a nail.

4/37 Programming Paradigms... Programming with state. Imperative Programming Also known as procedural programming. The first to emerge in the 1940s-50s. Still the way most people learn how to program. FORTRAN, Pascal, C, BASIC. Programming with values. Functional Programming Arrived in the late 50s with the LISP language. LISP is still popular and widely used by AI people. LISP, Miranda, Haskell, Gofer.

5/37 Programming Paradigms... Object-Oriented Programming Programming with objects that encapsulate data and operations. A variant of imperative programming first introduced with the Norwegian language Simula in the mid 60s. Simula, Eiffel, Modula-3, C++. Logic Programming Programming with relations. Introduced in the early 70s. Based on predicate calculus. Prolog is popular with Computational Linguists. Prolog, Parlog.

6/37 Procedural Programming We program an abstraction of the Von Neumann Machine, consisting of a store (memory), a program (kept inthestore), A CPU anda program counter (PC): Computing x:=x+1 Addresses X: CPU Program: PC := PC + 8; X := X + 1 Data 53 Jump Store PC Update store 1 Compute x s address, send it to the store, get x s value back. 2 Add 1 to x s value. 3 Send x s address and new value to the store for storage. 4 Increment PC.

7/37 Procedural Programming... The programmer... uses control structures (IF, WHILE,...) to alter the program counter (PC), uses assignment statements to alter the store. is in charge of memory management, i.e. declaring variables to hold values during the computation. function fact (n: integer ): integer ; var s, i : integer := 1; begin while i<=n do s:=s i ; i:=i +1; end; return s ; end fact.

8/37 Procedural Programming... Procedural programming is difficult because: 1 A procedural program can be in a large number of states. (Any combination of variable values and PC locations constitutes a possible state.) The programmer has to keep track of all of them. 2 Any global variable can be changed from any location in the program. (This is particularly true of languages like C & C++ [Why?]). 3 It is difficult to reason mathematically about a procedural program.

Functional Programming

10/37 Functional Programming In contrast to procedural languages, functional programs don t concern themselves with state and memory locations. Instead, they work exclusively with values, and expressions and functions which compute values. Functional programming is not tied to the von Neumann machine. It is not necessary to know anything about the underlying hardware when writing a functional program, the way you do when writing an imperative program. Functional programs are more declarative than procedural ones; i.e. they describe what is to be computed rather than how it should be computed.

11/37 Functional Languages Common characteristics of functional programming languages: 1 Simple and concise syntax and semantics. 2 Repetition is expressed as recursion rather than iteration. 3 Functions are first class objects. I.e. functions can be manipulated just as easily as integers, floats, etc. in other languages. 4 Data as functions. I.e. we can build a function on the fly and then execute it. (Some languages).

12/37 Functional Languages... 5 Higher-order functions. I.e. functions can take functions as arguments and return functions as results. 6 Lazy evaluation. Expressions are evaluated only when needed. This allows us to build infinite data structures, where only the parts we need are actually constructed. (Some languages). 7 Garbage Collection. Dynamic memory that is no longer needed is automatically reclaimed by the system. GC is also available in some imperative languages (Modula-3, Eiffel) but not in others (C, C++, Pascal).

13/37 Functional Languages... 8 Polymorphic types. Functions can work on data of different types. (Some languages). 9 Functional programs can be more easily manipulated mathematically than procedural programs. Pure vs. Impure FPL Some functional languages are pure, i.e. they contain no imperative features at all. Examples: Haskell, Miranda, Gofer. Impure languages may have assignment-statements, goto:s, while-loops, etc. Examples: LISP, ML, Scheme.

Specifying Functions

15/37 What is a function? A function maps argument values (inputs) to result values (outputs). A function takes argument values from a source set (or domain). A function produces result values that lie in a target set (or range). Source (Domain) Sweden USA NZ Function Capital Target (Range) Washington DC Stockholm Wellington Copenhagen

16/37 More on functions A function must not map an input value to more than one output value. Example: True Jenny IsPretty False not a function!

17/37 More on functions... If a function F maps every element in the domain to some element in the range, then F is total. I.e. a total function is defined for all arguments. Int Int Plus Int

18/37 More on functions... A function that is undefined for some inputs, is called partial. Int Divide Int Int Divide is partial since? 0 =? is undefined.

19/37 Specifying functions A function can be specified extensionally or intentionally. Extensionally: Enumerate the elements of the (often infinite) set of pairs (argument, result) or Argument Result. The extensional view emphasizes the external behavior (or specification), i.e. what the function does, rather than how it does it. double = {, (1,2), (5,10), } even = {, (0,True), (1,False), } double = {, 1 2, 5 10, } ishandsome={chris True,Hugh False}

20/37 Specifying functions... Intensionally: Give a rule (i.e. algorithm) that computes the result from the arguments. The intentional view emphasizes the process (or algorithm) that is used to compute the result from the arguments. double x = 2 * x even x = x mod 2 == 0 ishandsome x = if isbald x then True else False

21/37 Specifying functions... Graphically: The graphical view is a notational variant of the intentional view. even even or eithereven

22/37 Function Application The most important operation in a functional program is function application, i.e. applying a function to its argument(s), and retrieving the result: double x = 2 * x even x = x mod 2 == 0 double 5 10 even 6 True

23/37 Function Composition Function composition makes the result of one function application the input to another application: double x = 2 * x even x = x mod 2 == 0 even (double 5) even 10 True

24/37 Function Definition Example Example: How many numbers are there between m and n, inclusive? Extensional Definition: sumbetween m n = { (1,1) 1,(1,2) 2,,(2,10) 9} Intentional Definition: sumbetween m n = ((m + n) * (abs (m-n) + 1)) div 2 m + Graphical Definition: * n abs 1 + 2 div sumbetween

Function Signatures To define a function we must specify the types of the input and output sets (domain and range, i.e. the function s signature), and an algorithm that maps inputs to outputs. int double int The Signature! * 2 int even Bool 2 mod 0 == 25/37

What s so Good About FP?

27/37 Referential Transparency The most important concept of functional programming is referential transparency. Consider the expression (2 3)+5 (2 3) (2 3) occurs twice in the expression, but it has the same meaning (6) both times. RT means that the value of a particular expression (or sub-expression) is always the same, regardless of where it occurs. This concept occurs naturally in mathematics, but is broken by imperative programming languages. RT makes functional programs easier to reason about mathematically.

28/37 Referential Transparency... Consider this Java expression: f () + f () Could we replace it by the expression 2 f () If this was mathematics, we could! But, in Java...

29/37 Referential Transparency... If our definition of f() was int f () { return 5; } then f()+f() and 2*f() both mean the same. But, if f() is int X=5; int f () { X++; return X; } then f()+f()=6+7=13 and 2*f()=2*6=12!

30/37 Referential Transparency... What about these two Java expression: f () + g() and g() + f () Are they equivalent? In math they are...

31/37 Referential Transparency... But, Java isn t math: int X=5; int f () { X++; return X; } int g() { return X; } then f()+g()=6+6=12 and g()+f()=5+6=11!

32/37 Referential Transparency... Because of such side-effects, Java isn t referentially transparent. The same is true of any procedural language (Pascal, C, Modula-2, etc) and object-oriented language (Java, C++, C#).

33/37 Referential Transparency... Pure functional programming languages are referentially transparent. This means that it is easy to find the meaning (value) of an expression. We can evaluate it by substitution. I.e. we can replace a function application by the function definition itself.

34/37 Referential Transparency... Evaluate even (double 5) : double x = 2 * x even x = x mod 2 == 0 even (double 5) even (2 * 5) even 10 10 mod 2 == 0 0 == 0 True

35/37 Referential Transparency... In a pure functional language 1 Expressions and sub-expressions always have the same value, regardless of the environment in which they re evaluated. 2 The order in which sub-expressions are evaluated doesn t effect the final result. 3 Functions have no side-effects. 4 There are no global variables.

36/37 Referential Transparency... 5 Variables are similar to variables in mathematics: they hold a value, but they can t be updated. 6 Variables aren t (updatable) containers the way they are imperative languages. 7 Hence, functional languages are much more like mathematics than imperative languages. Functional programs can be treated as mathematical text, and manipulated using common algebraic laws.

37/37 Exercise Here is a mathematical definition of the combinatorial function ( n r) n choose r, which computes the number of ways to pick r objects from n: ( n r ) = n! r! (n r)! Give an extensional, intentional, and graphical definition of the combinatorial function, using the notations suggested in this lecture. You may want to start by defining an auxiliary function to compute the factorial function, n! = 1 2 n.