Blog entries for 2009

Oct 10 2009

Functions of functions

The following is some theoretical learning I have done about functions of functions in Haskell and F#. It uses haskells type and function notation but has a couple of F#s (perhaps actually ocaml?) names for functions because they are clearer for my purposes because they have clear directionality, and the flipped versions show some of the similar properties more clearly than the commonly used versions.

There are two main operations you can do with function values: Apply them, and compose them. One other operation referenced here is flip, which swaps the argument order of the function.

First up, the function application operator, with its haskell name and then the F# versions that i’m going to prefer:

1
2
3
4
5
$ :: (a -> b) -> a -> b
<| :: (a -> b) -> a -> b  -- Function application; 
<| = ($)
|> :: a -> (a -> b) -> b  -- Pipeline
|> = flip ($) 
The second common operator on functions is function composition; Again the haskell name followed by the F# names. The latter is not to be confused with haskell’s monad operator of the same name <<.
1
2
3
4
5
. :: (b -> c) -> (a -> b) -> (a -> c)
<< :: (b -> c) -> (a -> b) -> (a -> c)
<< = (.)
>> :: (a -> b) -> (b -> c) -> (a -> c) 
>> = flip (.)

In Haskell its not immediately obvious that monadic bind is related to function application; in this case apply a function to a value in a monadic context. With the flipped function application (aka pipeline) operator we can see clear similarities in the types:

1
2
3
4
|> :: a -> (a -> b) -> b 
>>= :: Monad m => m a -> (a -> m b) -> m b 
<| :: (a -> b) -> a -> b
=<< :: Monad m => (a -> m b) -> m a  -> m b 
Note that in both cases there is a function applied to a value, in the case of the bind operators that value is in a Monad type. The difference is that bind has a more specific type than plain application, in the form of a type that implements the monad interface. This provides richer semantics about the function application.

We know that there is a relationship between function application and function composition. This is particularly clear with pipeline and right facing compose. In this expression, g and h are both functions.

1
f val = val |> g |> h == f = g >> h
And given we know there is a relationship between bind and application, it can easily be supposed that there is a similar operation to composition specifically for monadic functions.

If we replace the three functions in the type of compose with monadic operations, we get an operator with a type such as:

1
2
3
4
>> :: (a -> b) -> (b -> c) -> (a -> c) 
>=> :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
<< :: (b -> c) -> (a -> b) -> (a -> c) 
<=< :: Monad m => (b -> m c) -> (a -> m b) -> (a -> m c)
Indeed this operator does exist and is known as Kleisli composition of monads.

Generalized functions of functions

A key feature that Monads bring is that they allow a single operator to generalize many different kinds of function applications. The types that implement the Monad typeclass in haskell each provide different contexts to manage how various functions are applied. The identity monad effectively is the function application operator dressed up with a more complex type.

Given there is a generalization for application, it seems likely that there would be a generalisation for composition too as we have already seen two general patterns. It turns out that there is, and in Haskell it is known as the Arrow typeclass. Of particular interest here, Arrow provides an operator:

1
>>> :: Arrow ar => ar b c -> ar c d -> ar b d 
An arrow type ‘ar a b’ represents a transformation from some type a to type b. For example:
1
2
3
4
a -> b -- becomes:
Arrow ar => ar a b
Monad m => a -> m b -- becomes:
(Arrow ar, Monad m) => ar a (m b)
This, the function application (>>) and Kleisli operators (>=>) can be represented in terms of Arrows as:
1
2
3
4
>> :: (a -> b) -> (b -> c) -> (a -> c) 
>>> :: Arrow ar => ar b c -> ar c d -> ar b d
>=> :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
>>> :: (Arrow ar, Monad m) => ar b (m c) -> ar c (m d) -> ar b (m d) 

What’s the point?

All this shows just shows a correspondence between monads, arrows and the primative operations of functions. In particular how those operations can be generalised when more specific types are involved.

iPod Rejects

If you are like me, the amount of music you have on your computer is much greater than the amount of space on your iPod. Heres a tip on how to make managing your iPod selection significantly easier.

First up, you should have an playlist that the iPod is loaded from. My iPod uses “Andrew’s iPod Selection”. This is a normal static playlist that I manage by hand.

I have created a Smart Playlist that shows me all the tracks that are not on my iPod. I called it my iPod rejects.

Creating the playlist

To create this playlist, go to the File menu and click on New Smart Playlist.

The playlist editor will open. You want to make it match all the songs that are not in the ipod selection, and have a media type of music. Below you can see a screenshot that shows the setup.

When you first create a smart playlist there is only one rule. You will need to click the + button on the right to add a second. Make sure you have Live updating and match the following rules checked.

Set a Playlist rule to is not your iPod Selection playlist. Set the second — a Media Kind rule — to is and Music. This second rule is to make sure podcasts, videos, etc don’t get collected up with the music.

Managing the playlists

