Abstract Data Types and Object Oriented Programming are two prominent methods of software design. Abstract Data Types used in Functional Programming paradigm employ type abstraction and are centred around the operations. While objects use procedural abstraction and organised around the constructors. In this talk, we will analyse differences between these orthogonal implementation techniques and their effects on extensibility, efficiency and typing. The talk includes code examples for illustration purpose but it doesn't emphasises on specific programming language or implementation method.
3. FP vs OOP :
Design Methodology
Compairing Data Abstraction techiniques employed by
Objects Oriented Programming and Abstract Data Types
Harshad Nawathe
3
4. Something about me...
● I ♡ Programming.
● Like to learn new programming languages.
○ Languages I know :
[“C++”, “C”, “Python”, “Haskell”, “Ruby”, “Go”, “Rust”]++[“Java”]
○ Wish list so far : [“D”, “Erlang”, “Clojure”] ++ manyMoreToCome
● Currently working on Barclaycard - UK project.
4
6. Object Orientation is a Hoax!
● Alexander Stepanov used this interesting phrase.
● http://c2.com/cgi/wiki?ObjectOrientationIsaHoax
● Full interview: http://www.stlport.org/resources/StepanovUSA.html
6
7. Who is Alexander Stepanov ?
● Russian Mathematician
● Programmer by profession
● Advocate of Generic Programming
style
● Primary designer and implementer of
C++ Standard Template Library
● Wrote 2 fantastic books on
programming
○ Elements of Programming
○ From Mathematics to Generic Programming
https://upload.wikimedia.org/wikipedia/commons/th
umb/1/1c/Alexander_Stepanov.jpg/250px-
Alexander_Stepanov.jpg
7
8. Question:I think STL and Generic Programming mark a definite departure from the common C++
programming style, which I find is almost completely derived from SmallTalk. Do you agree?
Answer: Yes. STL is not object oriented. I think that object orientedness is almost as much of a hoax as
Artificial Intelligence. I have yet to see an interesting piece of code that comes from these OO people. In
a sense, I am unfair to AI: I learned a lot of stuff from the MIT AI Lab crowd, they have done some really
fundamental work: BillGosper's Hakmem is one of the best things for a programmer to read. AI might not
have had a serious foundation, but it produced Gosper and RichardStallman (Emacs), Moses
(Macsyma) and GeraldSussman (Scheme, together with GuySteele). I find OOP technically unsound. It
attempts to decompose the world in terms of interfaces that vary on a single type. To deal with the real
problems you need multisorted algebras - families of interfaces that span multiple types. I find OOP
philosophically unsound. It claims that everything is an object. Even if it is true it is not very interesting -
saying that everything is an object is saying nothing at all. I find OOP methodologically wrong. It starts
with classes. It is as if mathematicians would start with axioms. You do not start with axioms - you start
with proofs. Only when you have found a bunch of related proofs, can you come up with axioms. You
end with axioms. The same thing is true in programming: you have to start with interesting algorithms.
Only when you understand them well, can you come up with an interface that will let them work.
http://c2.com/cgi/wiki?ObjectOrientationIsaHoax
8
9. Question:I think STL and Generic Programming mark a definite departure from the common C++
programming style, which I find is almost completely derived from SmallTalk. Do you agree?
Answer: Yes. STL is not object oriented. I think that object orientedness is almost as much of a hoax as
Artificial Intelligence. I have yet to see an interesting piece of code that comes from these OO people. In
a sense, I am unfair to AI: I learned a lot of stuff from the MIT AI Lab crowd, they have done some really
fundamental work: BillGosper's Hakmem is one of the best things for a programmer to read. AI might not
have had a serious foundation, but it produced Gosper and RichardStallman (Emacs), Moses
(Macsyma) and GeraldSussman (Scheme, together with GuySteele). I find OOP technically unsound. It
attempts to decompose the world in terms of interfaces that vary on a single type. To deal with the real
problems you need multisorted algebras - families of interfaces that span multiple types. I find OOP
philosophically unsound. It claims that everything is an object. Even if it is true it is not very interesting -
saying that everything is an object is saying nothing at all. I find OOP methodologically wrong. It starts
with classes. It is as if mathematicians would start with axioms. You do not start with axioms - you start
with proofs. Only when you have found a bunch of related proofs, can you come up with axioms. You
end with axioms. The same thing is true in programming: you have to start with interesting algorithms.
Only when you understand them well, can you come up with an interface that will let them work.
http://c2.com/cgi/wiki?ObjectOrientationIsaHoax
9
10. Data Abstraction
● “It attempts to decompose the world in terms of interfaces that vary on a
single type”
● “multisorted algebras - families of interfaces that span multiple types.”
OOP
Abstract Data Type = ADT
● Technique for defining and manipulating data in abstract fashion
● It deals with conceptual view of properties of data
● Abstract type can be a “type” that is conceived apart from the concrete
realities
10
11. 11
● This paper provides the core for this talk
https://www.cs.utexas.edu/users/wcook/papers/OOPvsADT/CookOOPvsADT90.pdf
12. Course of Action
● Implement a data abstraction for List of integers using both methods
● Compare the implementation using following criteria
○ Extending the implementation by adding more concrete implementation
○ Extending the implementation by adding more operations
○ Optimization
12
15. ● Primary mechanism is “Procedural Abstraction”
● Objects can be called as PDAs - Procedural Data Abstraction
● Data is abstract because it is accessed from a Procedural Interface
● PDAs are organized around constructors
● Observations become attributes and methods of the procedural data
values
● Procedural data value is defined as combination of all the possible
observations on it
15
Implementation using OOP - Remarks
16. Implementation using ADT
16
List Nil Cons(x:Int, t’:List)
Abstract Type Concrete Types
nil :: List -> Bool True False
length :: List -> Int 0 1 + (length t’)
head :: List -> Int error x
tail :: List -> List error t’
Observations
✅ ✅ ✅
⛔
⛔
⛔
⛔
⛔
17. 17
module MyList
( IntList
, makeNil
, appendTo
, head
, tail
, length
, nil
) where
-- Data
data IntList = Nil | Cons Int IntList deriving(Show)
-- Constructor functions
makeNil :: IntList
makeNil = Nil
appendTo :: Int -> IntList -> IntList
appendTo = Cons
18. 18
-- Operations
head :: IntList -> Int
head Nil = error "head called on Nil Int List"
head (Cons x _) = x
tail :: IntList -> IntList
tail Nil = error "tail called on Nil Int List"
tail (Cons _ xs) = xs
length :: IntList -> Int
length Nil = 0
length (Cons _ xs) = 1 + (length' xs)
nil :: IntList -> Bool
nil Nil = True
nil (Cons _ _) = False
19. Implementation using ADT - Remarks
● Primary mechanism is Type Abstraction
● Data abstraction is achieved by “opaque” types
● ADTs are organized around observations
● Each observation defined as the operation upon the concrete
representation derived from the constructors
● Constructors are also defined as operations that create values in
represenation types
● Representation is shared among the operations, but hidden from the
clients of the ADT
19
21. Summary
● OOP
○ New concrete representations of abstract data can be added.
○ New operations cannot be added
● ADT
○ New operations can be added
○ New concrete representations cannot be added
21
29. 29
● The limitation of OOP is one has to choose a fixed decomposition of the
system in the design of class hierarchy
● Example:
Classifications of Trees and Plants from the view of Lumberjack and Bird.
Tree
HardWood SoftWood
Cherry Mahogany PineMango
View of a Lumberjack
Plant
NectorPlant
Cherry
View of a Bird
InsectsPlant
Pine MahoganyMango
30. 30
Typeclasses
class Tree a where
treeFunction1 :: a -> Int
treeFunction2 :: a -> b -> Bool
.
.
class Tree a => HardWood where
hardwoodFunction :: a -> [b]
class Tree a => SoftWood where
softwoodFunction :: a -> [b]
class Plant a where
plantFunction1 :: a -> Int
plantFunction2 :: a -> String
.
.
class Plant a => NectorPlant where
sweetness :: a -> Int
class Plant a => InsectPlant where
insectTypes :: a -> [b]
31. Instantiation
instance HardWood CherryTree where
treeFunction1 …
treeFunction2 …
hardwoodFunction ...
instance NectorPlant CherryTree where
plantFunction1 …
plantFunction2 …
sweetness ...
31
type SomeCherryTreeAttributes = ( Int, Int, String, ….. )
data CherryTree = CherryTree SomeCherryTreeAttributes
32. A Real Life Example
32http://www.karambelkar.info/downloads/java_collections/Java-Collections_API-List-ImageMap.html
33. Quiz
● Is someFunction is of Quadratic complexity ?
public static void someFunction(List<String> list ) {
int[] indices = ….;
for( int index : indices ) {
list.get(index);
}
}
● What is the complexity of
java.util.collections.binarySearch(List< T > … )
33
34. ADT approach
● Standard Template Library in C++
● Divided between Containers, Algorithms and Iterators.
● STL is not OO
● Algorithms are defined using iterators
● Iterators encapsulate access to Containers
● Container types
○ Sequence containers: array, forward_list, list, vector, deque
○ Container adaptors: stack, queue, priority_queue
○ Associative containers (ordered): set, map, multiset, multimap
34
35. 35
forward
_list
list vector array
Forward Range
Bidirectional
Range
Random Range
Single Pass algorithms
eg. for_each, transform, copy, find, rotate etc.
Algorithms with bidirectional access
eg. reverse, partition*
Random access algorithms
eg. sort, binary_search etc