Saturday, October 31, 2015

October cycling

Usual cycling round-up, with the end of the month numbers 12548.3, 2615.3 and 6.1 (odo newly fitted to my folding bike), plus 136.2 on another CycleBreaks holiday, for 94.7 + 207.5 + 6.1 + 136.2 off-meter or 444.5 miles total for the month, and 3629 year to date -- so even more than last month despite patches of wet weather where I wimped out and drove to work.

With the weather set to be El Niño mild for the next month, managing the 4200 goal for the year is looking even more achievable (a few bright mornings where I feel inspired to take the long way around to work are all that I need). Have to see whether 4250 or 4300 ought to be the stretch goal.



Wednesday, September 30, 2015

September Cycling

Despite a few wet days, and stubbing a toe badly (fortunately coinciding), it wasn't so bad a month for cycling -- 12453.6 and 2407.8, for 142.8 + 282.8 or 425.6 miles total for the month (way more than August), and 3185 year to date, so on target for 4200 in the year. A number of fine early mornings that were too good not to take the long way around to work helped, compensating for weekends where I didn't manage to get many miles in (and a summer in general without much in the way of long leisure rides).

The long way around to work also has the advantage that there are next to no other cyclists going the same way, except for on the busway from Oakington down, so I don't have to negotiate past people going along at barely more than walking pace on bikes with wheelbarrows for the kids attached, or who come to a stop blocking paths while waiting for pelican crossings; and there's no need for guessing whether the guy in front has tried to indicate or is just a limp-wristed wossname flouncing along.

Tuesday, September 01, 2015

August cycling

Usual cycling round-up, with the end of the month numbers 12310.8 and 2125.0, for 49.3 + 192.4 + 60 off-meter or 301.7 miles total for the month, and 2760 year to date -- so down even more on last month due to generally miserable weather (culminating in typical Bank Holiday weather), and holidays including forced non-cycling days like driving to and from Southport, plus keeping the garden in check in the gaps.

Toshiba Satellite Pro error C1900101-4000D (Windows 10 upgrade fails)

Note: In the months since this post, an official update has now been issued through Windows Update, and would have reached any device with this card in the Anniversary Update, if not before.

After upgrading my wife's older Dell laptop from Win7 to WinX with no problems, I was rather taken aback that my Toshiba Satellite Pro L770 (bought just before Win8 released, to tide me over until the next version after) failed some time around the 80% mark, and rolled back. And more so when the Toshiba site indicated that my laptop was not supported for upgrade, even if the Microsoft scan gave the all OK.

Cutting a long story short, it turns out that it's the WiFi card (Qualcomm Atheros AR9002wb-1ng) which doesn't have a suitably modern driver. Disabling this from the device manager before trying again let the upgrade go through, and a generic WiFi-n low-profile dongle from Maplin which I had to hand "just worked" when I plugged it into a USB socket.

Don't re-enable the Atheros card, as that gets you into a blue-screen/re-boot loop until you can get into safe mode without networking to disable it again.


Later (12-Sep): Uninstalling the Realtek PCIe GBE Family Controller device, then scanning for hardware changes and re-installing it with the latest driver already on the machine (version 9.1.401.2015 dated 4/1/2015) also deleted and reinstalled the Atheros card, leaving it enabled and not crashing the machine (while still using the dongle for WiFi).

Trying to disable or uninstall the Atheros card at this point caused me blue-screens with the same thread exception as before; but at least not immediately on start-up, so I was able to shift-Restart into safe mode, and uninstall it there. On restarting, the "new" hardware was detected, and the old 9.x.x.x driver dated some time in 2010 which previously had been claimed to be the most recent version was replaced by version 10.0.0.270, dated 10/17/2013. This was not such an improvement -- it blue-screened in familiar style when trying to disable the device.

