infinite lazy-lists and folding them in f#

On key-feature in Haskell is it’s laziness, meaning that it does it’s evaluations in normal-order (an expression is evaluated when the value is needed). F# on the other hand uses applicative evaluation (for example: parameters to a function are evaluated before the function is called – so not the expressions but their values are passed).

Normally this is fine and there is no problem, but if you want to work with infinite lists things are getting interesting.

Look at this example in Haskell:

let natN = [0..]
let posN = foldr (\ a s -> (a+1):s) [] natN

It creates the natural numbers (starting at 0) – a infinite list natN and uses a right-fold to just increment each value by 1 (getting the positive natural numbers) – aside from the fact that this is a rather stupid example it shows that – being lazy – Haskell has no problem folding infinite lists under certain circumstances.

This article tries to explain how you could do something similar in F#, introducing the nice lazy-computation syntax on the go.

Defining a lazy-list type

So let’s start by introducing our own lazy-list – as each list those can be either empty or can be a “con” – a pair of a value and the rest of the list (another lazy-list) but in our case the rest or tail should be lazy (i.e. only constructed on demand). You could either use functions of type unit –> ‘a for the laziness, or – as I choose – use the lazy computations F# comes bundled with:

type LazyList<'a> =
   | Empty
   | Cons of 'a * Lazy<LazyList<'a>>
   with
   member l.Head =
      match l with
      | Cons (h, _) -> h
      | Empty -> failwith "empty list"
   member l.Tail =
      match l with
      | Cons (_, t) -> t.Force()
      | Empty -> failwith "empty list"

As you can see I implemented two simple properties to get to the Head/Tail part of the list.

Maybe you haven’t seen the lazy syntax yet. It’s very simple: if you wrap a expression inside a lazy( … ) F# won’t evaluate the expression till you call .Force() on the resulting value. Force() will return the evaluated value and even memoize it so that it’s only computed once – nice.

The basics

Of course you should provide the common primitives, namely singleton, cons, map and iter for this list, and it’s almost a no-brainer (“let the types guide you…”):

