Will's Research Blog

miniKanren and declarative programming and stuff. And ponies.

Closures and Mutable State

John Carmack recently tweeted:

I’m still waiting for the insight that makes closures usefully cool instead of a scary way to hide mutable state. Any great examples?

and

I have no issue at all with capturing immutable state, and I don’t have enough experience to decry arbitrary capture, but it gives me pause.

John asks an interesting question, so I decided to collect answers that won’t fit in 140 characters. If you send me your favorite example of using mutable state within closures, I’ll add it to this post.

The canonical example of using mutable state with closures is to encode object-oriented programming within a functional language. For example, Dan Friedman’s paper on Object-Oriented Style uses mutable state, closures, and syntax-rules hygienic pattern-matching macros to add a simple object system to Scheme.

Kent Dybvig (formerly of Indiana University, now at Cisco) gives several examples of mutable state and closures in The Scheme Programming Language, 4th ed including memoization, multitasking with engines (“a high-level process abstraction supporting timed preemption”), and—of course—an object system.

The first submitted answer is from my friend and co-worker Andy Keep. I’ve taken the liberty of adding appropriate hyperlinks, for context.

Consider a simple counter:

1
2
3
4
5
6
7
8
9
10
11
(define next-count
  (let ((count 0))
    (lambda ()
      (let ((nc count))
        (set! count (+ count 1))
        nc))))

(next-count) ;=> 0
(next-count) ;=> 1
(next-count) ;=> 2
(next-count) ;=> 3

Here, the closure will contain the count variable, which is mutable (though one way to deal with this is closure conversion, which is what we do in the [Indiana University P423] class compiler and Chez [Scheme]).

In general any time you have some local-state that can be changed amongst a set of functions internally you are going to have mutable state in a closure. (In fact, there is almost no point of using a mutable variable if it isn’t going to be captured in a closure, because in that case it can almost always be modeled by simply rebinding and shadowing.)

Just to be clear the closures themselves are never mutated, they just contain a pointer to data structure (in our case a pair) that gets mutated. This is as opposed to languages like Ruby, where it is actually possible to mutate the closure.

In both cases this is an implementation detail, but it is an one to note, because in Ruby it can lead to mutation that is not statically determinable, where as in Chez the only fields that can be altered are those that set! was called on somewhere in the program.

Otherpony’s Music to Hack By

Here are the hacking playlists and songs that have been sent to me after my previous post:

Scott Kilpatrick’s Spotify chill list

Cameron Swords suggests two songs that might fit into my playlist:

Mimosa - Flux For Life

The Polish Ambassador - Exiting The Womb

Christian Romney suggests Vivaldi, and Philip Glass instrumentals:

Vivaldi, The Four Seasons (Recomposed By Max Richter)

The Essential Philip Glass - Deluxe Edition

Philip Glass: Symphony No. 9 (Bruckner Orchester Linz & Dennis Russell Davies)

Thanks!

Music to Hack By

Tonight Chung-chieh Shan encouraged me to blog more. Since I’ve never actually blogged, blogging more shouldn’t be that hard.

When I’m hacking on miniKanren the right music really helps me get into a state of flow. When working in public, sometimes I rock out a little too much (usually when my code finally works)–random people will walk across the restaurant or coffee shop to ask what I am listening to, and will usually ask for a song list. Before tonight, I would just list a few songs off the top of my head. Now I can do better.

For my own convenience, and potentially for your listening pleasure, I’ve created a YouTube channel of music I hack to. Much of the music is electronica or chill or ambient or some such thing, with a few chiptunes and catchy pop-ish songs thrown in. If you like a song, let me know and I can point you to more music in that style. If you don’t share my tastes, post your own hacking list, and I’ll include a link on this page. Since YouTube videos have a habit of disappearing, I’ve included song and artist names and YouTube links below. If a song is taken down, a quick Google search will usually find another version. Please let me know if you notice a broken link.

I’m always looking for new coding music; if you have any suggestions, please let me know! I find that chill, quasi-repetitive music I can put on repeat works best. The sorts of songs that don’t have “seams” at the ends, if that makes sense. For example, I listened to Air’s La Femme D’Argent for hours at a time while working on my dissertation (1,700+ listens, according to iTunes). Under the Sea by Digby Jones is another song I can code to for hours. To paraphrase Alan Perlis, one song I can listen to 1,000 times is much better than 10 songs I can listen to 100 times each. If I become too aware of a song while coding, which invariably happens during a long coding session, I immediately change to another one I can forget about consciously.

I also listen to a selected subset of these songs to moderate my mood, or to pump myself up before a big talk, especially if I’ve had little sleep the night before.

