Tag Archives: Clojure

Change Time

After some time trying to think about almost nothing, the last 24 hours have been an alarm call. As others come out of hibernation too, they post interesting stuff and Radio 4 provoked me with a discussion on facts and truth. Now Marc Cooper is at it, with difficult  links about computation and I’m all on Edge https://www.edge.org/response-detail/26733
Before I read about “discrete tensor networks”, I need to write down my own ideas about time, so I will know in the future what I thought, before my mind was changed.

I am ill-equipped for this task, having only 1 term of university maths to my name so I intend to talk in vague, abstract terms that are hard to argue with.

Much of physics is very dependent on Time, like almost all of computer science and business management theory. You can’t have change without time, it seems. Einstein talked about space-time, mostly in the language of mathematics. I can just about order a beer in math(s) but I can’t hold a whole conversation. I know what the first 3 dimensions are: left-right, up-down and back-forward. My personal model of the 4th dimension is that same space in continuous state-change through time. There are a few things I’m not happy about:

  • There is no evidence that time is either continuous or constant.
  • We only have evidence of time being a one-way dimension.
  • What the heck does ‘continous state-change’ mean? Is state a particle or a wave? Make your mind up, physics!
  • There’s that troubling many-worlds interpretation of the universal ‘WAVE’function (which I don’t understand either) which says that everything that might have happened did, in other universes. I don’t like this. Yes, that’s my entire justification – I don’t like the conclusion of a thought process I don’t even understand. It doesn’t feel right.

I’ve been learning about the functional programming language Clojure which does not ‘mutate (change) state’. It doesn’t have ‘variables’ like the more common imperative languages such as FORTRAN, BASIC, C, Java or Python. In Clojure, data flows through functions and is transformed from one form to another on the way. It is basically magic. In a pure functional program, no state is changed. State-change is called a “side-effect”. Sadly, side-effects are required to make a program do anything useful in the real world. Arguably, the purest magic is encapsulated in the world of mathematics and the physical world is a messy place that breaks things.

Clojure models time. It does not model the real world by replacing the current value in a variable and throwing the old value away but by chaining a new value onto the end of a list of all previous values.

Now let us extend this idea ‘slightly’ in a small thought-experiment, to a 3-D network of every particle state in the universe.

Space-time now has 2 regions:

  1. The past – all historic states of those particles as a theoretical chain of events
  2. The future – all possible future states of the universe; effectively an infinity of all possible future universes that could exist, starting from now.

Which brings us to what I mean by ‘now’ – a moving wave at the interface between the past and the future, annihilating possible future universes. Time becomes a consequence of the computation of the next set of states and the reason for it being a one-way street becomes obvious: the universe burned its bridges. Unless the universe kept a list, or we do, the past has gone. Time doesn’t need to be constant in different parts of the universe, unless the universe state ticks are synchronous but it seems likely to be resistant to discontinuities in the moving surface. I imagine a fishing net, pulled by current events.

It’s just an idea. Maybe you can’t have Time without change.

[ Please tell me if this isn’t an original idea, as I’m not very well read.
I made it up myself but I’m probably not the first. ]

A Functional Mindset

When I started learning Clojure, I thought I knew what functional programming was but I’ve learned that the functional paradigm is now more than I expected.

Everyone agrees that it’s a computational model based on evaluation of mathematical functions, which return values. This is generally contrasted with imperative programming languages such as FORTRAN, C, JavaScript or Python, which are also procedural and some of which are object-oriented but may make functional coding possible, in a hybrid style. I wouldn’t recommend learning functional concepts in a language that gives you short-cuts to stray back  to more familiar territory.

Clojure is a member of the Lisp family, first specified in 1958. The unusual feature of Lisps is their homoiconicity – code and data are the same thing. Learning Clojure has informed my thinking about business process change.

Some modern, functional languages such as Clojure use immutable data whenever possible, to eliminate side-effects. This allows better use of multi-core processors but requires a complete change in thinking, as well as programming style. ‘Variables’ are replaced by fixed ‘values’, so loops have to be replaced by recursive functions. New data can be created but it doesn’t replace old data. Yesterday’s “today’s date” isn’t automatically wiped when we decide today has happened.

Objects with their methods and local data were designed for simulating the current state of real-world objects by changing (mutating) object data state. The object model, like relational databases, has no inbuilt representation of time. Functional programming splits these objects back into separate functions and data structures and because values can’t change, they may be transformed by flowing through networks of functions, some recursive, to keep doing something until a condition is satisfied. Eventually, code must have a side effect, to tell us the answer.

Rather than computation being a conditional to-do list with data being moved between boxes, it becomes a flow of data through a network of ‘computing machines’; and the data and machines can be transformed into each other.

I hear that map, reduce & filter data transformation functions will change my world again.

Becoming Functional

