Power of mathematics: Reasoning about functional types

One of the most amazing aspects of mathematics is that it applies to such a wide range of areas. The same mathematical rules can be applied to completely different objects (say, forces in physics or markets in economics) and they work exactly the same way.
In this article, we'll look at one such fascinating use of mathematics - we'll use elementary school algebra to reason about functional data types.
In functional programming, the best way to start solving a problem is to think about the data types that are needed to represent the data that you will be working with. This gives you a simple starting point and a great tool to communicate and develop your ideas. I call this approach Type-First Development and I wrote about it earlier, so I won't repeat that here.
The two most elementary types in functional languages are tuples (also called pairs or product types) and discriminated unions (also called algebraic data types, case classes or sum types). It turns out that these two types are closely related to multiplication and addition in algebra...
Read the complete article (English)
Tuesday, May 14, 2013
Async in C# and F#: Asynchronous gotchas in C#
Back in February, I attended the annual MVP summit - an event organized by Microsoft for MVPs. I used that opportunity to also visit Boston and New York and do two F# talks and to record a Channel9 lecutre about type providers [5]. Despite all the other activities (often involving pubs, other F# people and long sleeping in the mornings), I also managed to come to some talks!

One (non-NDA) talk was the Async Clinic [1] talk about the new async
and await
keywords
in C# 5.0. Lucian and Stephen talked about common problems that C# developers face when
writing asynchronous programs. In this blog post, I'll look at some of the problems from
the F# perspective. The talk was quite lively, and someone recorded the reaction of the
F# part of the audience as follows...
Read the complete article (English)
Monday, April 15, 2013
F# Data: New type provider library

