CS 457/557: Functional Languages

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

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Shell CSCE 314 TAMU. Haskell Functions

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read.

Standard prelude. Appendix A. A.1 Classes

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

Programming Languages 3. Definition and Proof by Induction

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

CS 440: Programming Languages and Translators, Spring 2019 Mon

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

Lecture 19: Functions, Types and Data Structures in Haskell

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

CS 320: Concepts of Programming Languages

An introduction introduction to functional functional programming programming using usin Haskell

Algebraic Types. Chapter 14 of Thompson

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

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

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

Lecture 4: Higher Order Functions

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

Lecture 2: List algorithms using recursion and list comprehensions

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

CSc 372 Comparative Programming Languages

CSCE 314 Programming Languages

INTRODUCTION TO HASKELL

Applicative, traversable, foldable

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Shell CSCE 314 TAMU. Higher Order Functions

A general introduction to Functional Programming using Haskell

CSE 3302 Programming Languages Lecture 8: Functional Programming

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

Applicative, traversable, foldable

Logic - CM0845 Introduction to Haskell

Programming Paradigms

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18)

Background Operators (1E) Young Won Lim 7/7/18

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

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

Shell CSCE 314 TAMU. Functions continued

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

CS 457/557: Functional Languages

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

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

Introduction to Haskell

301AA - Advanced Programming

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

Functional Programming TDA 452, DIT 142

Overloading, Type Classes, and Algebraic Datatypes

SML A F unctional Functional Language Language Lecture 19

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

College Functors, Applicatives

Programming with Math and Logic

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

Functional Programming Mid-term exam Tuesday 3/10/2017

CSCE 314 Programming Languages

List Functions, and Higher-Order Functions

The Haskell HOP: Higher-order Programming

CS 340 Spring 2019 Midterm Exam

F28PL1 Programming Languages. Lecture 12: Standard ML 2

! " Haskell Expressions are treated as: !" Thunks. !" Closures. !" Delayed Computations. !" Suspensions. ! " Expressions are evaluated: !

Standard ML. Data types. ML Datatypes.1

CS 320: Concepts of Programming Languages

CS 360: Programming Languages Lecture 12: More Haskell

Haskell through HUGS THE BASICS

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

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Advanced features of Functional Programming (Haskell)

Watch out for the arrows. Recollecting Haskell, Part V. A start on higher types: Mapping, 1. A start on higher types: Mapping, 2.

Typed Scheme: Scheme with Static Types

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Functional Programming TDA 452, DIT 142

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Advanced Functional Programming

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Principles of Programming Languages

The Algebra of Programming in Haskell

Haskell: From Basic to Advanced. Part 3 A Deeper Look into Laziness

Typed Racket: Racket with Static Types

CS 320 Midterm Exam. Fall 2018

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

Datatype declarations

Introduction to Functional Programming in Haskell 1 / 56

CSCE 314 Programming Languages

Programming Languages Fall 2013

CSE341 Spring 2017, Midterm Examination April 28, 2017

CS 11 Haskell track: lecture 1

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

SMURF Language Reference Manual Serial MUsic Represented as Functions

Functional Logic Programming Language Curry

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Lecture #23: Conversion and Type Inference

General Computer Science (CH ) Fall 2016 SML Tutorial: Practice Problems

Introduction to Programming, Aug-Dec 2006

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

CSci 4223 Principles of Programming Languages

CMSC 330: Organization of Programming Languages

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

Background Type Classes (1B) Young Won Lim 6/28/18

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

Transcription:

CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University 1

Why Lists? Lists are a heavily used data structure in many functional programs Special syntax is provided to make programming with lists more convenient Lists are a special case / an example of: An algebraic datatype (coming soon) A parameterized datatype (coming soon) A monad (coming, but a little later) 2

Naming Convention: We often use a simple naming convention: If a typical value in a list is called x, then a typical list of such values might be called xs (i.e., the plural of x) and a list of lists of values called x might be called xss A simple convention, minimal clutter, and a useful mnemonic too! 3

Prelude Functions: (++) :: [a] -> [a] -> [a] reverse :: [a] -> [a] take :: Int -> [a] -> [a] drop :: Int -> [a] -> [a] takewhile :: (a -> Bool) -> [a] -> [a] dropwhile :: (a -> Bool) -> [a] -> [a] replicate :: Int -> a -> [a] iterate :: (a -> a) -> a -> [a] repeat :: a -> [a] 4

