wiki:OOP

Object Oriented Programming

OOP is not just overrated, it is very very overrated, over-hyped and oversold, because, your CPU, for example, can do nothing, but calls and jumps.

In Berkeley's CS61A prof. Brain Harvey told us that All you need in Lambda, but nobody listened.

We shall say that all we need is Lambda and a list structure,, made out of pairs. Let's try to explain it.

They beauty of Scheme's model of evaluation is that it represents the essence of computation - how data are represented and how procedures being called.

Suppose we'are evaluating this expression:

(define (square x) (* x x))

It creates a new binding in the current frame of environment. It means that somewhere in memory a mapping between a symbol and a procedure's body were stored. Actually, it is just a pointer to the entry point of a procedure.

((square x) (* x x) env)

There are nothing but numbers in computer memory, there are nothing but symbols in Scheme. For us it is much natural to think and express ideas in terms of symbols. The compiler does the mapping to numbers for us.

If we are keeping in mind what computers are, how memory is organized and what operations processor can do, we can keep the our high-level code less disconnected from reality, which leads to simpler and more natural idioms, which could be more effectively compiled into optimized machine code.

So, what is the frame of environment? It is a list of bindings, an associative list which represents a look-up table, a map.

((f . (+ a b)
 (a . 1)
 (b . 2))

So, what is what they call a class? It is a frame of environment with bindings. What is instance? It is just another frame. What is a frame? It is a list of pairs. Everything is a list of symbols in Scheme. Everything is a sequence of numbers in memory.

The crucial thing to realize is that we can express the data in the same language, because code itself is data. Everything is represented as ordered sequences of numbers in memory, but should be expressed as lists of symbols (made out of pairs) in our language.

The main selling points of OOP is that they have a hierarchy of classes and could mix code and data in them.

But all those classes are some structures made out of pointers. Because memory is flat, they essentially are lists. Lists of pointers - lists of references, list of symbols - this is what we have in Scheme.

We have closures, which is a binding from a name to some other list of bindings. We do not say to code and to data, because there is no difference - code is data.

Now, what is a class? A list of lists. What is an instance? A list of lists. What is a class-variable? Just another element in a list. What is a instance-variable? Just a binding in a list of bindings. Lists could be nested - here is your hierarchy.

To illustrate this simple and beautiful idea in CS61A they use a full lecture of drawing colorful diagrams on a board. Then you can realize that everything is just a list.

What if we need to 2D structures? A Table, or a Tree? Nothing. They are, naturally, closures.

How do they communicate? By passing messages. What is message-passing? Copying lists of symbols between closures, or, in a fancy terms - passing by value.

What is the point? We don't need any special OOP in our language, just because it is nothing but conventions. We can implement OO mechanisms we need on-demand, on the go.

We don't need to think that everything is an object. It isn't. Everything is a symbol. Or a number.

Is it efficient implementation? If we do not forget what processor are, how memory is organized and what instructions available to us and what calling conventions are, the resulting compiled code will be much more efficient that all those fancy VMs could even produce. Why?

Because we do less, and we do it the right way.

Last modified 7 years ago Last modified on Nov 11, 2012, 1:34:38 PM
Note: See TracWiki for help on using the wiki.