FP vs OOP : Design Methodology by Harshad Nawathe
-
Upload
chandu-kavar -
Category
Technology
-
view
190 -
download
3
Transcript of FP vs OOP : Design Methodology by Harshad Nawathe
1
http://cowbirdsinlove.com/432
FP vs OOP : Design Methodology Compairing Data Abstraction techiniques employed by
Objects Oriented Programming and Abstract Data Types
Harshad Nawathe
3
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
Object Orientation is a hoax !!!
5
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
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/thumb/1/1c/Alexander_Stepanov.jpg/250px-Alexander_Stepanov.jpg
7
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
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
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
● This paper provides the core for this talk
https://www.cs.utexas.edu/users/wcook/papers/OOPvsADT/CookOOPvsADT90.pdf
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
Implementation
13
Implementation using OOP
14
List
isEmpty():bool
length():int
head():int
tail():List
EmptyList()
true
0
error
error
LinkedList(data:int, next:List)
false
1 + self.next.length()
self.data
self.next
⛔ ⛔ ⛔
✅
✅
✅
✅
✅
Abstract Type Concrete Implementations
Observations
● 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
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
module MyList( IntList, makeNil, appendTo, head, tail, length, nil) where
-- Datadata IntList = Nil | Cons Int IntList deriving(Show)
-- Constructor functionsmakeNil :: IntListmakeNil = Nil
appendTo :: Int -> IntList -> IntListappendTo = Cons
18
-- Operationshead :: IntList -> Inthead Nil = error "head called on Nil Int List"head (Cons x _) = x
tail :: IntList -> IntListtail Nil = error "tail called on Nil Int List"tail (Cons _ xs) = xs
length :: IntList -> Intlength Nil = 0length (Cons _ xs) = 1 + (length' xs)
nil :: IntList -> Boolnil Nil = Truenil (Cons _ _) = False
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
Extensibility
20
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
Optimization
22
Optimizing ADTs● As operations can inspect the representations of the arguments it is easy
to improve efficiency of the operations
23
24
data IntList = Nil | Cons Int IntList | IntRange Int Word deriving(Show)
instance Eq IntList where (==) Nil Nil = True (==) _ Nil = False (==) Nil _ = False (==) (IntRange f1 n1) (IntRange f2 n2) = (f1 == f2) && (n1 == n2) (==) list1 list2 = (head1 == head2) && (tail1 == tail2) where head1 = head list1 head2 = head list2 tail1 = tail list1 tail2 = tail list2
Optimizing PDAs
25
● Optimizing operations is difficult as the representation of the argument cannot be inspected
26
interface IntList {boolean empty();int size();int head();IntList tail();
boolean checkIfEquals(IntList rhs);}
27
public boolean checkIfEquals(IntList rhs) {IntList lhs = this;while( !lhs.empty() && !rhs.empty() ) {if( lhs.head() != rhs.head() ) return false;
lhs = lhs.tail();
rhs = rhs.tail();
}
return lhs.empty() && rhs.empty();}
What if …
IntList list1 = new IntRange(1, 1000000 );IntList list2 = new IntRange(1, 1000001 );
list1.checkIfEqual(list2);
Tyranny of Dominant Decomposition
28
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
Typeclassesclass 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]
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
A Real Life Example
32http://www.karambelkar.info/downloads/java_collections/Java-Collections_API-List-ImageMap.html
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
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
○ Associative containers(unordered): unordered_set, unordered_multiset, unordered_map, unordered_multimap
34
35
forward_list
list vector array
Forward Range
Bidirectional Range
Random Range
Single Pass algorithmseg. for_each, transform, copy, find, rotate etc.
Algorithms with bidirectional accesseg. reverse, partition*
Random access algorithmseg. sort, binary_search etc
Thank [email protected]
36