Note that there are some explanatory texts on larger screens.

plurals
  1. POHaskell readFile type problem
    text
    copied!<p>I have some troubles with Haskell type system.</p> <p>Situation:</p> <ul> <li>Following program is taking list of filenames on the cmdline</li> <li>For all filename it reads its content using the function readFile</li> <li>Content of each file is passed to inputParser (Parsec)</li> <li>Rest is not so important</li> <li>Main problem is in function read_modules</li> <li>First two statements of the 'do' expression are invalid in Haskell type system</li> <li>Problem is conflict between [String] x IO String x [Char] x ...</li> <li>Function 'parse' should take String but when it gets it, it wants IO String suddenly (on the same argument), otherwise it wants String</li> </ul> <p>What do I want:</p> <ol> <li>Read each file content</li> <li>Pass that content to the 'parse' function as third argument</li> </ol> <p>Here is the code.</p> <pre><code>module Main where import System.IO import System.Environment import Text.ParserCombinators.Parsec import InputParser import Data usage :: IO () usage = putStrLn "Usage: x file file file option" parse_modules :: String -&gt; [Char] -&gt; Either ParseError [Module] parse_modules filename input = parse inputParser filename input read_modules :: [String] -&gt; [Module] read_modules [] = []::[Module] read_modules (filename:rest) = do content &lt;- readFile filename -- HERE is the problem modules &lt;- case parse_modules filename content of -- HERE is problem too Left error -&gt; do putStr "parse error at " print error Right out -&gt; out ++ (read_modules rest) return modules use :: [String] -&gt; IO () use args = do init &lt;- last args filenames &lt;- take (length args - 1) args modules &lt;- read_modules filenames return () main :: IO () main = do args &lt;- getArgs if length args &lt; 2 then usage else use args </code></pre> <p>Here are the GHC output errors</p> <pre><code>ghc --make -o x.hs input-parser.hs data.hs [3 of 3] Compiling Main ( x.hs, x.o ) x.hs:19:4: Couldn't match expected type `IO String' against inferred type `[String]' In a stmt of a 'do' expression: content &lt;- readFile filename In the expression: do content &lt;- readFile filename modules &lt;- case parse_modules filename content of { Left error -&gt; do ... Right out -&gt; out ++ (read_modules rest) } return modules In the definition of `read_modules': read_modules (filename : rest) = do content &lt;- readFile filename modules &lt;- case parse_modules filename content of { Left error -&gt; ... Right out -&gt; out ++ (read_modules rest) } return modules -- THIS ERROR is somewhat not important x.hs:30:4: Couldn't match expected type `[Char]' against inferred type `IO Char' Expected type: String Inferred type: IO Char In a stmt of a 'do' expression: init &lt;- last args In the expression: do init &lt;- last args filenames &lt;- take (length args - 1) args modules &lt;- read_modules filenames return () make: *** [x] Error 1 </code></pre> <p>What is the problem:</p> <ul> <li>I cannot understand what I should pass where. I kind of know what I want, but I don't get the syntax or the style.</li> <li>I am haskell greenie (not solving here)</li> <li>TYPES</li> </ul> <p>What are the questions:</p> <ul> <li>How do I fix presented type issue? What should I put into 'parse' function? What 'readFile' gives to me? Are those two types compatible? Isn't there need for some type of conversion?</li> </ul> <p>Relevant weblinks:</p> <ul> <li><a href="http://book.realworldhaskell.org/read/using-parsec.html" rel="nofollow noreferrer">http://book.realworldhaskell.org/read/using-parsec.html</a></li> <li><a href="http://www.zvon.org/other/haskell/Outputprelude/readFile_f.html" rel="nofollow noreferrer">http://www.zvon.org/other/haskell/Outputprelude/readFile_f.html</a></li> <li><a href="http://www.haskell.org/pipermail/haskell/2002-November/010748.html" rel="nofollow noreferrer">http://www.haskell.org/pipermail/haskell/2002-November/010748.html</a></li> </ul> <p>Thank you all for your hints and comments.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload