Mittwoch Sep 24, 2008

A Kiss from the Lambda Calculus

berlin.jpg

This month, I went to the Berlin.jar conference (a Java conference in Berlin), a two-day event at the FHTW Berlin (university of applied sciences). This year, it was the first Berlin.jar and it's planned to do it again next year.

One of the many presentations was about the Ruby programming language. Like some other languages, the language contains the lambda keyword. Historically it comes from the λ-calculus, a theory intended to be a foundation for mathematics (like Frege's logicism, this attempt failed). The keyword lambda introduces an (anonymous) function (object). In particular a function can be returned by a function and can be a parameter to a function, including itself. The lambda example, given in the presentation, was an anonymous function which takes no arguments and returns a random number.

This wouldn't be possible in the λ-calculus, nor in a pure functional programming language (e.g. Haskell). There the purpose of λ is to bind variable names, just like quantifiers do it in predicate logic, and a λ without variables is syntactically incorrect. It would also be impossible to have a function that doesn't always return the same value for the same arguments, a function with no formal parameters is simply a constant. In contrast, an imperative language allows side effects (the result of a function does not only depend on its arguments), therefore the meaning of the Ruby lambda is not the same.

An imperative λ. How ironic. Walking around after the conference, I made this picture of a remaining segment of the Berlin Wall near Ostbahnhof (Brezhnev kisses Honecker, above "Lord, help me to survive" is written in Russian).

Links

Dienstag Jun 17, 2008

The Many Faces of a Sequence Comprehension

Last month I held a presentation about the JavaFX Script programming language. It was easy to impress the audience by showing how fast a graphical user interface can be done. Last year I worked as a teaching assistant for a Java course, where I had to use the Eclipse IDE. Due to not having a visual GUI editor, I spent a whole day preparing a simple Swing source code example (the students then used it for an exercise). In the JavaFX Script programming language the more intuitive way of writing (and reading) Swing applications is a great relief.

In the functional part of my presentation I used a sequence comprehension, similar to that I found in the draft language reference [1], in order to decide if a number is a prime number or not.

for (i in [2..n/2] where n % i == 0) i       (1)

It returns a sequence of factors of a natural number n, not including 1 and n, so that n is prime iff the sequence of factors is empty. In mathematics one could say, there is a corresponding set of factors of n denoted by

{i | i ∈ [2..n/2], n mod i = 0}.

A sequence comprehension consists of three parts: a list of input sequences, a filter and an expression, which is "applied" to the filtered input. The expression in (1) is i, so that just the identity function is applied to the filtered input. We get the same result by only filtering [2..n/2].

   function filter (  lst : Integer [] 
                    , f   : function(:Integer):Boolean ) : Integer [] { 

       var res : Integer [] = [];

       for (i in lst) {
           if (f(i)) {
               insert i into res;
           }
       }

       return res;
   }

   function factors (n : Integer) : Integer [] {
       return filter(  [2..n/2]
                     , function(i) { return n % i == 0; } );
   }

Instead of defining a filter function, that takes a sequence and a predicate and returns a new sequence, we could use the built-in selection syntax.

[2..n/2][i | n % i == 0]

On the other hand, the filter function demonstrates the possibility of passing functions as arguments. The predicate passed by factors is an anonymous function.

There is also an equivalent sequence comprehension for a sequence syntax without filtering. The filter has just to be always true.

Link

Montag Apr 21, 2008

Demonstrating ZFS

Last week I held a presentation about ZFS. Simplicity is one of the features of ZFS, but there are some things beyond ZFS, and the parts all together are everything but trivial. This text is a step-by-step instruction on how such a presentation can be done.

Preparatory steps

Get some USB flash drives of the same size. I used eight 1 GB USB flash drives with LED lights (so the activity on the storage pool was visible) and two 4-port USB hubs. Don't worry about the uselessness of owning eight memory sticks, at the end of the presentation you'll give them away to the people.

Install an OpenSolaris distribution. I used the SXDE 1/08 distribution on a 1.7 GHz Pentium M laptop with 1 GB of main memory and two USB ports. During the presentation you'll use a video file. Download a Chaplin movie from www.archive.org. Install mplayer from www.blastwave.org and tell the file browser to use it as the default application for the video file. In Preferences → Removable Drives and Media disable Mount removable drives when hot-plugged.

Creating a zpool

Type su in a terminal window and become root. Connect one of the hubs to the laptop and the second hub to the first (on a laptop with two USB ports the free port can be used for a mouse). Connect two sticks to the first hub and four sticks to the second.

Find out the device names of the flash drives, rmformat lists all removable devices when started without parameters. The names are like /dev/rdsk/c2t0d0p0 or /dev/rdsk/c18t0d0p0, where c = controller, t = target, d = disk, and p0 means "whole disk". On all six sticks, delete the data at the beginning of the space. Write a shell script for that, use

   dd if=/dev/zero of=/dev/rdsk/DeviceName count=2048

to fill the first 2048 sectors with zeroes (delete this data on the remaining two sticks too). Create a zpool that contains three mirrors and call it usbpool, the command may look like

   zpool create -f usbpool mirror c2t0d0p0 c4t0d0p0 \\
      mirror c18t0d0p0 c19t0d0p0 mirror c20t0d0p0 c21t0d0p0

Run the zpool list command to see that the pool is actually there, zpool status shows a tree structure of the pool. The pool is automatically mounted and is accessible through the /usbpool directory. Add rights, so that you can write to it as a normal user: chmod a+rw /usbpool.

Simulating a disk failure

Open the file browser, copy the Chaplin movie to /usbpool and open it from there. Depending on your laptop, the video may appear only on the laptop, so that the mplayer window remains blank on the projector output, try to use a different video output driver (mplayer -vo help prints a list of drivers). While playing the movie copy something else to the pool, you can copy the /opt/csw directory, which has 232 MB after installing mplayer. To simulate a disk failure, disconnect one stick from the first hub and one from the second. If every mirror is completely on one hub, no data is lost. In order to have some activity on the pool, delete a directory within /usbpool/csw. Now the zpool status command says, that two sticks have been removed. The video keeps playing. Connect the sticks back to the hubs, the ports do matter, which stick you connect to a port does not. Delete one more directory within the pool. The changes are automatically distributed across all sticks. The zpool status command now says, that all sticks are online. Stop the video and close the file browser.

Export, import, add, replace, destroy

Unmount the pool with the zpool export usbpool command. Disconnect all sticks and put them back in some random order. This step shows, that you could take the data with you and reinstall the pool somewhere else. The zpool import usbpool command brings everything back. You can now open the video again. Connect a new stick to the first hub and add it to the pool.

   zpool add -f usbpool c5t0d0p0

With the zpool list command you can see that the size of the pool has grown. Disconnect one of the mirror sticks and replace it with a new one. The zpool status command says, that the state of one of the sticks is FAULTED.

   zpool replace usbpool c21t0d0p0

Now the state of all sticks should be ONLINE. Cancel all activity and destroy the pool.

   zpool destroy usbpool

Before giving the sticks away, you should first create an FAT32 file system on each of them. Use the fdisk utility and manually create an FAT32 primary partition on one of the sticks. Save the partition table to a file.

   fdisk -W ptable c2t0d0p0

The partition table is now stored in a file called ptable. Write a shell script, that creates the file system on all connected sticks at once.

   #!/bin/bash

   #
   # Create partition tables, ptable is a file created with fdisk -W ...,
   # ptable describes the partition table to be written to the sticks,
   # do not use this method, unless the sticks have the same capacity/geometry.

   # WARNING: make sure there are only USB sticks in this range
   for i in `seq 2 21`; do
      fdisk -F ptable c"$i"t0d0p0
   done

   #
   # Create FAT32 file systems, label=SUNTECHDEMO

   for i in `seq 2 21`; do
      echo "Creating FAT32 on $i . . ."
      echo y | mkfs -F pcfs -o fat=32,b=SUNTECHDEMO /dev/rdsk/c"$i"t0d0p0:c
   done

Coincidentally, there were eight people in my presentation, so I was able to give a USB stick to each of them. If there are more people you might want to use random numbers. A Python interpreter is preinstalled in the SXDE 1/08 distribution, so you could draw the numbers there, for instance. If one of the numbers repeats, it would be a bad idea to give the stick to a person sitting next to a person that already has a stick, because for the former the probability of getting a stick is greater than for the people who are not sitting next to an already drawn number.

Try out RAID-Z.

More material

  • There is a video (in German) on YouTube, where a similar presentation is done (update: there is also an English version).
  • http://www.opensolaris.org/os/community/zfs/demos/zfsdemo.pdf
  • About

    Sun Campus Ambassador,
    University of Frankfurt (Germany)

    Search

    Categories
    Archives
    « April 2014
    MoDiMiDoFrSaSo
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
        
           
    Heute