The Writer monad is a programming design pattern which makes it possible to compose functions which return their result values paired with a log string. The final result of a composed function yields both a value, and a concatenation of the logs from each component function application.

Demonstrate in your programming language the following:

 Construct a Writer monad by writing the 'bind' function and the 'unit' (sometimes known as 'return') function for that monad (or just use what the language already provides)
 Write three simple functions: root, addOne, and half
 Derive Writer monad versions of each of these functions
 Apply a composition of the Writer versions of root, addOne, and half to the integer 5, deriving both a value for the Golden Ratio φ, and a concatenated log of the function applications (starting with the initial value, and followed by the application of root, etc.)
BEGIN
    MODE MWRITER = STRUCT( LONG REAL value
                         , STRING    log
                         );
    PRIO BIND = 9;
    OP   BIND = ( MWRITER m, PROC( LONG REAL )MWRITER f )MWRITER:
    (    MWRITER n := f( value OF m );
         log OF n  := log OF m + log OF n;
         n
    );
 
    OP   LEN     = ( STRING s )INT: ( UPB s + 1 ) - LWB s;
    PRIO PAD     = 9;
    OP   PAD     = ( STRING s, INT width )STRING: IF LEN s >= width THEN s ELSE s + ( width - LEN s ) * " " FI;
 
    PROC unit    = ( LONG REAL v, STRING s )MWRITER: ( v, "  " + s PAD 17 + ":" + fixed( v, -19, 15 ) + REPR 10 );
 
    PROC root    = ( LONG REAL v )MWRITER: unit( long sqrt( v ), "Took square root" );
    PROC add one = ( LONG REAL v )MWRITER: unit( v+1, "Added one" );
    PROC half    = ( LONG REAL v )MWRITER: unit( v/2, "Divided by two" );
 
    MWRITER mw2 := unit( 5, "Initial value" ) BIND root BIND add one BIND half;
    print( ( "The Golden Ratio is", fixed( value OF mw2, -19, 15 ), newline ) );
    print( ( newline, "This was derived as follows:-", newline ) );
    print( ( log OF mw2 ) )
END