On Sun, Dec 15, 2013 at 3:32 PM, Nikita Karetnikov <nikita@karetnikov.org> wrote:
  let n = c_strcmp s' t'
  -- free s'
  -- free t'
  return $ case () of
    _ | n == 0    -> EQ
      | n <  0    -> LT
      | otherwise -> GT

Two questions:

1. May I safely use unsafePerformIO in such cases?

Yes, although you might prefer to use the variant specified by the FFI standard, unsafeLocalState.
 
2. What’s the proper way of using free here?  If I uncomment the above,
   the function returns incorrect results.

Note that you have not forced evaluation of `n` when you free the CString-s, so `c_strcmp` has not necessarily been called yet. Control.Exception.evaluate may be of use here.

--
brandon s allbery kf8nh                               sine nomine associates
allbery.b@gmail.com                                  ballbery@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net