Dynamic Lookup in F#
Many people view dynamic and statically-typed languages as two distinct groups (and this is often a reason for never-ending discussions). In this article, I'll try to show one interesting example, which demonstrates that these two groups are not in fact that distinct and that you can implement a common dynamic language feature in F#, which is undoubtedly statically-typed. The feature that I'm talking about is dynamic invoke using a symbolic representation of the member (this is something that can be done using symbols in Ruby, but I'll shortly explain what exactly I mean).
I intentionally wrote statically-typed and dynamic instead of dynamically-typed. In my understanding dynamic is a broader term while dynamically-typed and statically-typed are of course two distinct groups. On the other side dynamic refers to language features that are usually available in dynamically-typed languages, just because it is easy to support them in a nice way. This doesn't mean that having a dynamic feature in a statically-typed language would be impossible - it is just more difficult to implement it in a way that would be similarly elegant.
Read the complete article (English)
Wednesday, June 04, 2008
Thesis: Client-side Scripting using Meta-programming
I realized that I haven’t yet posted a link to my Bachelor Thesis, which I partially worked on during my visit in Microsoft Research and which I successfully defended last year. The thesis is about a client/server web framework for F# called F# WebTools, which I already mentioned here and its abstract is following:
“Ajax” programming is becoming a de-facto standard for certain types of web applications, but unfortunately developing this kind of application is a difficult task. Developers have to deal with problems like a language impedance mismatch, limited execution runtime in web browser on the client-side and no integration between client and server-side parts that are developed as a two independent applications, but typically form a single and homogenous application. In this work we present the first project that deals with all three mentioned problems but which still integrates with existing web technologies such as ASP.NET on the server and JavaScript on the client. We use the F# language for writing both client and server-side part of the web application, which makes it possible to develop client-side code in a type-safe programming language using a subset of the F# library, and we provide a way to write both server-side and client-side code as a part of single homogeneous type defining the web page logic. The code is executed heterogeneously, part as JavaScript on the client, and part as native code on the server. Finally we use monadic syntax for the separation of client and server-side code, tracking this separation through the F# type system.
The full text is available here: Client side scripting using meta-programming (PDF, 1.31MB)
Read the complete article (English)
Monday, March 17, 2008
F# Support for ASP.NET and Notes on Samples
As I mentioned earlier, I spent three months as an intern in Microsoft Research in Cambridge last year and I was working with Don Syme and James Margetson from the F# team. Most of the time I was working on the F# Web Toolkit, which I introduced on the blog some time ago [1], but I also worked on a few additions that are now part of the F# release. Probably the most useful addition is a new implementation of the CodeDOM provider for the F# language which makes it possible to use ASP.NET smoothly from F# (but it can be used in some other scenarios as well) together with two ASP.NET sample applications that you can explore and use as a basis for your web sites. This was actually a part of the distribution for a few months now (I of course wanted to write this article much earlier...), so you may have already noticed, but anyway, I'd still like to write down a short description of these ASP.NET samples and also a few tips for those who're interested in writing web applications in F#.
Read the complete article (English)
Saturday, March 08, 2008
Infinite Cheese Fractal using WPF 3D and F#
I always liked fractals, because they look like objects from another world, but on the other side if you look at some things in our world you can see many similarities with fractals (but not quite as ideal with the infinite level of precision). One of my favorite fractals is 3D version of Sierpinski carpet [1], which itself is based on very famous Cantor set. Quite long time ago I thought that it would be nice to implement animation of flying through this fractal, but I was never good in 3D graphics and it looked like a lot of work, so I never get to doing it. Luckily, now with F#, which makes it very easy to write the code to generate the fractal and with WPF 3D, which can be easily used to animate the fractal, I finally had everything I needed to do it, so here it is!
Read the complete article (English)
Saturday, November 24, 2007
F# Overview (IV.) - Language Oriented Programming
In the fourth article of the F# overview series, I will shortly describe how I understad the language oriented paradigm and how the F# language can be used for developing libraries using this paradigm. We will look how discriminated unions relate to this paradigm and at three specific features that support this paradigm, namely active patterns, computation expressions and quotations.
Defining precisely what the term language oriented programming means in context of the F# language would be difficult, so I will instead explain a few examples that will demonstrate how I understand it. In general, the goal of language oriented programming is to develop a language that would be suitable for some (more specific) class of tasks and use this language for solving these tasks. Of course, developing a real programming language is extremely complex problem, so there are several ways for making it easier. As the most elementary example, you can look at XML files (with certain schema) as language that are processed by your program and solve some specific problem (for example configuring the application). As a side note, I should mention that I'm not particularly happy with the term ‘language’ in this context, because the term can be used for describing a wide range of techniques from very trivial constructs to a complex object-oriented class libraries, but I have not seen any better term for the class of techniques that I’m going to talk about.
Read the complete article (English)
Saturday, November 03, 2007
F# Overview (III.) - Imperative and Object-Oriented Programming
In the third part of the F# Overview article series, we will look at language features that are mostly well known, because they are present in most of the currently used programming languages. Indeed, I'm talking about imperative programming, which is a common way for storing and manipulating application data and about object oriented programming which is used for structuring complex programs.
In general, F# tries to make using them together with the functional constructs described in the previous part [^] as natural as possible, which yields several very powerful language constructs.
Read the complete article (English)
Saturday, November 03, 2007
F# Overview (II.) - Functional Programming
In the second part of the F# overview we will look at functional programming, which is probably the most important paradigm used with the F# language, because F# is built on the same grounds as many functional languages. We will first examine the standard F# data types, which are useful to allow the functional programming style and we will also look at a few functional tricks.
As already mentioned in the Introduction for this article series, F# is a typed functional language, by which I mean that types of all values are determined during the compile-time. However, thanks to the use of a type inference, the types are explicitly specified in the code very rarely as we will see in the following examples. Basic data types (aside from a standard set of primitive numeric and textual types that are present in any .NET language) available in F# are tuple, discriminated union, record, array, list, function and object. In the following quick overview, we will use the F# interactive, which is a tool that compiles and executes the entered text on the fly.
Read the complete article (English)
Saturday, November 03, 2007
F# Overview (I.) - Introduction
In my bachelor thesis I included a short introduction that covered all of the important aspects of the F# programming language and I thought that it may be useful to extend it a little bit to cover also a topics that were not important for my thesis and post it as an article, so there is one and relatively short article that introduces all the interesting F# features. The article got however a bit longer than I expected, so I decided to split it into a three parts that would introduce three different paradigms that are supported by F#. Of course, this series won't teach you everything about F#, but it tries to cover the main F# design goals and (hopefully) presents all the features that make F# interesting and worth learning. In this first part I will shortly introduce F# and the supported paradigms that will be discussed in the upcoming articles.
Read the complete article (English)
Saturday, November 03, 2007
F# Quotations Samples on CodePlex
Some time ago, Granville Barnett (see his homepage and old blog [^] or a new blog [^]) had a great idea and started a CodePlex project called F# Samples [^] to host various samples written in F# to demonstrate the most important concepts of both functional programming and F#. I quite like this idea, so I asked Granville if I could join and add some samples that I wrote and today I finally found a time to update what I wanted to upload to the latest version of F# and put it online.
Read the complete article (English)
Thursday, September 20, 2007
Building LINQ Queries at Runtime in F#
In an article about building LINQ queries at runtime in C# 3.0, I described how you can build a LINQ query dynamically, for example by combining a set of conditions using the 'or' operator in the where clause. I mentioned that the way I implemented it is largely influenced by the F# language, which provides very natural way for manipulations with code like this. In this article I will first shortly introduce FLINQ sample, which is an F# library implementing LINQ support and than I will implement the same examples I presented in the earlier article in F#.
Read the complete article (English)
Saturday, August 18, 2007
F# Web Tools: "Ajax" applications made simple
Traditional "Ajax" application consists of the server-side code and the client-side part written in JavaScript (the more dynamicity you want, the larger JS files you have to write), which exchanges some data with the server-side code using XmlHttpRequest, typically in JSON format. I think this approach has 3 main problems, which we tried to solve in F# Web Toolkit. There are a few projects that try to solve some of them already - the most interesting projects are Volta from Microsoft [1], Links language [3] from the University of Edinburgh and Google Web Toolkit [2], but none of the projects solve all three problems at once.
- Limited client-side environment
- Discontinuity between server and client side
- Components in web frameworks are only server-side
The aim of the F# Web Toolkit is to solve all these three problems...
Read the complete article (English)
Friday, July 13, 2007
Keep your multi-core CPU busy with F#
The growth of computer CPU speed is slowly being replaced by the growth of number of CPUs (or cores) in the computer at least for the close future. This causes a revolution in the way software is written, because traditional and most widely used way of writing concurrent applications using threads is difficult and brings several serious issues. Some predictions say that within a few years, almost every computer will have about 16 cores, so there is a huge need for programming paradigms or idioms that help developers write concurrent software easily (see also The Free Lunch Is Over [^] written by Herb Sutter).
Functional programming languages (especially pure functional languages) are interesting from this point of view, because the program doesn't have side-effects which makes it very easy to parallelize it (programs in pure functional languages can't have any side-effects by design, in other functional languages like F# the side-effects can be eliminated by following functional programming style).
This article describes the code that makes it possible to parallelize some common F# constructs like the List.map and List.filter...
Read the complete article (English)
Saturday, March 24, 2007
Quotations Visualizer for F# 1.1.13.8
Quotation Visualizer is my tool that shows visual representation of F# quotations. Stephen noticed (Thanks!) that Quotation Visualizer doesn't compile with the latest version of F# (1.1.13.8), so here is the updated version:
- Download executable (51kB)
- Download sources (77kB)
If you want to know more about this tool, here are links to previous articles about it:
What was wrong?
This tool is written using F# light syntax, which means that you don't have to write all the semicolons and F# compiler uses whitespace to determine end of the blocks (and some other constructs). This compiler option is relatively new so it is still evolving. First issue that I had is with class declaration - the following code is not valid with the latest F# version:
type MyForm = class inherit Form as base // ... end
I used it because it looks more familiar to C# developers :-). Instead of it you have to write the following:
type MyForm = class inherit Form as base // ... end
The second issue was with the pipeline operator which is useful when working with lists (or other sequences). The correct syntax for using pipeline operator (for expressions that are longer than one line) is following:
let filteredList = oldArray |> Array.to_list |> List.map ( fun m -> /* ... */ ) |> List.filter ( fun m -> /* ... */ )
Permanent link & comments (English)
Wednesday, February 14, 2007
F# presentation at Czech .NET Group meeting
On 2nd of November I did a presentation on F# and functional programming at the Czech .NET User Group meeting. Because I spent quite a lot of time with puting the presentation together I wanted to make it available to wider audience, so I translated the slides and examples to English (anyway, translating the content took me only a few minutes :-)). In case that some of the readers prefer Czech version, I attached the original documents too.
In the presentation I tried to introduce some basic concepts of functional programming (immutable values, lazy evaluation) to the audience with no experience with functional programming, as well as present some of the most interesting features of F# (like strict type system based on type inference, .NET interoperability and metaprogramming). The whole contents of the presentation is following:
- Functional programming in F# - Introduction to the F# type system
- Some useful functional idioms - How to do Foldl/Map/Filter functions and Lazy evaluation in C#
- Interactive scripting - What is important for scripting, mathematical simulation
- Interoperability between F# and other .NET languages - How to use .NET libraries from F# and F# libraries from ohter .NET languages
- F# as a language for ASP.NET - How to use F# as a language for ASP.NET development
- Meta-programming in F# - Meta-programming features in F# and the FLINQ project
Downloads
Read the complete article (English)
Tuesday, November 14, 2006
F# metaprogramming and classes
F# quotations allows you to easily write programs that manipulate with data representation of program source code. If you're not familiar with quotations I recommend reading my previous article [1] that contains short introduction to this topic first. Quotations can be used for example for translating subset of the F# language to another code representation or another language.
To get the quotation data of the expression you can either use <@ .. @>
operator or resolveTopDef function. In the first case the code written
between the "<@" and "@>" is converted to data during the compilation.
The resolveTopDef function allows you to get quotation data of top-level
definition (function) from compiled library at runtime (you have to use --quotation-data
command line switch while compiling the library). I mentioned that quotations can be used to represent
only subset of the F# language. Currently, one of the quotation limitations is that it's not possible to
enclose the whole class in the quotation operators. It is also not possible to get the representation
of the whole class at runtime nor the representation of class members (for example methods).
In this article I'll present a simple method that makes it possible to use F# quotations for working with classes as well, however this is rather a prototype that can be used for experimenting and discovering areas where working with classes might be useful, not a fully working solution.
Read the complete article (English)
Saturday, October 14, 2006
F# quotations visualizer - reloaded!
Some time ago, I wrote an article about useful utility called F# quotations visualizer.
This utility can be used to show visual representation of F# quotations, that can represent (subset of) source code written in F#.
There are two ways that you can use to get F# quotations - first is using operators <@@ ... @@> (this
returns quotation of the code written inside the operator), second method is to get quotation of top level definition from compiled
F# assembly (you have to explicitly enable this using command line switch --enable-quotation-data while compiling assembly).
Because I added several new features to the original Quotations visualizer, I decided to publish the latest version - here is the list of main improvements:
- Rewritten using active patterns (new F# language feature)
- It is possible to extract quotations from compiled F# assembly (if it contains quotation data)
- Added support for several missing language constructs
Read the complete article (English)
Sunday, October 01, 2006
F# CodeDOM Provider on the CodePlex
You probably already saw my post regarding CodeDOM generator for the F# language and how to use it with ASP.NET. To make it more accessible for everyone, I created project at the new Microsoft community site called CodePlex [^].
- To learn more about the project, visit Project Home Page
- If you want to send some comments, go to Discussion Board
- You can also download latest source code - Latest Check-Ins
- And finally, the latest releases can be found at the Releases Page
BTW: CodePlex looks like a really good site. It is based on Visual Studio Team System (which means that developers of the project can do most of the work directly from Visual Studio). It provides management of "Work Items" (TODO list), source control and many other useful things! For example if you have any feature requests or bug requests, send them to the discussion and I can easilly create work item from the message in the discussions.
If you are interested in this project and you want to help with developing of some parts, or if you are working on a project that is related to CodeDOM and F#, please let me know. Any help or feedback is kindly welcome!
Read the complete article (English)
Wednesday, August 23, 2006
ASP.NET web applications in F#
CodeDOM (Code Document Object Model) is set of objects (located in System.CodeDom namespace) that can be used
for representing logical structure of .NET source code. These classes are used for generating Web service references
(using wsdl.exe tool), for generating typed datasets and in many other situations. The most interesting
use of CodeDOM classes is in ASP.NET where ASP.NET generates code from aspx/ascx files and
compiles this code into web site assemblies (together with the code written in code behind files).
This means, that you can use any language for developing ASP.NET web sites, as long as you implement CodeDOM provider
that generates source code from CodeDOM structure and can compile these source files (this can be simply done by executing
command line compiler).
I was recently working on CodeProvider for the F# language, and finally it supports everything
what is needed by ASP.NET (however it is complete and it doesn't work for example with wsdl.exe). Using this
CodeDOM provider you can write ENTIRE web site in F# (including in-line code enclosed in <% ... source code ... %>).
I also created project template for F# web site that can be imported to Visual Studio 2005, so you can easilly try
writing web pages in F#...
Read the complete article (English)
Sunday, August 13, 2006
F# quotations visualizer
I already explained what F# quotations are and I explained how you can do some simple manipulations with it. In this article I'd like to present an application that I wrote and that can be helpful when working with quotations. It displays clear graphical representation of given F# quotation (using Windows Forms TreeView control).
Read the complete article (English)
Wednesday, June 21, 2006
Slides and demos from F# presentation
This semester I attended Advanced .NET Seminar that was led by Tomas Matousek [^] who is one of the authors of Phalanger project [^] (Which is an amazing project by the way. It takes PHP source code and compiles it without any modification to .NET). Seminar was mostly focused on Rotor and .NET internals, so if you want to learn more about these topics you can look at Advanced .NET programming [^] slides (by Tomas Matousek).
I did one presentation at this seminar too. It was about the F# language developed at Microsoft Research. It was just a quick overview of F# features, because F# is very rich topic, so it coveres only the language (functional vs. imperative behavior), F# type system, compilation of F# constructs to .NET and interoperability with .NET (for example how to create windows forms application in F#). At the end, I also mentioned F# meta-programming that allows you to look at F# code as data.
- The F# language (67kB) - presentation slides in PDF
- F# Samples (54kB) - zipped VS 2005 solution with samples
Read the complete article (English)
Friday, June 09, 2006
F# - Simple quotations transofration
This article describes very simple code that I wrote while learning how to work with the F# quotations library. Using the F# quotations you can get tree representation of the quoted expression. This allows you to write code that takes code written in F# as data and performs some code analysis or compiles/translates that code to different language. This is very similar to the new C# 3.0 expression trees where you can get expression tree from lambda expression and translate this tree for example to SQL (using DLINQ). However expression trees in C# 3.0 are very limited when compared with F# quotations, so that's one of the many reasons why F# is interesting language.
Read the complete article (English)
Sunday, May 28, 2006