I’ve been playing with the idea of doing some functional programming for a while now. I’ve been trying to learn and paddling around in the shallows but this week I dived right in the emacs/CIDER pool. I was aware of some dangers lurking beneath the surface: recursion, immutable data structures and the functional holy trinity of map, reduce & filter, so I came up with some ideas to face my fears. I’ve also realised my maths has got rusty so: Some of That Too.

  1. I’ve ‘done recursion’ before but I thought I’d read that my chosen weapon Clojure didn’t do tail-end recursion. This isn’t true. What it can’t do is automatic optimisation of tail-end  recursion, to stop it blowing the stack after a few thousand iterations but Clojure has a ‘recur’ expression to manually signal tail recursion and fix that. I knocked off the programme in a couple of hours and went to bed happy. My code was happily printing the first n numbers of the Fibonacci sequence but a day later I still couldn’t get it the return the numbers as a sequence.
  2. I was finding out about immutable data the hard way. You can’t build up an immutable vector, 1 element at a time. You get to keep the empty vector you created first. It’s a big mind-set change to not have variables that can vary. In my next post, I’ll try to say what I’ve learned. On this occasion it was lazy sequences.
  3. I mentioned the Algorave in my last post. I only found out about that because of an idea I had for improving my theoretical understanding of music. I realised that I could write, for example, a function that would return the 1st, 3rd and 5th notes in a major scale, using a map function.While working the theory out, I found out that Lisps are already popular in the live-coding world.
  4. At Algorave, I was inspired by the live-coded graphics to try automatically generating some graphics too, to work out the maths of mapping triangular grids onto Cartesian co-ordinates. I need that for another idea.

Three basic working programmes in about a week. They aren’t ‘finished’ but is software ever? They have delivered value via increased Clue.

My First Algorave

@algobbz

On Saturday night I went to ‘Algorave Birmingham’, curated  by Antonio Roberts at Vivid Projects. I said I might write ‘a review’ but I’m not going to, because I wouldn’t know how. This is ‘a reaction’ – a digital feedback loop, an emission from the event horizon (should have worn my ‘Big Bang’ T-Shirt – the noughties Brum band, not the nerd show.)

My background is information technology. My current work is writing. I use the word ‘work’ in the artistic sense: something I spend my time on but may never get paid for. Themes recur. Are science and art actually different things? Is maths real or a model? Is software any different to magic, existing only outside the physical realm and communicating via intermediary objects?

Q: How much can you strip away from music and it still exist as an idea: melody, scales, pitch?

I came to Algorave via my functional programming experiments. I’m trying to learn Clojure, a member of the Lisp family of languages but with added time-travel. It messes with whether time is a wave or a set of discrete steps that can be retraced. Not real time, obviously but the model of time our software deals with. Time travel outside of the magical realm would be crazy-talk.

Dance music is often first. Drum machines. I got really frustrated the first time I saw how hard it was to programme beats. Where was the programmatic interface? Sampling, pitch-shifting, the ‘sound’ being manipulated by code. Digits being manipulated by digits, like the higher order functions of functional programming. I wondered a few weeks ago if processors had got fast enough to generate live noises. They have. A Raspberry Pi has http://sonic-pI noti.net/http://sonic-pi.net/. From there I discovered Clojure has, via ‘Overtone’ on ‘SuperCollider’ http://sam.aaron.name/, which resonates with my theory of a super-massive idea colider to mash-up memes.

Algorave Birmingham presented live coders generating sound and visuals. At times I felt that the graphics were pulsing to the beats but I don’t know if that really happened. I saw two pixelated women on the screen typing on ‘real’ laptops and a live drummer on digital drums. Virtuality virtuosos. I had a chat about how to make a hit record and forgot the name of the Kaiser Chiefs but remembered Black Wire who were the first band with a drum machine that I actually liked, because it didn’t sound mechanical, then The Kills who insisted everything was analogue, but now I’m looping.

A: I enjoyed the pulsing white noise. Software can do things that aren’t possible in Reality.

Lispbian Pi. A Lambda Delta.

I’m conflicted. Part of me says that ‘us old timers’ shouldn’t assume ‘the way things were when we were kids’ were better but we know the Raspberry Pi was an attempt to recapture the spirit of the BBC Micro Model B and that seems to have gone quite well. I got a Pi 2 and I’ve worked out that it is more powerful than the first computer I worked on, a DEC VAX-11/780 which supported about 16 terminals, most used for teaching college level computing. Having that machine to myself would have been an unimaginable amount of processing power for one developer. Banks ran their financial modelling software on boxes like that. So why does the Pi feel so slow? We wasted our gains on GUI fluff.

When I started computing you learned just enough of the command language to get going. So, that’s bash on a pi. Then an editor. For reasons that should become obvious, let us choose emacs. When I first used the VAX/VMS operating system, it didn’t have command line editing. If you made an error, you typed it all again. Getting the facility to press up-arrow, edit the command and re-execute it was a big advance. We should keep it. Bash has that, using a sub-set of the emacs keys, so that’s a way into emacs.

The next big improvements I remember were X windowing and symbolic debugging. We got debugging first but it became far more powerful with multiple terminal windows. The GUI was OK, I guess but DEC didn’t give us many free toys so the main advantage to a developer was having lots of terminal windows. emacs can do that, without the overhead of X.

