[Update to doc HTML files. alistair@abayley.org**20070106230225] { hunk ./doc/html/Database-Enumerator.html 104 ->Bind Parameters +>Rank-2 types and ($) hunk ./doc/html/Database-Enumerator.html 109 +>Bind Parameters +
In some examples we use the application operator ($) instead of parentheses + (some might argue that his is a sign of developer laziness). + At first glance, ($) and normal function application seem to be interchangeable + e.g. +
liftIO (putStrLn (show x)) +
is equivalent to +
liftIO $ putStrLn $ show x +
But they're not, and the places where they differ usually involve + higher-rank types, like our DBM monad. + That's because ($) has type (a -> b) -> a -> b, which limits it + to rank-1 typed functions, a restriction which does not affect normal + function application. +
Here's an example where ($) fails: + we supply a simple test program in the README file. + If you change the withSession line to use ($), like so + (and remove the matching end-parenthese): +
withSession (connect "sqlite_db") $ do +
then you get the error: +
Main.hs:7:38: + Couldn't match expected type `forall mark. DBM mark Session a' + against inferred type `a1 b' + In the second argument of `($)', namely + ... +
Another way of rewriting it is like this, where we separate the + DBM action into another function: +
{-# OPTIONS -fglasgow-exts #-} + module Main where + import Database.Sqlite.Enumerator + import Database.Enumerator + import Control.Monad.Trans (liftIO) + main = flip catchDB reportRethrow $ + withSession (connect "sqlite_db") hello + + hello = withTransaction RepeatableRead $ do + let iter (s::String) (_::String) = result s + result <- doQuery (sql "select 'Hello world.'") iter "" + liftIO (putStrLn result) +
which gives this error: +
Main.hs:9:2: + Inferred type is less polymorphic than expected + Quantified type variable `mark' is mentioned in the environment: + hello :: DBM mark Session () (bound at Main.hs:15:0) + ... +
However, if you add this type declaration: +
hello :: DBM mark Session () +
then the compiler is happy, which shows that our rank-2 typed + DBM monad isn't entirely incompatible with ($). +