Is tail-recursive IO can always be optimized?

8 views
Skip to first unread message

Eugene Perederey

unread,
Nov 19, 2011, 5:01:28 AM11/19/11
to Stanford CS240h 2011 Autumn
I have nested IO actions like

runServer :: IO ()
runServer = do
...
procRequests sock state

procRequests :: Socket -> State -> IO ()
procRequests sock state = do
(conn, addr) <- accept sock
newState <- updateState conn state
procRequests sock newState

where updateState probably reads/writes data to conn and returns the
updated state,
and it is tail recursive.
I found that depending on updateState I get either normal execution or
stack overflow.
I wonder if I can rely on tail IO calls being optimized and shall I
need any special directive other than -O.

David Terei

unread,
Nov 20, 2011, 6:17:16 PM11/20/11
to stanford-1...@googlegroups.com
So in general I would make sure you understand why you are getting a
stack overflow and try to fix it if simple even for -O0. Sadly its
pretty easy to cause stack overflows at -O0 but its not a good idea to
rely on -O1 or higher for correct behavior. That's my personal opinion
anyway. You are pretty safe relying on GHC picking these things up
with -O1.

Eugene Perederey

unread,
Nov 20, 2011, 6:31:02 PM11/20/11
to Stanford CS240h 2011 Autumn
Thanks David, I guess that stack overflows were due to my
misunderstanding of Attoparsec.
Inside updateState a parser was called. It works fine without the
parsing step -- I still need to work on this part.

On Nov 20, 3:17 pm, David Terei <davidte...@gmail.com> wrote:
> So in general I would make sure you understand why you are getting a
> stack overflow and try to fix it if simple even for -O0. Sadly its
> pretty easy to cause stack overflows at -O0 but its not a good idea to
> rely on -O1 or higher for correct behavior. That's my personal opinion
> anyway. You are pretty safe relying on GHC picking these things up
> with -O1.
>

Reply all
Reply to author
Forward
0 new messages