Induction, Data Types and Type Classes Practice Curtis Millar CSE, - - PowerPoint PPT Presentation

induction data types and type classes practice curtis
SMART_READER_LITE
LIVE PREVIEW

Induction, Data Types and Type Classes Practice Curtis Millar CSE, - - PowerPoint PPT Presentation

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Software System Design and Implementation Induction, Data Types and Type Classes Practice Curtis Millar CSE, UNSW (and Data61) 10 June 2020 1 Data Types


slide-1
SLIDE 1

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Software System Design and Implementation

Induction, Data Types and Type Classes Practice Curtis Millar

CSE, UNSW (and Data61) 10 June 2020

1

slide-2
SLIDE 2

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Product Types

data Point = Point Float Float deriving (Show, Eq) data Vector = Vector Float Float deriving (Show, Eq) movePoint :: Point -> Vector -> Point movePoint (Point x y) (Vector dx dy) = Point (x + dx) (y + dy)

2

slide-3
SLIDE 3

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Records

data Colour = Colour { redC :: Int , greenC :: Int , blueC :: Int , opacityC :: Int } deriving (Show, Eq)

3

slide-4
SLIDE 4

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Sum Types

data LineStyle = Solid | Dashed | Dotted deriving (Show, Eq) data FillStyle = SolidFill | NoFill deriving (Show, Eq)

4

slide-5
SLIDE 5

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Constructors

Constructors are how an value of a particular type is created.

5

slide-6
SLIDE 6

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Constructors

Constructors are how an value of a particular type is created. data Bool = True | False

6

slide-7
SLIDE 7

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Constructors

Constructors are how an value of a particular type is created. data Bool = True | False data Int = .. | -1 | 0 | 1 | 2 | 3 | ..

7

slide-8
SLIDE 8

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Constructors

Constructors are how an value of a particular type is created. data Bool = True | False data Int = .. | -1 | 0 | 1 | 2 | 3 | .. data Char = 'a' | 'b' | 'c' | 'd' | 'e' | ..

8

slide-9
SLIDE 9

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Custom Constructors

data Point = Point Float Float deriving (Show, Eq) data Vector = Vector Float Float deriving (Show, Eq)

9

slide-10
SLIDE 10

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Custom Constructors

data Point = Point Float Float deriving (Show, Eq) data Vector = Vector Float Float deriving (Show, Eq) Here, Point and Vector are both constructors.

10

slide-11
SLIDE 11

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Algebraic Data Types

Just as the Point constructor took two Float arguments, constructors for sum types can take parameters too, allowing us to model different kinds of shape: data PictureObject = Path [Point] Colour LineStyle | Circle Point Float Colour LineStyle FillStyle | Polygon [Point] Colour LineStyle FillStyle | Ellipse Point Float Float Float Colour LineStyle FillStyle deriving (Show, Eq) type Picture = [PictureObject] Here, type creates a type alias which provides only an alternate name that refers to an existing type.

11

slide-12
SLIDE 12

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Patterns in Function Definitions

12

slide-13
SLIDE 13

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Patterns in Function Definitions

1

Patterns are used to deconstruct an value of a particular type.

13

slide-14
SLIDE 14

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Patterns in Function Definitions

1

Patterns are used to deconstruct an value of a particular type.

2

A pattern can be a binding to a hole ( ), a name, or a constructor of the type.

14

slide-15
SLIDE 15

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Patterns in Function Definitions

1

Patterns are used to deconstruct an value of a particular type.

2

A pattern can be a binding to a hole ( ), a name, or a constructor of the type.

3

When defining a function, each argument is bound using a separate pattern.

15

slide-16
SLIDE 16

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Patterns in Function Definitions

if' :: Bool -> a -> a -> a if' True then' _ = then' if' False _ else' = else'

16

slide-17
SLIDE 17

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Patterns in Function Definitions