Constructor Functions: What if you can t find a function in the prelude that will do what you want to do? Every list takes the form: [], an empty list (x:xs), a non-empty list whose first element is x, and whose tail is xs Equivalently: the list type has two constructor functions: The constant [] :: [a] The operator (:) :: a -> [a] -> [a] Using pattern matching, we can also take lists apart 5

Functions on Lists: null :: [a] -> Bool null [] = True null (x:xs) = False head :: [a] -> a head (x:xs) = x tail tail (x:xs) :: [a] -> [a] = xs 6

Recursive Functions: last last (x:[]) last (x:y:xs) :: [a] -> a = x = last (y:xs) init :: [a] -> [a] init (x:[]) = [] init (x:y:xs) = x : init (y:xs) map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x : map f xs 7

continued: inits :: [a] -> [[a]] inits [] = [[]] inits (x:xs) = [] : map (x:) (inits xs) in List library subsets subsets [] = [[]] subsets (x:xs) :: [a] -> [[a]] = subsets xs user defined ++ map (x:) (subsets xs) 8

Why Does This Work? What does it mean to say that [] and (:) are the constructor functions for lists? No Junk: every list value is equal either to [], or else to a list of the form (x:xs) (ignoring non-termination, for now) No Confusion: if x y, or xs ys, then x:xs y:ys A pair of equations f [] = f (x:xs) = defines a unique function on list values 9

Algebraic Datatypes: 10

Algebraic Datatypes: Booleans and Lists are both examples of algebraic datatypes : No Junk: Every Boolean value can be constructed using either False or True Every list can be described using (a combination of) [] and (:) No Confusion: True False [] (x:xs) and if (x:xs)=(y:ys), then x=y and xs=ys 11

In Haskell Notation: data Bool = False True introduces: A type, Bool A constructor function, False :: Bool A constructor function, True :: Bool data List a = Nil Cons a (List a) introduces A type, List t, for each type t A constructor function, Nil :: List a A constructor function, Cons :: a -> List a -> List a 12

More Enumerations: data Rainbow = Red Orange Yellow Green Blue Indigo Violet introduces: A type, Rainbow A constructor function, Red :: Rainbow A constructor function, Violet :: Rainbow No Junk: Every value of type Rainbow is one of the above seven colors No Confusion: The seven colors above are distinct values of type Rainbow 13

More Recursive Types: data Shape = Circle Radius Polygon [Point] Transform Transform Shape data Transform = Translate Point Rotate Angle Compose Transform Transform introduces: Two types, Shape and Transform Circle :: Radius -> Shape Polygon :: [Point] -> Shape Transform :: Transform -> Shape -> Shape 14

More Parameterized Types: data Maybe a = Nothing Just a introduces: A type, Maybe t, for each type t A constructor function, Nothing :: Maybe a A constructor function, Just :: a -> Maybe a data Pair a b = Pair a b introduces A type, Pair t s, for any types t and s A constructor function Pair :: a -> b -> Pair a b 15

General Form: Algebraic datatypes are introduced by top-level definitions of the form: data T a 1 a n = c 1 c m where: T is the type name (must start with a capital letter) a 1,, a n are (distinct) (type) arguments/parameters/ variables (must start with lower case letter) (n 0) Each of the c i is an expression F i t 1 t k where: t 1,, t k are type expressions that (optionally) mention the arguments a 1,, a n F i is a new constructor function F i :: t 1 -> -> t p -> T a 1 a n The arity of F i, k 0 Quite a lot for a single definition! 16

No Junk and Confusion: The key properties that are shared by all algebraic datatypes: No Junk: Every value of type T a 1 a n can be written in the form F i e 1 e k for some choice of constructor F i and (appropriately typed) arguments e 1,, e k No Confusion: Distinct constructors or distinct arguments produce distinct results These are fundamental assumptions that we make when we write and/or reason about functional programs. 17

Pattern Matching: In addition to introducing a new type and a collection of constructor functions, each data definition also adds the ability to pattern match over values of the new type For example, given data Maybe a = Nothing Just a then we can define functions like the following: orelse :: Maybe a -> a -> a Just x `orelse` y = x Nothing `orelse` y = y 18

Pattern Matching & Substitution: The result of a pattern match is either: A failure A success, accompanied by a substitution that provides a value for each of the values in the pattern Examples: [] does not match the pattern (x:xs) [1,2,3] matches the pattern (x:xs) with x=1 and xs=[2,3] 19

Patterns: More formally, a pattern is either: An identifier Matches any value, binds result to the identifier An underscore (a wildcard ) Matches any value, discards the result A constructed pattern of the form C p 1 p n, where C is a constructor of arity n and p 1,,p n are patterns of the appropriate type Matches any value of the form C e 1 e n, provided that each of the e i values matches the corresponding p i pattern. 20

