Monday, December 31, 2018

Back to Subversion

First read https://svnvsgit.com/ which contains an excellent list of reasons why you should probably prefer Subversion over Git, all of which I agree with.

Granularity


Part of the reason Subversion works better for me is related to point 12 in the article. It's not that I need access controls, as I'm only using Subversion for my own projects, but I do appreciate the granularity. With all my projects in one repository I can export a directory, with full history, at any level: it could be a project, some small reusable part of a project, or a directory containing multiple projects with inter-dependencies.

With Git I either have to put multiple projects in one repository, which means I can't easily distribute them separately, or I have to put each one in its own repository which entails deciding what exactly each project is, and then dealing with the relationships between them outside of version control (or using submodules or subtrees which I will discuss in a moment).

It is useful to be able to distribute projects separately: I might have a well polished low-level library which I want to distribute, and a project that uses that library that's kind of a mess. The latter might a place where I explore new things before moving them into the core library; it might also be a testing ground for library functionality.

If I develop those things in separate Git repos, then the relationship between the two projects is not (easily) recorded in version control. Yes, you have Git submodules or subtrees, but they are more complex than simply having the two (or more) projects sitting wherever you want them in Subversion. Things are more complex because you have now different kinds of directories inside your top-level repository: normal directories and sub-repos. You cannot simply move a file or directory from one place to another and commit that change as a single atomic update if it crosses repository boundaries.

If you don't want to deal with submodules or subtrees (which many people don't) then any time you want to make a change that affects multiple inter-related projects, you will have to make separate changes to each repository. This is more work and puts a burden on your configuration management because you now have to know, for all the projects, which versions can be deployed with which. With Subversion you can make this kind of change as a single commit. Then every revision of the source tree automatically represents a valid configuration of project versions.

The granularity advantage of Subversion is also seen when deleting projects. With Subversion you can freely delete a project knowing that if you ever want to go back and look at it again, it will still be there in the revision history. Having deleted it in Subversion, you don't have to see it any more. With a Git repo, you can't really delete the whole thing if you think you might ever possibly want to look at it again. You have to keep all these old dead repos hanging around forever.

Branching


It might come as a surprise to realise that Subversion actually has more flexibility than Git when it comes to branching. If you work strictly with a trunk, branches and tags top-level, you can already do everything that Git allows (minus the risk of accidentally wiping out all your branches and tags). But you can also create branches and tags at any level you want, for projects here, sub-projects there, groups of projects elsewhere. You can also make side-by-side branches if you find it helpful to have project and project-v2, checked out at the same time.

Branching is conceptually simpler in Subversion, because it's not some special operation that changes the entire contents of the repository: it is just copying. The tip of each branch is a directory somewhere, and you can think of the set of branches as growing through time, with each revision being a cross section through all the branches. Every currently living branch is present in the latest revision.

With Git, branches are modal: you can only be on one branch at any given moment and whatever that is, it makes up the entire contents of your working tree. You can only really branch the whole repository and you can't have two branches checked out at the same time without having two copies of the repository checked out.

With Subversion you can safely delete a branch (directory) that you have finished with. It will always be there in the history if you ever want to look at it again so you don't have to keep it around. By contrast, when you delete a branch or a tag in Git, you've made it difficult or impossible to ever find it again. So, just like the old repos themselves, branches will tend to hang around.

The way Git does branches means that the tree structure is simpler: commits (each representing an entire working tree) can be represented as points connected by edges.

With Subversion, any pair of directories having common ancestry represents a branch structure, so branches can be different 'sizes' (they are not all whole-repo sized). Theoretically you could have branches inside branches, and do things like partial merges. A Subversion branch structure is potentially much more complex and subtle than Git's directed acyclic graph, and I'm not familiar with any tooling that might help with discovering and visualising that structure.

Sunday, July 24, 2016

The fundamental laws of C++ wrappers

  1. Every C-library has one;
  2. it is always out-of-date and often poorly designed;
  3. you are going to write your own anyway so you might as well start now.