factorial :: Int -> Int factorial 0 = 1 factorial n = n * factorial (n - 1)

17

slide-18
SLIDE 18

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Patterns in Function Definitions

isVowel :: Char -> Bool isVowel 'a' = True isVowel 'e' = True isVowel 'i' = True isVowel 'o' = True isVowel 'u' = True isVowel _ = False

18

slide-19
SLIDE 19

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Records and Accessors

data Colour = Colour { redC :: Int, greenC :: Int , blueC :: Int, opacityC :: Int }

  • - Is equivalent to

data Color = Color Int Int Int Int redC (Color r _ _ _) = r greenC (Color _ g _ _) = g blueC (Color _ _ b _) = b

  • pacityC (Color _ _ _ o) = o

19

slide-20
SLIDE 20

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Patterns in Expressions

factorial :: Int -> Int factorial x = case x of 0 -> 1 n -> n * factorial (n - 1)

20

slide-21
SLIDE 21

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Newtype

newtype allows you to encapsulate an existing type to add constraints or properties without adding runtime overhead. newtype Kilometers = Kilometers Float newtype Miles = Miles Float kilometersToMiles :: Kilometers -> Miles kilometersToMiles (Kilometers kms) = Miles $ kms / 1.60934 milesToKilometers :: Miles -> Kilometers milesToKilometers (Miles miles) = Kilometers $ miles * 1.60934

21

slide-22
SLIDE 22

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Natural Numbers

data Nat = Zero | Succ Nat add :: Nat -> Nat -> Nat add Zero n = n add (Succ a) b = add a (Succ b) zero = Zero

  • ne = Succ Zero

two = add one one

22

slide-23
SLIDE 23

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Natural Numbers

data Nat = Zero | Succ Nat add :: Nat -> Nat -> Nat add Zero n = n add (Succ a) b = add a (Succ b) zero = Zero

  • ne = Succ Zero

two = add one one

1

Nat is recursive as it has the (Succ) constructor which takes a Nat.

23

slide-24
SLIDE 24

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Natural Numbers

data Nat = Zero | Succ Nat add :: Nat -> Nat -> Nat add Zero n = n add (Succ a) b = add a (Succ b) zero = Zero

  • ne = Succ Zero

two = add one one

1

Nat is recursive as it has the (Succ) constructor which takes a Nat.

2

Nat has the Zero constructor which does not recurse and acts like a base case.

24

slide-25
SLIDE 25

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

More Cool Graphics

Example (Live Coding of Fractal Trees)

25

slide-26
SLIDE 26

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Type Classes

26

slide-27
SLIDE 27

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Type Classes

1

A type class has nothing to do with OOP classes or inheritance.

27

slide-28
SLIDE 28

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Type Classes

1

A type class has nothing to do with OOP classes or inheritance.

2

Type classes describe a set of behaviours that can be implemented for any type.

28

slide-29
SLIDE 29

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Type Classes

1

A type class has nothing to do with OOP classes or inheritance.

2

Type classes describe a set of behaviours that can be implemented for any type.

3

A function or type class instance can operate on a type variable constrained by a type class instead of a concrete type.

29

slide-30
SLIDE 30

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Type Classes

1

A type class has nothing to do with OOP classes or inheritance.

2

Type classes describe a set of behaviours that can be implemented for any type.

3

A function or type class instance can operate on a type variable constrained by a type class instead of a concrete type.

4

A type class is similar to an OOP interface.

30

slide-31
SLIDE 31

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Type Classes

1

A type class has nothing to do with OOP classes or inheritance.

2

Type classes describe a set of behaviours that can be implemented for any type.

3

A function or type class instance can operate on a type variable constrained by a type class instead of a concrete type.

4

A type class is similar to an OOP interface.

5

When creating an instance of a type class with laws, you must ensure the laws are held manually (they cannot be checked by the compiler).

31

slide-32
SLIDE 32

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Type Classes

1

