Tuesday 30 April 2013

Parking Charge Notice

A Parking Charge Notice arrived in the post last week for parking for more than two hours in an empty car park after the shops had closed, near Dewsbury Sports centre; charging £100, reduced to £60 if paid quickly enough that there is no time to think about it.

I thought about it.

First, there are a few flaws with the original notice:
  1. It does not properly identify the car park; only a 1 line car park name with no mention of town or county.
  2. It states that signage showing the terms and conditions is clearly displayed at the entrance - it was not, or it would have been noticed.
  3. That by parking, the driver has agreed to be bound by the terms and conditions - not so! Not being aware of the sign, one could not know that parking there would bind one by the terms and conditions. (Even if one was aware it is still possible to park there without agreeing to the terms and conditions though this might constitute trespass instead of breach of contract).
And then the usual misleading characteristics of such threatening demands, the letter is so designed with the sole narrative of having the reader pay some money now, because the letter tells them to.

The letter states that as the keeper one should identify the driver, and comments here http://www.honestjohn.co.uk/faq/private-parking-penalties/ suggest that one must identify the driver.
The British Parking Association in 2011 lobbied Parliament for a clause in the Protection of Freedoms Bill (Chapter 2, Clause 56) to make vehicle keepers liable for private parking penalties in the same way they can be held liable for road traffic offences and street parking offences if they do not identify the driver of the car at the time of the offence
and
Two comments from Nev Metson who knows more about this issue than anyone else:
...
The Achilles heel in this legislation for the private parking company is that once the registered keeper has named the driver then the registered keeper can no longer be held liable, irrespective of whether or not the private parking company gets it's money or not from the driver.
but their letter does not state how to do this. of course not, they would rather that one just paid.

One might think that you could notify them of the driver by writing to the registered office address in small print at the bottom of page 2. The only large and obvious contact details are for paying. I did this by registered post, thus discharging the only legal liability as I understand it.

And in true cheapskate fashion it asks the reader to do their dirty work for them, stating
If you were not the driver at the time, you should tell us the name and current postal address of the driver and pass this notice to them. 
Well, actually, no, I don't think I have to do your delivery work for you, you can send a notice to the driver yourself. (Which they then did).

The appeals procedure is interesting, but to follow such a procedure would imply that one agreed with the original assertion that one had entered into a contract with them. They can have an appeals procedure if they like, but I don't need to use it.

£100

The size of the claim is interesting.  At http://www.honestjohn.co.uk/faq/private-parking-penalties/ we read:
Unfair Private Parking Penalties

(With thanks to Alan Salsbury)

Ticketed car parking in a car park or on private land is a contract between the car driver and the owner of the land or the company managing paring on the land. If an unfair penalty for parking in an off road car park is imposed by a parking enforcement company due to a breach, often due to circumstances beyond the car owner's control, there is a remedy. (Thinking in particular of the driver who, having purchased a train station parking ticket for two days, arrived back at the car 11 minutes late.)

The law covers this situation adequately with the Unfair Contract Terms Act 1977 which clearly provides under group 5 para 1(e)** that "Terms may be unfair if they have the object or effect requiring any consumer who fails to fulfil his obligation to pay a disproportionate high sum in compensation". In other words, the company owning or managing the parking space can only charge a penalty which accurately reflects the loss of income they suffered arising from that breach. Insofar as the driver arrived 11 minutes late the penalty, if any, should be the cost of an 11 minute ticket. If there were several other parking spaces available, then no loss was suffered.

"I have quoted this act several times to such organisations and in each case they have cancelled the ticket, albeit without prejudice to future decisions to avoid creating a precedent. It has worked every time. However, anybody thinking of using this law to avoid buying a ticket. beware. You have to purchase a ticket to establish a contract."
(** Amended by Schedule 2 under regulation 5 (5) of the Unfair Terms in Consumer Contracts Regulations 1999.) 
So what was the actual loss by my parking in an almost empty car park after the shops were closed? Nothing!