Thursday, July 30, 2009

MVC web applications (part two)

In a traditional web application user interaction may be facilitated by JavaScript (validating form fields, cute animations etc.) but when the user needs new information from the server, form fields are POSTed to the server and the server responds with a whole new page.

This is actually quite an awkward way to work because HTTP is a stateless protocol: the only way for the server to keep track of where the user is with something is for the POST to tell it. That could mean posting back the entire state of the page, posting back an identifier for the session so that the server can remember the state, or some combination of the two. (Strictly speaking one can also GET the next page and send state in url parameters, though this is likely to violate the intention that GETs should not modify server state in any meaningful way and could fall foul of HTTP caching.)

It's not surprising then that a plethora of web frameworks have been developed to make things easier. Using one of these frameworks we can design the page layout and express it in some form of templating language. Parts of the page will be form fields and other controls (widgets). The framework takes care of re-rendering the page each time it is posted back to the server by some user interaction. It also takes care of tracking the state of each widget. Somewhere in the page's definition we can write handlers for events which will appear to come directly from user actions, but in fact are generated by the framework when the server receives a post.

With such a framework most code is executed on the server, and the approach to building an application is very like that of building a GUI for a desktop application. Thus we can practice MVC in just the way we usually would.

The downside of this approach is that each time the user does anything on a page they will see it reload. Depending on server load and network latency, they could even be waiting for some time before they can carry on.

The alternative to the traditional approach is what has become known as AJAX. I'm not a big fan of the term. Given that it basically just means using XMLHttpRequest (nowadays), and that XML is often not involved at all, it is hardly an illuminating acronym. Nonetheless, that's the term which has stuck.

As with a traditional application, a page will be rendered with various widgets and whatever other eye-candy the designer and developer care to throw in. The difference is that most of the code running the application is written in JavaScript and runs on the client. When the user does something which requires information to pass to or from the server, the JavaScript code sends a GET or a POST to the server using an XMLHttpRequest. The server responds to requests not with a complete web page but with some fragment of information, which may be HTML, XML, or very commonly JSON. The response is passed back to the client side code and is used to update the page (without a reload).

Curiously, despite the various difficulties involved, it is actually a much more natural way to think about running a web application. The page just stays where it is so there is no problem with remembering state. The page communicates with the server via messages which need only contain enough information for the task at hand. For operations which may affect the appearance of the page but not require information from the server, there is no need to talk to the server at all. If web frameworks hadn't made the traditional approach manageable, I'm sure we wouldn't ever consider writing a web application any other way.

Now, a client-server application, of which a web application is one example, usually has multiple tiers. One of these is the client tier which talks directly to the user. It contains the code for managing presentation and user interaction. It also talks to the middle tier(s). The middle tier takes care of authentication, validation and other business logic, load balancing and what not. The last tier is typically a database. It takes care of the integrity and reliable persistence of data.

It may be tempting to think that the layers of a three-tier application are synonymous with the view, controller and model respectively, however (as far as I can tell) this isn't really the case.

In my recipe builder, described in part one, the initial page load shows a list of recipes and two empty lists. JavaScript is used to make list items selectable. The list of recipes is built as an HTML table server side using PHP:

<?PHP require "php/getRecipes.php"; ?>

The named PHP script queries the database for a list of recipes and writes them out as an HTML table. The rest of the main page is pure HTML. I could mix PHP and HTML but I don't. If that's your dirty little habit, just don't come near me with it.

Quick summary:

  • The database contains a table of recipes with numeric ids, a table of ingredients with numeric ids, and a table of ingredient_id, recipe_id pairs.
  • When the user selects a recipe, an XMLHttpRequest is sent for php/getIngredients.php?recipe_id=nn. This is then inserted as the innerHTML of the ingredients list.
  • When the user selects items from the ingredients list and clicks the 'Add selected' button they are copied to the shopping list. No server interaction is involved because I have not yet made shopping lists persistent.
  • Other features, such as the ability to add and delete both recipes and ingredients but that's probably enough for now..