When F# 3.0 type providers were still in beta version, I wrote a couple of type
providers as examples for talks. These included the WorldBank type provider
(now available on Try F#) and also type provider for
XML that infered the structure from sample.
For some time, these were hosted as part of FSharpX
and the authors of FSharpX also added a number of great features.
When I found some more time earlier this year, I decided to start a new library that would be fully focused on data access in F# and on type providers and I started working on F# Data. The library has now reached a stable state and Steffen also announced that the document type providers (JSON, XML and CSV) are not going to be available in FSharpX since the next version.
This means that if you're interested in accessing data using F# type providers, you should now go to F# Data. Here are the most important links:
Before looking at the details, I would like to thank to Gustavo Guerra who made some amazing contributions to the library! (More contributors are always welcome, so continue reading if you're interested...)
Read the complete article (English)
Thursday, March 28, 2013
Upcoming F# talks and trainings (London, NYC & Boston)

After a few years, I finally managed to find the time to attend the MVP Summit this year. The Summit is an annual event where people who are active in Microsoft technical communities come to the Microsoft headquarters to learn about new things (and, of course, tell them how they should be doing their job :-)). The Summit is a great place to meet interesting people (so if you're reading this and will be there, definitely get in touch!)
The good news - for those interested in F# - is that I'll be doing two F# talks on the way and we also have a new F# course scheduled for later in the year. Continue reading to learn more!
Read the complete article (English)
Thursday, February 07, 2013
Announcing: Literate programming tools for F#

For some time now, I've been writing my F# blog posts (and other F# articles published elsewhere) by combining F# code snippets and Markdown formatting. In fact, I even wrote a Markdown parser in F# so that I can post-process documents (to generate references etc). You can read about the Markdown parser in an upcoming F# Deep Dives book - currently, it is available as a free chapter!
During the Christmas break, I finally found enough time to clean-up the code I was using and package it properly into a documented library that is easy to install and use. Here are the most important links:
- F# Formatting home page
- F# Formatting source code on GitHub
- F# Formatting package on NuGet
To learn more about the tool and how to use it, continue reading!
Read the complete article (English)
Tuesday, January 22, 2013
Processing trees with F# zipper computation
One of the less frequently advertised new features in F# 3.0 is the query syntax.
It is an extension that makes it possible to add custom operations in an F#
computation expression. The standard query { .. }
computation uses this to define
operations such as sorting (sortBy
and sortByDescending
) or operations for taking
and skipping elements (take
, takeWhile
, ...). For example, you can write:
1: query { for x in 1 .. 10 do 2: take 3 3: sortByDescending x }
In this article I'll use the same notation for processing trees using the zipper pattern. I'll show how to define a computation that allows you to traverse a tree and perform transformations on (parts) of the tree. For example, we'll be able to say "Go to the left sub-tree, multiply all values by 2. Then go back and to the right sub-tree and divide all values by 2" as follows:
1: tree { for x in sample do 2: left 3: map (x * 2) 4: up 5: right 6: map (x / 2) 7: top }
This example behaves quite differently to the usual query
computation. It mostly
relies on custom operations like left
, right
and up
that allow us to navigate
through a tree (descend along the left or right sub-tree, go back to the parent node).
The only operation that does something is the map
operation which transforms the
current sub-tree.
This was just a brief introduction to what is possible, so let's take a detailed look at how this works...
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.query
Calls Linq.QueryBuilder.Take
Calls Linq.QueryBuilder.SortByDescending
| Node of Tree<'T> * Tree<'T>
| Leaf of 'T
override ToString : unit -> string
Full name: Tree-zipper-query.Tree<_>
Full name: Tree-zipper-query.Tree`1.ToString
| Node(l, r) -> sprintf "(%O, %O)" l r
| Leaf v -> sprintf "%O" v
| Top
| Left of Path<'T> * Tree<'T>
| Right of Path<'T> * Tree<'T>
override ToString : unit -> string
Full name: Tree-zipper-query.Path<_>
Full name: Tree-zipper-query.Path`1.ToString
| Top -> "T"
| Left(p, t) -> sprintf "L(%O, %O)" p t
| Right(p, t) -> sprintf "R(%O, %O)" p t
| TZ of Tree<'T> * Path<'T>
override ToString : unit -> string
Full name: Tree-zipper-query.TreeZipper<_>
Full name: Tree-zipper-query.TreeZipper`1.ToString
Full name: Tree-zipper-query.left
Navigates to the left sub-tree
Full name: Microsoft.FSharp.Core.Operators.failwith
Full name: Tree-zipper-query.right
Navigates to the right sub-tree
Full name: Tree-zipper-query.current
Gets the value at the current position
Full name: Tree-zipper-query.branches
Full name: Tree-zipper-query.sample
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Full name: Tree-zipper-query.up
Full name: Tree-zipper-query.top
val unit : v:'a -> TreeZipper<'a>
Full name: Tree-zipper-query.unit
Build tree zipper with singleton tree
--------------------
type unit = Unit
Full name: Microsoft.FSharp.Core.unit
Full name: Tree-zipper-query.bindSub
Transform leaves in the current sub-tree of 'treeZip'
into other trees using the provided function 'f'
type TreeZipperBuilder =
new : unit -> TreeZipperBuilder
member Current : tz:TreeZipper<'a> -> 'a
member For : tz:TreeZipper<'T> * f:('T -> TreeZipper<'T>) -> TreeZipper<'T>
member Left : tz:TreeZipper<'a> -> TreeZipper<'a>
member Right : tz:TreeZipper<'a> -> TreeZipper<'a>
member Select : tz:TreeZipper<'a> * f:('a -> 'a) -> TreeZipper<'a>
member Top : tz:TreeZipper<'a> -> TreeZipper<'a>
member Up : tz:TreeZipper<'a> -> TreeZipper<'a>
member Yield : v:'a -> TreeZipper<'a>
Full name: Tree-zipper-query.TreeZipperBuilder
--------------------
new : unit -> TreeZipperBuilder
Full name: Tree-zipper-query.TreeZipperBuilder.For
Enables the 'for x in xs do ..' syntax
Full name: Tree-zipper-query.TreeZipperBuilder.Yield
Enables the 'yield x' syntax
Full name: Tree-zipper-query.tree
Global instance of the computation builder
type CustomOperationAttribute =
inherit Attribute
new : name:string -> CustomOperationAttribute
member AllowIntoPattern : bool
member IsLikeGroupJoin : bool
member IsLikeJoin : bool
member IsLikeZip : bool
member JoinConditionWord : string
member MaintainsVariableSpace : bool
member MaintainsVariableSpaceUsingBind : bool
member Name : string
...
Full name: Microsoft.FSharp.Core.CustomOperationAttribute
--------------------
new : name:string -> CustomOperationAttribute
Full name: Tree-zipper-query.TreeZipperBuilder.Left
Full name: Tree-zipper-query.TreeZipperBuilder.Right
Full name: Tree-zipper-query.TreeZipperBuilder.Up
Full name: Tree-zipper-query.TreeZipperBuilder.Top
Full name: Tree-zipper-query.TreeZipperBuilder.Current
Extracts the current value and returns it
Full name: Tree-zipper-query.TreeZipperBuilder.Select
Transform the current sub-tree using 'f'
type ProjectionParameterAttribute =
inherit Attribute
new : unit -> ProjectionParameterAttribute
Full name: Microsoft.FSharp.Core.ProjectionParameterAttribute
--------------------
new : unit -> ProjectionParameterAttribute
Calls TreeZipperBuilder.Right
Calls TreeZipperBuilder.Left
Calls TreeZipperBuilder.Current
Extracts the current value and returns it
Calls TreeZipperBuilder.Select
Transform the current sub-tree using 'f'
Calls TreeZipperBuilder.Up
Calls TreeZipperBuilder.Top
Full name: Tree-zipper-query.tree
Global instance of the computation builder
Full name: Tree-zipper-query.sample
Calls TreeZipperBuilder.Left
Calls TreeZipperBuilder.Select
Transform the current sub-tree using 'f'
Calls TreeZipperBuilder.Up
Calls TreeZipperBuilder.Right
Calls TreeZipperBuilder.Top
Read the complete article (English)
Wednesday, December 19, 2012
Manning: F# Deep Dives deal of the day

The F# language has been around for longer than many people suspect. My first, completely outdated, blog post was from May 2006. The Microsoft Research releases, sometime around 2006 were the first stable versions that gained some interest and slowly attracted commercial users.
A lot has changed since the early days. F# now includes powerful features like computation expressions and asynchronous workflows and F# 3.0 comes with unique type provider mechanism.
There is an increasing number of users from diverse domains: F# is used to model complex domains in finance and science; asynchronous and concurrent features are used to write server-side components of social games and trading systems, but also in web programming; the expressivity of F# is used by machine learning experts to handle dirty data or classify XBox players. Moreover, the F# Software Foundation has been recently founded to support the collaboration between different commercial users, open-source community and academia.
There is an increasing interest in F#, but many of those who approach it ask (excellent) questions such as: "In what problem domains can I benefit from F#?" or "How do I use F# in finance/science/gaming or web programming?" and most importantly "How do I approach different problems in F#?"
Read the complete article (English)
Tuesday, December 18, 2012
Learn F# at TechMesh and SkillsMetter

Autumn is a busy period and I already invited you to a couple of interesting events, but there are two more events that you definitely should not miss. In only two weeks, you can come to two-day Progressive F# Tutorials packed with tutorials for both F# beginners and experts. At the beginning of December, the TechMesh Conference comes with three days of talks about alternative (and future) technologies.
I'll be speaking at both Progressive F# Tutorials and TechMesh and I'm also doing a tutorial at TechMesh, so if you want to learn about F#, type providers in F# 3.0 and financial computing with F#, here are some talks that you should not miss...
Read the complete article (English)
Sunday, October 21, 2012
Applicative functors: definition and syntax
In a recent blog post, Edward Z. Yang talks about applicative functors.
He mentions two equivalent definitions of applicative functors - the standard
definition used in Haskell libraries (Applicative
) and an alternative that
has been also presented in the original paper, but is generally less
familiar (Monoidal
).
The standard definition makes a perfect sense with the standard uses in Haskell,
however I always preferred the alternative definition. Edward uses the alternative
(Monoidal
) definition to explain the laws that should hold about applicative
functors and to explain commutative applicative functors, but I think it
is even more useful.
The Monoidal
definition fits nicely with a trick that you can use to
encode applicative functors in C# using LINQ and I also used it as
a basis for an F# syntax extension that allows writing code using applicative
functors in a similar style as using monads (which is discussed in my draft
paper about writing abstract computations in F#). And I also think that
commutative applicative functors deserve more attention.
Read the complete article (English)
Tuesday, August 21, 2012
Why type-first development matters

Using functional programming language changes the way you write code in a number of ways. Many of the changes are at a small-scale. For example, you learn how to express computations in a shorter, more declarative way using higher-order functions. However, there are also many changes at a large-scale. The most notable one is that, when designing a program, you start thinking about the (data) types that represent the data your code works with.
In this article, I describe this approach. Since the acronym TDD is already taken, I call the approach Type-First Development (TFD), which is probably a better name anyway. The development is not driven by types. It starts with types, but the rest of the implementation can still use test-driven development for the implementation.
This article demonstrates the approach using a case study from a real life: My example is a project that I started working on with a friend who needed a system to log journeys with a company car (for expense reports). Using the type-first approach made it easier to understand and discuss the problem.
In many ways, TFD is a very simple approach, so this article just gives a name to a practice that is quite common among functional and F# programmers (and we have been teaching it at our F# trainings for the last year)...
Read the complete article (English)
Thursday, August 16, 2012