Lecture 12: Abstract data types



Similar documents
Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

Topic VII. Data abstraction and modularity SML Modules a. References: Chapter 7 of ML for the working programmer (2ND

Fun with Phantom Types

Practical Generic Programming with OCaml

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

Towards Interface Types for Haskell

Rigorous Software Development CSCI-GA

Tutorial on Writing Modular Programs in Scala

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Glossary of Object Oriented Terms

The countdown problem

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

DATA STRUCTURE - STACK

Software Engineering Techniques

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

This session. Abstract Data Types. Modularisation. Naming modules. Functional Programming and Reasoning

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Type Classes with Functional Dependencies

Stacks. Linear data structures

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Data Structures and Algorithms Lists

Specification-driven Unit Test Generation for Java Generic Classes

Chapter 8: Bags and Sets

An Informal Formal Method for Systematic JUnit Test Case Generation

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Abstraction and Abstract Data Types

Object Oriented Software Design

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Abstract Data Types in Event-B An Application of Generic Instantiation

Object-Oriented Databases

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Lecture Data Types and Types of a Language

Algorithms and Data Structures

7.1 Our Current Model

Feedback to Some standard container ADTs

ML for the Working Programmer

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

Adding GADTs to OCaml the direct approach

Stacks. Data Structures and Data Types. Collections

Chapter 7: Functional Programming Languages

Functional Programming

7. Object-Oriented Programming

C++ INTERVIEW QUESTIONS

Regular Expressions and Automata using Haskell

Detecting Pattern-Match Failures in Haskell. Neil Mitchell and Colin Runciman York University

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

The C Programming Language course syllabus associate level

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Timber: A Programming Language for Real-Time Embedded Systems

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Non-Parametric Parametricity

Anatomy of Programming Languages. William R. Cook

Part 1 Foundations of object orientation

Mail User Agent Project

Lecture 11: Tail Recursion; Continuations

CS 111 Classes I 1. Software Organization View to this point:

Special Directions for this Test

Classes and Objects. Agenda. Quiz 7/1/2008. The Background of the Object-Oriented Approach. Class. Object. Package and import

CmpSci 187: Programming with Data Structures Spring 2015

Cours de C++ Utilisations des conteneurs

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Algorithms and Data Structures Written Exam Proposed SOLUTION

Programming Fundamentals. Lesson 20 Sections

Software Engineering

MLS module Sealing and Functor Design

CMSC 132: Object-Oriented Programming II. Design Patterns I. Department of Computer Science University of Maryland, College Park

D06 PROGRAMMING with JAVA

Abstract Data Types. Chapter 2

Collaboration policy. Where to get help (but no code in ) Office hours Lab TAs in Friend 008/009 Bounce ideas (but not code) off classmates

Infinite Pretty-printing in exene

Lecture J - Exceptions

Code Refactoring and Defects

Testing and Tracing Lazy Functional Programs using QuickCheck and Hat

ML-Flex Implementation Notes

Programming and Reasoning with Algebraic Effects and Dependent Types

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

HW3: Programming with stacks

Lecture 1: Introduction

GENERIC and GIMPLE: A New Tree Representation for Entire Functions

PROGRAM-ing Finger Trees in COQ

2/9/2010. Standard Approach to Distribution. Remote Procedure Calls (RPC) Example: Music Jukebox in the Cloud

Tool-Assisted Unit-Test Generation and Selection Based on Operational Abstractions

Formal System Modelling Using Abstract Data Types in Event-B

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

C++ Support for Abstract Data Types

CSc 372. Comparative Programming Languages. 21 : Haskell Accumulative Recursion. Department of Computer Science University of Arizona

Integration of Application Business Logic and Business Rules with DSL and AOP

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Data Structures. Level 6 C Module Descriptor

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

SOLUTION Trial Test Grammar & Parsing Deficiency Course for the Master in Software Technology Programme Utrecht University

Chapter 3: Restricted Structures Page 1

How To Write A Test Engine For A Microsoft Microsoft Web Browser (Php) For A Web Browser For A Non-Procedural Reason)

Java (12 Weeks) Introduction to Java Programming Language

Classes: Relationships Among Objects. Atul Prakash Background readings: Chapters 8-11 (Downey)

Contents. 9-1 Copyright (c) N. Afshartous

Transcription:

