Sunday
May012011

Conopid Fly

A Conopid fly, probably a Myopa sp (Diptera: Conopidae).  Possibly Myopa tesselatipennis or Myopa testacea. Note the two dark spots on the wings.

Photo taken in the Wilderness, Whiteknights Park, Reading University grounds, Reading, UK, on 2010-04-29.

Saturday
Apr302011

Picture-Wing Fly

A female celery leaf-miner fly, Euleia heraclei (Diptera: Tephritidae).  In this species the colour of the body of the female  can be anything from orange to black.  The larvae mine the leaves of various umbellifers (Apiaceae); the above fly was found close to some water dropwort (Oenanthe sp).

Photos taken on the dam at the lower end of the large lake in Whiteknights Park, Reading University Grounds, Reading, UK, on 2011-04-27.

Friday
Apr292011

Rose Chafer Beetle

A rose chafer beetle, Cetonia aurata (Coleoptera: Scarabaeidae) on a white dead nettle (Lamium album).  A fairly large beetle, about 2cm long, it flew off with whirring wings, rather like a female stag beetle would.  For more details also see this page at the Natural History Museum.

Photo taken in the field below Chazey Wood, near Caversham, UK, on 2011-04-25.

Thursday
Apr282011

IO Functions with no Side-Effects

In chapter 9 of Real World Haskell (O'Sullivan, Goerzen, and Stewart, 2009, page 214) we read:

However, the doesDirectoryExist function is an action; its return type is IO Bool, not Bool.

This is why we have to write things like:

  exists <- doesDirectoryExist path
if exists
then ...
else ...

instead of:

  if doesDirectoryExist path
then ...
else ...

However, it looks wrong to me to say that doesDirectoryExist is an action. To my mind an action has side effects, but doesDirectoryExist is a pure function and just returns a value with no side-effects. Why then is doesDirectoryExist in the IO monad; why can't we just use the "if doesDirectoryExist path" version above?  This seems to go against the command-query separation principle.

The reason is that, while doesDirectoryExist produces no side-effects itself, it does depend upon the side-effects produced by previous IO actions. Consider the following code:

if doesDirectoryExist path
then do
createDirectory path
if doesDirectoryExist path
then ...
else ...
else ...

If doesDirectoryExist was not in the IO monad, the second of the two occurrences of "doesDirectoryExist path" could just return the memoized value of the first occurrence, and we would not be able to detect that the path directory had been created as a side-effect of the createDirectory function.

This reasoning also explains why the other functions returning IO Bool, such as doesFileExist, hIsEOF and hIsWritable, must also be in the IO monad.

Thursday
Apr282011

Woundwort Shieldbugs

A pair of woundwort shieldbugs, Eysacoris venustissimus (Hemiptera: Pentatomidae), mating on a white dead nettle (Lamium album).  These bugs can also be found on hedge woundwort (Stachys sylvatica), hence their common name.

Photo taken in the field below Chazey Wood, near Caversham, UK, on 2011-04-24.