There may be more than one way to realize this as MVC but I'll now describe what I did.

Because we're going to be using events quite a bit, I have Publisher class which all my model and view classes can inherit from (or mix in). There are two methods called 'on' and 'fire'.

// f is some function.
var p = new Publisher;
// subscribe f to select events on p.
p.on('select', f);
// cause f to be called with argument 'a'
p.fire('select', 'a');


What's the model? Certainly the database is part of the model. But if the model lives entirely on the server we're going to have a problem doing MVC because the model is supposed to publish events to which the view can subscribe. There are ways of getting the server to push events to the client but none that I know of are particularly straightforward and I'd rather not go there. Instead we'll make a proxy for the model on the client.

In fact, we make proxies for three models: the RecipeListModel, IngredientListModel and the ShoppingListModel. All of them implement methods 'addItem', 'removeItems' and may fire events 'ItemAdded' and 'ItemsRemoved'. The IngredientListModel also implements 'setRecipeId' and fires 'ItemsChanged'. Using David Flanagan's 'http.js' script, the code for 'addItem' on the IngredientListModel is

this.addItem = function(ingredient) {
var info = {recipe_id:self.recipe_id, ingredient:ingredient};
HTTP.post('php/addIngredient.php', info, function(id) {
self.fire('ItemAdded', ingredient, id);
});
};

In other words, we send the current recipe_id and the new ingredient to the server and when it tells us what id it has assigned to the new ingredient, we fire the 'ItemAdded' event.

So our models now behave pretty much as they need to to participate in MVC. We don't handle the case of another page updating the database at the same time. For that we'd need to get extra messages (events) from the server to the client, either piggybacking on the response to one of our posts or by some other means. Be that as it may, it's at least reduced to a question of exactly how full-featured we want our model implementation to be; the interface doesn't need to change.

What about the views? There is some JavaScript code for making a table selectable and registering handlers for 'Select' and 'SelectionChanged' events. On top of this is a ListView class in JavaScript to which I can pass a model with the interface described above. Select events from the underlying JavaScript augmented table simply fire 'Select' events of the ListView.

When I first wrote this there were separate classes for the RecipeListView and the IngredientListView. They talked directly to the server, not to model proxies, made different method calls for recipes v.s. ingredients, and directly implemented the required results of the 'Select' event. As you can imagine there was nothing remotely testable or reusable about them.

Now there are only two differences between the ListViews we create for the recipe list and the ingredients list: the recipe list is single-select and it is passed a table already present on the page; the ingredients list is multi-select and creates its table element from scratch, inserting it into a div which it is given as the designated container.

Which brings me to the point which gave me the most trouble in thinking about how to apply MVC here. When the model fires an 'ItemsChanged' event the ListView has to reconstruct its table from scratch. (Only when we're using a table in a container div do we handle 'ItemsChanged', which is not to say we couldn't do it but not in quite the way we do.) I did not want to write PHP code to send back the list of ingredients as JSON and then have the JavaScript build its table from that. Why? Because getting PHP to write HTML is just as easy, and installing HTML into a div on the client is just 'container.innerHTML = text' rather than some big ugly mess. So I endowed my IngredientListModel with a getAsHTML method (passing the response to a callback because XMLHttpRequest is asynchronous) and let the ListView call that when it gets an 'ItemsChanged' event.

Now we've mixed model and view: the model is constructing part of the view by returning HTML. Not only that but it's doing it on the server!

This is interesting though. We already were constructing part of the view on the server with the original page load. We certainly don't want to start with a blank page and construct everything on it using JavaScript. So it looks like some of the view will always be implemented server-side.

