Monday, July 28, 2014

Composing queries in F#


In our ICFP 2013 paper last year, we introduced a formal model of Microsoft's "Language-Integrated Query", as implemented in F#.  The basic idea is to allow programmers to write queries in a way that more closely resembles ordinary code, can be typechecked alongside ordinary code, but still (hopefully) generates reasonable SQL queries.  For example, in F# one can write something like


query {
        for student in db.Student do
        where (student.Age.Value >= 10  
               && student.Age.Value < 15)
        select student
    }

to select all students with ages in the interval $[10,15)$.

Observe that the above query does not depend on any data in the F# host language.  There's a single SQL query that we can generate to answer this query on the database:

SELECT  student
FROM Student student
WHERE student.Age >= 10 AND student.Age < 15


(here, I'm glossing over differences in SQL syntax between Microsoft SQL server and other DBMSs).

Read more »

Labels: ,