PPL Unit-IV
PPL Unit-IV
Abstract Types
Prepared By:
Mrs.K.Pranathi
Asst.Professor
Syllabus
Abstract types: Data Abstraction and encapsulation.
Introductions to data abstraction, design issue Language
examples, C++ Parameterized ADT, Object oriented
programming in small talk.
C++, Java,C#, Ada 95 Concurrency: Subprogram level
Concurrency.Semaphores,Monitors, Message Passing . Java
threads,C# threads.
Exception Handling: exceptions,exception propogation.
Exception Handler in Ada.C++ and Java.Logical programming
Language: Introduction and overview of logic programming,
Basic elements of prolog, application of logical programming.
The Concept of Abstraction
• An abstraction is a view or representation of an
entity that includes only the most significant
attributes
• The concept of abstraction is fundamental in
programming (and computer science)
• Nearly all programming languages support
process abstraction with subprograms
• Nearly all programming languages designed
since 1980 support data abstraction
Introduction to Data Abstraction
• An abstract data type is a user-defined data type
that satisfies the following two conditions: –
• The representation of, and operations on, objects
of the type are defined in a single syntactic unit.
• The representation of objects of the type is
hidden from the program units that use these
objects, so the only operations possible are those
provided in the type's definition.
Advantages of Data Abstraction
• Advantage of the first condition – Program
organization, modifiability (everything associated
with a data structure is together), and separate
compilation.
• Advantage the second condition – Reliability--by
hiding the data representations, user code cannot
directly access objects of the type or depend on
the representation, allowing the representation to
be changed without affecting user code.
Language Requirements for ADTs
• A syntactic unit in which to encapsulate the
type definition
• A method of making type names and
subprogram headers visible to clients, while
hiding actual definitions
• Some primitive operations must be built into
the language processor
Design Issues
• What is the form of the container for the
interface to the abstract data type?
• Can abstract types be parameterized?
• What access controls are provided?
Language Examples: Ada
• The encapsulation construct is called a package –
Specification package (the interface) – Body
package (implementation of the entities named in
the specification)
• Information Hiding – The specification package has
two parts, public and private – The name of the
abstract type appears in the public part of the
specification package.
• This part may also include representations of
unhidden types
• The representation of the abstract type
appears in a part of the specification called
the private part.
• Private types have built-in operations for
assignment and comparison
• Limited private types have NO built-in
operations
Language Examples:Ada (contd..)
• Reasons for the public/private spec package:
• The compiler must be able to see the
representation after seeing only the spec
package (it cannot see the private part)
• Clients must see the type name, but not the
representation (they also cannot see the
private part)
Language Examples:Ada (contd..)
• Having part of the implementation details (the
representation) in the spec package and part
(the method bodies) in the body package is not
good
• One solution: make all ADTs pointers Problems
with this:
• Difficulties with pointers
• Object comparisons
• Control of object allocation is lost
An Example in Ada
package Stack_Pack is
type stack_type is limited private; max_size:
constant := 100;
function empty(stk: in stack_type) return
Boolean;
procedure push(stk: in out stack_type; elem:in Integer);
procedure pop(stk: in out stack_type);
function top(stk: in stack_type) return Integer;
Stack (100);
Parameterized ADTs in C++ (continued)
The stack element type can be parameterized by making the class a
template class
template <class Type>
class Stack { private:
Type *stackPtr; const int maxLen; int topPtr;
public:
Stack() {
stackPtr = new Type[100]; maxLen = 99;
topPtr = -1;
}
…
}
Parameterized Classes InJava 5.0
• Generic parameters must be classes.
• Most common generic types are the
collection types, such as LinkedList and
ArrayList
• Eliminate the need to cast objects that are
removed
• Eliminate the problem of having multiple
types in a structure
Parameterized Classes in C# 2005
• Similar to those of Java 5.0
• Elements of parameterized structures can be
accessed through indexing
Encapsulation Concepts
• Large programs have two special needs:
– Some means of organization, other than simply
division into subprograms
– Some means of partial compilation (compilation
units that are smaller than the whole program)
• Obvious solution: a grouping of
subprograms that are logically related into
a unit that can be separately compiled
(compilation units)
• Such collections are called encapsulation
Nested Subprograms
• Organizing programs by nesting subprogram definitions
inside the logically larger subprograms that use them
• Nested subprograms are supported in Ada, Fortran
95, Python, and Ruby
Encapsulation in C
• Files containing one or more subprograms can be
independently compiled
• The interface is placed in a header file
• Problem: the linker does not check types
between a header and associated implementation
• #include preprocessor specification – used to
include header files in applications
Encapsulation in C++
• Can define header and code files, similar to those of
C
• Or, classes can be used for encapsulation
– The class is used as the interface (prototypes)
– The member definitions are defined in a
separate file
• Friends provide a way to grant access to private
members of a class
Ada Packages
• Ada specification packages can include any
number of data and subprogram declarations
• Ada packages can be compiled separately
• A package’s specification and body parts can be
compiled separately
C# Assemblies
• A collection of files that appear to be a single
dynamic link library or executable
• Each file contains a module that can be
separately compiled
• A DLL is a collection of classes and methods that
are individually linked to an executing program
• C# has an access modifier called internal; an internal
member of a class is visible to all classes in the
assembly in which it appears.
Naming Encapsulations
• Large programs define many global names; need a
way to divide into logical groupings
• A naming encapsulation is used to create a new
scope for names
• C++ Namespaces
– Can place each library in its own namespace and qualify
names used outside with the namespace
– C# also includes namespaces
Naming Encapsulations(contd..)
• Java Packages
– Packages can contain more than one class
definition; classes in a package are partial friends
– Clients of a package can use fully qualified name
or use the import declaration
• Ada Packages
– Packages are defined in hierarchies which
correspond to file hierarchies
– Visibility from a program unit is gained with the
“with" clause
Naming Encapsulations(contd..)
• Ruby classes are name encapsulations, but Ruby also has
modules
• Typically encapsulate collections of constants and methods
• Modules cannot be instantiated or subclassed, and they
cannot define variables
• Methods defined in a module must include the
module’s name
• Access to the contents of a module is requested with the
require method
-
Summary
• The concept of ADTs and their use in program
design was a milestone in the development of
languages
• Two primary features of ADTs are the packaging of data with
their associated operations and information hiding
• Ada provides packages that simulate ADTs
• C++ data abstraction is provided by classes
• Java’s data abstraction is similar to C++
• Ada, C++, Java 5.0, and C# 2005 support
parameterized ADTs
• C++, C#, Java, Ada, and Ruby provide naming
encapsulations
Object Oriented Programming in
Smalltalk: Language Overview
• Smalltalk was the first programming language that fully
supported object-oriented programming.
• The Smalltalk programming is based upon objects from
integer constants to large complex computer software
systems.
• All computing in Smalltalk is done by sending a
message to an object to invoke one of its methods.
• A reply to a message is an object , which either returns
the requested information or simply notifies the sender
that the requested processing has been completed.
Language Overview(contd…)
• The basic difference between a message and a sub-
program call is : A message is sent to a data object,
specifically to one of the methods defined for the
object.
• The called method is then executed often modifying
the data of the object to which the message was sent.
• A subprogram call is a message to the code of a
subprogram.
• In smalltalk object abstractions are classes, which are
very similar to the classes of SIMULA 67,C++ and java.
Example Program in Smalltalk
• The following is a class definition,
instantiations of which can draw equilateral
polygons of any number of sides
class name Polygon
superclass Object
instance variable names ourPen
numSides
sideLength
“Class methods”
“create an instance”
new
^super new getPen
“Get a pen for drawing polygons”
getPen
ourPen<-Pen new defaultNib:2
“Instance methods”
“Draw a polygon”
draw
numSides timesRepeat: [ourPen go: sideLength; turn:360 //numSides]
“Set length of sides”
Length: len
sideLength<- len
“Set number of sides”
sides: num
numSides<- num
OOP in C++
• C++ has both functions and methods, hence it
supports both procedural and object oriented
programming.
• Operators in C++ can be overloaded, means the
user can create operators for existing operators or
user-defined types.
• C++ methods can also be overloaded , means, the
user can define more than one method with the
same name provided either the numbers or types
of their parameters are different.
OOP in C++(contd..)
• Dynamic binding in C++ is provided by virtual
methods.
• These methods define type dependent operations,
using overloaded methods, within a collection of
classes that are related through inheritance.
• A pointer to an object of class A can also point to
objects of classes that have class A as an ancestor.
When this pointer points to an overloaded virtual
method, the method of the current type is chosen
dynamically.
OOP in C++(contd..)
• Both methods and classes can be templated,
which means that they can be parametirized.
• For example, a method can be written as a
template method to allow it to have versions
for a variety of parameter types.
• C++ also supports multiple inheritance.
OOP in C++(contd..)
• C++ remains a widely used language because of the
availability of good and inexpensive compilers.
• One more reason is that it is almost completely
backward compatible with C meaning that c programs
can be compiled as c++ programs with few changes.
• But C++ is very large and complex language , it suffers
the drawbacks similar to those of PL/I.
• It inherited most of the insecurities of C, which makes
it less safe than languages such as ADA and Java.
OOP in java (The Design Process)
• The design process of java started off when there is
a need to develop software for consumer electronic
devices.
• As neither c nor c++ provided the necessary level of
reliability, java was developed.
• Although C was small, it did not provide the support
for object oriented programming.
• C++ supported object-oriented but it was very large
and complex because it also provided support for
procedure oriented programming.
OOPS in Java(Contd..)
• Java is based on c++ but it was specifically designed to
be smaller, simpler and more reliable.
• Like C++, Java has both classes and primitive types.
• Java arrays are instances of a pre-defined class,
whereas in C++ they are not.
• Java does not have pointers but its reference types
provide some of the capabilities of pointers.
• Java has a primitive Boolean type named boolean used
mainly for control expressions of its control statements
such as if and while.
OOPS in Java(Contd..)
• A significant difference between Java and many of its
predecessors that support object oriented programming
including C++, is that it is not possible to write stand-alone
sub-programs in Java.
• All Java sub-programs are methods and are defined in the
class. Furthermore methods can be called through a class or
object only.
• Another important difference between C++ and Java is that C+
+ supports multiple inheritance directly in its class definitions .
• Among the C++ constructs that were not copied into Java are
structs and unions.
Concurrency
• Concurrency in software execution can occur at four different
levels:
-Instruction level(executing two or more machine instructions
simultaneously)
-Statement level(executing two or more high-level language
statements simultaneously)
-Unit level(executing two or more sub-program units
simultaneously)
-Program level(executing two or more programs simultaneously).
Because there are no language issues in instruction- and program-
level concurrency, they are not addressed here
The Evolution of Multiprocessor Architectures
Usually thrown by
Errors thrown by the JVM JVM when a user
Errors never thrown by user programs program causes
and should never be handled there an error
Exception Handling in Java: Exception
Handlers
• A try construct includes a compound statement
called the try clause and a list of exception handlers:
try {
//** Code that is expected to raise an exception }
catch (formal parameter) {
//* A handler body
}
…
catch (formal parameter) {
//** A handler body
}
Exception Handling in Java: Binding
Exceptions to Handlers
• An exception is thrown using the throw statement.
E.g.: throw new MyException (“a message to specify the
location of the error”)
• Binding: If an exception is thrown in the compound
statement of a try construct, it is bound to the first
handler (catch function) immediately following the try
clause whose parameter is the same class as the
thrown object, or an ancestor of it. If a matching
handler is found, the throw is bound to it and it is
executed.
Exception Handling in Java: The
finally clause
• Sometimes, a process must be executed regardless of
whether an exception is raised or not and handled or
not.
• This occurs, for example, in the case where a file must
be closed or an external resource released, regardless
of the outcome of the program.
• This is done by adding a finally clause at the end of
the list of handlers, just after a complete try
construct.
• The finally clause is executed in all cases whether or
not try throws an exception, and whether or not it is
caught by a catch clause.
Exception Handling in C++
• In C++ an exception is "thrown and caught"
whereas in Ada it is "raised and handled.“
• An exception handler, if present, is always the
final segment of the executable part of a
program unit or block.
• It follows the reserved word exception and
precedes the final end of the program unit or
block.
Logical Programming
• The major difference between logic
programming and other programming
languages (imperative and functional)
– Every data item that exist in logic programming
has written in specific representation (symbolic
logic)
• Prolog is a logic programming that widely
used logic language
Introduction
• Prolog specified the way of how the computer
carries out the computation and it is divided
to 3 parts:
– logical declarative semantic of prolog
– new facts that can infer from the given fact
– explicit control information supplied by the
programmer
Symbolic representation: Predicate Calculus
• mathematical representation of
Predicate Calculus formal logic
• First order predicate logic is a
particular form of symbolic logic
FOPL Higher-order that is used for logic programming
PL
Symbolic
Logic
Logic
Formalism
Proposition
Symbolic representation: Predicate Calculus
Predicate Calculus
FOPL Higher-order
PL
• symbolic logic used for the three basic need of
Symbolic formal logic
Logic • to express propositions
• to express the relationships between propositions
Logic • to describe how new propositions can be inferred
Formalism from other propositions that are assumed to be
true
Proposition
Symbolic representation: Predicate Calculus
Predicate Calculus
FOPL Higher-order
PL
Symbolic
Logic
• Formal logic was developed to provide a method
Logic for describing proposition.
Formalism
Proposition
Symbolic representation: Predicate Calculus
Predicate Calculus
FOPL Higher-order
PL
Symbolic
Logic
Logic
Formalism
• Proposition is a logical statement also known as fact
Proposition • consist of object and relationships of object to
each other
Proposition
• Object:
– Constant represents an object, or
– Variable represent different objects at different times
• Simple proposition called as atomic propositions, consist of
compound terms – one element of mathematic relation
which written in a form that has the appearance of
mathematical function notation.
Example (constants):
single parameter (1-tuple): man(jake)
double parameter (2-tuples): like(bob,steak)
Proposition
• Two modes for proposition:
– proposition defined to be true (fact), and
– the truth of the proposition is something that is to
be determined (queries)
• Compound propositions have two or more
atomic proposition, which are connected by
logical operator (is the same way logic
expression in imperative languages)
Logic operators
Name Symbol Example Meaning
negation ¬ ¬a not a
disjunction ab a or b
ab b implies a
Compound propositions
Example:
a b c
a b d
(a (b)) d
Precedence: higher
lower
Variables in Proposition
• Variable known as quantifiers
• Predicate calculus includes two quanifiers, X –
variable, and P – proposition
Example:
X.(woman(X) human(X))
X.(man(X) human(X))
Clausal Form
Example:
likes(bob, trout) likes(bob, fish) fish(trout)
consequent antecedent
• Characteristics of CF:
– Existential quantifiers are not required
– Universal quantifiers are implicit in the use of variables
in the atomic propositions
– No operator other than conjunction and disjunction
are required
Proving Theorems
• Method to inferred the collection of
proposition
– use a collections of proposition to determine
whether any interesting or useful fact can be
inferred from them
P1 P2 and Q1 Q2
which given
P1 is identical to Q2
Q1 P2
Proving Theorems
Example:
• Unification
– Hypotheses : original propositions
– Goal: presented in negation of the theorem
– Proposition in unification must be presented in
Horn Clauses
Applications of Symbolic Computation
• Relational databases
• Mathematical logic
• Abstract problem solving
• Understanding natural language
• Design automation
• Symbolic equation solving
• Biochemical structure analysis
• Many areas of artificial intelligent