About mixing imperative and functional…

Making a custom programming language is a tricky thing… Here come a few issues of the constructs as I thought them.

It was initially planned to use both funtional and imperative programming by providing two constructs, one for true equality and another for affectation. Moreover, this sounded like to provide nice ways to interact. With variables for data and equalitites used to give a formatted view of these. …Sounds weird? Ok, here is a small example:

declare width, height
surface = width * height
width := 2
height := 3
print surface # prints 6
width := 4
height := 7
print surface # prints 28

In the previous example, we declare the surface to be equal to the width times the height. It has the same effect than making a function out of area but is simply more expressive.

However …it goes with its issues as well. For example, the lack of references initially planned encounters some issues. In java, affectation of “primitive” elements are done by copy while affectation of “compound” elements (i.e. objects) are done by reference. However, I wanted to avoid the distinction between what is “primitive” and what not. While on the same time avoiding pointers.

In leo, equality is “referential equality”. Thus, when we write:

a = b

The identifier ‘a’ becomes equal to ‘b’, not to it’s content. We do not even evaluate ‘b’ at this moment. On the opposite, an affectation like this:

a := b

takes the content of b at this moment in time and copies the content into a. …The thousand question point is now: does it copy “in depth” the object or does it merely copy a handle to the content of ‘b’?

Let us take a few examples to illustrate this. Suppose we have a group of people, let’s say the pupils of a class with each one having a best friend. If we do:

alice.bestFriend := zoe
bob.bestFriend := zoe

Then we copy the content of zoe into both variable …here we would whish that both zoes are the same even if they are modified afterwards. Thus a referencial copy. What about:

a := 1
b := 1

In this case, there is nothing to do on the ones. Referencial copy has no sense, just copy that value. …But what if the integer would be 60 pages long? And what about this:

date = 07/05/2008
a := date

The issue doesn’t change …is it arbitrarily copied by reference or by value?
…In any case, the issue is the arbitrary frontier between what is primitive and what not, as well as how to decide to make the affectation by copying references or values.

The adopted solution is to always store a pointer in ‘a’! …even in the case of “a := 1”, ‘a’ will point to an anonymous “node” containing itself the value 1.

Thus the internal representation for:

a = b
b := 2


a –> b –> anon_int : 2

So each number would be represented by a small little data object …nice 🙂
Notice also that = makes them always point to symbols while := always point to data objects.

By the way, == would:
first compare references, if they are same then reply true
else, compare the types and content.

Let us go over to another issue with a more practical example. Assume we have some graph and want to modify some properties of the node having the highest score based on some computation. There should be a function that returns the node having the highest score so that we can modify it accordingly.

graphA = ...
getBestNode = function (graph) -> (node)

graphB := graphA

nodeA := gestBestNode(graphA)
nodeB := gestBestNode(graphB)

nodeA.property := … # error!
nodeB.property := … # ok

In the last line …it means that we do not operate on the same data structure than graphA.
In this case, the nodeWe cannot do that!!! …it has no sense …graph cannot be equal to something that we modify afterwards
The greatest argument of functional programming is that it is stateless and deterministic. In the above case, it makes no sense anymore. Indeed, we retrieve an element of a graph to modify it afterwards. …so does this still fit in functional programming? …barely. I am no theorist anyway. The other issue is:

let us be firends…

alice.friend := zoe
bob.friend := zoe

what if

zoe = buildZoe()

Then zoe is either not modifiable or zoe is a copy.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: