CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Abstract Data Types and Modules Lecture 11 – ADT and Modules, Spring 2008 1 CSE3302 Programming.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Type Systems and Object- Oriented Programming (III) John C. Mitchell Stanford University.
MT311 (Oct 2007) Java Application Development Object-Oriented Programming Languages Tutorial 8.
Data Structure & Abstract Data Type
Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
C++ Programming Languages
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1 Spring 2012.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
CPS 506 Comparative Programming Languages Abstract Data Type and Encapsulation.
Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Abstract data types & object-oriented paradigm. Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Object Oriented Programming
Abstract data types What does ‘ abstract ’ mean? From Latin: to ‘ pull out ’— the essentials –To defer or hide the details –Abstraction emphasizes essentials.
Chapter 11 Abstract Data Types and Encapsulation Concepts.
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
© Kenneth C. Louden, Chapter 9 – Abstract Data Types and Modules Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Data Abstraction and Modularity abstraction is the key principle to control the complexity electro-physics transitor microprocessor operating system library.
ADT data abstraction. Abstraction  representation of concepts by their relevant features only  programming has two major categories of abstraction process.
Programming Languages Third Edition Chapter 11 Abstract Data Types and Modules.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Cs776(Prasad)L112Modules1 Modules value : type : function :: structure : signature : functor.
Object Oriented Programming in Java Habib Rostami Lecture 7.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Templates 3 Templates and type parameters The basic idea templates is simple: we can make code depend on parameters, so that it can be used in different.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Abstract Data Types and Modules Lecture 12 – ADT and Modules, Fall CSE3302 Programming Languages,
CSE 3302 Programming Languages
Abstract Data Types and Encapsulation Concepts
ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:
11.1 The Concept of Abstraction
Abstract Data Types and Modules
slides created by Marty Stepp
Abstract Data Types (ADTs)
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Programming Languages and Paradigms
Dynamic allocation (continued)
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Abstract Data Types (ADTs)
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Data Structures & Programming
Presentation transcript:

CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Abstract Data Types and Modules Lecture 11 – ADT and Modules, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

Data Types Predefined Type constructors: build new data types How to provide “queue”? – What should be the data values? – What should be the operations? – How to implement (data representation, operations)? Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

What are inadequate here? The operations are not associated with the data type – You can use the operation on an invalid value. Users see all the details: direct access to date elements, implementations – Implementation dependent – Users can even mess up with things Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

What do we want? For basic types: – 4 bytes or 2 bytes, users don’t need to know. – Can only use predefined operations. Similarly, for the “Queue” data type: ? Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Abstract Data Type Encapsulation: all definitions of allowed operations for a data type in one place. Information Hiding: separation of implementation details from definitions. Hide the details. Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Algebraic Specification of ADT Syntactic specification (signature, interface): the name of the type, the prototype of the operations Semantic specification (axioms, implementation): guide for required properties in implementation mathematical properties of the operations They don’t specify: – data representation – implementation details Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Syntactic Specification type queue(element) imports boolean operations: createq: queue enqueue: queue  element  queue dequeue: queue  queue frontq: queue  element emptyq: queue  boolean imports: the definition queue needs boolean Parameterized data type (element) createq: not a function, or viewed as a function with no parameter Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Algebraic Specification variables: q: queue; x: element axioms: emptyq(createq) = true emptyq(enqueue(q,x)) = false frontq(createq) = error frontq(enqueue(q,x)) = if emptyq(q) then x else frontq(q) dequeue(createq) = error dequeue(enqueue(q,x)) = if emptyq(q) then q else enqueue(dequeue(q),x) error axiom (exceptions) Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, Stack type stack(element) imports boolean operations: createstk: stack push: stack  element  stack pop: stack  stack top: stack  element emptystk: stack  boolean variables: s: stack; x: element axioms: emptystk(createstk) = true emptystk(push(s,x)) = false top(createstk) = error top(push(s,x)) = x pop(createstk) = error pop(push(s,x)) = s

Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, Axioms How many axioms are sufficient for proving all necessary properties?

Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, Some Heuristics type stack(element) imports boolean operations: createstk: stack push: stack  element  stack pop: stack  stack top: stack  element emptystk: stack  boolean variables: s: stack; x: element axioms: emptystk(createstk) = true emptystk(push(s,x)) = false top(createstk) = error top(push(s,x)) = x pop(createstk) = error pop(push(s,x)) = s Constructor: createstk push Inspector: pop top emptystk 2 * 3 = 6 rules

Binary Search Tree type BST(element) imports boolean, int operations: createbst: BST emptybst: BST  boolean insert: BST  element  BST delete: BST  element  BST getRoot: BST  element getHeight: BST  int max: BST  element search: BST  element  boolean variables: t: bst; x: element axioms: emptystk(createbst) = true … Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Other Examples of ADT Stack Queue Tree Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, Set Map Vector List Priority Queue Graph …

ADT Mechanisms Specific ADT mechanisms – ML abstype General module mechanism : not just about a single data type and its operations – Separate compilation and name control: C, C++, Java – Ada, ML Class in OO languages Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, ML Abstype abstype 'element Queue = Q of 'element list with val createq = Q []; fun enqueue(Q lis, elem)= [elem]); fun dequeue(Q lis) = Q(tl lis); fun frontq(Q lis) = hd lis; fun emptyq(Q []) = true | emptyq(Q(h::t)) = false; end; type 'a Queue val createq = - : 'a Queue val enqueue = fn : 'a Queue * 'a -> 'a Queue val dequeue = fn : 'a Queue -> 'a Queue val frontq = fn : 'a Queue -> 'a val emptyq = fn : 'a Queue -> bool - val q = enqueue(createq,3); Val q = - : int Queue

