Abstract Data Types. Functional Programming and Reasoning. Dr Hans Georg Schaathun. University of Surrey. Spring 2010 Week 4

Similar documents
Composite Datatypes Lists and Tuples

Software Design and Analysis for Engineers

Functional Programming in Haskell Part I : Basics

This session. Recursion. Planning the development. Software development. COM1022 Functional Programming and Reasoning

Shell CSCE 314 TAMU. Haskell Functions

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Getting started with Hugs on Linux

Getting started with Hugs on Linux

DATA STRUCTURES CHAPTER 1

A Brief Haskell and GHC Refresher

CSCE 314 Programming Languages

OBJECT ORİENTATİON ENCAPSULATİON

World Champion chess player Garry Kasparov was defeated by IBM s Deep Blue chess-playing computer in a sixgame match in May, The match received

An introduction to functional programming. July 23, 2010

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

Introduction to Haskell

Advanced features of Functional Programming (Haskell)

A A B U n i v e r s i t y

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

Logical Methods in... using Haskell Getting Started

THE CONCEPTION AND APPLICATION OF PFL : A PROCESS FUNCTIONAL PROGRAMMING LANGUAGE

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Data Structures and Algorithms. Chapter 1

COMP3141 Assignment 2

Algebraic Types. Chapter 14 of Thompson

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Lecture 7: Data Abstractions

CSCE 314 Programming Languages

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Programming Language Concepts, CS2104 Lecture 7

Dot and Scope Resolution Operator

Shell CSCE 314 TAMU. Higher Order Functions

Programming Paradigms Written Exam (6 CPs)

Programming for Engineers in Python

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

CSE 3302 Programming Languages Lecture 8: Functional Programming

C++ (classes) Hwansoo Han

Programming in Haskell Aug-Nov 2015

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Object Oriented Programming

Overloading, Type Classes, and Algebraic Datatypes

CE221 Programming in C++ Part 1 Introduction

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Haskell through HUGS THE BASICS

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

UMBC CMSC 331 Final Exam

Absolute C++ Walter Savitch

Introduction to Programming Using Java (98-388)

Introduction. chapter Functions

CS 251 Intermediate Programming Methods and More

DATA TYPES. CS 403: Types and Classes DATA TYPES (CONT D)

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Chapter 4 Defining Classes I

Unit 1: Preliminaries Part 1: Course Introduction, Abstraction, and ADT s

Data Structures. Datatype. Data structure. Today: Two examples. A model of something that we want to represent in our program

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

What is an algorithm?

CSCI Compiler Design

Lecture 20: templates

Programming Paradigms

CS 457/557: Functional Languages

Functional Programming Language Haskell

Advances in Programming Languages

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

GADTs. GADTs in Haskell

Types and Type Inference

Programming Paradigms Functional languages

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Haskell Overview III (3A) Young Won Lim 10/4/16

Review: C++ Basic Concepts. Dr. Yingwu Zhu

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

Introduction to Object- Oriented Programming

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Arrays. myints = new int[15];

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Programming Languages Fall 2013

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

COEN244: Class & function templates

Programming II (CS300)

Functional programming Primer I

An introduction introduction to functional functional programming programming using usin Haskell

Object Orientated Analysis and Design. Benjamin Kenwright

Programming II (CS300)

Topic 7: Algebraic Data Types

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

CS 11 Haskell track: lecture 1

2. The object-oriented paradigm!

Programming in Omega Part 1. Tim Sheard Portland State University

CSI33 Data Structures

Chapter 6 Abstract Datatypes

Week 6: Data. Let's focus first on application domain data.

2. Introducing Classes

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

Transcription:

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