Haskell: abort writeFile on error - haskell

I have a program that generates JSON and prints it to file. The function that generates the JSON can fail midway through the code generation.
At the moment when there is a failure in the JSON generation, the code that is generated up to that point still gets written to a file, giving me an incomplete/incorrect output file.
I am trying to figure out how to prevent the bad output from being written. Something like:
main = do
let
output = encodingfunction
print' (Just x) = writeFile "outputfile" output
print' _ = putStrLn "Encoding Failed Miserably"
print' ouput
return ()
encodingfunction :: Maybe String

You might wanna force the evaluation of the returned string so that the encoding function is for sure done before you start writing to the file. Mabye you could check its length before printing it.

Related

Convert data read from a file to a variable in Haskell

I have a problem - I am writing in a file in Haskell (I wanna write a sentence in the file and everytime I write in it I want to overwrite the content of the file so this func does the work for me completely fine)
writeFunc message = writeFile file message where
file = "abc.txt"
And then reading from the same file
readFunc = do
let file = "abc.txt"
contents <- readFile file
return contents
And then I wanna save the things I have read in a variable:
In the terminal doing this
let textFromAFile = readFunc
results into this:
*Main> let textFromAFile = readFunc
*Main> textFromAFile
"okay"
But when I use let textFromAFile = readFunc inside my code, the code wont compile
[1 of 1] Compiling Main ( tree.hs, interpreted )
tree.hs:109:29: error:
parse error (possibly incorrect indentation or mismatched brackets)
Failed, modules loaded: none.
I wanna save it in a variable so I can use it later in other functions. Why it works in the terminal but wont compile and what I can do to make it work? ReadFunc returns IO String and is there a possible way to convert it to s String so I can use it in a pure func?
readFunc has type IO String, you can use it in another IO expression with:
someIO = do
textFromAFile <- readFunc
-- use textFromFile (String) …
-- …
for example:
someIO = do
textFromAFile <- readFunc
writeFunc (textFromAFile ++ "/")
The reason it works in the GHCi terminal is that the terminal evaluates IO a objects, so while textFromAFile is an IO String, and the terminal will thus evaluate textFromAFile.

Haskell: Why does this function keep asking for user input and not terminating

I'm learning some Haskell and I came across this small program
reverseLines :: String -> String
reverseLines input =
unlines (map reverse (lines input))
main :: IO ()
main = interact reverseLines
This program will keep asking the user for more input and reverse the input and print it on the screen.
Most of this is straight forward but one thing I can't wrap my head around is why does this function keeps running and ask the user for more input whereas if I just replace the reverseLines function with a function the simply returns some string it will not happen.
This program will stop after one execution:
foo input = "Stops"
main :: IO ()
main = interact foo
Why?
If you look at the source of interact you see this:
interact f = do s <- getContents
putStr (f s)
see the getContents? This is where the magic starts - it will read everything till EOF
Now in Haskell this is lazy-IO which can be bad but here is almost magical - see the string is read lazily and passed to your reverseLines - this one of course will only generate output as soon as it saw \n characters (the lines) and so it seems your program is some kind of REPL.
In the second one you don't consume any of the lazy-string at all so it stops ASAP
As I wrote in the comments you can play with this by either passing content into the program using a file (or echo) and pipes on the terminal:
echo "Hello World\nBye Bye" | runhaskell LazyIO.hs
or using CTRL-D to pass in the EOF yourself.
To get a feeling for it I would play with the functions more - what happens if you use something that needs to see the complete input first (try reverse without the maps)? What happens with words instead of lines, ...?
Have fun!

Haskell write a list in file and read later