So perhaps these are not really pure model proxies, they are proxies for calls to the server and they allow us access to parts of the view there as well. As far as the ListView is concerned, some part of its implementation is moved server-side via the getAsHTML method. I'm not sure I have any good way to explain this away other than to say that I think it would have to be a common compromise to have to make when applying MVC to an AJAX web application.

Funnily enough, it doesn't look so bad from the server-side perspective. We have a bunch of PHP scripts each of which represents a remote procedure call essentially. They query the model, or update it and they return their results in HTML or JSON. The ones that return HTML (including the initial page load) are part of the view (already bound server-side to a fixed model) and are necessarily concerned with layout and presentation. And indeed there are other cases where we want to do some of our view-related work server-side; notably computation and storage intensive operations like image processing.

With that out of the way, the rest turns out to be just as simple and elegant as MVC in a desktop application.

There is a top-level JavaScript view object which is associated with the entire page. It locates all controls on the page by id, hooks up event handlers to forward events for user interactions, exposes properties to allow the controller to query the state of the page, and instantiates ListView objects for its non-trivial sub-views.


function MainView(rlModel, model, slModel)
{
var recipeList = this.recipeList =
new ListView(rlModel, {table:id('recipesTable')});

var holder = id('ingredientListHolder');
var ingredients = this.ingredients =
new ListView(model, {
title:'Ingredients', holder:holder, multiSelect:true});

var recipeSearch = this.recipeSearch = new SearchBox('recipeSearch');
recipeSearch.fetchCompletions = function(prefix, cb) {
HTTP.post('php/completions.php', {prefix:prefix, table:'recipe'}, cb);
};
...
}

The 'id' function is a short-cut for 'document.getElementById'. I have not bothered here to go through a model proxy when hooking up the fetchCompletions callback on the SearchBox view. (This component was not written with MVC in mind; I might do it a little differently now.)

The controller is also quite simple and straightforward.

function Controller(rlModel, iModel, slModel, view)
{
var focusList = null;
var compact = false;

view.recipeList.on('Select', function(recipe_id)
{
iModel.setRecipeId(recipe_id);
focusList = this;
});

view.ingredientSearch.on('Enter', function()
{
var ingredient = view.ingredientSearch.getValue();
iModel.addItem(ingredient);
view.ingredientSearch.clear();
});
...
}


And that's really about it.

The conclusions I've drawn at this point (which may be subject to future revision) are that

  • Part of the model has to live on the client as some form of proxy. Notifications from the model proxies may be generated purely client-side.
  • Parts of the view will always live server side. Some of the binding of views to models will therefore also be taking place server-side. This appears to mean that the model proxies will be a little impure.
  • Overall MVC still works really well in this setting.

Tuesday, July 28, 2009

MVC web applications (part one)

The Model-View-Controller (MVC) pattern is amazing. It seems to have the power to turn a tangled and confusing mess of code into something clean and apparently simple. It seems to magically turn special purpose UI code into elegant reusable components. It separates that almost untestable thing, the event driven GUI, into lovely little testable chunks. Well, I confess I haven't yet got around to proving that my chunks are testable, but I know that what I had before was not!

About half-way down this page there is an excellent diagram which summarizes very nicely the responsibilities and interactions of the actors in MVC.

I've been teaching myself about MVC by applying it to two little projects of mine. The first is a password saver called Bob. It saves encrypted passwords on a web-server so that you can pick them up wherever you happen to be; it also keeps a local cache so that you still have access to it even if you are not online. The client is a typical desktop app written in Python/Tkinter.





The second is a shopping list builder. You have a list of recipes you can edit, each of which has a list of ingredients. To build a shopping list you choose a recipe, select the ingredients you'll need to buy, and click a button to add them to your list. I like the retro feel of this: back in the day when home computers were new and nobody really knew what to do with them, recipe databases were all the rage! The client in this case is a web page with JavaScript / DHTML list boxes and so forth. It talks to a back-end built on PHP and MySQL.



In this part I'll just be talking about Bob. In the second part I'll talk about what (I think) I've learned about applying MVC to a JavaScript / DHTML web application.

