1

I have the following code which does the job of changing the configuration file:

mutateConfig :: (Config -> Config) -> IO ()
mutateConfig f = do
    !cfg <- readConfig
    let !newCfg = f cfg
     in writeConfig newCfg

Bang patterns are used to disable lazy evaluation: i need writeConfig to be called after an old configuration was parsed and modified with function f.

But there is still a problem: imagine that f gives an error. In this case writeConfig has already opened the file for writing (because it is called first by lazy evaluation) when an error occures, so the configuration file gets lost.

I have tried to compose a simple step-by-step logic from IO monads (in order to prevent the loss of data) like this

!cfg <- readConfig
newCfg <- return $ f cfg
writeConfig newCfg

but this doesn't work either (I kind of expected that).

What would be the correct way to accomplish this goal?

4
  • Laziness doesn't explain any problems you may be having, unless readConfig uses lazy IO. If it does, I'd argue that's a bug that should be fixed.
    – Carl
    Commented Jul 18, 2015 at 20:28
  • @Carl readConfig uses the readFile function, which is (I believe) lazy. So yes, you are right! Which function should I use to read configurations strictly? Commented Jul 18, 2015 at 20:30
  • 1
    Are you sure Config is fully strict? If it was any errors in f would be caught before attempting to write the file because of the bang pattern on newCfg, even if the file was read lazily (I just tested this). You should still make sure the file is read strictly because it could cause problems when trying to write back to the config file. You can do this with the strict package. (but melphomene's temporary and rename is probably a better solution)
    – cchalmers
    Commented Jul 18, 2015 at 21:42
  • @cchalmers I missed a bang pattern in one of the fields of Config. I've just fixed this and mutateConfig is working just I expect it to. Thank you very much for your assistance! Commented Jul 18, 2015 at 23:12

1 Answer 1

4

If Config is completely strict (no embedded bottoms within), you can do this:

do
    cfg <- readConfig
    let newCfg = f cfg
    evaluate newCfg
    writeConfig newCfg

If it's not, you could do something like evaluate (deepseqnewCfg ()).

But there's another problem: In general, writing a file can fail due to I/O errors (for example because the disk is full). To avoid this, you can write to a temporary file and rename it to the real target at the end. Doing it this way guarantees you only overwrite the config if there were no errors during the writing.

7
  • Hi! This still doesn't work! Btw I don't need deepseq here since Config is indeed strict. Commented Jul 18, 2015 at 19:09
  • What do you mean by "this" and what do you mean by "doesn't work"?
    – melpomene
    Commented Jul 18, 2015 at 21:25
  • This - what you suggested. Doesn't work - still changes the file (and I am left with an empty file after the error is displayed). Commented Jul 18, 2015 at 22:53
  • Anyways, I just found out that Config was not completely strict (due to a bug). See my comment above. Thanks! Commented Jul 18, 2015 at 23:13
  • 1
    a better way to write evaluate (deepseq newCfg ()) is evaluate (force newCfg)
    – mniip
    Commented Jul 19, 2015 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.