So, once more into safe mode to disable it again, until such a time as a real WinX driver appears, if ever. Yes there is a 10.0.0.308 mentioned on the unofficial Czech site, but the summary list doesn't show any WinX support, so I'm skeptical about it (especially as the device manager update function claims that I already have the latest version).

Sunday, August 16, 2015

"Getting" Functional Programming

We all know there's something to "get" -- but is it one massive "Eureka!" moment, or is it a series of smaller "Aha!" steps? I think that it's the latter, but that most functional languages drop them in your lap in one undigested lump, because I had an odd introduction to the style.

Back around the turn of the century, I encountered the famous Why Functional Programming Matters paper; and while I’m a mathematician by background, that was always in Applied -- crunching differential equations or re-casting multiple integrals as Fourier transforms -- and I simply didn't get it. How could you do anything in your programs if your variables didn't vary? Then after some years, I encountered an article -- no longer on the web, alas -- suggesting that Erlang was well placed to be the "next Java", and my curiosity was piqued.

Some while later, I spotted Joe Armstrong's Programming Erlang on the shelves of the local Borders, so picked it up; and though the syntax and conventions of the language were bizarre and alien -- very "spiky" looking -- the mystery of how you did things with immutables was dispelled in the first couple of chapters. And it was revealed to be quite mundane -- you keep your scopes small (general good practise, anyway) to avoid running out of sensible write-once names, and you recurse or use callbacks rather than iterate.

So, despite its unrelentingly immutable nature (process dictionary aside), it turned out that Erlang had a very low barrier to the program after “Hello world!” Writing non-trivial programs (a few hundred lines of networking code) that accomplished and outperformed what had taken me thousands of lines in ‘C’ was easy. Yes, there were compilation errors along the way, but those were mostly simple syntax (putting the correct punctuation marks at the ends of lines), and I debugged at run-time as usual.

Really, despite the alien syntax and strict immutability, this felt like OO in the abstract -- you send an object process a message, and it responds in a Smalltalk-esque (or even Win32 message loop-alike) fashion. First class functions were just familiar callbacks with a different name, even in cases like a transformative map over a list. Indeed so OO is the paradigm that people talked about implementing Ruby in an "object → process" style on the Erlang VM, and then actually implemented such a near-Ruby.

Then I found out that this F# language was not Fortran.net, but a functional language on the CLR and gave it a whirl. And I felt like an utter dunce who “didn’t get it” for months; not helped by the lack of good introductory material -- like the original Foundations of F# book, which for the newcomer was frankly terrible on account of the volume of assumed knowledge it brought along.

In hindsight, the big shift was between Erlang's dynamic type model, and the static, inferred, typing of F#; the feature that means that a program that compiles will probably also work as well. Unfortunately, that also means that you have in a sense to debug your program at this stage, which is the massively unfamiliar bit. And it's not helped when your program fails to compile with a message that's about as comprehensible as a C++ template failure -- it wasn't until F# for Scientists that I found my Wittgenstein's Ladder, a book that actually explained enough of the the types and type notations, and suddenly made sense of the bizarre arrow-filled compile failures (and API definitions).

Then, and only then, did the list-processing nature of LISP, and Haskell's mysterious monads, all suddenly become things that fall naturally out of the new style of thought. The extra constraint of type exactness is indeed what makes operations like bind to transform types a meaningful and necessary concept rather than being approached ad hoc.

The other shift, related to the debugging at compile time is that, like template metaprograming in C++, suddenly computation can happen as compilation -- a pure function with fixed compile time inputs can in principle be evaluated at that point. And where your program does take inputs, the way of looking at it approaches being an aggregation of functions where input comes in and results come out through a transformative process built of little stateless sub-transformations; rather than as a collection of stateful objects talking to each other about the input.

And at that point, I think what is being practised is functional programming.

Thursday, August 13, 2015

A cycling anniversary