I also learn that although the car park is managed by ParkingEye, I do not need to deal with ParkingEye I can deal with the land owner (in the same way that I can deal with BBC instead of TV Licensing).

The driver will be identified (to ParkingEye) and after that I will have fun drawing the other points to them one at a time. I will leave the legal capacity of the actual driver in being able to enter into a contract till the very end. I will post updates on my blog.

A repeat demand addressed the actual driver was received today. The driver has no business relationship with ParkingEye and has elected to ignore the notice.

Using for as a brace-less scope

I commented previously on Jens Gustedt's trick of using a for construct as a brace-less scope in which additional variables may be introduced, but the disadvantage is that any enclosed break statement will apply to the abused for statements instead of a containing explicit for statement.

So I attempted to detect a the operation of a break statement in order to propagate it to an outer loop.

In the code below, highlighted lines would be inserted by a macro, the purposes is to have the inner break be propagated to the outer loop.

#include <stdio.h>

int main() {
  int a=0;
  for(a=0; a<2; a++) {
    for (int o=0; o>=0; (o==1) && break; )
      for (int i=0; !o && (o=1), i==0; o=-1, i=-1 ) {
        printf("In loop %d o=%d i=%d\n",a, o, i);
        break;
      }
  }
  printf("Finished with %d\n", a);
}

Sadly, (o==1) && break is not legal.

This works, ({ if (o==1) break; }) but sadly only for gcc.

Monday 29 April 2013

Complete Computations in the Compiler

Interesting archived post here; A C++ program to test for the primality of the number 13

http://web.archive.org/web/20111113164723/http://homepage.mac.com/sigfpe/Computing/peano.html

The work is done in the compiler, so although that might take some time, the program will be very quick to complete!

Saturday 27 April 2013

Yet more try/finally/except

