CSE 3302 Programming Languages Lecture 8: Functional Programming

Similar documents
Functional Languages and Higher-Order Functions

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

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Overloading, Type Classes, and Algebraic Datatypes

Lecture 19: Functions, Types and Data Structures in Haskell

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

A general introduction to Functional Programming using Haskell

Programming Language Concepts: Lecture 19

CSCE 314 Programming Languages

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

Functional Programming and Haskell

CS 457/557: Functional Languages

What does my program mean?

Data Types The ML Type System

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Haskell Overview II (2A) Young Won Lim 8/9/16

CS 440: Programming Languages and Translators, Spring 2019 Mon

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

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

Polymorphism and Type Inference

An introduction introduction to functional functional programming programming using usin Haskell

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

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

Functional Programming

Haskell Overview II (2A) Young Won Lim 9/26/16

Lambda Calculus and Type Inference

Functional Programming

Polymorphism and Type Inference

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

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

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

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

CSC324 Principles of Programming Languages

Programming Paradigms

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1

Example problems. Combinatorial auction. Towers of Hanoi Rectangle packing Shortest route. 8 queens Soduko Maximizing (minimizing) costs

CS 11 Haskell track: lecture 1

Logic - CM0845 Introduction to Haskell

Functional Programming. Overview. Topics. Recall λ-terms. Examples

Haskell Overview II (2A) Young Won Lim 8/23/16

Programming in Haskell Aug-Nov 2015

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Standard ML. Data types. ML Datatypes.1

Functional Programming. Big Picture. Design of Programming Languages

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

CITS3211 FUNCTIONAL PROGRAMMING

Organization of Programming Languages CS3200/5200N. Lecture 11

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

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

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Logical Methods in... using Haskell Getting Started

LECTURE 16. Functional Programming

Lecture #23: Conversion and Type Inference

CSCC24 Functional Programming Scheme Part 2

Lambda Calculus and Type Inference

Lecture 5: Lazy Evaluation and Infinite Data Structures

Functional Programming in Haskell Part I : Basics

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Functional Programming. Pure Functional Programming

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Imperative languages

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

Chapter 11 :: Functional Languages

Shell CSCE 314 TAMU. Higher Order Functions

SML A F unctional Functional Language Language Lecture 19

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh

COP4020 Programming Assignment 1 - Spring 2011

Lambda Calculus. Variables and Functions. cs3723 1

CS 457/557: Functional Languages

Types and Type Inference

Programming Languages Fall 2013

Types and Type Inference

Types and Programming Languages. Lecture 5. Extensions of simple types

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Recursive Definitions, Fixed Points and the Combinator

Exercise 1 ( = 24 points)

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

Advanced features of Functional Programming (Haskell)

Urmas Tamm Tartu 2013

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

λ calculus is inconsistent

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Introduction to Functional Programming in Haskell 1 / 56

CSCE 314 Programming Languages

Functional Programming. Pure Functional Languages

Programming Language Concepts: Lecture 14

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

Haskell through HUGS THE BASICS

CS 320: Concepts of Programming Languages

Functional Programming

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Programming Languages

Exercise 1 (2+2+2 points)

Transcription:

CSE 3302 Programming Languages Lecture 8: Functional Programming (based on the slides by Tim Sheard) Leonidas Fegaras University of Texas at Arlington CSE 3302 L8 Spring 2011 1

Functional Programming Languages Programs consist of functions with no side-effects Functions are first class values Build modular programs using function composition No accidental coupling between components No assignments, statements, for-loops, while-loops, etc Supports higher-level, declarative programming style Automatic memory management (garbage collection) Emphasis on types and type inference Built-in support for lists and other recursive data types Type inference is like type checking but no type declarations are required Types of variables and expressions can be inferred from context Parametric data types and polymorphic type inference Strict vs lazy functional programming languages CSE 3302 L8 Spring 2011 2

Lambda Calculus The theoretical foundation of functional languages is lambda calculus Formalized by Church in 1941 Minimal in form Computationally complete Syntax: if e 1, e 2, and e are expressions in lambda calculus, so are Constant: c Variable: v Application: e 1 e 2 Abstraction: λv. e Bound vs free variables Beta reduction: (λv. e 1 ) e 2 e 1 [e 2 /v] (e 1 but with all free occurrences of v in e 1 replaced by e 2 ) need to be careful to avoid the variable capturing problem (name clashes) CSE 3302 L8 Spring 2011 3

Examples λx. + 1 x In Lisp: (lambda (x) (+ 1 x)) In Haskell: \x -> x+1 Beta reduction: (λ x. + 1 x) 2 => (+1 2) => 3 (λ x. + ((λ y. ((λ x. * x y) 2)) x) y) => (λ x. + ((λ y. (* 2 y)) x) y) => (λ x. + (* 2 x) y) CSE 3302 L8 Spring 2011 4