Today concludes my first year logging my journeys on the Cambridgeshire Cycle Challenge website, over which span I've logged 601 journeys covering 4204 miles; supposedly 1372.0 kg CO2 saved (ignoring my increased respiration when burning the 155,532 calories it tells me I have spent doing so) and saving £989.69 on fuel (ignoring the costs of fueling me or running through consumables like chains).

The serious cases are probably all on Strava, because that performance puts me in the top few of those who use the site.

But then it's better to be a big fish in a small pond, than continuously broadcast one's recent whereabouts to the world.

Saturday, August 01, 2015

July cycling

Usual cycling round-up, with the end of the month numbers 12261.5 and 1932.6, for 162.5 + 252.2 or 414.7 miles total for the month, and 2458 year to date -- so down on last month due to generally miserable weather, and having to take advantage of what good weather there was at weekends to keep the garden under control.

Even so, all it took was a run of a few wet days for the caterpillars to devastate the new broccoli plants, some down to the stalks, while I wasn't watching.



Wednesday, July 01, 2015

June Cycling -- On the move II

Usual cycling round-up, with the end of the month numbers 12099.0 and 1680.4, for 148.5 + 310.4 or 458.9 miles total for the month, and 2044 year to date, only just short of a hoped for 2100. And that without doing any particular long excursions.

OTOH, work now having moved to the Science Park, the nicer -- as in less encounters with other road users -- routes to the office are between 1 and 5 miles longer than before, so there's been a systematic increase in my weekly mileage.



Sunday, May 31, 2015

Late May holiday cycling

We took the Bank Holiday week to go down to Netley Waterside again, after a couple of year gap, and for the first time, taking the folding bike I got last spring for holiday purposes, which meant that in just an initial quick ramble I could go further in either direction than the long walks I did back then.


The weather was fine, except for a wet Friday, and generally pleasant to be out in, even if at times through an uneasy balance of sultry heat and strong breezes, as for the expedition to the Sir Harold Hillier Gardens on the Bank Holiday Monday. In fact, the weather usually exceeded expectations set by the forecasts, such that I ended up acquiring more of a tan than I'd expected.

Having forgotten to pack a map, I ended up following the National Cycle Network 2 signs a lot, filling in the gaps by dead reckoning, especially in the places where the paved surface suddenly stopped, but being hemmed in between coast and motorway, that didn't leave too much scope for getting lost -- just scope for somewhat more ups-and-downs than I needed, grinding in bottom gear, as in the sub-optimal routes through the back doubles in Bursledon, and too much main A-road for crossing the Hamble. Next time, having discovered the foot ferry on the last way back, I might be able to cut out much of the main road and hills.

Hovercraft Museum, Lee-on-Solent

Hovercraft Museum, Lee-on-Solent

Also, lacking a map, I didn't realise quite how close I came to running out of road entirely, not realising that Portsmouth Harbour was that close.

D-Day plaque

D-Day plaque at the Rising Sun, Warsash

Many of the pubs I passed on my wanderings were rather sea-sidey, but the Rising Sun at Warsash was a more traditional rural pub, that overcame the handicap of being a Greene King house, and served most excellent sandwiches. Their take on chicken bacon mayo was not the vaguely unpleasant mush by the same name that were offered as packed lunches by the Revitalise team, but a coarsely sliced chicken breast, a couple of rashers, and a pot of mayo to add to taste on doorstops from a fresh soft bloomer loaf -- washed down with a pint of Rsing Sun bitter, an excellent lunch!

On the move I

Usual cycling round-up, with the end of the month numbers 11950.5 and 1370.4, with the belated arrival of the rains eating into my cycling days, for a disappointing 50.4 + 168.2 + 78 off-meter or 296.7 miles total for the month, and 1585 year to date, making 2000 for the half year a stretch.

Also, on Saturday 16th, I put my car past the 11,000 mark, so 1000 miles in 52 weeks, and under 2^10 in the year.


Thursday, May 14, 2015

F# under the covers XV -- functions, types, and what you see isn't what you get