While using a for statement to declare a variable for the duration of a brace-less scope like this (requires gcc's c99 mode):

for(jmp_buf active_jmp_buf; ...; ...)

I needed a way to make sure that the loop only executed once, and really does execute once. This requires a variable that I can set after the first iteration. Where should such a variable come from? Naturally it would be declared in the same way.

for(jmp_buf active_jmp_buf, int cont=1; cont; cont=0)

However I can't make a for loop declare variables of different types, so I need 2 for loops (but they can use the variable of the outer loop).

for(int cont=1; cont; cont=0)
  for(jmp_buf active_jmp_buf; cont; cont=0) ...

And that will cause ... to be executed once, and give two scope local variables, cont which is used to terminate the loops after one iteration, and active_jump_buf which is the one we need.

So that gives:

for(int cont=1; cont; cont=0)
  for(jmp_buf active_jmp_buf; cont; cont=0)
    switch ((errno=setjmp(active_jmp_buf))) 
    if (0) {
      case 0: // first time around
           {
             ...
             ... throw(123);
             ...
           }
      break;
    } else {
      default: // break not needed for finally
           ...
    }

Yet another attempt at try/finally/except

Combined with setjmp/longjmp I previously tried to use a switch statement to hold the try/finally branches of code after noting the similarity between the C# except clauses and a switch/case statement.

I then rather too hastily abandoned this attempt under the impression that the curly braces are an essential part of the switch statement merely because that's how everyone shows them.

As I read further macro tricks I came across the blog of Jens Gusted, author of the unfortunately QT licensed P99 macro set which provides excellent capabilities, including try, catch and except in the syntax form I was seeking which does not involve trailing macros.

So I took a look at how Jens managed it and noticed a few tricks, including use of for (type var=...;...;...) as a way to start an indefinitely chainable single-statement scope that did not need a close-curly-brace but could declare a variable for that scope. But what most caught my eye was his use of the switch statement without curly braces.

It soon became apparent that the switch statement is just a statement, and that the case clauses are merely goto-label placeholders. Curly braces are normally used in switch to form multiple statements into a compound statement.

So if I can express my try and finally/except blocks without the need for me to provide curly braces, there will be no need for me to supply a close-brace.

An examples of brace-less switch statement:
http://stackoverflow.com/questions/8118009/is-there-a-useful-case-using-a-switch-statement-without-braces


switch (x)
    default:
    if (prime(x))
        case 2: case 3: case 5: case 7:
            process_prime(x);
    else
        case 4: case 6: case 8: case 9: case 10:
            process_composite(x);

and blessed day, as Johan Bezem shows further on that page, it allows you to start part-way through a loop, avoiding the continuation test on entry - that is useful!

uint8_t counter;
/* counter will get its value here somewhere */
switch (counter)
    default:
        while (0 < counter)
        {
            case 0:
                /* Perform action */
                counter--;
        }

Also, some people prefer switch(0) default: ... instead of do { ... } while(0) but Jens prefers if (1) { ... } else (void(0))

Try Again

So I repeat my switch based attempt, the parts in bold are provided by the macros:


for(jmp_buf active_jmp_buf; ...; ...)
  switch ((errno=setjmp(active_jmp_buf))) 
  if (0) {
  case 0: // first time around
       {
         ...
         ... throw(123);
         ...
       }
  break; } else default: // break not needed for finally
       {
         ...
       } 

Note that the if/else is bogus, merely a trick to hold two statements without using curly braces to denote a compound statement; although I took care to make it if (0) in order to get legitimate fall-through from the try branch into the finally branch.

I need to finish the for loop so that it only loops once, and perhaps include an extra single-case switch statement within each branch so that misplaced break directives will not affect our exception handling switch statement.

I also wonder if I can have multiple catch clauses like the C# except clauses or if I embed those within a single except clause with an explicit switch statement based on errno.



Friday 26 April 2013

Another attempt at try/except/finally in C

Since previous work on C cleanup functions I've been trying various means to implement try/finally in C using macros that allow a natural in-code expression of a typical form, like:

try {
  ...
  ... throw(123);
  ...
} finally or except {
  ...
}

It was not possible to use cleanup functions to implement the finally block for many reasons including difficulty of syntax construction, but ultimately because a cleanup function must return normally and so cannot chain an exception:
 It is undefined what happens if cleanup_function does not return normally. http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html 
(Or can they? as I proceed with setjmp I think they can exit with longjmp but at the cost of missing out other cleanup handlers, so maybe I just contradicted myself there.)

I was reading about call-with-current-continuation for C and was re-reminded about setjmp/longjmp and wondered if these might work. (For GENSYM see my previous post).

jmp_buf GENSY(jmp_buf);
if((errno=setjmp(GENSYM(jmp_buf)))==0) {
  ...
  ... longjmp(????, 123);
  ...

} nothing or else { // nothing for finally, and else for except
...
}

the main advantage with setjmp/longjmp is the effective go-back-to consequence of longjmp which avoids the need to recall the names of any generated reference further on in the code. The main difficulty with throw is knowing the reference ???? to use in the longjmp. It should be the most recent active jump_buf --- but how to tell which it is?

We could have a local variable in the try scope which stores the address of the most recent jmp_buf, or in other words we don't need GENSYM macro after all so long as we start a new scope:

{ jmp_buf active_jmp_buf;
if ((errno=setjmp(active_jmp_buf))==0) {
  ...
  ... throw(123);
  ...
} } nothing or else {
  ...
}

which could be generated by these

#define try { jmp_buf active_jmp_buf; \
              if ((errno=setjmp(active_jmp_buf))==0)
#define finally }
#define except } else
#define throw(thrown) longjmp(active_jmp_buf, thrown)

Note how the exception number is smuggled into the exception handler through use of errno.

After looking at some exception handler examples for C# and seeing how much like a switch statement it appeared, I decided I wanted to be able to separate exception types with their own handler and realised that a case statement might do for this:

{ jmp_buf active_jmp_buf;
  switch (setjmp(active_jmp_buf)) {
  case 0:
       {
          ...
          ... throw(123);
          ...
       }
       ...
  case 123:
       {
         ...
       } 
  }
}

Which, though incomplete (consider the lack of break statements) might be generated by something like this:

#define try { jmp_buf active_jmp_buf; \
  switch (setjmp(active_jmp_buf)) { \
  case 0:
#define finally }
#define except(exception) case exception:
#define throw(thrown) longjmp(active_jmp_buf, thrown)

and so expressed like this:

try {
  ...
  ... throw(123);
  ...
} except(123) {
  ...
} except(default) {
  ...
}

Except that we don't have enough close-braces to close the switch statement or the scope containing the jmp_buf which now contains the entire switch statement. This is the sort of problem that plagued previous attempts based on cleanup functions.

With the last new keyword being the finally or the except, any special scopes must be constrained to the try keyword that finally or except can clean them up. Therefore the switch statement is off limits as it requires an extra close-brace, although Francesco Nidito was not too proud to introduce ETRY to solve this problem with his C exceptions (along with Duff's device).

Friday 19 April 2013

Big heap o' stack frames

Why can't stack frames be allocated on the heap?

Of course I don't mean the heap, I want a stack frame to grow dynamically as much as anyone else, so a fresh memory map region might need allocating for each function cough; but of course dear reader you really want to know why.

Working with Samba multiple concurrent(ish) asynchronous network functions much work is done with continuations or callback functions which are invoked when forwarded network'd rpc responses are returned.

Co-routines

I had previously tried another way of working this by writing in synchronous style using libpcl, and spawning such functions as a private coroutine.

With every rpc-send function Samba has an rpc-receive function which will block in a nested event loop until the response packet is received (if the response has not already been received).

I modified the nested event loop code to switch back to the main co-routine. The packet-received callback routine then just has to switch back to my private co-routine and the rpc-receive function would then return with the now received packet.

It worked well enough but highlighted other bugs which came to light when concurrency became possible; such as use of a connection before it is properly established.

The reason co-routines were used here at all was to obtain a private stack as a simple way to preserve state when waiting for the response, as well as aid debugging - after all a linear stack trace is nicer than a state-machine state any day!

But if the main aim is to protect the stack from being torn down and destroyed, why not allocate the stack frame on a heap? This would allow a function to return to the caller (in some fashion) while preserving state so that mid-re-entry could be achieved at the callback. This is what C#'s yield return does and is perhaps along the lines of native C-ish support for call-with-current-continuation from Scheme (or Lisp). It would not be a full implementation of course unless a full copy of the application environment were made - but we don't want to re-invoke the return path anyway.

So can lightweight fibres be built into C, using such notation as:

do_something_slow(int a, int b) {
  int answer = callback(ask_question(a));
  printf("Answer %d\n", answer);
}

heap(do_something_slow(a, b));
printf("Task started\n");

where control will return to print "Task started" but sometime later the answer will be printed?

Of the two magic words heap and callback, the word heap signifies that a new non-returning stack frame would be allocated and the word callback signifies that the function ask_question will receive a callback address in some form which would cause execution to continue on the next line. This looks all very hand-wavy but of course the callback mechanism would have to be standardized, probably based on the event-loop mechanism by which the callbacks would be delivered.

This all corresponds very closely to my original case where a callback function was explicitly provided to the async function, and the sole role of the callback function was to switch execution to the saved stack. Having called the async function we then switch to the main stack and therefore the process which invoked heap.

I'll post my libpcl code soon, it has some custom varargs task launchers.

Sunday 14 April 2013

Come, friendly hell

I felt inspired to develop Betjeman's poem Slough after learning that over 55 millions babies had been killed since the end of the WWII. Did the Nazis just switch sides after all?
Come friendly hell and burn us now!
We're burning babies anyhow,
It's hardly worth you waiting now.
Swarm over, death

It's a work in progress, verse 2 addresses the institutions and their emblems of progress