Reductions REDucible EXpression (redex) an application expression is a redex abstractions and variables are not redexes Use beta reduction to reduce (λx. + x x) 5 is reduced to 10 Normal form = no more reductions are available Reduction is confluent (has the Church-Rosser property) normal forms are unique regardless of the order of reduction Weak normal forms (WNF) no redexes outside of abstraction bodies Call by value (eager evaluation): WNF + leftmost innermost reductions Call by name: WNF + leftmost outermost reductions (normal order) Call by need (lazy evaluation): call by name, but each redex is evaluated at most once terms are represented by graphs and reductions make shared subgraphs CSE 3302 L8 Spring 2011 5

Polymorphic Lambda Calculus Typed lambda calculus: Constant: c Variable: v Application: e 1 e 2 Abstraction: λv:t. e Polymorphic lambda calculus: Constant: c Variable: v Application: e 1 e 2 Abstraction: λv:t. e Type abstraction: Λv. e Type instantiation: e[t] Types are: Base type: int Function: t 1 t 2 Types are: Base type: int Type variable: v Universal quantification: v. t Function: t 1 t 2 CSE 3302 L8 Spring 2011 6

Functional Languages Functional languages = polymorphic lambda calculus + syntactic sugar Functional languages support parametric (generic) data types data List a = Nil Cons a (List a) data Tree a b = Leaf a Node b (Tree a b) (Tree a b) Cons 1 (Cons 2 Nil) Cons a (Cons b Nil) Polymorphic functions: append (Cons x r) s = Cons x (append r s) append Nil s = s The type of append is a. (List a) (List a) (List a) CSE 3302 L8 Spring 2011 7

Type Inference Functional languages need type inference rather than type checking λv:t. e requires type checking λv. e requires type inference (need to infer the type of v) Type inference is undecidable in general Solution: type schemes (shallow types): a 1. a 2. a n. t no other universal quantification in t ( b. b int) ( b. b int) is not shallow When a type is missing, then a fresh type variable is used Type checking is based on type equality; type inference is based on type unification A type variable can be unified with any type Example in Haskell: let f = λx. x in (f 5, f a ) λx. x has type a. a a Cost of polymorphism: polymorphic values must be boxed (pointers to heap) CSE 3302 L8 Spring 2011 8

Haskell Web site: http://haskell.org/ Tutorials: http://haskell.org/tutorial/ http://haskell.org/haskellwiki/learning_haskell > hugs Hugs 98: Based on the Haskell 98 standard Copyright (c) 1994-2005 --- World Wide Web: http://haskell.org/hugs Bugs: http://hackage.haskell.org/trac/hugs Version: September 2006 Haskell 98 mode: Restart with command line option -98 to enable extensions Type :? for help Hugs> CSE 3302 L8 Spring 2011 9

Functions Functions are defined in files and loaded into Hugs: :l file.hs Functions on numbers Type of numbers: Int and Float Conversion functions: frominteger round Functions on Booleans Relational operators < > <= >= == /= Combinators && not Examples? 5 > 7 False? 1==4 False CSE 3302 L8 Spring 2011 10

? [1,2] ++ [4] [1, 2, 4] Lists in Haskell? null [2] False? take 3 [1,2,3,4,5,6] [1, 2, 3]? drop 3 [1,2,3,4,5,6] [4, 5, 6] CSE 3302 L8 Spring 2011 11

The Empty List [] The "Cons" ( : ) Constructor? 3 : [3,4,5] [3, 3, 4, 5] The Dot Dot notation? [1.. 4] [1, 2, 3, 4] Constructing Lists The Comprehension Notation? [x + 1 x <- [2..4]] [3, 4, 5]? [ (x,y) x <- [1..2], y <- [3,5,7]] [(1,3), (1,5), (1,7), (2,3), (2,5), (2,7)]? [ x * 2 x <- [1..10], even x] [4, 8, 12, 16, 20] CSE 3302 L8 Spring 2011 12

? head [1,2,3] 1 Taking Lists Apart? tail [1,2,3] [2, 3]? take 2 [1,2,3] [1,2]? drop 2 [1,2,3] [3] CSE 3302 L8 Spring 2011 13

Rules for Patterns All the patterns (on the left) should have compatible types The cases should (but are not required to) be exhaustive There should be no ambiguity as to which case applies Ordering fixes ambiguity if there is any (the first match is chosen) A Pattern is: A variable x A constructor applied to patterns x:xs or Branch(x,y,z) A constant 3 or [] A tuple of patterns (x,3,y:ys) CSE 3302 L8 Spring 2011 14

Recursive Functions Simple recursive functions: fact n = if n == 0 then 1 else n*(fact (n-1)) len [] = 0 len (x:xs) = 1 + (len xs) Hugs session? fact 3 6? len [1,2,3,4] 4 CSE 3302 L8 Spring 2011 15

