Archive for April, 2006

Contract Programming

This was originally posted as the first entry in my then private research blog on Feb 5, 2006. Reposted here today because (a) I somehow didn’t manage to transfer it when I switched to Wordpress, and more importantly (b) I’m getting extremely interested in this again. I’m looking now at doing this without continuations, instead using discrete reusable contract chunks as contract “clauses,” and keeping reference to which clause is current.

In the PLW today we spent some time discussing directions for further research and development of Openstudio. Many interesting ideas came up, some new and some recurrences from past discussions. A recurring theme was that of the economy, especially the lack of incentives for users to participate in the economy. How can we make the economy more fluid? How can we find its equilibrium? How do we make buying and selling more interesting? How do we explore and add more sophisticated transactions?

Many of the UROP projects have involved extensions to the currently simple economic model. Kate, for instance, is working on her “Hire a Designer” contract/commissioning system, in which one member of OS can hire another for a particular project. Other UROPs are developing stock markets and virtual cafes and new types of aggregate documents. Each of these projects adds further expressiveness and sophistication to the current economic system.

Perhaps inspired by my recent participation in an economics course at the Harvard Business School, I wonder if we can somehow formalize and abstract these various sorts of transactions and contracts. Specifically, I am thinking of a sort of programming language or library for describing and executing contracts. Hopefully the following very rough pseudo-code will convey the idea:

def my_contract(client, designer, document, instructions, fee)
    unless commit(client, designer, self)
        return # both parties did not commit to this
    end
    account_transfer :from => client.account,
                     :to => self.escrow,
                     :amount => fee
    designer_chances = 3
    while designer_chances > 0
        send :to => designer,
             :attachment => document,
             :content => instructions
        deliverables = nil
        while deliverables.nil?
            if time.now > deadline
                return # didn't meet the deadline
            end
            deliverables = receive_deliverables(designer)
        end
        if not verify(client, deliverables)
            designer_chances -= 1
        end
    end
    send :to => client,
         :attachment => deliverables
    account_transfer :from => self.escrow,
                     :to => designer.account,
                     :amount => fee
end

Is this interesting? Doable? Reminds me a bit of the sort of pseudo-code used to explain the Amazon mechanical turk.

Constraint Propagation + Junior Mints

Scheme + Junior Mints

At this very moment my world seems to have narrowed to only scheme and junior mints (yum!). I have a nice assignment to add a set value system to the constraint propagation being developed by Sussman and Chris Hanson in 6.891. Here is a transcript showing off where I am with this.

(declare (usual-integrations))
;Unspecified return value

(load "load")
;Loading "load.scm"
;Loading "ps-util.scm" -- done
;Loading "tms.scm" -- done
;Loading "constraints.scm" -- done
;Loading "equality-constraint.scm" -- done
;Loading "boolean-constraints.scm" -- done
;Loading "numerical-constraints.scm" -- done
;Loading "line-prefix.scm" -- done
;Loading "proofs.scm" -- done
;Loading "handler.scm" -- done
;Loading "solve.scm" -- done
-- done
;Value: d2?

(load "set-constraints")
;Loading "set-constraints.scm"
;Loading "set.scm" -- done
-- done
;Value: cp:identity

(define network
(cp:make-network 'network
(cp:make-value-model set:is_set?
set:=?
set:smallest)))
;Value: network

