haskell - No instance for Show arising from a use in "main" level -
i have code reads files , parses using uu.parsing lib returns abstract sintax tree , shows on screen.
i received error message "no instance show" in functions originated in tokensparsertobytestring , applyparser using parseio (of uu.parsing lib) , inherited signatures until main. fixed signatures problem in main function. added instance show in signature have next compilation error:
no instance (show (io j2s)) arising use of ‘main’ in expression: main when checking type of io action ‘main’
the complete error message is:
$ cabal build building java2scala-1.0... preprocessing library java2scala-1.0... in-place registering java2scala-1.0... preprocessing executable 'java2scala' java2scala-1.0... preprocessing executable 'test' java2scala-1.0... [5 of 5] compiling main ( test/main.hs, dist/build/test/test-tmp/main.o ) test/main.hs:27:1: no instance (show (io j2s)) arising use of ‘main’ in expression: main when checking type of io action ‘main’
some idea, problem?
main module
{-# language flexiblecontexts #-} module main import uu.parsing ... import content main :: (show (io j2s)) => io() main = f <- getline let command = test f command test :: (show (io j2s)) => string -> io() test "testparser" = testparser
test module
{-# language flexiblecontexts #-} module j2s.parser.test 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 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 loop path = contents path () //> \newpath -> 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 loop = 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 = 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 -> let loop = eof <- tryio $ hiseof handle unless eof $ bs <- tryio $ b.hgetsome handle chunksize respond bs loop loop
a signature show (io j2s) => io ()
never makes sense. expresses “provided universe crafted such io j2s
has show
instance, give io ()
action”. well, if universe has property, give io ()
action right now. keep nasty chipsconstraints!
constraints make sense if apply them type variables, i.e. if you're writing code that's polymorphic on several different, not all types. (like checkp p
). constraint applied concrete types little more defer type errors.
io j2s
has no show
instance. , can't have such instance: io action. complete subprogram might execute costly computations, call commercial third-party library code, launch missiles... , in end return j2s
value. how expect pack information of possibly complex simple string?
what possibly have show
instance j2s
. if you're in io
monad anyway , have io j2s
action, can @ point fetch j2s
value monad-binding action (i.e. executing subprogram) , showing j2s
value. in case:
tokensparsertobytestring :: [token] -> io b.bytestring tokensparsertobytestring tokens = fmap (b8.pack . show) $ parseio pj2s tokens
i case you're confused fmapping in io functor, equivalent to
tokensparsertobytestring :: [token] -> io b.bytestring tokensparsertobytestring tokens = j2svalue <- parseio pj2s tokens return . b8.pack $ show j2svalue
of course need adapt applyparser
because tokensparsertobytestring
io
action. easy enough =<<
operator:
applyparser :: proxy p => string -> consumer p b.bytestring io () applyparser path = runidentityp loop loop = bs <- request () let sc = classify (initpos path) (b8.unpack bs) lift $ b8.putstrln =<< tokensparsertobytestring sc
Comments
Post a Comment