haskell - Conduit simple file read expression not typechecking -
i'm trying simple conduit example under belt, i'm failing @ type-checking phase. in this example, i'm seeing resourcet
being used conduit "executor", if makes sense. know that, time, resourcet
has been factored-out it's own package. however, still can't simple example working. here code, inspired this article:
import qualified data.conduit c import qualified data.conduit.binary cb import qualified data.conduit.list cl import control.monad.trans.resource (runresourcet) import data.bytestring.char8 (unpack) import data.bytestring (bytestring (..)) import system.io printfile file = runresourcet $ cb.sourcefile file c.$$ print' print' :: c.sink bytestring io () print' = cl.mapm_ $ putstrln · unpack
and here error i'm getting (my package called "conduit-playground":
preprocessing library conduit-playground-0.0.0... [2 of 2] compiling playground ( src/playground.hs, dist/dist-sandbox-13da96d6/build/playground.o ) src/playground.hs:12:57: couldn't match type ‘io’ ‘control.monad.trans.resource.internal.resourcet m’ expected type: c.sink bytestring (control.monad.trans.resource.internal.resourcet m) () actual type: c.sink bytestring io () relevant bindings include printfile :: filepath -> m () (bound @ src/playground.hs:12:1) in second argument of ‘(c.$$)’, namely ‘print'’ in second argument of ‘($)’, namely ‘cb.sourcefile file c.$$ print'’ cabal: error: packages failed install: conduit-playground-0.0.0 failed during building phase. exception was: exitfailure 1
if leave out runresourcet
, i'm still getting typeclass error there not being instance of monadresource
io
. how should tackle problem?
the problem print'
has incompatible type
print' :: c.sink bytestring io ()
you can't have io
base monad of conduit if using resourcet
(which need in case reading file). simplest monad lets both print , handle file resources resourcet io
i.e. io
monad transformed resourcet
transformer.
print' :: c.sink bytestring (resourcet io) () print' = cl.mapm_ $ liftio . putstrln · unpack
the liftio
needed "lift" expression of type io ()
resourcet io ()
. use more generic signature
print' :: monadio m => c.sink bytestring m ()
to make utility function works conduit able perform io.
monadio
, liftio
can imported module control.monad.io.class
.
Comments
Post a Comment