A type class has nothing to do with OOP classes or inheritance.

2

Type classes describe a set of behaviours that can be implemented for any type.

3

A function or type class instance can operate on a type variable constrained by a type class instead of a concrete type.

4

A type class is similar to an OOP interface.

5

When creating an instance of a type class with laws, you must ensure the laws are held manually (they cannot be checked by the compiler).

6

When using a type class with laws you can assume that all laws hold for all instances of the type class.

32

slide-33
SLIDE 33

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Show

Show simply allows us to take a type and represent it as a string.

33

slide-34
SLIDE 34

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Show

Show simply allows us to take a type and represent it as a string. Haskell Definition class Show a where show :: a -> [Char]

34

slide-35
SLIDE 35

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Show

Show simply allows us to take a type and represent it as a string. Haskell Definition class Show a where show :: a -> [Char] This is implemented for all of the built-in types such as Int, Bool, and Char

35

slide-36
SLIDE 36

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Read

Effectively the ’dual’ of Show, Read allows us to take a string representation of a value and decode it.

36

slide-37
SLIDE 37

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Read

Effectively the ’dual’ of Show, Read allows us to take a string representation of a value and decode it. You can think of read as having the following definition, but it is actually somewhat more complex. Definition class Read a where read :: [Char] -> a

37

slide-38
SLIDE 38

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Read

Effectively the ’dual’ of Show, Read allows us to take a string representation of a value and decode it. You can think of read as having the following definition, but it is actually somewhat more complex. Definition class Read a where read :: [Char] -> a This is implemented for all of the built-in types such as Int, Bool, and Char

38

slide-39
SLIDE 39

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Ord

Ord allows us to compare two values of a type for a partial or total inequality.

39

slide-40
SLIDE 40

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Ord

Ord allows us to compare two values of a type for a partial or total inequality. Haskell Definition class Ord a where (<=) :: a -> a -> Bool

40

slide-41
SLIDE 41

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Ord

Ord allows us to compare two values of a type for a partial or total inequality. Haskell Definition class Ord a where (<=) :: a -> a -> Bool

1

Tranisitivity: x ≤ y ∧ y ≤ z → x ≤ z

41

slide-42
SLIDE 42

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Ord

Ord allows us to compare two values of a type for a partial or total inequality. Haskell Definition class Ord a where (<=) :: a -> a -> Bool

1

Tranisitivity: x ≤ y ∧ y ≤ z → x ≤ z

2

Reflexivity: x ≤ x

42

slide-43
SLIDE 43

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Ord

Ord allows us to compare two values of a type for a partial or total inequality. Haskell Definition class Ord a where (<=) :: a -> a -> Bool

1

Tranisitivity: x ≤ y ∧ y ≤ z → x ≤ z

2

Reflexivity: x ≤ x

3

Antisymmetry: x ≤ y ∧ y ≤ x → x = y

43

slide-44
SLIDE 44

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Ord

Ord allows us to compare two values of a type for a partial or total inequality. Haskell Definition class Ord a where (<=) :: a -> a -> Bool

1

Tranisitivity: x ≤ y ∧ y ≤ z → x ≤ z

2

Reflexivity: x ≤ x

3

Antisymmetry: x ≤ y ∧ y ≤ x → x = y

4

Totality (total order): x ≤ y ∨ y ≤ x

44

slide-45
SLIDE 45

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Eq

Eq allows us to compare two values of a type for an equivalence or equality.

45

slide-46
SLIDE 46

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Eq

Eq allows us to compare two values of a type for an equivalence or equality. Haskell Definition class Eq a where (==) :: a -> a -> Bool

46

slide-47
SLIDE 47

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Eq

Eq allows us to compare two values of a type for an equivalence or equality. Haskell Definition class Eq a where (==) :: a -> a -> Bool

1

Reflexivity: x = x

47

slide-48
SLIDE 48

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Eq

