abstract data types
play

Abstract Data Types Functional Programming and Reasoning Dr Hans - PowerPoint PPT Presentation

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 Modules 1 Abstract Data Types


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

  2. Outline Modules 1 Abstract Data Types 2 Specification 3 Data Structures 4 5 Closing Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 2 / 32

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

  4. Modules Outline Modules 1 Abstract Data Types 2 Specification 3 Data Structures 4 5 Closing Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 4 / 32

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

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

  7. Modules Sample module 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

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

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

  10. Modules Program versus module 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

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

  12. Modules Sample Main module 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

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

  14. Abstract Data Types Outline Modules 1 Abstract Data Types 2 Specification 3 Data Structures 4 5 Closing Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 14 / 32

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

  16. 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 Library (empty library) initial :: 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

  17. 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 Library (empty library) initial :: 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

  18. 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 Library (empty library) initial :: 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

  19. Abstract Data Types Abstract Datatype 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

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

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

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

  23. Abstract Data Types Advantages of ADT-s 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

  24. Specification Outline Modules 1 Abstract Data Types 2 Specification 3 Data Structures 4 5 Closing Dr Hans Georg Schaathun Abstract Data Types Spring 2010 – Week 4 22 / 32

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

  26. Specification Specification example Library catalogue 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

Recommend


More recommend