Originally Bob was just a command line script. In fact I can still run it that way if I need to, and the model part of the code is shared. The model is a PasswordDB object which encapsulates encryption, local caching, and talking to the server. I won't be talking about how any of that works in this post.

class PasswordDB(object):
def __init__(self, server, db_name):
# sets the server and database to sync with
def unlock(self, master_password):
# sets the encryption key to use
def set(self, account, name, password):
# save user-name and password for the account
def get(self, account):
# retrieve user-name and password for this account
def accounts(self):
# get list of all accounts
def sync(self):
# synchronize with the server

Mostly, this just behaves like an associative array mapping accounts to (name, password) pairs. It must be unlocked before get and set can be used. (The list of accounts is not encrypted, just the passwords.) For the command-line client this is all we need.

For the GUI we want a little bit more. We display a list of accounts so that clicking on the account allows us to see the user-name and password. When we add a new password or sync with the server the list of accounts may change and we want it to stay up to date in the UI.

If we just wrote what came naturally, we'd hook code up to the 'Save password' button (or the 'Sync' button) which first called set (or sync) and then refreshed the list view from the PasswordDB.

# non-MVC code
def handleSaveClicked(self): # bound to the Save button
account, name, password = ... # query input fields
self.model.set(account, name, password)
self.view.updateAccountList() # don't forget!

Why is this less than ideal? Simply because it means that every piece of code which modifies the model must also know about the UI. Not just about the account list but about any user-name and password currently on display which could be changed by a 'Sync' operation. Given m possibly affected UI components and n ways to change the model we have to write up to m*n refresh calls.

There is a much better way, which is to put the model and view into a publish-subscribe relationship. This is how the MV part of MVC works. We extend the model with 'save' and 'sync' events. The view can subscribe to these events by providing a callback which the model will call whenever 'set' or 'sync' are called on it. Here is a simple way to do that in Python:

class PasswordDB(object):
def __init__(self, ...):
self.set_listeners = []
...
def set(self, account, name, password):
...
for listener in self.set_listeners:
listener(account, name, password)

Now the code behind the 'Save password' button needs to do one thing only: call 'set' on the model with the appropriate parameters. The model tells the view and the view updates itself.

The view knows about the model: it has to call query methods on the model ('accounts' and 'get') but neither the model nor anything else which updates it need to know anything about the view. The way a view and a model interact is pleasantly stylized. The view gets a reference to the model. It subscribes to all relevant events on the model, providing callbacks which tell the view to query the model (if necessary) and update itself appropriately.

class AccountListView(Tkinter.Frame):
def __init__(self, model):
model.set_listeners.append(self.handleSet)
...
def handleSet(self, account, name, password):
if account not in self.accounts:
# insert account into the view


For the very simple case, we've written a bit more code than we had before. On the other hand we've conceptually simplified quite a lot. The model is responsible for storing our data, providing methods to query and update it, and telling us when anything has changed. Each view (and there may be several, and/or several components within a view) is responsible for tracking the state of the model. They subscribe to change events and calls query methods of the model.

Which brings us to the Controller. Our view has a 'Save password' button. Before we knew about MVC we'd probably just have the code behind the button get the selected account, and the name and password to save, and call 'model.set()' itself. Is this a problem? Well, maybe. The point is that a lot of different things could happen when we press a button. Suppose we wanted to check if the password was strong enough before letting it through. (Humour me.) We'd have some rather domain-specific code in the middle of our UI code. Another button, the 'New' button, makes the normally read-only account field writable so that a new account can be added; it changes the state of the UI.

We can take these functions out of the UI code and thereby reduce its set of responsibilities. That's a good thing to do! UI code is already (well, depending on the framework you're using, may be) responsible for layout and tracking the model. Rather than trying to do more, when the user does something -- selects a row, clicks a button, presses enter, or whatever -- the view will just fire an event and let someone else deal with it.

