Sunday, November 08, 2009

F# under the covers VIII

I'm in the tidying up stages for the little project I've been working on lately, a set of FxCop rules, mainly aimed at providing some complementary features for nCover-style code coverage, including static analysis for trivial methods -- or compiler generated ones. I've done the red -- add a set of methods that should give expected static analysis results -- and green -- write the simplest implementation that works when given the code base to analyse as a post-build activity. And now I'm on the refactor leg, abstracting out the common activities, taming sprawling methods into tighter ones, and reducing the level of imperative features (perhaps also making the code more idiomatic), even at times managing to delete whole methods.

And while I'm doing so, I'm running various tools over the code; FxCop and nCover over a small nUnit run every build, of course, but in turns Reflector and NDepend -- the results of which have been the inspiration for this series of posts.

And as we have seen, the code emitted by the F# compiler in its various iterations is unlike that coming from the more traditional .net langauges, in a way that doesn't always play nice with tools developed against C# or VB.net (and perhaps C++/CLI).

NDepend will deserve an essay of its own in due course -- part of the quid pro quo for the copy that Patrick Smacchia has generously donated -- but today's point of interest is that some IL code generated from F# cannot (at least with current Reflector builds) be easily folded back into C# source.

Take this active pattern used to match a property getter method that just returns a backing field with a name related to the property name, which is at a current intermediate stage of refactoring

where MapToTypes turns an FxCop StatementCollection into a list of their corresponding NodeTypes, and @ wraps String.StartsWith with ordinal comparison.

Confronted with a request to decompile into C#, Reflector 5.1.6.0 asks me to file a bug report (which I have done) for an exception "Invalid branching statement for condition expression with target offset 002A."

Now, as far as I can tell from the IL, this is part of line 2 where it is balking

Presumably -- I would have to build some equivalent C# code to validate -- the brfalse.s is not one that C# (or any of the other languages Reflector knows of) would actually emit. Certainly it seems that the new F# CTP compiler is fond of emitting it, and it breaks Reflector every time it does.

Another good reason for avoiding too many imperative constructs in your F# code, it would seem.


Later: this C# code that approximates the above F#

compiles to