(define a (cp:make-connector 'a network))
;Value: a

(define b (cp:make-connector 'b network))
;Value: b

(define c (cp:make-connector 'c network))
;Value: c

(cp:make-constraint cp:intersection #f network a b c)
;Value: #[cp:constraint 13]

(cp:assume-value b (set:construct_from_list '(x y z 1 2 3)))
;Unspecified return value

(cp:assume-value c (set:construct_from_list '(x z 2)))
;Unspecified return value

(cp:has-value? a)
;Value: #t

(cp:value-of a)
;Value: (*set* #[primitive-procedure eq?] 2 z x)

(cp:assume-value a (set:construct_from_list '(x z)))
;Warning: Contradiction: #[tms:node 14]
;Warning: Contradiction: #[tms:node 14]
;Warning: Contradiction: #[tms:node 14]
;Unspecified return value

(cp:value-of a)
;Value: (*set* #[primitive-procedure eq?] z x)

(cp:value-of b)
;Value: (*set* #[primitive-procedure eq?] z x)

(cp:value-of c)
;Value: (*set* #[primitive-procedure eq?] 2 z x)

What I’ve written so far does in fact allow intersection and union, which is nice, but I’m also looking to mix in the boolean system with cp:subset?

Vincent happened to stop by the PLW briefly while I was working on this thing, and when I explained it he suggested a networked, distributed version. This is in fact something I was thinking about several weeks ago, this idea of setting up simple connector and constraint P2P client programs. Input could come from anything: sensor values, humans, stock market, etc. Each type of constraint or value node is essentially offered as a service, and people can compose them to create new nodes.

I won’t be working on that just yet, but I’ll leave it percolating up there, swirling around with social contracts and slow computing. They all seem to relate. In the meantime I have a few more junior mints to eat.

Panning for Data

Panning for Data (Thumb)

The idea here is that physical characteristics can map to data fields, for example weight might be price and size might be popularity, and then color might be something else. Then via a command line interface the world can be adjusted, remapped, and manipulated. I could turn off gravity and let the records float around, or turn off collisions and filter them through a sieve.

So this is what I’ve got now, and there’ll be more to come in the next few weeks. I was previously trying to write my own physics engine, but the collision detection was very hard, so I got smart and started using PyODE. It’s still a little slower than I’d like when the collisions start building, like when they all pile up. I also had to limit the balls from rolling into the z-dimension, so there is some real wasted processing there.

DSL Followup: Law in Scheme

It so happens that in class yesterday Sussman showed off some amazing work that he and Chris have been doing rewriting certain legal documents within a scheme based rule system. They showed an example of the oft-cited, somewhat scary deadbeat dad federal law, showing the complicated set of rules necessary to successfully capture the legalese. I’ll get the link up when they put it up.

He took care to point out that the system works only when the investigator is looking for evidence for something already suspected, and wasn’t suited for generalized searching/fishing for transgressions within a large set of potential suspects. However I am confident it could be adapted to this more Orwellian system if desired.

Nevertheless, this sort of work is inspirational when I start thinking about the social contracts models I’ve been throwing around. The way I see it there are two possible ways to relate contract execution to law reasoning:

  1. like this sort of law system the contract could consist of a set of customized descriptive rules dictating what is allowed and expected from each party (essentially just translations of existing legal contracts),
  2. the contract is procedural logic, a description of actions taken by the computer rather than the people. People simply have to respond (or not) to the contract execution, and the software reacts and executes accordingly. I like this one better. For my interests in opt-in, independent contractor type situations I think this one makes more sense.

Getting into Domain Specific Languages

If I ever figure out how to conceptually frame this whole social contract direction, a working prototype will probably consist of some work creating a domain specific language (DSL). Kelly actually introduced me to the concept a while back when we started working with Rails, and then I’ve also seen some similar ideas in my scheme, er, symbolic programming class with Sussman. Ruby has been praised for how easily it accomodates creation and interaction of these domain specific languages, Rails being one of the leading examples.

Some resources that will hopefully act as hooks for me to learn more:

Writing Domain Specific Languages
Nice Ruby-oriented introduction.
More Ruby: method_missing
Related ways of overloading existing ruby methods to make them do special things.
Creating DSLs with Ruby
Another Ruby-based introduction from Artima.
First 6.891 PSet and solutions
Good scheme coding seems to essentially boil down to elegant domain specific language design.

I’m also looking at this stuff with an eye for the command line based physicalized information interaction system I’ve been slowly pushing forward. I’d like to expose a simple world model that makes it easy to map record fields to physical attributes of objects (ie price -> mass, name -> color, etc). I’m working in Python for this one, and I’m concerned that its rigidity (relative to Ruby) may be a hinderance.

Dangers of attempting ethnographic research while in a foul mood

Yesterday evening after a group project involving observing people “consuming media” in Best Buy and Borders, I found myself extremely challenged to concoct any interesting analysis of the experience. My frustration came out in my writing, and I guess I adopted a rather negative tone. As Jenn so nicely described it, I ended up dissing the majority of the mall going population:

The easily observable characteristics such as age, gender, race and apparent social relationships quickly seemed insufficient. I found myself wondering about the personalities and backgrounds of these people. Who goes to the mall on a Saturday afternoon? What sort of person hangs out in Best Buy to play guitar hero? What brings people to linger in the food court picking at cinnabuns to the drab sounds of Mall Radio?

I only ever visit the mall out of necessity, such as when I’m searching for easy Christmas presents for people I don’t know well, or when I need to simultaneously shop for pillows, coat hangers, pain relievers, and electric fans. Nowhere in the mall can I find items that I desperately want to purchase or consume. I’ve never had any happy accidents in the mall, never stumbled into something new and unfamiliar, never been forced to reexamine myself, my tastes or my assumptions. For me the term “mall” conjures only expectations of generalized boredom, a dead zone of mediocrity, far removed from both the enjoyable warmth of poppy kitsch and the happy surprises of the niche.

This attitude was obviously not shared by the many people we observed hanging around the mall that afternoon. Certainly some of them were like me, in and out for a specific goal. But judging by our observations in Best Buy and Borders, a significant portion of the people there were enjoying the mall as an experience rather than a shopping resource…

What to do now

A todo list on a blog is probably pretty damn boring, but for me this question of what to do (and, as Kelly points out, what not to do) seems to be an important one right now. My advisor has pointed out the meaningful experience of feeling comfortably lost: not knowing exactly where you’re going, but knowing that it is ok that you don’t know. So in an attempt to make this lost feeling more comfortable, here goes the list:

  • buy things for tea (today)
  • CMS group project (4/25)
  • 6.891 problem set (5/3)
  • 6.891 project presentation (5/3)
  • CMS final paper and presentation (end of May)
  • 6.891 project (end of May)
  • research on social and business contracts
  • finish edge collision detection on pyrticle (python physics system) and release code
  • get flights home for memorial day
  • make plans for july 4
  • make and send out cd club mix!! (very late)
  • glaze pots (next week)
  • dentist
  • more time with Jenn
  • finish blog layout
  • update résumé & portfolio
  • move postgres database to the linux machine
  • decouple openstudio transactions from documents (hard)
  • acts_as_sellable ruby module, or some similar integration of commerce into programming. make any object ownable by another object, sellable to yet another, with a history of owners. are objects that own themselves free?
  • scheme as contract programming language. disagree with John’s claim that C is the latin of computer science. look into ruby/scheme integration, perhaps via web services. scheme with thin ruby wrapper.
  • rhino contract programming as web service
  • burak and karen’s top secret project