Tony Davis

Simple-Talk Editor
News, views and good brews

Life at the F# end

Published Thursday, January 21, 2010 2:50 PM

In a fascinating interview in this issue of Simple-Talk, Don Syme discusses his work on the F# programming language, its journey from it academic roots in ML and Ocaml, to its burgeoning status as the hottest new thing in .NET. A central pin in the forthcoming "parallel processing, multi-core revolution", it may or may not be, but F# has certainly captured people's attention as a simple, multi-purpose language that can be used for anything from performing complex calculations, building websites, or as a powerful scripting language.

I've been slow to latch on to F#, but it was this apparent enthusiasm for F# as a scripting language that finally prompted me to take a look, along with an observation by Simple-Talk's Laila Lotfi that it was a language "that is surprisingly intuitive for any SQL Developer". Could this possibly be the "unifying" scripting language for developers and DBAs?

Being essentially a functional language, F# has a markedly different philosophy to an imperative language such as C#. Instead of providing a detailed set of instructions that must be followed to achieve the desired result, F# applies a series of transformations and filters (in the form of functions). This certainly sounds very "SQL like" in its approach, but how does it work in practice? Coincidentally, Chad Miller wrote a small blog post on SQLServerCentral showing how to script out all the tables in a SQL Server database using F# and SMO. Leaving out the few lines required to import the SMO libraries and so on, the code is as follows:

let svr = Server(@"ServerName\InstanceName")
let db = svr.Databases.["DatabaseName"]
   for t in db.Tables do
   for s in t.Script() do
   printfn "%s" s;;

It is certainly a very clean, short, uncluttered script, but it isn't, as the author acknowledges, very F#-like. One of the strengths, though some would argue weaknesses, of F# is that it allows many different ways to tackle the same problem. Here, we simply waddle through the usual for loop, scripting out the contents of the SMO Tables object. One could write a very similar-looking script in PowerShell or Python. If we express the script in a way more idiomatic of the F# language, it might look something like this:

db.Tables
   |> Seq.cast
   |> Seq.collect (fun (t:Table) -> t.Script() |> Seq.cast)
   |> Seq.iter (fun s -> printfn "%s" s);;

What this code does, in simple terms, is get a list of tables into a parameter of type table (t:Table), and then apply a pipeline of "transformations", scripting out each table, concatenating the results (seq.collect) and then "selecting" each one out (seq.iter) and printing it. The seq.cast function translates the Ienumerable type that's delivered by SMO into something F# can use. As a fully-fledged part of the .NET framework, one would hope that over time the need for these sorts of translations will disappear.

Overall the code is still short, at least, but certainly looks a lot less intuitive at first glance! However, if you start to think of those pipeline characters (|>), which funnel values from one transformation to the next, as akin to connecting arrows in a SQL execution plan, then perhaps one can start to see how, after a little practice, this style of scripting may come fairly naturally to the SQL developer and DBA. Or maybe not!

Perhaps I will forever hope in vain for a common scripting language to unite DBAs and developers, but I do see some potential in F# and, as powerful as PowerShell undoubtedly is for database maintenance, you can't reuse what you've learned to build websites or "bond" with your developers. How proud would developers be of their DBA if he scripted database tasks using F#?!

As always, we love to hear your thoughts and objections, so please add them as comments. This week, a $50 Amazon voucher goes to randyvol for his excellent contribution to the "Reckless Drivers" blog.

Cheers,

Tony.

Comments

 

acbups said:

While F# may become the Holy Grail of unified scripting languages you seek, I really doubt it!  I have two reasons for this assertion:

1 - Programming for parallel processing as a concept is like the concepts of pointers and set-based processing - many people just don't and never will really "get" them.  Present company excepted, of course ;)

2 - The more choices one is presented increase the likelihood of a standard failing to emerge, especially when many of the choices are good.  Throwing F# into the mix will not simplify the search for a unified scripting language but the opposite!

And my personal opinion of F# is that it will remain a niche tool, analogous to writing Assembler in-line when C or C++ is your language of choice.  You do it to make particular algorithms perform an order of magnitude better, but you won't find people writing the next killer app entirely in it.

All this said, I'm very excited about F# for myself.
January 21, 2010 11:19 AM
 

BuggyFunBunny said:

I was composing a reply, but discarded it because, not being in the WinWorld for awhile, I've not seen F#.  But, then I went and read the Geek piece with Don Syme.  His first language was Prolog, a language sometimes viewed as functional.  I was not surprised.  My brush with it led me to believe that it was the perfect match for relational databases.  In due time, I ran across Amzi! (the ! is part of the name!), which is Prolog for relational databases.  

Then, Don lists the classes of languages, with relational as a language class.  Not sure whether the greater world would accept that.

I spent some time with Erlang, via the Armstrong book.  My conclusion was that Erlang, and perhaps functional languages generally, are geared toward server code; in the hope of exploiting all those parallel multi-core/processor machines.  I babbled a bit about client applications not being amenable to being sliced up for such machines someplace, and a further commentator reminded us of Amdahl's Law ( http://en.wikipedia.org/wiki/Amdahl%27s_law ).  