Thanks to Hillary Davis, Al Bertrand, Sean ‘Day[9]’ Plott, and Astrid ‘MalyCocka’ Chodura for pointing me to great music. And thanks to Chung-chieh Shan, Lindsey Kuper, and Matt Might for encouraging me to blog.

Mux Mool - Merlinfist

Fuck Buttons - The Lisbon Maru

Ludique - Lost in the Woods

Purity Ring - Lofticries

The Pines - Cold

Mogwai - Scotland’s Shame

LCD Soundsystem - 45:33 Part 3

Royksopp - Royksopp Forever

M83 - Unrecorded

M83 - Midnight City

DJ Shadow - Giving Up The Ghost

Trentemøller - Take me into your skin

Amon Tobin - Kitchen Sink - Clark Remix

Appaloosa - The Day We Fell In Love

Emancipator - Lionheart

Emancipator - Anthem

Emancipator - Safe In The Steep Cliffs

Emancipator - Soon It will Be Cold Enough to Build Fires

µ-Ziq - Hasty Boom Alert

Ulrich Schnauss

Crystal Castles - Crimewave

Shpongle - Nothing Is Something Worth Doing

Ratatat - Loud Pipes

DJ Fresh - ‘Gold Dust’

The Future Sound of London — Papua New Guinea

Bonobo - Kong

dan le sac VS scroobius pip

MOVITS! - Fel Del Av Gården

Disco Science- Mirwais - Snatch

Air - La Femme D’Argent

Tomorrow People - La Femme D’Argent - cover

Clint Mansell - Welcome To Lunar Industries - Moon OST

Clint Mansell - Sacrifice - Moon OST

Clint Mansell - Two Weeks & Counting - Moon OST

Clint Mansell - We’re Going Home - Moon OST

Boards of Canada - Everything You Do is a Balloon

Boards of Canada - Dayvan Cowboy

Boards of Canada - Whitewater

Mother Mother - Hayloft

Pavement - Father To A Sister Of Thought

Carbon based lifeforms - Interloper

Dub FX - Love Someone - live, 10/10/2008

Dub FX - Flow, Feat. Woodnote - live, 4/18/2009

Tangerine Dream - Love on a Real Train

Tangerine Dream - The Dream Is Always The Same

Digby Jones - Under the Sea

The Crystal Method - Trip Like I Do

Junkie XL - Love Like A Razorblade

Kap Bambino - Batcaves

311 - Amber

The Gorillaz - 19-2000 - Soul Child Remix

OxygenStar - Left Out

Sneakers OST - Planning the Sneak, Composed by James Horner, Feat. Branford Marsalis

Crystal Castles - Untrust Us

METRIC - Gold Guns Girls

Crazy Penis - 3 Play It Cool - The Outernational Sound

Glenn Morrison - No Sudden Moves - Original Mix

Rammstein - Engel

Digital Insanity - Unreal Superhero 3

Moon Duo - Sleepwalker

Starfucker - Julius

Ronald Jenkees - Stay Crunchy

Cut Copy - Hearts On Fire

Foster the People - Broken Jaw - Live from SXSW

Glenn Morrison - Contact

Amsterdam Acoustics & The Asteroids Galaxy Tour - Satellite

Lazybatusu - Endless Road

Mausi - sol

Justice - DVNO

Ellen Allien & Apparat - Do Not Break

Memoryhouse - To The Lighthouse - Millionyoung Remix

The Bravery - An Honest Mistake

Wolf People - Cotton Strands

Dancing Pigeons - Ritalin

Down South - Sitting Here

Record - Tekk Me the Truth - live at Opolis 8/20/10

Sinner DC - Alice

Recorder - Wait for Me

The Whitest Boy Alive - Gravity

Digital Orgasm - Moog Eruption

Mylo - Guilty Of Love

Kostya Veter - Ad Astra - Original Mix

Kill The Noise - Building Blocks

Appaloosa - The Day We Fell in Love - Acid Girls Wept Remix

Yelle - Que Veux-tu - Madeon Remix

Digitalism - Idealistic

Battlefield 1942 Theme - loading music

Fenech-Soler - Demons - Sigma Remix

Madeon - Pop Culture

The Killers - Losing Touch

4mat - Vertical

Pavement - Grounded

Trentemøller - Nightwalker

UffiE - Dfficult

Clan Greco - Astrao

MLP: Friendship is Magic - Ponies are ravers - Extended/Reworked

The Killers - All these things that I’ve done

Mass Effect OST - Uncharted Worlds

Serge Gainsbourg & Brigitte Bardot - Bonnie And Clyde

Children of the Bong - Life on Planet Earth

Make Love - Daft Punk

The Entire Daft Punk Set From Coachella May 2006