I am trying to write a list into a file and later on I want to read the file contents into the list as well.
So I have a list like this ["ABC","DEF"]
I have tried things like
hPrint fileHandle listName
This just prints into file "["ABC","DEF"]"
I have tried unlines but that is priniting like "ABC\nDEF\n"
Now in both the cases, I cant read back into proper list. The output file has quotes and because of which when I read, I get like this ["["ABC","DEF"]""] i.e a single string in list.
As I am not succeeding in this, I tried to write the list line by line, I tried to apply a map and the function to write the list k = map (\x -> hPrint fileSLC x) fieldsBefore, it is not doing anything, file is blank. I think if I write everything in separate line, I will be able to read like (lines src) later on.
I know whatever I am doing is wrong but I am writing the code on Haskell for second time only, last time I just a wrote a very a small file reading program. Moving from imperative to functional is not that easy. :(
Try using hPutStrLn and unlines instead of hPrint. The hPrint internally calls show which causes Strings to be quoted and escaped.
hPutStr fileHandle (unlines listName)
Alternatively, use a mapM or a forM. A verbose example is:
forM_ listName $ \string ->
hPutStrLn string
This can be simplified ("eta-contracted", in lambda-calculus terminology) to
forM_ listName hPutStrLn
As you have seen, when you read from a file, you get a String. In order to convert this String into a list, you will need to parse it.
For k = map (\x -> hPrint fileSLC x) fieldsBefore to work, you need to use mapM or mapM_ instead of map.

How do I improve QuickCheck and Parsec debugging?

I am using Haskell and Parsec to parse a file format. My parsing function looks something like:
parseInput :: String -> Model
parseInput input = ...
data Model = Model { mNumV :: Int, mNumF :: Int, ... }
In order to test this, I am using QuickCheck. I have defined an Arbitrary instance that generates a String representing the contents of a formatted file:
instance Arbitrary File where
arbitrary = ...
data File = File { fContents :: String, fNumV :: Int, fNumF :: Int, ... }
One of my properties might check to determine if mNumV == fNumV after parsing the arbitrary String. This works well - when it works.
But if something fails, Parsec throws an error similar to:
*** Failed (after 1 test):
Exception:
(line 302, column 3):
unexpected "\n"
expecting space
This is useful - however, after the test fails the contents of the arbitrary file disappear. I can't go in and reference line 302.
The only alternative that I can see is to print the fContents of each arbitrary file after each test - but that seems like a terrible idea. The same goes for routing every arbitrary file to a file on disk for later reference.
Is there a common way around this?
You can use whenFail to print the offending string (or dump it to a file) upon failure.

How can I make file I/O more transactional?

I'm writing CGI scripts in Haskell. When the user hits ‘submit’, a Haskell program runs on the server, updating (i.e. reading in, processing, overwriting) a status file. Reading then overwriting sometimes causes issues with lazy IO, as we may be able to generate a large output prefix before we've finished reading the input. Worse, users sometimes bounce on the submit button and two instances of the process run concurrently, fighting over the same file!
What's a good way to implement
transactionalUpdate :: FilePath -> (String -> String) -> IO ()
where the function (‘update’) computes the new file contents from the old file contents? It is not safe to presume that ‘update’ is strict, but it may be presumed that it is total (robustness to partial update functions is a bonus). Transactions may be attempted concurrently, but no transaction should be able to update if the file has been written by anyone else since it was read. It's ok for a transaction to abort in case of competition for file access. We may assume a source of systemwide-unique temporary filenames.
My current attempt writes to a temporary file, then uses a system copy command to overwrite. That seems to deal with the lazy IO problems, but it doesn't strike me as safe from races. Is there a tried and tested formula that we could just bottle?
The most idiomatic unixy way to do this is with flock:
http://hackage.haskell.org/package/flock
http://swoolley.org/man.cgi/2/flock
Here is a rough first cut that relies on the atomicity of the underlying mkdir. It seems to fulfill the specification, but I'm not sure how robust or fast it is:
import Control.DeepSeq
import Control.Exception
import System.Directory
import System.IO
transactionalUpdate :: FilePath -> (String -> String) -> IO ()
transactionalUpdate file upd = bracket acquire release update
where
acquire = do
let lockName = file ++ ".lock"
createDirectory lockName
return lockName
release = removeDirectory
update _ = nonTransactionalUpdate file upd
nonTransactionalUpdate :: FilePath -> (String -> String) -> IO ()
nonTransactionalUpdate file upd = do
h <- openFile file ReadMode
s <- upd `fmap` hGetContents h
s `deepseq` hClose h
h <- openFile file WriteMode
hPutStr h s
hClose h
I tested this by adding the following main and throwing a threadDelay in the middle of nonTransactionalUpdate:
main = do
[n] <- getArgs
transactionalUpdate "foo.txt" ((show n ++ "\n") ++)
putStrLn $ "successfully updated " ++ show n
Then I compiled and ran a bunch of instances with this script:
#!/bin/bash
rm foo.txt
touch foo.txt
for i in {1..50}
do
./SO $i &
done
A process that printed a successful update message if and only if the corresponding number was in foo.txt; all the others printed the expected SO: foo.txt.notveryunique: createDirectory: already exists (File exists).
Update: You actually do not want to use unique names here; it must be a consistent name across the competing processes. I've updated the code accordingly.

Resources