Download presentation

Presentation is loading. Please wait.

Published byHenry Chambers Modified about 1 year ago

1
Com2010 - Functional Programming Algebraic Data Types Marian Gheorghe Lecture 12 Module homepage Mole & http://www.dcs.shef.ac.uk/~marian ©University of Sheffieldcom2010

2
19. Algebraic data types 19.1 What is an algebraic type? 19.2 Algebraic Types, More Systematically 19.2.1 Enumeration 19.2.2 Product 19.2.3 Nested 19.2.4 Recursive 19.2.5 Polymorphic 19.3 General syntax Summary ©University of Sheffieldcom2010

3
We have seen built-in data types: primitive data types: Int, Float, Bool, Char, …. composite data types (tuples, lists, strings etc) (Int, String), [Int], String … User-defined data types - these are called algebraic data types Ex: Maybe a Algebraic types: introduced by the keyword data, followed by the name_of_the_type, =, and then the constructor (s). The type name and the constructor(s) must start with upper case letters. Ex: data Pres = Result String | Fail Introduction ©University of Sheffieldcom2010

4
Consider the example data Pres = Result String | Fail Elements of this type: Fail :: Pres Result “Green”:: Pres Result “m.gheorghe”::Pres This type definition introduces the following constructors Result :: String -> Pres Fail :: Pres Result is like a function but with no equation definition. Result used like a function (!): map Result["a","b"] [Result "a",Result "b"] Data type construction ©University of Sheffieldcom2010

5
Every element of Pres is built up from the constructors Result and Fail. This makes it possible to define functions f :: Pres -> X – structural decomposition To define f x, where x :: Pres, deconstruct x into its components, and define f x from these (simpler) components. print’ :: Pres -> String print’ (Result x) = "Result " ++ x -- equation 1 (pattern Result x) print’ Fail = "Fail" -- equation 2 (pattern Fail) When use print’ arg then arg is of type Pres and the result is of type String Structural decomposition ©University of Sheffieldcom2010

6
Patterns are used for deconstructing elements of an algebraic type. They are basic values: these are all constants of types String, Bool, Char, Int, Float or more complex or algebraic types variables: identifiers starting with lower case letters wildcard _ : this is an anonymous variable for a subexpression as-patterns: they occur in the form var@pattern Examples: Fail Result “m.gheorghe” Result x Result _ x v@(Result x) -- like aliases Patterns ©University of Sheffieldcom2010

7
Patterns may be 1. overlapping - they are evaluated in order. The first pattern that matches is taken: isOK :: Pres -> Bool isOK (Result _) = True -- matched first isOK _ = False -- only when pattern ‘Result _’ fails 2. non-exhaustive - if no pattern matches, then we get a run time error: prop :: Pres -> String prop (Result x) = x --no pattern for constructor ‘Fail’ prop Fail -- does not match ⇒ Program execution error: {prop Pres_Fail} More on Patterns ©University of Sheffieldcom2010

8
We will become familiar with alternative compound nested recursive polymorphic algebraic data types Algebraic data types - hierarchy ©University of Sheffieldcom2010

9
In an enumeration type all constructors are constants. Type definition data Temp = Cold | Hot data Season = Spring | Summer | Autumn | Winter Ex: weather :: Season -> Temp weather Summer = Hot weather _ = Cold -- ordering important! Alternatives – Enumeration types ©University of Sheffieldcom2010

10
In product types there is one constructor with many parameters. Type definition data People = Person String Int Int Ex: aPerson :: People aPerson = Person "M Gheorghe" 111 21843 Like for Pres type, the constructors introduced by an algebraic type definition can be used as functions. Person :: Name -> Office->TelNo->People An alternative definition of type People is given by the type synonym type People =(Name, Office, TelNo) Advantages and limitations of algebraic and synonym types Compound – Product type ©University of Sheffieldcom2010

11
Type definition data Employees = Employee Name Gender Dates type Name = String data Dates = Date Day Month Year type Day = Int data Month = Jan | Feb | Mar | Apr |May |Jun | Jul | Aug | Sep | Oct | Nov | Dec type Year = Int data Gender = Male | Female Nested algebraic data types ©University of Sheffieldcom2010

