Abstract Data Types Functional Programming and Reasoning Dr Hans Georg Schaathun University of Surrey Spring 2010 Week 4 Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 1 / 32
Outline 1 Modules 2 Abstract Data Types 3 Specification 4 Data Structures 5 Closing Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 2 / 32
This session After this session, you should be able to use ADT-s in software design be able to implement simple ADT-s in haskell based on a specification Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 3 / 32
Outline Modules 1 Modules 2 Abstract Data Types 3 Specification 4 Data Structures 5 Closing Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 4 / 32
Modules Modularisation Divide large problems into smaller ones solve each subproblem separately Functions is one way to split large programs But large programs, with many functions, would still be hard to structure Modules allow you to split programs into multiple files Each file is one module Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 5 / 32
Modules Naming modules module Ant where It is good practice to give your modules a name The filename should match the module name the Ant module resides in Ant.hs This is necessary to import the module into another Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 6 / 32
Sample module Modules module Ant where import Insect data Ants = Queen Int Float Worker Float anteater x = x - 1 Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 7 / 32
Modules Importing modules import Insect Whenever you need functions from a module you import it Access to all functions in the module Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 8 / 32
Modules Import Controls Limited imports import Ant ( Ants(..) ) Excluding functions import Ant hiding ( anteater ) import Prelude hiding ( words ) Fully qualified names import qualified Ant Ant.anteater Local name import Insect as Ant importing Ant, but calling it Insect All of these techniques may be important to avoid name clashes but you don t need them for now Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 9 / 32
Program versus module Modules We have not yet written a program only modules The hugs command line allows us to test modules we can evaluate any expression testing any function in the module A program needs to know what to evaluate that is an object main in a module Main Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 10 / 32
Modules The Main module A program needs a top-level module Main module Main where Defining an expression main this expression is evaluated when the program is run The main expression must be an IO type we will discuss how to do this later (Week 9?) Such a program can be run as a script runhugs main.hs or compiled using the Glasgow Haskell Compiler Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 11 / 32
Sample Main module Modules module Main where data Shape = Circle Float Rectangle Float Float area :: Shape -> Float area (Circle r) = pi*r^2 area (Rectangle x y) = x*y s :: Shape s = Circle 2.3 main = print (area s) Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 12 / 32
Modules Software design Which modules? Each module should have a clear purpose clear scope documentation Main contains the main routine only definitions particular to this programme separate modules for reusable functions Often, one module for each data type including functions on the data type Think about how you split the program into modules Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 13 / 32
Outline Abstract Data Types 1 Modules 2 Abstract Data Types 3 Specification 4 Data Structures 5 Closing Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 14 / 32
Abstract Data Types Encapsulation A module often contains many definitions only a few are docuemented intended for use in importing modules many auxiliary definitions and features for internal use This causes problems information overload for the programmer using the module name clashes with other modules Good practice: hide all unnecessary definitions Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 15 / 32
Abstract Data Types Example Inventory of a Store A data type to hold a library catalogue Possibly type Library = [(Book,Int)] using your Book type from the exercises storing the number of copies Do you need to know how the type is implemented? No. You need to know the functions working on the type. getstock :: Library -> Book -> Int initial :: Library (empty library) addbook :: Library -> Book -> Library All functionality available via the functions You do not need to know the implementation data Library = Lib [ (Book,Int) ], or data Library = Lib (Book -> Int) Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 16 / 32
Abstract Data Types Example Inventory of a Store A data type to hold a library catalogue Possibly type Library = [(Book,Int)] using your Book type from the exercises storing the number of copies Do you need to know how the type is implemented? No. You need to know the functions working on the type. getstock :: Library -> Book -> Int initial :: Library (empty library) addbook :: Library -> Book -> Library All functionality available via the functions You do not need to know the implementation data Library = Lib [ (Book,Int) ], or data Library = Lib (Book -> Int) Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 16 / 32
Abstract Data Types Example Inventory of a Store A data type to hold a library catalogue Possibly type Library = [(Book,Int)] using your Book type from the exercises storing the number of copies Do you need to know how the type is implemented? No. You need to know the functions working on the type. getstock :: Library -> Book -> Int initial :: Library (empty library) addbook :: Library -> Book -> Library All functionality available via the functions You do not need to know the implementation data Library = Lib [ (Book,Int) ], or data Library = Lib (Book -> Int) Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 16 / 32
Abstract Datatype Abstract Data Types An abstract data type (ADT) is a type which can only be accessed or manipulated using a limited set of clearly defined functions. New functions must be defined in terms of the primitive functions No direct access to the data many things will be impossible This prevents abuse of the data type and thus prevents many errors Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 17 / 32
Abstract Data Types Defining an ADT module Library( Library, initial, getstock, addbook ) where We list explicitely which names to export The type Library is exported its constructors are not Exported functions (initial, getstock, addbook) gives access to the type This Haskell s ADT mechanism Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 18 / 32
Abstract Data Types Export Controls Export Controls can be used in different ways ADT-s are special in that the constructors are hidden module Library( Library(..), initial, getstock, addbook ) where (..) means that the constructors should be exported as well this is not an ADT You can choose (almost) freely what to export Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 19 / 32
Abstract Data Types Classes and Instances Defining an ADT, you will often want to overload standard functions instance Show Library where Such instances are always exported Inherent properties of the data type cannot be hidden Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 20 / 32
Advantages of ADT-s Abstract Data Types Interchangeable different implementations of the same ADT can be interchanged... as long as the specification is the same Delegate the implementation if you agree on the interface (functions) you know it will work with other modules Updates with backwards compatibility add features while continuing to support old ones Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 21 / 32
Outline Specification 1 Modules 2 Abstract Data Types 3 Specification 4 Data Structures 5 Closing Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 22 / 32
Specification Specification A specification is a careful description of a program or module formal specification languages (such as Z) natural language It defines exactly how the module is intended to work exactly which functions are supported Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 23 / 32
Specification example Library catalogue Specification A type Library storing records of books number of copies per book Required functions addbook :: Library -> Book -> Library add a new book to the library if the book exists already, increase number of copies initial :: Library return a Library with no books getstock :: Library -> Book -> Int given a book return Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 24 / 32
Specification Why use specification Software projects are big communication and collaboration essential Agreeing on specifications allow independent implementation One person can implement the ADT Others can write modules using the ADT It is important to be able to program strictly to specifications otherwise the modules won t fit together Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 25 / 32
Outline Data Structures 1 Modules 2 Abstract Data Types 3 Specification 4 Data Structures 5 Closing Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 26 / 32
Data Structures Data structures ADT-s are common for defining Data Structures e.g. lists, queues, stacks, heaps, etc. Holding large quantities of data Different functions for data processing and access sorting searching last/first element Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 27 / 32
Data Structures Example: Queue First in, first out Insert elements at the end of the queue Retrieve elements from the head of the queue Usually no searching ADT functions emptyq (create empty queue) isemptyq (is the queue empty?) addq (add element) remq (remove/retrieve element) Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 28 / 32
Data Structures Different ways to define data types data NewType = Cons1 T11 T12... Cons2 T21 T22...... algebraic data type as discussed the most flexible construct newtype NewType = Cons1 T1 special case with only one constructor and one argument more efficient (internally) than data otherwise equivalent type NewType =... alias for another data type often tupples no type checking between the alias and the original type Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 29 / 32
Queue Implementation Data Structures module Queue(Queue,emptyQ,isEmptyQ,addQ,remQ) where newtype Queue a = Qu [a] -- parameterised emptyq - Qu [] isemptyq (Qu []) = True isemptyq _ = False addq x (Qu xs) = Qu ( xs++[x] ) remq Qu ([]) = error "Cannot remove from empty queue" remq Qu (x:xs) = (x,xs) Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 30 / 32
Outline Closing 1 Modules 2 Abstract Data Types 3 Specification 4 Data Structures 5 Closing Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 31 / 32
Closing Summary Module allows structuring your program An ADT is a module with a carefully controlled interface The coursework will require an ADT you are given a front-end module have to implement the ADT strictly to specification the use of ADT-s ensure compatibility Dr Hans Georg Schaathun Abstract Data Types Spring 2010 Week 4 32 / 32