I have both my iPod Selection and iPod Rejects playlists set up to use the grid view. To change to Grid view, click on the View menu, and select Grid. I also set the View > Grid view option Albums selected, and View > Sort albums > By Artist selected. This allows me to easily skim the albums by artwork.

I populate my iPod Selection playlist by browsing the iPod Rejects playlist instead of the Music library. If you have enabled Live updating on the Smart playlist then tracks should disappear from the Rejects playlist immediately after you add them to the Selection playlist.

Needless Facs

A pointless collection of factorial implementations in Clojure.

1
2
3
4
5
6
(defn fac1 
    "Basic recursive factorial"
    [n]
    (if (= n 1) 
        1 
        (* n (fac1 (dec n)))))

1
2
3
4
5
6
7
8
(defn fac2
    "Recursive factorial with loop/recur"
    [n]
    (loop [i n
           total 1]
        (if (= i 1)
            total
            (recur (- i 1) (* total i)))))

1
2
3
(defmulti fac3 identity)
(defmethod fac3 1 [_] 1)
(defmethod fac3 :default [n] (* n (fac3 (dec n))))

1
2
3
4
(defn fac4
    [n]
    "Factorial as an implicit hylomorphism" 
    (reduce * (range 1 (inc n))))

1
2
3
(defn fac5 
    [n]
    (apply * (take n (iterate inc 1))))

May 5 2009

Being about code

GitHub didn’t catch on because of Git. It may have helped, but it wasn’t the primary catalyst.

GitHub caught on because it’s about code. Sharing, finding, and contributing code.

Rubyforge and Sourceforge aren’t about code. They never were.

defunkt’s Gist

GitHub has been described as a social networking site. I think this is true, except that its the code that is social rather than the people. And it really works.

Unmythic

I have discovered that a lot of the problems I have with many fantasy stories and games seems to stem from settings with very concrete backgrounds; All histories and science but no room for anything really mythic.

What do I actually mean by mythic? Stories, each one ages old, that explains something about the way things are in terms of heros and villians. These stories stand largely on their own, even if they are part of a greater mythic picture. And importantly myths conflict with each other. The best myths seem metaphorical, even when they contain some concrete truth.

This struck me while reading Open Grave, the D&D 4e undead book. D&D in particular has a very strong tradition of settings where the early history of the world is very well established. Given the sparseness of the D&D Points of Light implicit setting, the concreteness is unfortunate. Open Grave presents a very scientific explanation of undeath in the setting. The result is some very cool ideas, but none of the horror that I want to associate with undead.

In contrast, the Wheel of Time series of novels has done well at presenting both the explained histories that the people of the world knows, alongside the rumors and speculation of ordinary people of the world. Another example is Trail of Cthulhu’s chapters on the various Gods and Great Old Ones of the setting where multiple possibilities are presented for the keeper to consider.

Hellboy is another good example. The characters could realistically know about the myth and murky past of the world but it is generally shown rather than explained and is presented in a much more snapshot view. There are lots of snippets for the reader to see, but the bigger picture eludes them.

Apr 4 2009

The Haunting: Terms

A follow-up to the recap of The Haunting about some of the terms and ideas mentioned there, and a couple of others from such sources as The Sons of Kyros. These are all simple techniques that can be applied to most RPGs and help things tick over more easily.

Consider Yes and Say Yes, or Roll The Dice

I learnt about this from the Sons of Kyros. Any time a player asks to try something, the GM should consider saying yes if it wont upset things. Additionally, if you choose not to say “Yes”, then you consider rolling the dice to determine the outcome.

Let It Ride

This is an idea from the Burning Wheel family of games. The idea is that every time you pick up the dice it should matter. You only roll once for any test and that result holds until circumstances change.

For example, you only make one Library Use test for a particular search, not one per every 4 hours. The result holds until the situation changes. This might be finding a new set of clues. Eg; Doing a search of a newspaper morgue for references to “21 Sheafe St” might fail. Later in the hall of records you discover it was once owned by one Walter Corbitt Esq. Time to hit the library again.

It is important to realize this goes both way; The players must accept failure and the GM must accept the success.

Failure

Both Mouse Guard and Trail of Cthulhu spend a lot of effort making failure not derail the story. Failure should create a new situation to be resolved rather than just cutting short the course of action.

Core Clues

Following from that view of failure, Trail of Cthulhu makes it possible to find every clue that is required to solve the scenario. Characters still need to actively hunt for the clues, but no dice are rolled. Secondary clues are subject to the normal resolution.

The Enmity Clause

A specific case of Say Yes, or Roll The Dice taken from Burning Wheel’s Circles subsystem. In a situation where the player wants to find a character thus far unknown. In Burning Wheel, you test your circles attribute; A failed roll results in finding the character, but for some reason the new character has enmity toward the PCs.

In our game, after some chaos in the basement that resulted in one character being shot in the back and another in the arm, the investigators went in search of a back street doctor to perform the necessary surgeries for them under the table. The Lawyer suggested that from his firms defense practices he would know someone suitable. I rolled either Law or Credit Rating for him, but he failed.