12
inJune :: [Employees]->[Dates] -- returns all male birthday dates in -- June inJune [] = [] inJune (Employee _ Male d@(Date _ Jun _):es = d:inJune es inJune _:es = inJune es Please note the use of as-pattern Caveat. To show the results obtained add deriving Show to both Dates and Month Question: is it possible to have a nested type with an inner type pointing to the current type? … Nested type - Example ©University of Sheffieldcom2010

13
Recursive type of simple expressions data Exp = Lit Int| Add Exp Exp| Sub Exp Exp Examples of expressions: 2 Lit 2 2+3 Add (Lit 2) (Lit 3) (3-1)+4 Add (Sub (Lit 3) (Lit 1)) (Lit 4) Do you remember any other recursive type? We define functions on type Exp by recursive pattern matching. eval :: Exp->Int –- evaluate expressions eval (Lit n) = n eval (Add e1 e2) = (eval e1) + (eval e2) eval (Sub e1 e2) = (eval e1) – (eval e2) eval e stops for e a finite expression; what if e is an infinite expression? …Yes: Recursive types ©University of Sheffieldcom2010

14
In Chapter 15 an important (recursive) polymorphic type was introduced, binary searching tree (look at the order of elements). Type definition of a binary searching tree (BSTree) data Tree a = Empty | Leaf a | Node a (Tree a) (Tree a) Specific instances are Tree Int, Tree String, or Tree (Tree Int). A tree of integers: intTree:: Tree Int intTree = Node 5 (Node 2 (Leaf 1) (Node 4 (Leaf 3) Empty)) (Node 8 (Node 6 Empty (Leaf 7)) Empty) Polymorphic types ©University of Sheffieldcom2010

15
BSTree of Integers ©University of Sheffieldcom2010 5 28 4 6 3 1 7

16
Binary searching tree application: traverse :: Tree a -> [a] -- traverse intTree = -- [1,2,3,4,5,6,7,8] ?? traverse Empty = [] traverse (Leaf x) = [x] traverse (Node x left right) = traverse left ++ [x] ++ traverse right Polymorphic functions on BSTrees (1) ©University of Sheffieldcom2010

17
Another polymorphic function on binary searching trees removeLast :: Tree a -> (a, Tree a) -- split off last element from a nonempty tree removeLast (Leaf x) = (x,Empty) removeLasT (Node y t_1 Empty)=(y,t_1) removeLast (Node y t_1 t_2) = (x, Node y t_1 t_3) where (x,t_3)=removeLast t_2 Polymorphic functions on BSTrees (2) ©University of Sheffieldcom2010

18
Remove the last element of a BSTree ©University of Sheffieldcom2010 2 =(8, 5 4 6 3 1 7)

19
Other polymorphic functions impose constraints on the type variable a. From Chapter 15 we have: tree_member :: Ord a => a->Tree a -> Bool tree_insert :: Ord a => a->Tree a -> Tree a For searching, the following polymorphic functions are useful: listToTree :: Ord a =>[a] -> Tree a -- turns a list into an ordered search tree listToTree [] = Empty listToTree (x:xs) = tree_insert x (listToTree xs) and treeSort :: Ord a => [a] -> [a] -- sorts a list via ordered tree treeSort xs = traverse(listToTree xs) Polymorphic functions on BSTrees (3) ©University of Sheffieldcom2010

20
Examples treeSort [2,91,7,35,28] ⇒ [2,7,28,35,91] treeSort['a','r','k',' ','9','i'] ⇒ [‘ ’,’9’,’a’,’i’,’k’,’r’] treeSort[[4,1],[3,9,5],[3],[9,1,0]] ⇒ [[3],[3,9,5],[4,1],[9,1,0]] Sorting using BSTrees ©University of Sheffieldcom2010

21
The general definition of an algebraic type has the form: data TypeName a_1 a_2 … a_n = ConstructName_1 T_(1,1) T_(1,2)…T_(1,k1) |ConstructName_2 T_(2,1) T_(2,2)…T_(2,k2) … |ConstructName_m T_(m,1) T_(m,2)…T_(m,km) Algebraic data type ©University of Sheffieldcom2010

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google