which indeed, as I suspected, uses a brtrue.s for the final branch out of the if statement (though there is a brfalse.s to perform the short-circuiting.

Code generation here is unchanged in 1.9.9.9 from the previous CTP.

Tuesday, November 03, 2009

F# under the covers VII

Working with nCover to examine the number of code-points represented by any given method, the October CTP made a large number of changes -- some up, some down -- to the code generated by the compiler.

Most interesting is the change to attribute setters, which used to be NOP;assign;return and are now just assign;return. Also the name of the implicit backing field has changed to be the property name with an appended '@', rather than being a case-mangled version of the property name.

(As always, talking about Debug builds).

Later ... This one is a bit too cryptic for me to decipher what I was writing about. Code like this:

Has internal fields like Line@ still at 1.9.9.9; and property setters as in

compile to the NOP-bearing

with the Feb 2010 CTP.

Links for 3-Nov

Vista presentation mode tips and enabling applications.

Attaching a debugger on process start.

Simplicity is complicated.

F# immutable queue and range.

Validating JavaScript syntax with a .net wrapper.

Underscore -- a functional programming utility library for JavaScript

Monday, November 02, 2009

Nature notes

Driving home, after dining with colleagues, in the hard bright moonlight of a Hunter's Moon, I actually saw a live badger ambling along the verge at one point, a rare spotting of some of the larger wildlife around here.

Quite how starkly bright a winter full moon can be always comes as a surprise.

Also, still harvesting a last few tomatoes from both outdoor and greenhouse plants.

Friday, October 23, 2009

The last days before winter


View Autumn Holiday - Thursday 22 Oct in a larger map
View Autumn Holiday 23 Oct 09 in a larger map

A quick cycle break in Constable country.

After torrential rain on Wednesday, everything was dripping on Thursday morning; and my first fording was immediately after turning out of the hotel drive. The route to Nayland was on single track roads, deep cut, so not terribly pleasant, but the leg to the west was open, and by this time the sun had burned off the early haze. I avoided the bridlepath return, diverting through Wormingford, and then took the main road to Stoke-by. The next leg started off again narrow and steep -- not pleasant cycling; and the land south of the main road towards the A12 is drear and desolate, so it was pleasant to come to Dedham, a pretty little village, for lunchtime.

The footpath to Flatford Mill was surprisingly free of any sign of the previous downpour, and the walk was nice, in the warm sunshine -- T-shirt conditions. Ate packed lunch on the riverbank, then returned to find my bike swarning with ladybirds.

Long loop to the north into more open country away from the river valleys, crossing the ford at Kersey dry-shod -- then just after Boxford getting one foot wet when suddenly encountering another. And so back, in the rapidly cooling evening sun -- at the hotel just after 5pm.

Friday, a shorter tour of the same sort of area -- sometimes avoiding narrow lanes where the vans and landrover+trailer traffic meant I had to squeeze to the sides, especially on the Stokt to Higham stretch. Despite the delivery van stuck behind me, the Green Lane north from Higham was the most pleasant such -- mainly because it was not cut 4+ feet into the terrain, and there were no high hedges blotting out the light.

Hot roast lamb with mint sauce baguette at the Cock Inn in Polstead, then the final stretch, reversing some of the previous day's, and, forewarned, getting dry across the ford this time. Again, a glorious day, again with none of the threatened showers -- just gorgeous late late Indian Summer weather; a third time lucky for this year's cycling holidays.

Tuesday, October 20, 2009

F# under the covers VI

First results from the October CTP, as ever, using Debug build


turns up FxCop warnings

warning : CA2208 : Microsoft.Usage : Method '...' passes 'source' as the 'paramName' argument to a 'ArgumentException' constructor. Replace this argument with one of the method's parameter names. Note that the provided parameter name should have the exact casing as declared on the method.

because the operation is in-lined, with code that maps back to C# as


and then immediately follows this with a similar in-lined ArgumentException.

With the new CTP we now get, on a method that used to check clean

On method 'Patterns.As<Target>(object)', prefix generic type parameter name 'Target' with 'T'.

but that's just a minor and easily fixed issue; and also


now gives

warning : CA1810 : Microsoft.Performance : Initialize all static fields in '...' when those fields are declared and remove the explicit static constructor.

A simple test with 1.9.9.9 reproduces the first two, but not the last. The object is now a static member of the synthetic <StartupCode$[assembly name]>.[FileName] static class, and exposed as a property of the module.

F# October CTP first impressions

It's nice to be able to get rid of all those OverloadID attributes; and to see that attributes now do need to have the AttributeTargets.Constructor usage to be applied to constructors.

Not so good -- output from a post-build step no longer appears in the Visual Studio output tab (at least it didn't for me); and when running unit tests from such a post-build step, an F# method being called from a C# test with an final expression of unit caused nUnit 2.5.2.9222 to fail with no diagnostic message (would have needed a post-mortem debug). Workround was to replace () by ignore aValue where ignore _ = (); but I'm still not sure what was going on there.

The code generation (debug builds at least) has changed significantly, as I'm now getting a number of FxCop warnings from code that ran clean with the May CTP. More on that, later.

This funny behaviour -- from code that maps to C#


remains in the February 2010 CTP, but is resolved by code that looks like


Go figure!

Monday, October 19, 2009

Review catch-up — Anime and Manga

Manga : Bokurano

There was a long pause in the scanlation of the manga, broken around the airing of the anime, leading to a somewhat fractured impression when following it as it became available.

It's Mohiro Kitoh, so of course, bad things happen to children. In Narutaru, he took on Pokémon; here, he subverts the super-robot genre in a way to make Neon Genesis Evangelion seem like a happy WAFFy tale. Every time you think he's done everything, he finds a way of twisting the knife again.

The real weakness is the ending -- the epilog chapter really didn't add anything that could not be inferred (and partly relies on your ability to remember the subtle distinctions between one and another of his character designs).

Anime : Sengoku BASARA

16th century Japan as it was in the movies, or at least ought to have been — on about the level of Henry VIII vs Oliver Cromwell with Maxim guns. Superpowered ninja, high-level fighters with awesome feats, forsoothly Japanese mixed with heavy-duty Engrish. Plus Norio Wakamoto as the arch-villain Oda Nobunaga.

Switch your brain off, put ya guns on, and enjoy!

Asura Cryin' first season

A series that bundles together so many standard elements — supernatural, high-school drama, miko, ghost-girl, yakuza, demons, childhood friend potential love interest, super-robots, school councils (three of them!), apocalyptic threats — that there is no room left for anything like a coherent plot.

Alas, the only other real mindless action series this year after Sengoku BASARA.

Saki

It's about school-girl mah-jongg tournaments, with the same character designer as last year's Strike Witches.

Akagi this is not : there is little of the psychological elements, the winning hands so casually made are even more improbable, and the metaphorical struggles get elevated to full-on mahou shoujo at times. But being about an all-girls competition, it redresses the gender balance.

Indeed, rather than taking many episodes over one hand, the actual games get rattled through rather briskly, with the focus being on the girls, and their individual back-stories and improbable playing styles, like "Stealth" Momo, who is so socially invisible, that nobody notices what tiles she discards or when she declares Riichi -- except for Nodoka, who has to pretend she's playing on-line (and thus ignore any of the people at the table in any case) or the real hax players who just have too many levels of talent to be slowed down by such things.

The real shame is, that, having developed so many interesting characters, most of them will fall by the wayside as and when a second season gets made, since they have been eliminated from the current tournament level. And there has at least to be the potential for a second series, as the driving motivation for the title character is to play against her estranged sister, and that can only happen in the next rounds of the tournament.

Chi's New Address

Continues last year's Chi's Sweet Home; as the family move from their old apartment to a new pet friendly one and settle in.

Old characters return, and new ones are met -- but, alas, it stops being quite the acute observation of what a kitten is about, and becomes rather more tales of anthropomorphised animals; still cute, but not quite the same.

Review catch-up — Films seen recently

Gilliam's The Imaginarium of Doctor Parnassus

The state of the art of SFX is such now that he can do in CGI/live action what he was doing with paper back in the Python days; and it managed to carry off the multiple actors filling in the gaps in Heath Ledger's role in a smooth and unobtrusive fashion (were it not for the fact that Ledger's Australian accent stood out from the understudies).

But (and you were waiting for the 'but') I found the supporting characters grating and unsympathetic, and the inclusion of "socially relevant" elements made the story lurch back and forth too much. Inclusion of the sordid nature of contemporary London for contrast with the pure world of the imagination, yes; but the mood swings were a bit too violent and "am I relevant yet?" in nature.

His Munchhausen was better, IMHO.

Once upon a time in the West

Reissued, remastered, very much of its time, a fairly archetypal "Man with No Name" western, where, with Charles Bronson as the revenge driven guy in the white hat, there are really only shades of grey.

District 9

This savage black comedy is the best new movie I've seen in a long while, with hints of Python and early Peter Jackson in a mix that savages so many stereotypes.

Saturday, October 03, 2009

SyntaxHighlighter 2.0 Brushes for F# and Erlang

Updating the Erlang brush

This also required the following in the CSS theme


And for F#


which is as incomplete as it always was; but doesn't use any extra CSS.


Later -- another brush for F# that was published at about the same time can be found at Pense-Moi.

Migration to SyntaxHighlighter 2.0

This will be coming soon. For the moment a little note to myself, expanding from my first how-to for version 1.5:

becomes


To get the legacy mode to work, I had to put the initialization into the onload function.

I'm not rolling this out until I can get the rewritten F# and Erlang brushes to work and not throw _a2.regex is undefined messages.

Later: There are a lot of initial-letter capitalization changes (in both directions) required in this port : the "_a2.regex is undefined" messages came from more of them than at first met the eye. I still can't find the equivalent mechanism to injecting CSS styles, though.

Later yet: There doesn't seem to be a programmatic injection of CSS values any more, so I hacked the values from the 1.5 brushed into the default CSS file.

Even later: To allow legacy and non-legacy highlight styles, the legacy part of the operation must be in the onload, the 2.0 initialization must not.

Yet more: A possible bug in the CSS as supplied -- I needed to add the following

to shCore.css to get the copy-to-clipboard icon to show in the toolbar.

Friday, October 02, 2009

C# under the covers

In an earlier exploration of debug build code patterns I unearthed bits of debug assistance built into simple field-backed getter properties in C#.

That same pattern -- evaluate to a temporary, and then unconditionally branch to a return -- also shows up in non-property methods that return a constant literal or a field. With purely empty methods, there is no such intermediate value to expose to the debugger, so the method just looks like

i.e. a block with a Nop and a Return (with no expression).

The canonical stub left by Visual Studio implementing an interface looks like

which resolves to

i.e. a block with a Nop and a Throw (with a Construct expression).

Nop and a Throw alone will catch any method that simply throws as its one and only operation (even if the exception is passed in from somewhere else); if you want to determine what is being thrown, then dissecting the expression would be required.

In writing a simple static analysis tool, identifying anything that just throws -- and has no decision making logic -- is sufficient for my purposes; for anyone tempted to put massive lambdas in the throw expression, testing for the Construct may be worthwhile.