They still found the doctor, but he was immediately suspicious of the wounds and blackmailed them to keep quiet about the incident. I planned to have the character create more trouble, but things didn’t quite work out that way.

Beliefs

Beliefs are a small but important part of how characters are defined in Burning Wheel. These are generally a one or two sentence statement that motivates the character. This might be a goal, a motivation, something immediate. The key thing is that it be something that pushes your character forward.

Of the Burning Wheel ‘BITs’ — Beliefs, Instincts and Traits — they are easiest to extract for use in other games even without the mechanical rewards built up around them.

See Also:

The Haunting

I ran the classic Call of Cthulhu scenario The Haunting with my D&D group over a two nights as an interlude in our usual Age of Worms epic campaign. I’ll skip over the story details because the haunting has been covered so many times already.

Rules-wise, I used the core Call of Cthulhu BRP rule set, summarized to a page cheat sheet for the players. Additionally I tried to apply concepts from Trail of Cthulhu and the Burning Wheel family of games. Ideas like Let It Ride and Core Clues I explicitly used. The general flavor of how Trail and Burning Wheel treat dice rolling and failure held for a lot of the game. My favorite ruling was quietly using the Enmity Clause when the players were effectively circling up a back alley doctor to help with their (self inflicted) gunshot wounds.

What rocked

Beliefs; We played a pickup style game. I made 7 pre-gens for 5 players, only one character outline was provided for me. Each character was bare bones attributes plus skills, and a weapon of some description. Instead of writing complex back story for each character matt and I cooked up 2 or 3 beliefs per character and I handed those out when players selected characters. The players did a great job of taking the general shapes of characters and fleshing them out. There was a lot of great drama and conflict in the party that really built things up once they action reached the house.

In addition to the Beliefs, I managed to restrain myself at the start of the game and give the characters a few minutes to role-play introductions and flesh out their characters in game. Worked great, I’m sure I’ll use this technique again.

The prop newspapers and photos I spent ages making worked great. They should hopefully be going up on Yog-Sothoth once they have had a final edit.

What could have been better

There were a couple of scenes where I lost hold of the BW/ToC ideas and slipped back into more traditional style and framed scenes poorly. In particular the Chapel of Contemplation scenes were very weak and full of whiffs.

The end of the scenario felt quite anti-climactic; I don’t know whether this was because it came after the confusion and drama that started with upstairs bedroom and continued down in the basement, or because its just not a very exciting final scene after all that has gone before it.

Feb 2 2009

Comments, revisited

No comments‽” was a common theme in the response to monday’s post a better blog. I did not intend to imply that I don’t value the opinions and thoughts of my audience. Rather that comments are a poor medium for discussion.

Firstly, commenting generally has a low barrier to entry. It is easy to belt out a hurried, flippant, or poorly considered response. By moving the comments off the site the barrier to entry is higher; The responder is more likely to invest time in the reply.

Related to this, the reply has a stronger attachment to the author’s identity, and “…they’re forced to defend their ideas on their own turf…It forces a level of consideration that, without fail, results in a higher quality exchange of ideas.”

Thirdly, comments are a ghetto filled with second class citizens. Your opinion is less valued than that of the post’s author. This is why I added the miscellany to my site. Every user has as much power there as I do, including the ability to edit their own and others words.

Two two

brehaut.net has reached another milestone today. I am calling the current state version 2.2; Although it is the fourth major version of the code-base, it is the second revision to the design I launched in December of 06. Since then we have created 235 pages in the miscellany with 3495 revisions!

The design and layout got a shakeup, and the blog is back in an active form (and the old archives are all but gone). The geeks among you may appreciate the changes to macro system on the wiki (syntax highlighting for all!).

So thank you for your contributions, critiques and suggestions.

A better blog

Some thoughts on a creating a better blog for myself from reading and studying sites I rate highly.

Why read a blog? I read blogs to follow one person’s attention, and to be directed to new ideas and gain new perspective on ideas I am already familiar with. Typically the focus of the blog will have a reasonable overlap with my own interests. I will also discover new things that I would not actively seek out myself.

Brevity is important. This is a lesson I learned from Twitter. I am happy to know about your lunch or trip to the zoo if I can digest it easily and move on. More detailed articles are better served by real pages or very focused blogs.

I have very little interest in comments. Sturgeon’s law applies doubly for blog comments. A communities social character is unavoidably tied to its technical details (see Group Enemy).

The only content I wish to see on my blog is interesting content. Pingbacks provide a possible solution here; If I am able to view a private report on posts that ping back, I can cherry-pick the most interesting and relevant responses for readers to see. No public commenting system would exist.

The design of writing interface is vital. Write and editing must be separate from posting. Quick, unedited thoughts should be discouraged by design. Software should warn about basic writing mistakes such as post length, double negation and passive voice.

See also