[Haskell-beginners] No instance for Show arising from a use in “main” level

T. Andrea Moruno Rodriguez tatiana.moruno at gmail.com
Wed Aug 24 00:10:34 UTC 2016


I have a code that reads files and parses using UU.Parsing lib that returns
an abstract sintax tree and shows on the screen.

I received the error message "No instance for Show" in my functions
originated intokensParserToByteString and applyParser using parseIO (of
UU.Parsing lib) and inherited signatures until main. I fixed the signatures
but my problem is in the main function. I added the instance Show in the
signature but I have the next compilation error:


No instance for (Show (IO J2s)) arising from a use of ‘main’
In the expression: main
When checking the type of the IO action ‘main’

Some idea, about the problem?

Main module
-----------------------------------------------------------------------
{-# LANGUAGE FlexibleContexts #-}
module Main where

import UU.Parsing
...
import Content

main :: (Show (IO J2s)) => IO()
main = do f <- getLine
      let command = test f
      command

test :: (Show (IO J2s)) => String -> IO()
test "testparser" = testParser

Test module
-----------------------------------------------------------------------
{-# LANGUAGE FlexibleContexts #-}
module J2s.Parser.Test where

import Content
import J2s.Ast.Sintax
import J2s.Parser
import UU.Parsing
...

testParser :: (Show (IO J2s)) => IO()
testParser  = (runSafeIO $ runProxy $ runEitherK $
                contentsRecursive "path/of/my/tests" />/ handlerParser) ::
(Show (IO J2s)) => IO()

Content module
-----------------------------------------------------------------------
{-# LANGUAGE FlexibleContexts #-}
module Content where

import Control.Monad(forM, liftM)
import System.Directory (doesDirectoryExist, getDirectoryContents)
import System.FilePath ((</>), splitExtension, splitFileName)
import J2s.Parser
import J2s.Ast.Sintax
import UU.Parsing

import Control.Monad (when, unless)
import Control.Proxy
import Control.Proxy.Safe hiding (readFileS)

import J2s.Scanner.Token
import Text.Show

import UU.Parsing


contentsRecursive
     :: (CheckP p)
     => FilePath -> () -> Producer (ExceptionP p) FilePath SafeIO ()
contentsRecursive path () = loop path
   where
     loop path = do
         contents path () //> \newPath -> do
             respond newPath
             isDir <- tryIO $ doesDirectoryExist newPath
             let isChild = not $ takeFileName newPath `elem` [".", ".."]
             when (isDir && isChild) $ loop newPath


applyParser :: (Proxy p, Show (IO J2s)) => String -> Consumer p
B.ByteString IO ()
applyParser path = runIdentityP loop
  where
    loop = do
        bs <- request ()
        let sc = classify  (initPos path) (B8.unpack bs)
        lift $  B8.putStrLn (tokensParserToByteString sc)

tokensParserToByteString :: (Show (IO J2s)) =>  [Token] -> B.ByteString
tokensParserToByteString tokens = B8.pack(show (parseIO pJ2s tokens))


handlerParser :: (CheckP p, Show (IO J2s)) => FilePath -> Session
(ExceptionP p) SafeIO ()
handlerParser path = do
    canRead <- tryIO $ fmap readable $ getPermissions path
    isDir   <- tryIO $ doesDirectoryExist path
    isValidExtension <- tryIO $ evaluate ((snd (splitExtension path) ==
".java" || snd (splitExtension path) == ".mora") && (snd (splitFileName
path) /= "EncodeTest.java") && (snd (splitFileName path) /=
"T6302184.java") && (snd (splitFileName path) /= "Unmappable.java"))
    when (not isDir && canRead && isValidExtension) $
        (readFileSP 10240 path >-> try . applyParser) path


readFileSP
:: (CheckP p)
=> Int -> FilePath -> () -> Producer (ExceptionP p) B.ByteString SafeIO ()
readFileSP chunkSize path () =
    bracket id (openFile path ReadMode) hClose $ \handle -> do
        let loop = do
                eof <- tryIO $ hIsEOF handle
                unless eof $ do
                    bs <- tryIO $ B.hGetSome handle chunkSize
                    respond bs
                    loop
        loop



--
Tatiana Andrea Moruno Rodriguez
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160823/ababcc45/attachment.html>


More information about the Beginners mailing list