Since I've been arguing for RDBMS as the core of applications, rather than the client as the core (a la COBOL of the 60's), any effort to drive applications from the database out to the client, rather than the other way 'round, is fine with me.  F#, if it behaves as Erlang and others, could well become the lingua franca of server centric development.  Not so much as a language to build clients, due to the absence of parallelism; not that functional languages are inappropriate to client code, just that they're not likely to be any better at exploiting multi-machines for such problems and therefore won't be seen as worth the effort.

As I was reading through, when I got to the  for  loop part, my teeth started to itch; a  for  loop isn't very set oriented.  That F# would accept such, for backwards compatibility with procedural coders' mindsets, I suppose, makes me think of C++, and not in a good way.  The knock on C++ is that it supports vanilla C, and coders end up perpetuating C in C++.  

If they can conjure up a code generation framework using F#:  making it easy to emit web pages from the catalog, for example, they'll have something.
January 22, 2010 12:02 AM
 

cmille19 said:

I wrote the original post you referenced, it’s nice to see an F#-like solution. Admittedly my time with F# consists of less than half a day reading and hacking just to see if can do something simple. I havent' spent enough time to get my head wrapped around F# or functional programming.

What’s peaked my interest in F# is a problem I want to solve involving scripting database objects in parallel for a single large database with many (perhaps thousands) of objects Linchi in his blog Performance impact: Speeding up SMO script generation http://sqlblog.com/blogs/linchi_shea/archive/2009/12/15/performance-impact-speeding-up-smo-script-generation-the-test-code.aspx demonstrates a C# solution that uses System.Threading.  Looking at the C# code I think it should be more natural/easier.

I’m more of a DBA than developer, but use scripting languages heavily. The past couple of years I created many Powershell scripts for DBA automation tasks , but I've found one thing missing from Powershell -- handling concurrency/workflow. I don’t think I need to worry about concurrency very often, but I can see use in the  database scripting example. Although Powershell V2 adds fan-out remoting and back ground jobs these aren't the same as threading and parallel programming.

Fan-out remoting allows you to execute a Powershell command on remote systems simultaneously.  The results are returned into a single collection to the calling client. This requires having the WSMAN remoting infrastructure installed and configured. I can see the usefulness in WMI calls collecting information from dozens of servers, but databases already have remoting in data connectivity. In any case when connecting to a single database Powershell remoting doesn’t fit the use case.

Back ground jobs in Powershell V2 are interesting. You can achieve some concurrency by back grounding several long running operations. There are some limitations. The back ground jobs are a one-way non-reusable runspaces that can be resource expensive to setup. If we take the example of scripting a large database we could create a separate back job for tables and procedures and get done faster, but still not as fast as the C# example. I also don’t like having to think about breaking things up to run in separate jobs, can’t a programming language do that for me?

I’m wondering if F# is the answer to the problem. I would like to spend more time learning—maybe I’ll pick up a good book on F# from Amazon and see how far I can take this.

Oh and to the previous commenter – sorry for making your teeth itch with my sample code. Also I've tried to use the terms concurrency, threads, and parallel in the correct way. Feel free to make corrections.
January 22, 2010 12:34 PM
 

Jason Haley said:

Interesting Finds: January 23, 2010
January 23, 2010 8:15 AM
 

Hello SMO (F#) World! - Chad Miller said:

January 26, 2010 6:56 AM
 

randyvol said:

I also despair of the 'unifying' language ever being extended to cover SQL developers and DBAs.  It is for a simple reason that I've held forth on before in various forms on this site, and which, ironically is also held forth on by Itzik Ben-Gan in the 1/6/2010 'Geek of the Week' and I quote him as follows: "For many people coming from procedural programming background the intuitive way to think of data processing is one record at a time, and in some order. The transition to set based thinking can take a long time."

This is the fundamental problem - every 'scripting' language put out there is almost by default going to be exciting mostly to the world of procedural developers, who I suppose keep looking for that silver bullett language that will at long last absolve them of that need to transition their thinking to set-based.

Since this will never happen (well, maybe I should not use the word never, but in the context of paradigm thinking I'm 99.9999% sure that procedural and set based thinking will remain worlds apart for some time), I do not think there will ever be a 'universal' language.

January 26, 2010 9:33 AM
 

mikedc said:

Hi Tony,

This doesn't seem to be that different from C# and linq. However C# is much more widely know and has a larger existing code base so would make a better choice imo. What do you think?
January 28, 2010 8:32 PM
 

Hello SMO (F#) World! | Sev17 said:

March 21, 2010 10:33 AM
You need to sign in to comment on this blog



















<January 2010>
SuMoTuWeThFrSa
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456
Raw Materials: Command-Line Nostalgia
 Arthur finds philosophy deep in a dialog box. Read more...

Increasing Email Size Limits for your High Profile Users in Exchange 2010
 If you ever need to set up fine-grained rules to control the maximum size of messages a subset of your... Read more...

Product Review: Schema Compare for Oracle
 One of the more important tasks in the process of rolling out incremental developments to a... Read more...

Implementing the OUTPUT Clause in SQL Server 2008
 In retrospect, it was probably the inclusion of the OUTPUT clause in the MERGE statement that gave... Read more...

SQL Source Control: The Development Story
 Often, there is a huge difference between software being easy to use, and easy to develop. When your... Read more...