module LazyList = 

   let single a = Cons (a, lazy ( Empty )) 

   let cons (a : 'a) (l : LazyList<'a>) = Cons (a, lazy ( l )) 

   let rec map (f : 'a -> 'b) (l : LazyList<'a>) : LazyList<'b> =
      match l with
      | Empty -> Empty
      | Cons (a, t) -> Cons (f a, lazy (map f (t.Force()))) 

   let rec iter (f : 'a -> unit) (l : LazyList<'a>) : unit =
      match l with
      | Empty -> ()
      | Cons (a, t) -> f a iter f (t.Force())

Ok – the lazy/force and it’s parentheses somewhat clutter the code (you certainly could improve here) but I think its succinct enough.

To really do something similar with a infinite list I need take too (well iter endlessly… well):

module LazyList = 

   let rec take nr (l : LazyList<'a>) : LazyList<'a> =
      match l with
      | Empty -> Empty
      | Cons (a, t) ->
         if nr = 0 then Empty
         else Cons (a, lazy (take (nr-1) (t.Force()))) [/sourcecode

Unfolding the naturals

So how to we get the infinite naturals in the first place? Well this is what unfold is for:

module LazyList = 

   let rec unfold (f : 's -> ('a*'s) option) (init : 's) : LazyList<'a> =
      match f init with
      | None -> Empty
      | Some (a, s) -> Cons (a, lazy ( unfold f s))

with this you can define the naturals by calling

let natNumbers = LazyList.unfold (fun n -> Some (n, n+1)) 0

Defining the right-fold

This is somewhat more tricky as we have to simulate Haskell’s laziness in just the right place. If you implement foldr the usual way:

let rec foldr' (f : 'a -> 's -> 's) (init : 's) (l : LazyList<'a>) : 's =
   match l with
   | Empty -> init
   | Cons (a, t) -> f a (foldr' f init (t.Force()))

you get the problem that F# begins to evaluate the current state for the call of the fold function f (in the Cons-case) and this will get you a nice stack-overflow exception in no time.

So we have to introduce laziness here:

let rec foldr (f : 'a -> Lazy<'s> -> 's) (init : 's) (l : LazyList<'a>) : 's =
   match l with
   | Empty -> init
   | Cons (a, t) -> f a (lazy (foldr f init (t.Force())))

That’s it – now we can to the Haskell equivalent:

let posNatNumbers =
   LazyList.foldr (fun n l -> Cons ((n+1), l)) Empty natNumbers 

posNatNumbers |> LazyList.take 20 |> LazyList.iter (printf "%d, ")

giving us the desired output:

> posNatNumbers |> LazyList.take 20 |> LazyList.iter (printf "%d, ");;

 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, val it: unit = ()

Disclaimer:

This is not meant to be production code or performance-optimized but I think it shows that there is a lot more to F# than you would expect but just toying around with seqs and lists.

a simple fractal using Paper.js

Currently I’m trying to get a bit more JavaScript and HTML5 exposure (no use denying it I guess – it’s here to stay) and found the Paper.js framework which seems to be really nice.

An easy way to test a vector-based framework often are fractals where you can get visual pleasing images that are easy to generate and are often very taxing on the frameworks and render-engines.

The fractal’s construction

The easiest one I can think of is Sierpinski’s triangle:

PaperJsSierpinski

It’s really easy to generate:

given 3 Points (of a triangle) A, B, C just create the midpoints on AB, AC and BC and create 3 new triangles (A, AB, AC), (AB, B, BC) and (AC, BC, C) from it – repeat recursively for each of those and you’ve got your fractal:

SierpinskiKonstruction

Implementing this using Paper.js

After all this is very straight-forward, as Paper.js is capable of performing basic vector.operations – so all there is to do is including the paper.js script into your page, adding a canvas-element in the body and doing the drawing in a special “text/paperscript” script:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Simple Fractal with Paper.Js</title>

    <script type="text/javascript" src="Assets/paper.js"></script>
    <script type="text/paperscript" src="Fractal.js" canvas="myCanvas"></script>

</head>
<body>
    <canvas id="myCanvas" resize="true"></canvas>
</body>
</html>

Here is the code for the fractals-drawing (fractal.js):

function genFractal (a, b, c, depth) {
    if (depth == 0) drawTriangle(a, b, c);
    else {
        var ab = (a + b) / 2;
        var ac = (a + c) / 2;
        var bc = (b + c) / 2;

        genFractal(a, ab, ac, depth - 1);
        genFractal(ab, b, bc, depth - 1);
        genFractal(ac, bc, c, depth - 1);
    }
}

function drawTriangle(a, b, c) {
    var path = new Path();
    path.strokeColor = 'red';
    path.moveTo(a);
    path.lineTo(b);
    path.lineTo(c);
    path.lineTo(a);
}

genFractal(new Point(10, 610), new Point(610, 610), new Point(310, 10), 7);

As you can see it’s very easy – just create the new points, generate another iteration of triangles and draw them on the final recursion depth – this will produce the red triangle you see above.

How-to: Setup your XAML-Project using multiple resource dictionaries

If you ever worked with a larger XAML Project the entries in your resource dictionaries will start to pile up and it’s really a pain to find where you put some Brush or Style or whatever.

Wouldn’t it be nice if you could separate your resources into different kinds and manage them in there own files? Well of course you can – and it’s very easy but there are some steps to remember.

I like having my resource dictionaries in a separate folder within my projects – I guess most people call this “Assets” but name mine “Resources” (go figure) – so first create a new folder with whatever name you want and put a new “Resource Dictionary” in there:

AddResourceDictionary

(Note I’m using the VS11 beta but it’s exactly the same in VS2010)

The file opens like this:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

</ResourceDictionary>

And you can begin filling your resources in there (of course there is support in Blend for this too).

The next step is telling your main resource-dictionary (usually in the App.xaml file) to merge with your newly created dictionary. This is done using something like the following:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <Color x:Key="Redish" R="155" />
                </ResourceDictionary>
                <ResourceDictionary Source="Resources/Templates.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

As you can see – all that there is to it is using the ResourceDictionary.MergedDictionaries collection and adding dictionaries. In this case I use one “in-place” and the Templates dictionary I created in the last step (setting the Source property).

That’s it – now you can use those resources as if you had defined them in your local or App resources.

get test support for XUnit and NUnit in VS11 beta

after banging my head against the wall because the test runner did not find my tests I finally found the reason: you have to install additional plugins into VS11 (beta).

And it seems like they are not part of the extension-manager lib at the moment.

You can find the plugin for NUnit here: NUnit Test Adapter (Beta) and the plugin for XUnit here: xUnit.net runner for Visual Studio 11 Beta.

Both work like a charm for me (after restart of VS11 of course).

Performance comparison

To get a quick overview I choose to test all three possibilities with this test-code:

module Utilities = 

     /// This is a sample function
     let sampleFunction argument1 argument2 =
         argument1 + argument2 

[<TestClass>]
type TestUtilities() =

    [<TestMethod>]
    member test.``sampleFunction mit 5 und 4 ergibt 9``() =
        Assert.AreEqual(9, Utilities.sampleFunction 5 4)

[<NUnit.Framework.TestFixture>]
type NUnitTests() =
    [<NUnit.Framework.Test>]
    member test.``NUNIT - sampleFunction mit 5 und 4 ergibt 9``() =
        Assert.AreEqual(9, Utilities.sampleFunction 5 4)

type XUnitTests() =
    [<Xunit.Fact>]
    member test.``XUNIT - sampleFunction mit 5 und 4 ergibt 9``() =
        Assert.AreEqual(9, Utilities.sampleFunction 5 4)

With this I get the following result (tested 10times with “Run all” and 4 times each with “Run selected” – the values won’t change too much on my machine):

MS-Test: ~8ms

NUnit: ~9ms

XUnit: ~167ms

TestRun

Don’t understand why XUnit is that slow and maybe it’s only the first loaded test, but I will stick with Nunit anyway.

a correct ‘modulus’ for F#

In math we use the remainder for modular-arithmetic. That is given two numbers n and m we already saw that we can express n as

n = mq + r

where r is in the range 0 \leq r < m – and we call this the remainder of dividing n by m, so for negative numbers we have for example:

 -14 = -3*5 + 1 so  -14 = 1 (mod 5).

But in F# we have:

% (modulus, mod)

Returns the remainder of a division operation. The sign of the result is the same as the sign of the first operand

So

> -14 % 5;;
val it : int = -4

So we have to change this if we want correct values for negative numbers.

There are a couple of options, ranging from checking the sign and using different formulas (cases) but the simples one is just using

let modulo n m = ((n % m) + m) % m

if m > 0.

> modulo -14 5;;
val it : int = 1

This should be sufficient for most problems but

    let inline modulo n m =
        let mod' = n % m
        if sign mod' >= 0 then mod'
        else abs m + mod'

might be more efficient if you deal with big integers (or some other kind of values where the %-operator is not easily computed).

Update:

After messing up a bit with the case, when n < 0 I should make sure that all works as it should – here is the test validating the second version for those special occasions:

[<Test>]
member test.``modulo should respect the math way``() =
   let testPairs =
      [ for d in [-3..3] do
        for r in [0..5] do
        yield (d,r)
      ]

   let testOnePair (d,r) =
      let z = d*(-6)+r
      let r' = modulo z (-6)
      r |> should equal r'

   testPairs |> List.iter testOnePair</pre>

an interesting combinatorial algorithm straight from elementary-school

The Problem:

Not long I was asked for advice, because a problem-type in elementary math (indeed from a first-grader) proved to be quite a challenge for my nephew and I was if I had some quick tips.

The problem is easily stated:

The pupils at this stage know about the basic coins and bills: 1€, 2€, 5€ and 10€ – now there is a given price for some nice item (for example a book for 10€) and there are a couple of purses each with a number of money-items (coins or bills) in them but not which.

For the book example there might be a purse with 5 items. The kids are asked to write down what coins or bills are in there – a sample solution here would be of course five 2€-coins.

The hard part is of course teaching this to a kid without our day-to-day experience with this kind of problem (it the end he found the spark and now it’s of course “easy”).

But the problem is just the right think for a short post on combinatorial algorithms without the 100’s copy of “how to get all permutations” (of course it’s not that different).

What the algorithm should find

I don’t want to solve the problem as it is but I want the algorithm to just find each combination of coins/bills for a given price (and available coins/bills).

So given a list of those:

avaiableUnits=[1.0; 2.0; 5.0; 10.0]

it should for example find all combinations for 12€ – here are the tests:

namespace LearnMoneyCombinatorics.Core.UnitTests

open System
open NUnit.Framework
open FsUnit

open LearnMoneyCombinatorics.Core

[<TestFixture>]
type ``Using Combinations for 12 EUR with only 1eur, 2eur, 5eur and 10eur - coins and notes``() =

    let generator = new Combinations(avaiableUnits=[1.0; 2.0; 5.0; 10.0])
    let amount = 12.0
    let validCombinations = generator.GetValidCombinations amount

    [<Test>]
    member test.
        ``there are 15 combinations``() =
            validCombinations |> Seq.toArray
            |> should haveLength 15

    [<Test>]
    member test.
        ``1 x 10€ and 1 x 2€ is a valid combination``() =
            validCombinations
            |> should contain (collect [10.0 |> times 1; 2.0 |> times 1] )

    [<Test>]
    member test.
        ``1 x 10€ and 2 x 1€ is a valid combination``() =
            validCombinations
            |> should contain (collect [10.0 |> times 1; 1.0 |> times 2] )

    [<Test>]
    member test.
        ``2 x 5€ and 1 x 2€ is a valid combination``() =
            validCombinations
            |> should contain (collect [5.0 |> times 2; 2.0 |> times 1] )

    [<Test>]
    member test.
        ``2 x 5€ and 2 x 1€ is a valid combination``() =
            validCombinations
            |> should contain (collect [5.0 |> times 2; 1.0 |> times 2] )

    [<Test>]
    member test.
        ``1 x 5€ and 3 x 2€ and 1 x 1€ is a valid combination``() =
            validCombinations
            |> should contain (collect [5.0 |> times 1; 2.0 |> times 3; 1.0 |> times 1] )

    [<Test>]
    member test.
        ``1 x 5€ and 2 x 2€ and 3 x 1€ is a valid combination``() =
            validCombinations
            |> should contain (collect [5.0 |> times 1; 2.0 |> times 2; 1.0 |> times 3] )

    [<Test>]
    member test.
        ``1 x 5€ and 1 x 2€ and 5 x 1€ is a valid combination``() =
            validCombinations
            |> should contain (collect [5.0 |> times 1; 2.0 |> times 1; 1.0 |> times 5] )

    [<Test>]
    member test.
        ``1 x 5€ and 7 x 1€ is a valid combination``() =
            validCombinations
            |> should contain (collect [5.0 |> times 1; 1.0 |> times 7] )

    [<Test>]
    member test.
        ``6 x 2€ is a valid combination``() =
            validCombinations
            |> should contain (2.0 |> times 6)

    [<Test>]
    member test.
        ``5 x 2€ and 2 x 1€ is a valid combination``() =
            validCombinations
            |> should contain (collect [2.0 |> times 5; 1.0 |> times 2])

    [<Test>]
    member test.
        ``4 x 2€ and 4 x 1€ is a valid combination``() =
            validCombinations
            |> should contain (collect [2.0 |> times 4; 1.0 |> times 4])

    [<Test>]
    member test.
        ``3 x 2€ and 6 x 1€ is a valid combination``() =
            validCombinations
            |> should contain (collect [2.0 |> times 3; 1.0 |> times 6])

    [<Test>]
    member test.
        ``2 x 2€ and 8 x 1€ is a valid combination``() =
            validCombinations
            |> should contain (collect [2.0 |> times 2; 1.0 |> times 8])

    [<Test>]
    member test.
        ``1 x 2€ and 10 x 1€ is a valid combination``() =
            validCombinations
            |> should contain (collect [2.0 |> times 1; 1.0 |> times 10])

    [<Test>]
    member test.
        ``12 x 1€ is a valid combination``() =
            validCombinations
            |> should contain (1.0 |> times 12)

How to solve

The tests give a pretty good indication of how to solve this – start with the biggest bill/coin still fitting the needed price, subtract all possible multiples of this from the price and recursively find the combinations for the rest-price and combine those together.

This is really a standard algorithm to solve this kinds of problems – divide and conquer with recursion.

Let’s do a few steps to get a feeling for this.

We have to spend 12€ so we can use one or zero 10€-bills.

Let’s use one bill – then we still have to spend 2€.

For 2€ we can take no 10€ bill, no 5€ bill but one or zero 2€ coins so let’s take one – we have nothing more to pay so we have found a combination: [1x10€; 1x2€].

For the next trace back and take no 2€ – then we can take next two, one or none 1€ coins – take two and again we found a combination: [1x10€; 2x1€], …

The implementation

To hold the data I use two simple structures – one to remember the coin/bill together with its multiplicity (how many of those you use for a combination) and the combination itself being only a set of those structures from before – the code will be much more clearer than this “description” so don’t worry Smiley

Warning: I did not bother to make this tail-recursive (to make it a bit more readable) but I choose to use sequences to get a one-at-a time combination instead of waiting to get all at once – you can easily switch by changing the Seq.xyz methods to List.xyz ones and changing the seq {…} block to a List.combine.

Here is the code:

namespace LearnMoneyCombinatorics.Core

open System

type CombinationItem = int * float
type Combination = CombinationItem Set

[<AutoOpen>]
module CombinationHelpers =
    let collect items : Combination =
        items |> Set.unionMany
    let times count amount : Combination =
        (count, amount) |> Set.singleton
    let total (comb : Combination) : float =
        comb |> Set.fold (fun t (c, u) -> t+(float c * u)) 0.0

type Combinations(avaiableUnits : float seq) =

    let maxCount u (a : float) = Math.Floor(a / u) |> int
    let avUnitsDesc = avaiableUnits |> Seq.sortBy (fun a -> -1.0 * a) |> Seq.toArray

    let rec createCombinations restA i : Combination seq =
        if i >= avUnitsDesc.Length && restA > 0.0 then Seq.empty
        elif restA = 0.0 then Set.empty |> Seq.singleton
        else
        let curUnit = avUnitsDesc.[i]
        let maxC = restA |> maxCount curUnit
        let combine count =
            let cur = if count = 0 then Set.empty else curUnit |> times count
            let remaining = createCombinations (restA - float count * curUnit) (i+1)
            remaining |> Seq.map (fun r -> Set.union cur r)
        seq {
            for i in [maxC .. -1 .. 0] do
                yield! combine i
        }

    let combinations amount = createCombinations amount 0

    member c.GetValidCombinations(amount) : Combination seq = combinations amount

All the work is done inside createCombinations – a recursive function.

The parameters a first the rest-price to find and second a index into the ordered available coin/bill values (what next to try – so to say).

It first checks: if there is still money to spend (restA >  0.0) and we have no more coins to try we fail (Seq.empty back).

If-not (that is if we have paid for all) and we don’t have any more and return a empty combination.

In every other case we look check how many times we can use the current value (curUnit) to spend on the rest-amount of money (maxCount) combine every one of these recursively with all the possible combinations for the rest-amount to finally yield those in a sequence.

What’s important to understand is that – as in the permutation case – we have to remember that we tried – let’s say 10€ – and do not try those again – this is done here by using the ordered amount-list and the index into this.

That’s it for today – I hope you enjoyed this brief episode.

todays non-BUG

(or you get what you ordered)

I had a major clash with a nasty bug (or so I thought) today:

For a WPF application I had to load a image from some byte-array (loaded from a database) and show it. The code is not so difficult:

        private static BitmapImage LoadImage(byte[] imageData)
        {
            if (imageData == null || imageData.Length == 0) return null;
            var image = new BitmapImage();
            using (var mem = new MemoryStream(imageData))
            {
                mem.Position = 0;
                image.BeginInit();
                image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.UriSource = null;
                image.StreamSource = mem;
                image.EndInit();
            }
            image.Freeze();
            return image;
        }c

It’s rather simple: wrap the data into a memory-stream and use this to initialize a BitmapImage that gets returned.

So far so good …

This is done in a ViewModel that exposes the loaded Image – this is then bound to the Source-Property of a Image on the WPF-page and here the trouble started…

Letting this run and playing a bit I observed the memory-consumption of my app go from 40MB up to 500MB and more … WTF?

Trying to use the inbuilt Memory profiler (VS2010) is a pain but the ANTS-memory profiler pointed me to “unmanaged code” … WTF?

Ok – so I went and downloaded a trial-version of the .net memory profiler (can handle unmanaged code) and voila:

MemoryProfile2

Indeed seems like a problem in BitmapImage.EndInit()…

Ok – time to fire up Bing (or Google or whatever you want) – voila: seems like a “known bug” with a lot of comments and StackOverflow questions (for example: this) (ok – some sites claimed that this will be fixed in .net 3.5 SP 1 – and I’m using .net 4.0 – but seems to be the reasonable cause of it – doesn’t it?)

So I went and tried every fix proposed to no avail – but sure enough if I didn’t bind or load the bitmap the memory did behave…

First glimpse of the solution…

I did a lot of testing / trial and so I finally saw that the memory did not leak without bounds but always stayed below 600MB and on occasion got freed up quite a bit – this was the first clue – maybe it’s not bug but why does data coming from a 1MB jpg causes a >100MB raise … WAIT

What was it? 1 MB jpg? … look at it and finally see the “Pudels Kern”: the jpg has a resolution of about 10,000 x 4,000 pixel … wow – and this translates (unzips) into >100 MB as a simple calculation yields … DOH

So that’s it – no bug at all but some really bad data (those files where uploaded by users getting them by exporting some AutoCAD Images ….)

The final question:

Why did I forget the first rule “First check the user(-data)”

a new blog theme/design

As someone mentioned: the blog was just hard to read so I played a bit with the settings. It’s the same theme but now on the light sight and I enlarged all the latex-parts in the last posting.

Is this better? Or does someone have a great theme/setting I should use/try?

And as I’m on the questing-train: does someone have any tips concerning posts with lots of formula? I use the WP latex plugin (easy do install/setup but lacking in many other way) but it’s hard to change the sizes (have to put “size = “1”” everywhere) and I don’t like the way it places the formulas inline.

Maybe some “math” blog-expert can shed some light on this.

Thanks you.