Local Definitions: Where len2 [] = 0 Local Definitions len2 (x:xs) = one + z where z = len2 xs one = 1 Indentation matters! Same sequence of tokens but different meaning where a = f x y b = g z where a = f x y b = g z -- y is a variable used as -- an arg to function f -- y is the name of fun -- being defined Location of <newline> makes a big difference Rule of thumb: Definitions at the same scope should be indented equally far CSE 3302 L8 Spring 2011 16

Function Types & Prototyping Typing f a b c = a + b + c + 1 has type f :: Int -> Int -> Int -> Int Read as f:: Int -> (Int -> (Int -> Int)) Prototyping plus :: Int -> Int -> Int plus x y = if x == 0 then y else 1 + (plus (x-1) y) myand :: Bool -> Bool -> Bool myand True False = False myand True True = True myand False False = False myand False True = False CSE 3302 L8 Spring 2011 17

Overloading and Classes? :type difference difference:: Num a => a -> a -> a? difference 3 4-1? difference 4.5 7.8-3.3? The class Num is a predicate on types CSE 3302 L8 Spring 2011 18

Ways to Create Functions By defining: plusone x = x+1? plusone 3 4 By operator section? (3+) 5 8? map (3+) [2,3,4] [5, 6, 7] By lambda expression? (\x -> x+2) 5 7? map (\x -> x*2) [2,3,4] [4, 6, 8] CSE 3302 L8 Spring 2011 19

Creating Functions (cont.) By currying (partial application)? plus 3 plus 3? :type (plus 3) plus 3 :: Int -> Int? map (plus 3) [3,4] [6, 7] By composition? map (head. tail) [[2,3,4],[4,5,6]] [3, 5] By combinator: k x y = x Functions which return functions? map (k 3) [1,2,3] [3, 3, 3] CSE 3302 L8 Spring 2011 20

Expressions, Values, and Types There are three types of distinguished entities in Haskell Expressions 5 + 3 len [1,2,3] rev [2,9] len Values 8 3 [9,2] <<function>> Types Int Int [ Int ] [ a ] -> Int CSE 3302 L8 Spring 2011 21

Tuples Heterogeneous Collection of a fixed width Tuple Expressions (5+3, not True) (tl [1,2,3], [2]++[3,4], abc ) Evaluate to Tuple Values (8, False) ([2,3], [2,3,4], abc ) And have Tuple Types (Int, Bool) ([Int], [Int], String) CSE 3302 L8 Spring 2011 22

Typing Tuples? :type (1,"x",True) (1,"x",True)::(Int,String,Bool)? :type (1,2) (1,2) :: (Int,Int)? :type (2,("x",3)) (2,("x",3))::(Int,(String,Int)) Note: (Int,([Char],Int)) <> (Int,[Char],Int) Pattern matching on tuples? (\ (a,b) -> a) (2,3) 2? (\ (a,b) -> b + 2) (2,3) 5 CSE 3302 L8 Spring 2011 23

Used when returning multiple values Function that splits a list into two pieces at some particular position split 2 [1,2,3,4] --> ([1,2],[3,4]) split 3 [1,2,3,4,5,6,7] ---> ([1, 2, 3],[4, 5, 6, 7]) split 0 [1,2,3] --> ([],[1,2,3]) split 0 x = ([],x) split n [] = ([],[]) split n (x:xs) = (x:ys,zs) where (ys,zs) = split (n-1) xs CSE 3302 L8 Spring 2011 24

Polymorphism Consider: tag1 x = (1,x)? :type tag1 tag1 :: a -> (Int,a) Other functions have types like this consider (++)? :type (++) (++) :: [a] -> [a] -> [a]? :type ([1,2]++) ([1,2] ++) :: [Int] -> [Int] What are some other polymorphic functions and their types? id :: reverse :: head :: tail :: (:) :: split :: CSE 3302 L8 Spring 2011 25

Functions as arguments Consider: mymap f [] = [] mymap f (x:xs) = (f x):(mymap f xs) The parameter f is a function What is the type of mymap? mymap :: (a -> b) -> [a] -> [b] Result? map add1 map (\x -> 3) [1,2,3] CSE 3302 L8 Spring 2011 26

Abstracting myfoldr op e [] = e myfoldr op e (x:xs) = op x (myfoldr op e xs)? :t myfoldr myfoldr :: (a -> b -> b) -> b -> [a] -> b? myfoldr (+) 0 [1,2,3] 6 CSE 3302 L8 Spring 2011 27

Definition By Patterns Example on Booleans myand True False = False myand True True = True myand False False = False myand False True = False Pattern may contain constructors Constructors are always capitalized Order Matters Variables in patterns match anything myand2 True True = True myand2 x y = False What happens if we reverse the order of the two equations above? CSE 3302 L8 Spring 2011 28