Eq allows us to compare two values of a type for an equivalence or equality. Haskell Definition class Eq a where (==) :: a -> a -> Bool

1

Reflexivity: x = x

2

Symmetry: x = y → y = x

48

slide-49
SLIDE 49

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Eq

Eq allows us to compare two values of a type for an equivalence or equality. Haskell Definition class Eq a where (==) :: a -> a -> Bool

1

Reflexivity: x = x

2

Symmetry: x = y → y = x

3

Tranisitivity: x = y ∧ y = z → x = z

49

slide-50
SLIDE 50

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Eq

Eq allows us to compare two values of a type for an equivalence or equality. Haskell Definition class Eq a where (==) :: a -> a -> Bool

1

Reflexivity: x = x

2

Symmetry: x = y → y = x

3

Tranisitivity: x = y ∧ y = z → x = z

4

Negation (equality): x = y → ¬(x = y)

50

slide-51
SLIDE 51

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Eq

Eq allows us to compare two values of a type for an equivalence or equality. Haskell Definition class Eq a where (==) :: a -> a -> Bool

1

Reflexivity: x = x

2

Symmetry: x = y → y = x

3

Tranisitivity: x = y ∧ y = z → x = z

4

Negation (equality): x = y → ¬(x = y)

5

Substitutivity (equality): x = y → f x = f y

51

slide-52
SLIDE 52

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Derived Instances

When defining a new type we can have the compiler generate instances of Show, Read, Ord, or Eq with the deriving statement at the end of the definition.

52

slide-53
SLIDE 53

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Derived Instances

When defining a new type we can have the compiler generate instances of Show, Read, Ord, or Eq with the deriving statement at the end of the definition. Haskell Example data Colour = Colour { redC :: Int , greenC :: Int , blueC :: Int , opacityC :: Int } deriving (Show, Eq)

53

slide-54
SLIDE 54

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Derived Instances

When defining a new type we can have the compiler generate instances of Show, Read, Ord, or Eq with the deriving statement at the end of the definition. Haskell Example data Colour = Colour { redC :: Int , greenC :: Int , blueC :: Int , opacityC :: Int } deriving (Show, Eq) Derived instances of Ord will be total orders and will order by fields in the order they appear in a product type and will order constructors in the same order they are defined.

54

slide-55
SLIDE 55

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Derived Instances

When defining a new type we can have the compiler generate instances of Show, Read, Ord, or Eq with the deriving statement at the end of the definition. Haskell Example data Colour = Colour { redC :: Int , greenC :: Int , blueC :: Int , opacityC :: Int } deriving (Show, Eq) Derived instances of Ord will be total orders and will order by fields in the order they appear in a product type and will order constructors in the same order they are defined. Derived instances of Eq will be strict equalities.

55

slide-56
SLIDE 56

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Kinds of Types

56

slide-57
SLIDE 57

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Kinds of Types

1

Just as values and functions in the runtime language of Haskell have types, types in the type language of Haskell have kinds.

57

slide-58
SLIDE 58

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Kinds of Types

1

Just as values and functions in the runtime language of Haskell have types, types in the type language of Haskell have kinds.

2

The kind of a concrete type is *.

58

slide-59
SLIDE 59

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Kinds of Types

1

Just as values and functions in the runtime language of Haskell have types, types in the type language of Haskell have kinds.

2

The kind of a concrete type is *.

3

Just as functions exist over values (with the type a -> b), type constructors exist for types.

59

slide-60
SLIDE 60

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Kinds of Types

1

Just as values and functions in the runtime language of Haskell have types, types in the type language of Haskell have kinds.

2

The kind of a concrete type is *.

3

Just as functions exist over values (with the type a -> b), type constructors exist for types.

4

* -> * is a type constructor that takes a concrete type and produces a concrete type.

60

slide-61
SLIDE 61

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Maybe

61

slide-62
SLIDE 62

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Maybe

Haskell Definition

  • - Maybe :: * -> *