Consider this innocent example


When run in a new interactive session, it yields (after trimming the SOAP baggage, and noting that _x002B_ would be an encoding of + and _x0040_ of @)

FSI_0002+clo@20
<SOAP-ENV:..>
<SOAP-ENV:Body>
<a1:FSI_0002_x002B_clo_x0040_20 id="ref-1" xmlns:a1=...>
</a1:FSI_0002_x002B_clo_x0040_20>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

FSI_0002+clo@20-1
<SOAP-ENV:...>
<SOAP-ENV:Body>
<a1:FSI_0002_x002B_clo_x0040_20-1 id="ref-1" xmlns:a1=...>
</a1:FSI_0002_x002B_clo_x0040_20-1>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

FSI_0002+add2@11
<SOAP-ENV:...>
<SOAP-ENV:Body>
<a1:FSI_0002_x002B_add2_x0040_11 id="ref-1" xmlns:a1=...>
</a1:FSI_0002_x002B_add2_x0040_11>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

FSI_0002+clo@20-2
<SOAP-ENV:..>
<SOAP-ENV:Body>
<a1:FSI_0002_x002B_clo_x0040_20-2 id="ref-1" xmlns:a1=...>
</a1:FSI_0002_x002B_clo_x0040_20-2>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

-------------------
FSI_0002+it@25
<SOAP-ENV:...>
<SOAP-ENV:Body>
<a1:FSI_0002_x002B_it_x0040_25 id="ref-1" xmlns:a1=...>
</a1:FSI_0002_x002B_it_x0040_25>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

FSI_0002+it@25-1
<SOAP-ENV:...>
<SOAP-ENV:Body>
<a1:FSI_0002_x002B_it_x0040_25-1 id="ref-1" xmlns:a1=...>
</a1:FSI_0002_x002B_it_x0040_25-1>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

FSI_0002+add2@11
<SOAP-ENV:...>
<SOAP-ENV:Body>
<a1:FSI_0002_x002B_add2_x0040_11 id="ref-1" xmlns:a1=..>
</a1:FSI_0002_x002B_add2_x0040_11>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

FSI_0002+it@25-2
<SOAP-ENV:...>
<SOAP-ENV:Body>
<a1:FSI_0002_x002B_it_x0040_25-2 id="ref-1" xmlns:a1=...>
</a1:FSI_0002_x002B_it_x0040_25-2>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


val mhead : m:System.Object list -> System.Object list
val addN : n:int -> x:int -> int
val add2 : (int -> int)
val emitString : x:'a -> unit
val it : unit = ()

Where we observe that of the user input names, only add2 survives as part of the serialized name; and that the second instance of what looked like the same function is actually an instance of a different type.

Running the same code as a compiled .exe, we get

Program+clo@20
<SOAP-ENV:...>
<SOAP-ENV:Body>
<a1:Program_x002B_clo_x0040_20 id="ref-1" xmlns:a1=...>
</a1:Program_x002B_clo_x0040_20>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Program+clo@20-1
<SOAP-ENV:...>
<SOAP-ENV:Body>
<a1:Program_x002B_clo_x0040_20-1 id="ref-1" xmlns:a1=...>
</a1:Program_x002B_clo_x0040_20-1>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Program+add2@11
<SOAP-ENV:...>
<SOAP-ENV:Body>
<a1:Program_x002B_add2_x0040_11 id="ref-1" xmlns:a1=...>
<n>2</n>
</a1:Program_x002B_add2_x0040_11>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Program+clo@20-2
<SOAP-ENV:...>
<SOAP-ENV:Body>
<a1:Program_x002B_clo_x0040_20-2 id="ref-1" xmlns:a1=...>
</a1:Program_x002B_clo_x0040_20-2>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