Other Pattern Forms: For completeness: Sugared constructor patterns: Tuple patterns (p 1,p 2 ) List patterns [p 1, p 2, p 3 ] Strings, for example: "hi" = ( h : i : []) Labeled patterns Numeric Literals: Can be considered as constructor patterns, but the implementation uses equality (==) to test for matches as patterns, id@pat Lazy patterns, ~pat (n+k) patterns 21

Function Definitions: In general, a function definition is written as a list of adjacent equations of the form: f p 1 p n = rhs where: f is the name of the function that is being defined p 1,, p n are patterns, and rhs is an expression All equations in the definition of f must have the same number of arguments (the arity of f) 22

continued: Given a function definition with m equations: f p 1,1 p n,1 = rhs 1 f p 1,2 p n,2 = rhs 2 f p 1,m p n,m = rhs m The value of f e 1 e n is S rhs i, where i is the smallest integer such that the expressions e j match the patterns p j,i and S is the corresponding substitution. 23

Guards, Guards! A function definition may also include guards (Boolean expressions): f p 1 p n g 1 = rhs 1 g 2 = rhs 2 g 3 = rhs 3 An expression f e 1 e n will only match an equation like this if all of the e i match the corresponding p i and, in addition, at least one of the guards g j is True In that case, the value is S rhs j, where j is the smallest index such that g j is True (The prelude defines otherwise = True :: Bool for use in guards.) 24

Where Clauses: A function definition may also a where clause: f p 1 p n = rhs where decls Behaves like a let expression: f p 1 p n = let decls in rhs Except that where clauses can scope across guards: f p 1 p n g 1 = rhs 1 g 2 = rhs 2 g 3 = rhs 3 where decls Variables bound here in decls can be used in any of the g 25 i or rhs i

Example: filter filter :: (a -> Bool) -> [a] -> [a] filter p [] = [] filter p (x:xs) p x = x : rest otherwise = rest where rest = filter p xs 26

Example: Binary Search Trees data Tree = Leaf Fork Tree Int Tree insert :: Int -> Tree -> Tree insert n Leaf = Fork Leaf n Leaf insert n (Fork l m r) n <= m = Fork (insert n l) m r otherwise = Fork l m (insert n r) lookup :: Int -> Tree -> Bool lookup n Leaf = False lookup n (Fork l m r) n < m = lookup n l n > m = lookup n r otherwise = True 27

Case Expressions: Case expressions can be used for pattern matching: case e of p 1 -> e 1 p 2 -> e 2 p n -> e n Equivalent to: let f p 1 = e 1 f p 2 = e 2 f p n = e n in f e 28

continued: Guards and where clauses can also be used in case expressions: filter p xs = case xs of [] -> [] (x:xs) p x -> x:ys otherwise -> ys where ys = filter p xs 29

If Expressions: If expressions can be used to test Boolean values: if e then e 1 else e 2 Equivalent to: case e of True -> e 1 False -> e 2 30

Summary: Algebraic datatypes can support: Enumeration types Parameterized types Recursive types Products (composite/aggregate values); and Sums (alternatives) Type constructors, Constructor functions, Pattern matching Unifying features: No junk, no confusion! 31

Example: transpose transpose transpose [] = [] :: [[a]] -> [[a]] transpose ([] : xss) = transpose xss transpose ((x:xs) : xss) = (x : [h (h:t) <- xss]) : transpose (xs : [ t (h:t) <- xss]) Example: transpose [[1,2,3],[4,5,6]] = [[1,4],[2,5],[3,6]] 32

Example: say Say> putstr (say "hello") H H EEEEE L L OOO H H E L L O O HHHHH EEEEE L L O O H H E L L O O H H EEEEE LLLLL LLLLL OOO Say> 33

continued: say = ('\n':). unlines. map (foldr1 (\xs ys->xs++" "++ys)). transpose. map picchar picchar 'A' = [" A ", etc " A A ", "AAAAA", "A A", "A A"] 34

Composition and Reuse: Say> (putstr. concat. map say. lines. say) "A" A A A AAAAA A A A A A A A A A A AAAAA AAAAA A A A A A A A A A A A A A A A A A A A A A A A AAAAA AAAAA AAAAA AAAAA AAAAA A A A A A A A A A A A A A A A A A A A A A A A A A A AAAAA AAAAA A A A A A A A A A A A A A A AAAAA AAAAA A A A A A A A A Say> 35