data Maybe a = Just a | Nothing

62

slide-63
SLIDE 63

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Maybe

Haskell Definition

  • - Maybe :: * -> *

data Maybe a = Just a | Nothing

1

Maybe is a type constructor that takes a type and produces a type that may or may not hold a value.

63

slide-64
SLIDE 64

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Maybe

Haskell Definition

  • - Maybe :: * -> *

data Maybe a = Just a | Nothing

1

Maybe is a type constructor that takes a type and produces a type that may or may not hold a value.

2

Maybe Int is a concrete type that may or may not hold an Int.

64

slide-65
SLIDE 65

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

List

65

slide-66
SLIDE 66

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

List

Haskell Definition

  • - List :: * -> *

data List a = Cons a (List a) | Nil

66

slide-67
SLIDE 67

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

List

Haskell Definition

  • - List :: * -> *

data List a = Cons a (List a) | Nil

1

List a is recursive as it has the (Cons) constructor which takes a List a.

67

slide-68
SLIDE 68

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

List

Haskell Definition

  • - List :: * -> *

data List a = Cons a (List a) | Nil

1

List a is recursive as it has the (Cons) constructor which takes a List a.

2

List a has the Nil constructor which does not recurse and acts like a base case.

68

slide-69
SLIDE 69

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

List

Haskell Definition

  • - List :: * -> *

data List a = Cons a (List a) | Nil

1

List a is recursive as it has the (Cons) constructor which takes a List a.

2

List a has the Nil constructor which does not recurse and acts like a base case.

3

List is a type constructor that takes a type and produces a type that holds zero

  • r more of a value.

69

slide-70
SLIDE 70

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

List

Haskell Definition

  • - List :: * -> *

data List a = Cons a (List a) | Nil

1

List a is recursive as it has the (Cons) constructor which takes a List a.

2

List a has the Nil constructor which does not recurse and acts like a base case.

3

List is a type constructor that takes a type and produces a type that holds zero

  • r more of a value.

4

List Int is a concrete type that zero or more values of type Int.

70

slide-71
SLIDE 71

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Haskell List

71

slide-72
SLIDE 72

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Haskell List

Definition

  • - [ ] :: * -> *

data [a] = a : (List a) | []

72

slide-73
SLIDE 73

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Haskell List

Definition

  • - [ ] :: * -> *

data [a] = a : (List a) | []

1

[a, b, c] is syntactic sugar for the constructor (a : (b : (c : []))).

73

slide-74
SLIDE 74

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Haskell List

Definition

  • - [ ] :: * -> *

data [a] = a : (List a) | []

1

[a, b, c] is syntactic sugar for the constructor (a : (b : (c : []))).

2

"abc" is syntactic sugar for the constructor (’a’ : (’b’ : (’c’ : []))).

74

slide-75
SLIDE 75

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Haskell List

Definition

  • - [ ] :: * -> *

data [a] = a : (List a) | []

1

[a, b, c] is syntactic sugar for the constructor (a : (b : (c : []))).

2

"abc" is syntactic sugar for the constructor (’a’ : (’b’ : (’c’ : []))).

3

Both can also be used as patterns.

75

slide-76
SLIDE 76

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Tree

76

slide-77
SLIDE 77

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Tree

Haskell Definition

  • - Tree :: * -> *

data Tree a = Node a (Tree a) (Tree a) | Leaf

77

slide-78
SLIDE 78

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Tree

Haskell Definition

  • - Tree :: * -> *

data Tree a = Node a (Tree a) (Tree a) | Leaf

1

Tree a is recursive in the same manner as List a.

78

slide-79
SLIDE 79

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Tree

Haskell Definition

  • - Tree :: * -> *

data Tree a = Node a (Tree a) (Tree a) | Leaf

1

Tree a is recursive in the same manner as List a.

2

Tree is a type constructor that takes a type and produces a type that holds zero

  • r more of a value in a tree.