-------------------
Program+clo@25-4
<SOAP-ENV:...>
<SOAP-ENV:Body>
<a1:Program_x002B_clo_x0040_25-4 id="ref-1" xmlns:a1=...>
</a1:Program_x002B_clo_x0040_25-4>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Program+clo@25-5
<SOAP-ENV:...>
<SOAP-ENV:Body>
<a1:Program_x002B_clo_x0040_25-5 id="ref-1" xmlns:a1=...>
</a1:Program_x002B_clo_x0040_25-5>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Program+add2@11
<SOAP-ENV:...>
<SOAP-ENV:Body>
<a1:Program_x002B_add2_x0040_11 id="ref-1" xmlns:a1=...>
<n>2</n>
</a1:Program_x002B_add2_x0040_11>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Program+clo@25-6
<SOAP-ENV:...>
<SOAP-ENV:Body>
<a1:Program_x002B_clo_x0040_25-6 id="ref-1" xmlns:a1=...>
</a1:Program_x002B_clo_x0040_25-6>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

where the names are similar -- and now the closure add2 actually contains that 2 as part of its body.

So, what's going on? Well, let's look at what we get when we decompile.


This is the debug version, the release version just moves the main program locals to be static members of the Program class, with no other relevant changes.

This shows us that our named functions are compiled as normal functions as we expect in C#, but the first-class function objects we pass around are actually indirections to those functions; and different ones at each site, even if they close over nothing, and so must be identical, even in the release build.

So, what does this mean?

Well, for one thing, type-needy serialization methods like DataContract aren't going to work well with functions (including lambdas inside of methods of innocent looking types), because the types are instance-unique. And deserialization is only going to be possible in the same binary as the serialization came from (or at least have the same functions on the same types, methods and line numbers), unless you do a fair bit of decompilation to provide a translation binding.

The latter is a potential pitfall -- if you implement a Windows Workflow in F#, and have objects persisted, things could work just fine within the first working version of your program; but processes that are hibernating mid-task while you're rolling out a new version can break when they wake because these secret types are no longer there by the same names.


Friday, May 08, 2015

Getting the types in an F# union type II -- the perils of relying on undocumented implementation details

Recalling this example --


It was adequate for the use case I had of it at the time; but then I tried a different union type, at which point a bug showed up



yields

FSI_0003+Card
System.NullReferenceException: Object reference not set to an instance of an object.
   at FSI_0005.it@24-1.Invoke(Type x)
   at Microsoft.FSharp.Collections.SeqModule.Iterate[T](FSharpFunc`2 action, IEnumerable`1 source)
   at .$FSI_0005.main@()
Stopped due to error

And, of course, that is exactly what I deserved for relying on the undocumented internals.

A quick decompilation reveals this to be because union cases that carry no data are actually concrete base type instances, bearing the tag number as data, like --



rather than being redundant empty subtypes.

So we need to tweak our function to be




and all is well. For the moment. Until the representation changes.

Thursday, May 07, 2015

Getting the types in an F# union type

This arose in the context of performing data contract serialization of a union type, and needing to feed the concrete types in as known types. As the FSharp.Reflection facilities only let you at the names of the union cases, it's necessary to rely on the secret knowledge that the union cases are implemented as nested subtypes of the abstract union type



Then we can serialize objects of the union type like



where we feed the list of types to be known to the constructor for the serializer.


Thursday, April 30, 2015

April cycling

Readings of 11900.1 and 1202.2 mean 144.7 + 262.2, a total of 406.9 miles, 1288 YTD, having cycled at least a kilometer every day this month -- #30daysofbiking. And it was only today that we really had April showers to dampen the excitement.


Friday, April 24, 2015

It's that time again, the simplistic "Who should you vote for?" quiz. At least it's not quite so out of whack as it was a decade ago, or even last time around.

Your UK Election 2015 quiz results


UK Independence
 
53
Conservative
 
26
Liberal Democrat
 
10
Labour -30
 
Green -48
 

You expected: UKIP

Your result: UK Independence