[Takusen] Nested Iterations with Takusen

Jason Dagit dagitj at gmail.com
Tue Apr 19 15:16:57 BST 2011


On Tue, Apr 19, 2011 at 6:52 AM, Pepe Barbe <elventear at gmail.com> wrote:

>
> On Apr 18, 2011, at 11:08 PM, Jason Dagit wrote:
>
> > I think what GHC is trying to say is that you've declared something to
> work
> > for all Monads m, but you're also requiring it to work for IO.  In
> > particular, using withSession inside secondLevelQuery forces
> > secondLevelQuery to use the IO monad.
> >
> > So then in firstLevelQuery, this line:
> > count' <- secondLevelQuery
> >
> > Forces iter to only work for the IO monad.  GHC is complaining because
> iter
> > was declared to work for all monads but the way it's implemented forces
> it
> > to only work for the IO monad.
> >
> > If you change it to:
> > iter :: Integer -> IterAct IO (Integer, Integer)
> >
> > Does your type error go away?
>
> It doesn't work, I get the following error:
>
> sample.hs:27:27:
>    No instance for (Database.Enumerator.QueryIteratee
>                       (DBM mark Session)
>                       Database.PostgreSQL.Enumerator.Query
>                       (IO (IterResult (Integer, Integer)))
>                       (Integer, Integer)
>                       Database.PostgreSQL.Enumerator.ColumnBuffer)
>      arising from a use of `doQuery'
>    Possible fix:
>      add an instance declaration for
>      (Database.Enumerator.QueryIteratee
>         (DBM mark Session)
>         Database.PostgreSQL.Enumerator.Query
>         (IO (IterResult (Integer, Integer)))
>         (Integer, Integer)
>         Database.PostgreSQL.Enumerator.ColumnBuffer)
>    In the second argument of `withSession', namely
>      `(doQuery query iter (0, 0))'
>    In the expression:
>       withSession connection (doQuery query iter (0, 0))
>     In the expression:
>       do { let query = sql "SELECT count(*) from alarms"
>                iter :: Integer -> IterAct IO (Integer, Integer)
>                ....;
>            withSession connection (doQuery query iter (0, 0)) }
>
> I am already using an alternative using mapM and two functions similar to
> the firstLevel and secondLevel that are not nested. This question is more
> about the fundamentals and because am new to Haskell and Takusen.
>

Oh, I see. I focused too much on your error message and not enough on your
actual question.  I'm not sure what you mean by nested query, but I'll try
to give you some more ideas.


>
> I thought doing these nested queries would be an ok idea, but it doesn't
> seem to be so easy. So another question I have is if what I was proposing
> initially, doing nested queries, was a good idea or maybe there is a better
> way of doing what I intended.
>
>
Is this a case where you want to use withContinuedSession?
http://hackage.haskell.org/packages/archive/Takusen/0.8.7/doc/html/Database-Enumerator.html#g:7

It has some caveats and I think typically you want to avoid it in favor of
other things because you need to be careful not to use a closed session with
it.

Depending on what you're trying to accomplish, maybe you shouldn't make the
call to withSession on each query.  See the example main function here:
http://hackage.haskell.org/packages/archive/Takusen/0.8.7/doc/html/Database-Enumerator.html#g:1

Basically, when you want to do multiple queries you should work inside the
DBM monad and then have one outer withSession that executes all of the DBM
actions in the same session.

I hope that helps,
Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://projects.haskell.org/pipermail/takusen/attachments/20110419/2952cf44/attachment.htm>


More information about the Takusen mailing list