Lecture 12: Abstract data types Algebras Abstract data types Encapsulation and information hiding Parameterization Algebras Definition Which kinds of data do we want to discuss? What can we do with it? An algebra A is a tuple A = (D 1,,D m, f 1,,f n ) where every D i is a set (called domain) and every f j is a function f j between domains (called operation), i.e f j :X 1 X k Y where X 1,,X k,y {D 1,,D m. 1

Algebras vs programs Unfortunately programs have much higher complexity. Why? A program does not only describe the abstract functionality but first of all how it is realized (= implemented), often in detail. One problem: Other parts of the program may exploit implementation details that are not part of the functionality. Correctness depends on arbitrary random assumptions Every little detail may influence other parts of the program Abstract data types Abstract data types (ADT:s) is an attempt to apply algebraic concepts to programming languages: Characteristics of data and operations are described on an abstract level; nothing about implementation Other parts of the program cannot use implementation details Other units can only use the specified abstract (invariable) properties; not implementation-related details (which may change) This is an example of data abstraction 2

Encapsulation and information hiding To reach these goals an ADT must fulfil two conditions: Encapsulation The definition of an ADT specifies its name and its operations in one single syntactic unit (not scattered in several units). Other program units may refer to this definition (only) to use the ADT. Information hiding The internal representation of data is not accessible for other program units. Example of a formal specification Algebraic specifikation of ADT stack ADT stack is elem + boolean + operations: empty: stack push: stack x elem stack, pop: stack stack, top: stack elem, isempty: stack boolean equations: isempty(empty) = true, isempty(push(s, e)) = false, pop(push(s, e)) = s, top(push(s, e)) = e, pop(empty) = error, top(empty) = error 3

Formal specification (2) What operations? Constructors ( returns an object of the actual type) ( true constructors, modifiers, destructors) Inspectors (examines the inner structure) (selectors, predicates) Comparators (compares equality, if not in the language) What equations? Rule of thumb: One for each combination constructor - inspector One for each combination constructor - destructor Implementation Implementation of an ADT: Interface - definition of types and operations on types, available for the clients Internal representation of the objects, implementation of operations. Invisible for the clients For maximal flexibility: Need to be able to separate definition (interface) and implementation Rich & restrictive interface, completeness (cf DoA) Different languages give more or less support Only ADT:s, or general modules? Information hiding? Separation of definition & specification? How can we connect modules? Freedom fr block struct.? Parameterized modules? 4

Example: stack in Java import java.io.*; class stack { private Vector s = new Vector(); public void push(elem e) { s.addelement(e); public void pop() { s.removeelementat(s.size() - 1); public elem top() { return s.lastelement(); public boolean isempty() { return s.size() = 0; Parameterized ADT:s If an ADT builds upon other, simpler ADT:s we often wish to be able to create different instances using parameters stack(integer), stack(tree), stack(stack(tree)), sortedlist(integer), sortedlist(string), N.B: These parameters are data types! Problem: In some languages (e.g old versions of Java) it is not possible to explicitly create different instances We cannot distinguish between sortedlist(integer) and sortedlist(string) 5

exception EmptyStack Example: stack in ML abstype a stack = Stack of a list with val empty = Stack [] fun push (Stack s, e) = Stack (e::s) fun pop (Stack []) = raise EmptyStack pop (Stack (_::xs)) = Stack xs fun top (Stack []) = raise EmptyStack top (Stack (x::_)) = x fun isempty (Stack s) = s=[] val stack1 = push (empty, 1); > val stack1 = -:int stack val stack2 = push(push(empty,(1, Otto )),(2, Olga )); > val stack2 = -:(int*string)stack Example: MyStack in HASKELL module MyStack ( MyStack, empty, push, pop, top, isempty, smallest ) where newtype (Ord a) => MyStack a = StackOf [a] empty :: (Ord a) => MyStack a empty = StackOf [] push (StackOf xs) x = StackOf (x : xs) pop (StackOf (x:xs)) = StackOf xs top (StackOf (x:xs)) = x isempty (StackOf xs) = xs == [] smallest (StackOf (x:xs)) = getsmallest x xs getsmallest x [] = x getsmallest x (y:ys) x < y = getsmallest x ys otherwise = getsmallest y ys Not visible! visible The parameter must have an order relation `<` istack :: MyStack Integer sstack :: MyStack String 6

Modules in MLan example of encapsulation ML has very powerful, general (and therefore difficult to understand?) modules: > Structures: package with concrete definitions / implementations > Signatures: types of a structure > Functors: parameterized structures Example: stack in ML again signature StackItem = sig type item val isequal: item -> item -> bool signature Stack = sig type item type stack exception EmptyStack val empty: stack val push: (stack * item) -> stack....and so on functor MkStack(Item:StackItem):> Stack = struct type myitem = Item.item type stack = myitem list exception EmptyStack val empty = [] fun push (s,e) = e::s...etc 7

Example: stack in ML again (2) structure Pair:StackItem = struct type item = int * string fun isequal (i1,s1) (i1,s2) = i1=i2 andalso s1=s2 structure PairStack = MkStack(Pair); val mystack = PairStack.push (PairStack.empty, (3, Haskell )); 8