compiling-to-categories / concat

Compiling to Categories
http://conal.net/papers/compiling-to-categories
BSD 3-Clause "New" or "Revised" License
431 stars 49 forks source link

New application: Haskell to SQL (f="SELECT * FROM table;" length<$>f="SELECT COUNT(*) FROM table;") #21

Open runeksvendsen opened 6 years ago

runeksvendsen commented 6 years ago

Let's say we have an SQL database containing a comments table.

In our Haskell code, we have a DB function fetchComments :: m [Comment] which, behind the scenes, runs an SQL query and returns a list of comments. In the code that consumes this DB function, sometimes we would like to just get the number of comments, so we would write length <$> fetchComments.

So, I've been thinking: is it possible to use the library in this repo to have fetchComments generate, by default, an SQL string that looks something like "SELECT FROM comments;", whereas, if length is applied to the return value of fetchComments in the code, the resulting SQL string would be "SELECT COUNT(*) FROM comments;*"?

So, in other words, rather than having the SQL server send all the comments (possibly thousands, or millions) over the write, the length transformation -- that would otherwise be applied in application code -- is applied by the SQL server, since length <$> fetchComments is translated into a different SQL query than just fetchComments alone.