Although we don't want to prematurely generalize, notice that this approach gives us a wonderful opportunity to write reusable view components. A scrollable list of accounts is really just a scrollable list of short strings. Rather than give it our PasswordDB directly as its model, we can give it a small adaptor which views the PasswordDB as a collection of strings. All events and query methods can now have completely generic names, and the scrollable list can be used wherever we need such a thing. We can only do this if we don't have to know in the UI code what happens when the user clicks on a row: if it has to call view.showAccountDetails it is not reusable; if it simply fires the 'click' event, we can reuse.

As with model and view, the key to decoupling is to put view and controller into a publish-subscribe relationship. The view is responsible for firing events to report user interaction. The controller listens for those events; in response it may query the view (what does this input field contain etc.) and then it may update the model or modify the view in some way. Those are the two responsibilities of the controller: model updates and view selection. It can carry out validation of input before updating the model. It can change what the view displays, including such things as moving through a series of screens, causing a modal dialog to be displayed, enabling various controls and so forth.

Note that when the controller updates the model, it does not have to update the view as well; that just happens.

Because the controller has to know about both views and models, it is the least reusable of the three. On the other hand, by divesting it of responsibility for appearance and the underlying data, we have at least minimized the amount of code that has to be so specific. We have also made it testable: by providing it with mock views and models we can write unit tests for it.

Here is what a part of Bob's controller looks like:

class Controller(object):
def __init__(self, model, view):
self.model = model
self.view = view
self.view.save_listeners.append(self.doSave)
...
def doSave(self)
account, user, password = self.view.getAccount() # queries input fields
self.model.set(account, user, password)


So how does it all look in the end:

  • The model knows nothing about views or controllers. It is responsible for the integrity of the state it manages and for publishing notifications when the state changes.
  • The view knows about a model (which may be a simple adaptor in the case of a generic component). It is responsible for its own appearance, for tracking the model, and publishing notifications about user interactions. It must also provide query methods for any user-modifiable state.
  • The controller knows about the model and the view. It is responsible for turning user interaction events from the view into updates on the model and/or changing the view.

Tuesday, May 13, 2008

Is it just me?

I can do what I want in Python and I can do it in C++ but I'm having trouble doing it in Scala. (Replace A and B with HandInfo etc. from the previous post and you get my use-case.)

Suppose we have classes A and B, functions int fa(A&) and int fb(B&), and a class env with public instance members

struct env
{
A a;
B b;
}

Define "evaluation of fa or fb in the environment provided by env x" to mean f(x.a) when f is fa and f(x.b) when f is fb.

Problem: create a type which is able to hold references to fa or fb and, given an instance of env, is able to evaluate the held function.


#include <string>
#include <iostream>

using namespace std;

// Setup for the problem.

struct A
{
int member;
};

struct B
{
std::string member;
};

struct env
{
A a;
B b;
};

int fa(A& a)
{
cout << "fa called on an A with member = " << a.member << endl;
return a.member;
}

int fb(B& b)
{
cout << "fb called on a B with member = " << b.member << endl;
return int(b.member.length());
}

// First define a generic way to extract the member of type T from
// an instance of env. Note that this is non-intrusive: whatever
// the definition of env is, if it is possible to get an A and a B
// from it, the specialisations of tselect can do so.

template <class T>
struct tselect
{
};

template <>
struct tselect<A>
{
static A& member(env& x) { return x.a; }
};

template <>
struct tselect<B>
{
static B& member(env& x) { return x.b; }
};

// Check tselect performs as intended..

void test0(env& x)
{
cout << "a is " << tselect<A>::member(x).member << endl;
cout << "b is " << tselect<B>::member(x).member << endl;
}

// Interface for a held function.

class FuncHolder
{
public:
virtual ~FuncHolder() {}

virtual int invokeInEnv(env& x) const = 0;
};

// Generic implementation. This scales well:
// if we later want to add C,D,E and F we don't
// need to write any new code here.