When I decided to re-learn coding a while back, I got my shortlist of languages down to Python, Java and JavaScript but picked Python because I was already learning a new language and the Object paradigm, so I didn’t want to have to learn web at the same time. I heard about the modern Lisp dialect Clojure and changed horses mid-stream. I’m convinced by the argument that functions and immutability can save the universe from the parallel dimension.

Last night I deep-dived into emacs and found myself in an editor session with 4 windows. Why do I need more than that to learn about computation and data transformation? This guy seems to have come to a similar conclusion http://hackaday.com/2015/09/23/old-lisp-languaged-used-for-new-raspberry-pi-os/ I’ve also wondered whether a purely functional OS might make Sun’s ‘the network is the computer’ dream a little easier. emacs is written in Lisp.

I think a dedicated Lisp machine may be a step too far back. How would you browse in the world-wide hypertext library when you got stuck? But a Linux with bash, emacs, the Java Virtual Machine and libraries, Clojure via Leiningen and Cider to plug everything together might make a fine Lispbian Pi! Is all this chrome and leather trim completely necessary in the engine compartment?

It is unfortunate that the Raspbian upgrade left Leiningen broken.

Objects vs Functions

I learned to ‘programme computers’ long ago, almost before there was no “me” in ‘program’ and certainly before I knew how to ‘team’. I had a very brief and unsuccessful exposure to functional programming in LISP (not Lisp) then stopped. I did other ‘Data Processing’ things.

In recent years I’ve been working as an analyst, alongside people who write code according to the object model. I think I have a feel for objects but never having written code in an object-oriented language, I can’t be sure. I decided to try, in the Python language, then got distracted by the shiny Clojure language which is functional. I feel that right now I’m approximately equally confused by objects and functions, so I thought I’d write this quickly before I know what I’m talking about. I can come back later to laugh at my naivety, along with the rest of you.

Like the person who wrote this https://news.ycombinator.com/item?id=4246018 ,
I’ve been watching some talks online recently by Rich Hickey of Clojure fame”
The post asks “So if I follow Hickey’s advice, how am I supposed to represent a book? As a vector of vectors of vectors of vectors of strings? If so, then how do I prevent a change in the representation of the Book from breaking client code?”

I found the question very interesting because representing ‘books’ in a functional language is exactly what I want to do. I think differently to the author because  I’m not yet trapped inside the object paradigm. I can see that ‘book’ is a real-world class of objects, a very specific and limited implementation of the representation of a small subset of all the information in the world. That’s what my ‘book’ was going to be about and why I’m now playing with functions instead of writing it.

Objects are good at simulation of real life systems. They encapsulate small sub-systems of a process and it’s local data into an object. What I always struggled to understand was what you did with the data that didn’t want to be enclosed – “information wants to be free”. People seem to cope by inventing objects that don’t really exist: to be data shepherds.

Functions are good at abstraction. A book is a single output format from something much richer. That’s what I want to write. Data and processes are complex. Objects and functions are simplifying models; there may be others.

p.s. (not Lisp) Get it?

Why I want Clojure

In the final year of my degree, I had to complete a major piece of work; a kind of dissertation equivalent for the illiteratti of Computer Science. I chose to implement a neural network [ this originally said “semantic network” but that was Freudian slippage. ] My project supervisor, a PhD researcher, suggested that I use Lisp with a library for fuzzy logic.

We didn’t have The Interwebs in those days, so I went to a book shop to order a Lisp book, well, THE only Lisp book, which I had to order from the US:

(LISP 1.5 PRIMER (BY (CLARK WEISSMANN)))

Do you see what he did there, apart from the obvious shouting? All computers shouted, in those days.

It mostly went wrong from there on. My mate Mark wrote a comparison between Algol 68 which we’d been taught and BCPL, the forerunner of C; another block-structured imperative language but typeless. I had chosen to do something difficult in a language I knew nothing about. I tend to take the path less travelled, but Mark’s path led to a doctorate. “That’ll learn him”, as Badger would say.

I learned that it is easy to be taught a computer language by someone who knows how (if you think in the right way) and it’s quite easy to move to a slightly different language. I’ve done it several times, but Lisp has a completely different computing paradigm and the book didn’t arrive in time. I was beaten by Lisp once but I didn’t feel it was a fair fight. I arrived back at the Clojure shore of Lisp recently, determined on wreaking revenge. This time it was going to be different!

I started work seriously last week but had a few bad days. I still wasn’t “getting it”, but this weekend, I found myself a new sensei, Rich Hickey. He seems to understand my need for a conceptual framework to hang up the weapons I’m being handed. I may be able to function soon. I now know what “programming to abstractions” means. It doesn’t involve my head and a solid, vertical surface.

UPDATE: In Weismann’s introduction, he writes: “In learning a symbolic programming language such as LISP, however, we cannot call on our experience, because the formal skills for representing and manipulating symbolic data processing is not part of this experience. Thus, we have the added task of learning a basic set of formal skills for representing and manipulating symbolic data before we can study the syntax of the LISP 1.5 programing language.”
I guess that hasn’t got any easier with Clojure