79

slide-80
SLIDE 80

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Tree

Haskell Definition

  • - Tree :: * -> *

data Tree a = Node a (Tree a) (Tree a) | Leaf

1

Tree a is recursive in the same manner as List a.

2

Tree is a type constructor that takes a type and produces a type that holds zero

  • r more of a value in a tree.

3

Tree Int is a concrete type that holds zero or more values of type Int in a tree.

80

slide-81
SLIDE 81

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Semigroup

A semigroup is a pair of a set S and an operation • : S → S → S where the operation

  • is associative.

81

slide-82
SLIDE 82

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Semigroup

A semigroup is a pair of a set S and an operation • : S → S → S where the operation

  • is associative.

82

slide-83
SLIDE 83

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Semigroup

A semigroup is a pair of a set S and an operation • : S → S → S where the operation

  • is associative.

Haskell Definition class Semigroup a where (<>) :: a -> a -> a

83

slide-84
SLIDE 84

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Semigroup

A semigroup is a pair of a set S and an operation • : S → S → S where the operation

  • is associative.

Haskell Definition class Semigroup a where (<>) :: a -> a -> a

1

Associativity: (a • (b • c)) = ((a • b) • c)

84

slide-85
SLIDE 85

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Semigroup

A semigroup is a pair of a set S and an operation • : S → S → S where the operation

  • is associative.

Haskell Definition class Semigroup a where (<>) :: a -> a -> a

1

Associativity: (a • (b • c)) = ((a • b) • c) Example instance Semigroup [a] where (<>) = (++)

85

slide-86
SLIDE 86

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Monoid

A monoid is a semigroup (S, •) equipped with a special identity element.

86

slide-87
SLIDE 87

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Monoid

A monoid is a semigroup (S, •) equipped with a special identity element. Haskell Definition class (Semigroup a) => Monoid a where mempty :: a

87

slide-88
SLIDE 88

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Monoid

A monoid is a semigroup (S, •) equipped with a special identity element. Haskell Definition class (Semigroup a) => Monoid a where mempty :: a

1

Identity: (mempty • x) = x = (x • mempty)

88

slide-89
SLIDE 89

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Monoid

A monoid is a semigroup (S, •) equipped with a special identity element. Haskell Definition class (Semigroup a) => Monoid a where mempty :: a

1

Identity: (mempty • x) = x = (x • mempty) Example instance Monoid [a] where mempty = []

89

slide-90
SLIDE 90

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Inductive Proofs

Suppose we want to prove that a property P(n) holds for all natural numbers n.

90

slide-91
SLIDE 91

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Inductive Proofs

Suppose we want to prove that a property P(n) holds for all natural numbers n. Remember that the set of natural numbers N can be defined as follows: Definition of Natural Numbers

1

0 is a natural number.

2

For any natural number n, n + 1 is also a natural number.

91

slide-92
SLIDE 92

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Inductive Proofs

Suppose we want to prove that a property P(n) holds for all natural numbers n. Remember that the set of natural numbers N can be defined as follows: Definition of Natural Numbers

1

0 is a natural number.

2

For any natural number n, n + 1 is also a natural number. Therefore, to show P(n) for all n, it suffices to show:

1

P(0) (the base case), and

2

assuming P(k) (the inductive hypothesis), ⇒ P(k + 1) (the inductive case).

92

slide-93
SLIDE 93

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Natural Numbers Example

data Nat = Zero | Succ Nat add :: Nat -> Nat -> Nat add Zero n = n add (Succ a) b = add a (Succ b)

  • ne = Succ Zero

two = Succ (Succ Zero)

93

slide-94
SLIDE 94

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Natural Numbers Example

data Nat = Zero | Succ Nat add :: Nat -> Nat -> Nat add Zero n = n add (Succ a) b = add a (Succ b)

  • ne = Succ Zero