template <class T>
class FuncHolderImpl : public FuncHolder
{
public:
typedef int (*funcT)(T&);

funcT func_;

FuncHolderImpl(funcT func) : func_(func) {}

virtual int invokeInEnv(env& x) const
{
return func_(tselect<T>::member(x));
}
};

// Convenience function for creating FuncHolders.
// In the event that fa or fb were overloaded we'd
// have to fall back on explicitly choosing the
// type of the implementation class to construct.

template <class T>
FuncHolder* makeNewHolder(int (*func)(T&))
{
return new FuncHolderImpl<T>(func);
}

// And here it is..

int main()
{
env e;
e.a.member = 1;
e.b.member = "hello";

test0(e);

FuncHolder* fha = makeNewHolder(&fa);
FuncHolder* fhb = makeNewHolder(&fb);

fha->invokeInEnv(e);
fhb->invokeInEnv(e);

return 0;
}

When compiled and run this produces

a is 1
b is hello
fa called on an A with member = 1
fb called on a B with member = hello

Templates keep track of all the type information. They know that fa needs an A and the tselect template knows how to go off and find one.

How about Python, a language in which templates are not needed because there are no static types to worry about? We need to give it just a tiny bit of help because fa can't tell us it needs an A: it doesn't know itself.


class A:
def __init__(self, m):
self.m = m

class B:
def __init__(self, n):
self.n = n

def fa(a):
print 'fa called on a with a.m = ', a.m

def fb(b):
print 'fb called on b with b.n = ', b.n

class env:
def __init__(self, a, b):
self.a = a
self.b = b

class FuncHolder:
def __init__(self, func, argTypeKey):
self.func = func
self.argTypeKey = argTypeKey

def invokeInEnv(self, e):
return self.func(e.__dict__[self.argTypeKey])

fha = FuncHolder(fa, 'a')
fhb = FuncHolder(fb, 'b')

e = env(A(1), B('hello'))

fha.invokeInEnv(e)
fhb.invokeInEnv(e)

No doubt there are ways to make this cleaner and slicker but even as it stands it's usable and fairly concise. We give the FuncHolder a key telling it what type the function it is holding requires as an argument.

So what goes wrong when I try to write this in Scala using type parametrization? Firstly, because of type erasure, a generic FuncHolderImpl[T] can't decide what to do on the basis of the type of T because this is not known (inside the object). The only way it can change what it does is if it has an object of type T or related to it: then it can call an overridden member of some base of T to get it to do the work that varies. So we have to give it some kind of type marker (or at least that's what seemed like a possible solution).

But then we run into the second problem. Nowhere can we write code like this:

if (...)
f(e.a)
else
f(e.b)

One branch or the other won't typecheck. Making a common base doesn't work unless, for each and every function, we write a second version which takes the base but expects the right subclass and casts to it (via pattern matching).

I've tried all sorts of other permutations but for some reason which I can't quite put my finger on, this seems to be hard to do. Suggested solutions in Scala or Java are most welcome.

Looking for help with homework assignment

Bridge is a great game. I play with an 80 something y/o lady and it's good to be able to look around the room and feel like one of the young ones. (Sad too, I won't have anyone left to play with once I'm her age.)

One thing which separates the great players from the rest is the ability to count the hand: after a few rounds of cards have been played and one or two people have failed to follow suit it is often possible to tell exactly how many cards are left in each suit, in each of the hidden hands. I can't do it, it takes too much effort and concentration. And if I tried it too often in a real game, people would tell me to get a move on before the director comes and takes the cards away.