Lazy Evaluation Consider: repeat x = x : (repeat x)? repeat 't' tttttttttttttttttttttttttttttttttttttttt tttttttttttttt^c{interrupted!} But we can use a finite prefix? take 10 (repeat 't') tttttttttt iterate creates infinite lists? take 5 (iterate (+1) 1) [1, 2, 3, 4, 5]? take 5 (iterate (*2) 1) [1, 2, 4, 8, 16]? take 5 (iterate (/10) 142) [142, 14, 1, 0, 0] CSE 3302 L8 Spring 2011 29

Kinds of datatypes enumerated types Defining New Datatypes records (or products or struct) variant records (or sums) pointer types arrays Haskell s data declaration provides many of these kinds of types in a uniform way which abstracts from their implementation details The data declaration defines new functions and constants, which provide an abstract interface to the newly defined type CSE 3302 L8 Spring 2011 30

The Data Declaration Enumeration Types data Day = Sun Mon Tue Wed Thu Fri Sat deriving Eq The names on right hand side are constructor constants and are the only elements of the type valday 0 = Sun valday 1 = Mon valday 2 = Tue valday 3 = Wed valday 4 = Thu valday 5 = Fri valday 6 = Sat? valday 3 Wed CSE 3302 L8 Spring 2011 31

Constructors & Patterns data defined types define new constructors and can be accessed by patterns Constructors without arguments are constants Example using case dayval x = case x of Sun -> 0 Mon -> 1 Tue -> 2 Wed -> 3 Thu -> 4 Fri -> 5 Sat -> 6 CSE 3302 L8 Spring 2011 32

Patterns in Declarations In a declaration patterns can be used. Possible to have many lines for a definition if each pattern is distinct. dayval Sun = 0 dayval Mon = 1 dayval Tue = 2 dayval Wed = 3 dayval Thu = 4 dayval Fri = 5 dayval Sat = 6? dayval Tue 2 CSE 3302 L8 Spring 2011 33

Variant Record-like Types data Temp = Celsius Float Fahrenheit Float Kelvin Float Use patterns to define functions over this type: tokelvin (Celsius c) = Kelvin(c + 273.0) tokelvin (Fahrenheit f) = Kelvin( (f - 32.0) * (5.0/9.0) + 273.0 ) tokelvin (Kelvin k) = Kelvin k CSE 3302 L8 Spring 2011 34

Shape types from the Text data Shape = Rectangle Float Float Ellipse Float Float RtTriangle Float Float Polygon [ (Float,Float) ] deriving Show Deriving Show tells the system to build a show function for the type Shape Using Shape: functions returning shape objects circle radius = Ellipse radius radius square side = Rectangle side side CSE 3302 L8 Spring 2011 35

Functions over Shape Functions over shape can be defined using pattern matching area :: Shape -> Float area (Rectangle s1 s2) = s1 * s2 area (Ellipse r1 r2) = pi * r1 * r2 area (RtTriangle s1 s2) = (s1 *s2) / 2 area (Polygon (v1:pts)) = polyarea pts where polyarea :: [ (Float,Float) ] -> Float polyarea (v2 : v3 : vs) = triarea v1 v2 v3 + polyarea (v3:vs) polyarea _ = 0 CSE 3302 L8 Spring 2011 36

Higher Order Functions Useful Functions on lists Let: x = [1,2,3,4] y = ["a","b","c","d"] Then: map f x = [f 1, f 2, f 3, f 4] filter even x = [2,4] foldr (+) e x = 1 + (2 + (3 + (4 + e))) foldl (+) e x = (((e + 1) + 2) + 3) + 4 iterate f 1 = [1, f 1, f(f 1),f(f(f 1)),... ] CSE 3302 L8 Spring 2011 37

Higher-Order Functions Map a function f over every element in a list map:: (a b) [a] [b] map f [] = [] map f (a:s) = (f a):(map f s) e.g. map (\x x+1) [1,2,3,4] = [2,3,4,5] Replace all cons list constructions with the function c and the nil with the value z foldr:: (a b b) b [a] b foldr c z [] = z foldr c z (a:s) = c a (foldr c z s) e.g. foldr (+) 0 [1,2,3] = 6 e.g. append x y = foldr (++) y x e.g. map f x = foldr (\a r (f a):r) [] x CSE 3302 L8 Spring 2011 38

String and Char String is defined to be a list of char: [Char]? "abc" abc? ['a', 'b', 'c'] abc Note: "a" is a string (A List of Char with one element) 'a' is a Char `a` is an operator Since String = [Char] all the list operations work on strings? abc ++ xyz abcxyz? reverse abc cba CSE 3302 L8 Spring 2011 39