Modules Module: A program unit with a public interface and a private implementation; all services that are available from a module are described in its public interface and are exported to other modules, and all services that are needed by a module must be imported from other modules. In addition to ADT, module supports structuring of large programs: Separate compilation and name control Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

C: Separate Compilation queue.h : header file #ifdef QUEUE_H #define QUEUE_H struct Queuerep; typedef struct Queuerep * Queue; Queue createq(void); Queue enqueue(Queue q, void* elem); void* frontq(Queue q); Queue dequeue(Queue q); int emptyq(Queue q); #endif Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, Incomplete type: Separate implementation Simulate Parameteric polymorphism

C: Separate Compilation queue.c : queue implementation #include "queue.h“ struct Queuerep { void* data; Queue next; }; Queue createq(void) { return 0; } void* frontq(Queue q) {return q->next->data; }... Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

C: Separate Compilation q_user.C : client code #include "queue.h" int *x = malloc(sizeof(int)); int *y = malloc(sizeof(int)); int *z; *x =2; *y =3; Queue q = createq(); q = enqueue(q,x); q = enqueue(q,y); q = dequeue(q); z = (int*) frontq(q); Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

C: Separate Compilation Not real ADT – casting, allocation: for parametric polymorphism – header file directly incorporated into q_user.c: definition / usage consistent – data not protected: user may manipulate the type value in arbitrary ways – The language itself doesn’t help in tracking changes and managing compilation/linking: thus tools like make Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, queue.h q_user.cqueue.c

C++: Namespaces queue.h: #ifdef QUEUE_H #define QUEUE_H namespace MyQueue { struct Queuerep; typedef struct Queuerep * Queue; Queue createq(void);... } #endif queue.c: #include "queue.h“ struct MyQueue::Queuerep { void* data; Queue next; };... Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

C++: Namespaces q_user.cpp: #include "queue.h“ using std::endl; using namespace MyQueue; main(){ int *x = malloc(sizeof(int)); int *y = malloc(sizeof(int)); int *z; *x =2; *y =3; Queue q = MyQueue::createq(); q = enqueue(q,x); q = enqueue(q,y); q = dequeue(q); z = (int*) frontq(q); … } Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Java: Packages Queue.java: package queues.myqueue; … PQueue.java: package queues.myqueue; … Q_user.java: import queues.myqueue.Queue; import queues.myqueue.*; queues.myqueue.Queue; Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, directory: queues/myqueue class files: Queue.class, PQueue.class queues/myqueue in CLASSPATH

Example Package java.util summary.html Interface Collection tml Class PriorityQueue ue.html Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Ada: Packages Package Specification generic type T is private; package Queues is type Queue is private; function createq return Queue; function enqueue(q:Queue;elem:T) return Queue; function frontq(q:Queue) return T; function dequeue(q:Queue) return Queue; function emptyq(q:Queue) return Boolean; private type Queuerep; type Queue is access Queuerep; end Queues; Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, Prevents direct access parameterized package: Parameteric polymorphism Pointers: Hide implementation details. Just making Queue incomplete won’t work.

Ada: Packages Package Body package body Queues is type Queuerep is record data: T; next: Queue; end record; function createq return Queue is begin return null; end createq;... end Queues; Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He,

Ada: Packages User Code with Queues; procedure Quser is package IntQueues is new Queues(Integer); use IntQueues; package FloatQueues is new Queues(Float); use FloatQueues; iq: IntQueues.Queue := createq; fq: FloatQueues.Queue := createq; begin iq := enqueue(iq,3); fq := enqueue(fq,3.5); end Quser; Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, Import packages: Specify dependency Parametric polymorphism Overloading

ML: Modules Signature (interface) Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, singature QUEUE = sig type 'a Queue val createq: 'a Queue val enqueue: 'a Queue * 'a -> 'a Queue val dequeue: 'a Queue -> 'a Queue val frontq: 'a Queue -> 'a val emptyq: 'a Queue -> bool end; Parametric polymorphism

ML: Modules Structure (implementation) Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, structure Queue1: QUEUE = struct datatype ‘a Queue = Q of ‘a list val createq = Q []; fun enqueue(Q lis, elem)= [elem]); fun dequeue(Q lis) = Q(tl lis); fun frontq(Q lis) = hd lis; fun emptyq(Q []) = true | emptyq(Q(h::t))=false; end;

ML: Modules Use the queue Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, val q = Queue1.enqueue(Queue1.createq,3); val q = Q [3] : int Queue1.Queue Queue1.frontq q; val it = 3 : int - val q1 = Queue1.dequeue q; val q1 = Q [] : int Queue1.Queue - Queue1.emptyq q1; val it = true : bool Must refer to implementation

ML: Modules -open Queue1; Lecture 11 – ADT and Modules, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, val q = enqueue(createq,3); val q = Q [3] : int Queue - frontq q; val it = 3 : int - val q1 = dequeue q; val q1 = Q [] : int Queue - emptyq q1; val it = true : bool Without qualification