So to get practice I thought I'd play against Bridge Baron. (Nowadays there are probably much better programs out there.) Didn't work. I am so impatient with the stupid program, and hate losing to it so much, that first I forget about counting and just try to win, then I get tired and start losing (my partner's fault) and eventually give up in disgust. Sigh.

So I set myself a homework assignment: write a program which plays the cards as if we were playing a hand but doesn't know anything about trying to win so it won't sucker me into thinking about that side of it. The program allows me to say at any point how many cards are in each hand and tells me how clever I am if I get it right.

I wrote that in Python and wrote a retro gui for it using curses. All good fun. Practiced for a while. Eventually it got boring and still I wasn't much better at counting the hands.

Something was missing. Mostly in real games you get a lot of help from the bidding: declarer opened 1S so he has at least 5 of them. That means partner has at most one, so play my ace and give partner a ruff. My program was missing out on this much more satisfying aspect of counting.

Homework assignment #2: write a simple-minded bit of code to bid the hands so I can make inferences about the distribution and get much more realistic counting practice.

Actually, it's hard. (I found it hard. If anyone has any smart ideas how to make it easy..)

Here is where I started heading: each hand has a shape (4 numbers with sum 13) and a number of high-card points (hcp). That's enough to do some simple bidding with. To select an opening bid you need some rules that look a bit like this:

3 < hcp < 10 -> open pre_empt(longest)
6 < hcp < 12 and suits_maxlen == 6 -> open weak_two(longest)
16 <= hcp <=18 and !5cardmajor -> open 1NT
12 <= hcp < 21 -> open one(5cardmajor ? longest : longest-minor)
...

Lovely. And that's just the opening bid. The rules for partner to respond are different and depend very much on what was opened. The rules for the people to the left or right of the opener are different again and equally sensitive to what bidding has gone before.

So let's take a step back and see what we need. It looks like we need shape and points for the hand we're bidding (a HandInfo class), information from the auction up to this point (an Auction class) and inferences from the auction as to the shape and strength of partner's hand and the opponents (if we trust their bidding!) so a HandInferences class maybe. (Theoretically that's all derivable from the current Auction object (and the bidding system being used) but it is going to involve significant work to calculate it, so we'll make it a separate class.)

Then we'll have expressions like the ones above to define the bidding system. At each point in the auction the program will have to evaluate various boolean valued expressions to determine what bid to choose. The decision tree selected for evaluation will depend on the bidder's CurrentRole, which would be something like "opener" before anyone has bid, "responder" for the partner of the opening bidder and so on.

In our expressions each (integer valued) symbol, other than a constant, will take its value from one of the three objects of type HandInfo, Auction and HandInferences. The hcp symbol comes from the HandInfo object; a partners_suit would come from the Auction object; partners_length(suit) (a function symbol) would need to use the HandInferences object.

One more thing about the HandInferences is that we have to get them by analysing the Auction and the bidding rules. For that reason we can't simply express the rules as code: we have to be able to traverse the expression tree in one way to evaluate it and in another way to infer from the selected bid what constraints apply to hand shape and high-card points. (Code as data sounds very lisp-like and perhaps I will find I have to go there to solve my homework assignment eventually.)

There's more but let's forget about bridge now: we have an expression tree representing arithmetic and boolean values and we have objects which form part of the evaluation environment. We want certain leaves of the expression tree to take values from objects in the environment.

My first stab at doing this was in Python. The expressions were just in strings and I used eval(expr_text, environment) to evaluate them. I had to do some text parsing to analyse the expressions for computing the hand inferences and it kind of worked but it was getting to the point where eval() on strings was becoming a liability. Then I got a job and had no spare time any more so it got shelved for a while.

Two years later. I've learned a lot of about C++, JavaScript and C#. I've lurked on Artima and played with fun stuff like Ruby and Erlang. The latest language to sound exciting is Scala and I decided to resurrect my bridge bidding project and rewrite it in Scala as a way to learn the language. There is a lot to like about the language and really, I've only scratched the surface, so don't come here for a deep critique. I did run into a curious an frustrating problem though which I'll describe in my next post.

They told me to do it

so I'm doing it.

I'm going to want to post code.. lemme see now

#include <cstdio>

int main()
{
printf("hello world\n");
return 0;
}
Yay!!

Thank you to Guogang Hu.