induction data types and type classes practice curtis
play

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


  1. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Type Classes A type class has nothing to do with OOP classes or inheritance. 1 Type classes describe a set of behaviours that can be implemented for any type. 2 A function or type class instance can operate on a type variable constrained by a 3 type class instead of a concrete type. A type class is similar to an OOP interface. 4 When creating an instance of a type class with laws , you must ensure the laws are 5 held manually (they cannot be checked by the compiler). When using a type class with laws you can assume that all laws hold for all 6 instances of the type class. 32

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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 Tranisitivity : x ≤ y ∧ y ≤ z → x ≤ z 1 41

  11. 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 Tranisitivity : x ≤ y ∧ y ≤ z → x ≤ z 1 Reflexivity : x ≤ x 2 42

  12. 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 Tranisitivity : x ≤ y ∧ y ≤ z → x ≤ z 1 Reflexivity : x ≤ x 2 Antisymmetry : x ≤ y ∧ y ≤ x → x = y 3 43

  13. 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 Tranisitivity : x ≤ y ∧ y ≤ z → x ≤ z 1 Reflexivity : x ≤ x 2 Antisymmetry : x ≤ y ∧ y ≤ x → x = y 3 Totality (total order): x ≤ y ∨ y ≤ x 4 44

  14. 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

  15. 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

  16. 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 Reflexivity : x = x 1 47

  17. 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 Reflexivity : x = x 1 Symmetry : x = y → y = x 2 48

  18. 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 Reflexivity : x = x 1 Symmetry : x = y → y = x 2 Tranisitivity : x = y ∧ y = z → x = z 3 49

  19. 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 Reflexivity : x = x 1 Symmetry : x = y → y = x 2 Tranisitivity : x = y ∧ y = z → x = z 3 Negation (equality): x � = y → ¬ ( x = y ) 4 50

  20. 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 Reflexivity : x = x 1 Symmetry : x = y → y = x 2 Tranisitivity : x = y ∧ y = z → x = z 3 Negation (equality): x � = y → ¬ ( x = y ) 4 Substitutivity (equality): x = y → f x = f y 5 51

  21. 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

  22. 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

  23. 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

  24. 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

  25. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Kinds of Types 56

  26. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Kinds of Types Just as values and functions in the runtime language of Haskell have types , types 1 in the type language of Haskell have kinds . 57

  27. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Kinds of Types Just as values and functions in the runtime language of Haskell have types , types 1 in the type language of Haskell have kinds . The kind of a concrete type is * . 2 58

  28. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Kinds of Types Just as values and functions in the runtime language of Haskell have types , types 1 in the type language of Haskell have kinds . The kind of a concrete type is * . 2 Just as functions exist over values (with the type a -> b ), type constructors exist 3 for types. 59

  29. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Kinds of Types Just as values and functions in the runtime language of Haskell have types , types 1 in the type language of Haskell have kinds . The kind of a concrete type is * . 2 Just as functions exist over values (with the type a -> b ), type constructors exist 3 for types. * -> * is a type constructor that takes a concrete type and produces a concrete 4 type. 60

  30. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Maybe 61

  31. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Maybe Haskell Definition -- Maybe :: * -> * data Maybe a = Just a | Nothing 62

  32. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Maybe Haskell Definition -- Maybe :: * -> * data Maybe a = Just a | Nothing Maybe is a type constructor that takes a type and produces a type that may or 1 may not hold a value. 63

  33. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Maybe Haskell Definition -- Maybe :: * -> * data Maybe a = Just a | Nothing Maybe is a type constructor that takes a type and produces a type that may or 1 may not hold a value. Maybe Int is a concrete type that may or may not hold an Int . 2 64

  34. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework List 65

  35. 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

  36. 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 List a is recursive as it has the ( Cons) constructor which takes a List a . 1 67

  37. 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 List a is recursive as it has the ( Cons) constructor which takes a List a . 1 List a has the Nil constructor which does not recurse and acts like a base case . 2 68

  38. 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 List a is recursive as it has the ( Cons) constructor which takes a List a . 1 List a has the Nil constructor which does not recurse and acts like a base case . 2 List is a type constructor that takes a type and produces a type that holds zero 3 or more of a value. 69

  39. 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 List a is recursive as it has the ( Cons) constructor which takes a List a . 1 List a has the Nil constructor which does not recurse and acts like a base case . 2 List is a type constructor that takes a type and produces a type that holds zero 3 or more of a value. List Int is a concrete type that zero or more values of type Int . 4 70

  40. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Haskell List 71

  41. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Haskell List Definition -- [ ] :: * -> * data [a] = a : (List a) | [] 72

  42. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Haskell List Definition -- [ ] :: * -> * data [a] = a : (List a) | [] [a, b, c] is syntactic sugar for the constructor (a : []))) . (b : (c : 1 73

  43. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Haskell List Definition -- [ ] :: * -> * data [a] = a : (List a) | [] [a, b, c] is syntactic sugar for the constructor (a : []))) . (b : (c : 1 "abc" is syntactic sugar for the constructor (’a’ : []))) . (’b’ : (’c’ : 2 74

  44. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Haskell List Definition -- [ ] :: * -> * data [a] = a : (List a) | [] [a, b, c] is syntactic sugar for the constructor (a : []))) . (b : (c : 1 "abc" is syntactic sugar for the constructor (’a’ : []))) . (’b’ : (’c’ : 2 Both can also be used as patterns. 3 75

  45. Data Types Type Clases I Type Parameters Type Classes II Inductive Proofs Homework Tree 76

  46. 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

  47. 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 Tree a is recursive in the same manner as List a . 1 78

  48. 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 Tree a is recursive in the same manner as List a . 1 Tree is a type constructor that takes a type and produces a type that holds zero 2 or more of a value in a tree. 79

  49. 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 Tree a is recursive in the same manner as List a . 1 Tree is a type constructor that takes a type and produces a type that holds zero 2 or more of a value in a tree. Tree Int is a concrete type that holds zero or more values of type Int in a tree. 3 80

  50. 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

  51. 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

  52. 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

  53. 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 Associativity : ( a • ( b • c )) = (( a • b ) • c ) 1 84

  54. 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 Associativity : ( a • ( b • c )) = (( a • b ) • c ) 1 Example instance Semigroup [a] where (<>) = (++) 85

  55. 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

  56. 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

  57. 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 Identity : ( mempty • x ) = x = ( x • mempty ) 1 88

  58. 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 Identity : ( mempty • x ) = x = ( x • mempty ) 1 Example instance Monoid [a] where mempty = [] 89

  59. 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

  60. 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 0 is a natural number. 1 For any natural number n , n + 1 is also a natural number. 2 91

  61. 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 0 is a natural number. 1 For any natural number n , n + 1 is also a natural number. 2 Therefore, to show P ( n ) for all n , it suffices to show: P (0) (the base case ), and 1 assuming P ( k ) (the inductive hypothesis ), 2 ⇒ P ( k + 1) (the inductive case ). 92

  62. 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) one = Succ Zero two = Succ (Succ Zero) 93

  63. 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) one = Succ Zero two = Succ (Succ Zero) Example (1 + 1 = 2) Prove one ‘add‘ one = two (done in editor) 94

  64. 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 [] is a list. 1 For any list xs , x:xs is also a list (for any item x ). 2 95

  65. 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 [] is a list. 1 For any list xs , x:xs is also a list (for any item x ). 2 This means, if we want to prove that a property P ( ls ) holds for all lists ls , it suffices to show: P ( [] ) (the base case) 1 P ( x:xs ) for all items x , assuming the inductive hypothesis P ( xs ). 2 96

  66. 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

  67. 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

  68. 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 for all xs : [] ++ xs == xs 1 for all xs : xs ++ [] == xs 2 (done in editor) 99

  69. 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

Recommend


More recommend