two = Succ (Succ Zero) Example (1 + 1 = 2) Prove one ‘add‘ one = two (done in editor)

94

slide-95
SLIDE 95

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Induction on Lists

Haskell lists can be defined similarly to natural numbers. Definition of Haskell Lists

1

[] is a list.

2

For any list xs, x:xs is also a list (for any item x).

95

slide-96
SLIDE 96

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Induction on Lists

Haskell lists can be defined similarly to natural numbers. Definition of Haskell Lists

1

[] is a list.

2

For any list xs, x:xs is also a list (for any item x). This means, if we want to prove that a property P(ls) holds for all lists ls, it suffices to show:

1

P([]) (the base case)

2

P(x:xs) for all items x, assuming the inductive hypothesis P(xs).

96

slide-97
SLIDE 97

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

List Monoid Example

(++) :: [a] -> [a] -> [a] (++) [] ys = ys

  • - 1

(++) (x:xs) ys = x : xs ++ ys

  • - 2

97

slide-98
SLIDE 98

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

List Monoid Example

(++) :: [a] -> [a] -> [a] (++) [] ys = ys

  • - 1

(++) (x:xs) ys = x : xs ++ ys

  • - 2

Example (Monoid) Prove for all xs, ys, zs: ((xs ++ ys) ++ zs) = (xs ++ (ys ++ zs))

98

slide-99
SLIDE 99

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

List Monoid Example

(++) :: [a] -> [a] -> [a] (++) [] ys = ys

  • - 1

(++) (x:xs) ys = x : xs ++ ys

  • - 2

Example (Monoid) Prove for all xs, ys, zs: ((xs ++ ys) ++ zs) = (xs ++ (ys ++ zs)) Additionally Prove

1

for all xs: [] ++ xs == xs

2

for all xs: xs ++ [] == xs

(done in editor)

99

slide-100
SLIDE 100

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

List Reverse Example

(++) :: [a] -> [a] -> [a] (++) [] ys = ys

  • - 1

(++) (x:xs) ys = x : xs ++ ys

  • - 2

reverse :: [a] -> [a] reverse [] = []

  • - A

reverse (x:xs) = reverse xs ++ [x]

  • - B

Example To Prove for all ls: reverse (reverse ls) == ls

(done in editor)

100

slide-101
SLIDE 101

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

List Reverse Example

(++) :: [a] -> [a] -> [a] (++) [] ys = ys

  • - 1

(++) (x:xs) ys = x : xs ++ ys

  • - 2

reverse :: [a] -> [a] reverse [] = []

  • - A

reverse (x:xs) = reverse xs ++ [x]

  • - B

Example To Prove for all ls: reverse (reverse ls) == ls

(done in editor)

First Prove for all ys: reverse (ys ++ [x]) = x:reverse ys

(done in editor)

101

slide-102
SLIDE 102

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Graphics and Artwork

data PictureObject = Path [Point] Colour LineStyle | Circle Point Float Colour LineStyle FillStyle | Polygon [Point] Colour LineStyle FillStyle | Ellipse Point Float Float Float Colour LineStyle FillStyle deriving (Show, Eq) type Picture = [PictureObject]

102

slide-103
SLIDE 103

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Homework

103

slide-104
SLIDE 104

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Homework

1

Last week’s quiz is due before on Friday. Make sure you submit your answers.

104

slide-105
SLIDE 105

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Homework

1

Last week’s quiz is due before on Friday. Make sure you submit your answers.

2

Do the first programming exercise, and ask us on Piazza if you get stuck. It is due by the start if my next lecture (in 7 days).

105

slide-106
SLIDE 106

Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework

Homework

1

Last week’s quiz is due before on Friday. Make sure you submit your answers.

2

Do the first programming exercise, and ask us on Piazza if you get stuck. It is due by the start if my next lecture (in 7 days).

3

This week’s quiz is also up, it’s due next Friday (in 9 days).

106