<br><font size=2 face="sans-serif">Thanks Martin, that really helped.</font>
<br>
<br><font size=2 face="sans-serif">After many days reading the source,
I'm still trying to grok HAppS.</font>
<br>
<br><font size=2 face="sans-serif">Meanwhile, here is a patch that adds
examples to HAppS/Examples/HTTP1.hs for combining state and io, based on
your advice.</font>
<br>
<br><font size=2 face="sans-serif">see especiallly the handler stateioH
accepts an arbitrary state action, arbitrary io action, and a function
for combining the two.</font>
<br>
<br><font size=2 face="sans-serif">Thomas.</font>
<br>
<br>
<br><font size=2 face="Courier New">New patches:</font>
<br>
<br><font size=2 face="Courier New">[add examples showing state and io</font>
<br><font size=2 face="Courier New">thomas.hartman@db.com**20070906215844]
{</font>
<br><font size=2 face="Courier New">hunk ./src/HAppS/Examples/HTTP1.hs
7</font>
<br><font size=2 face="Courier New">+import System.Directory</font>
<br><font size=2 face="Courier New">hunk ./src/HAppS/Examples/HTTP1.hs
68</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+
,h ["showbashrc"] GET $ ioshowH readbashrc</font>
<br><font size=2 face="Courier New">+
,h ["showhttp1"] GET $ ioshowH readhttp1</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+
,h ["incrementstate"] GET $ stateH $ incnshow</font>
<br><font size=2 face="Courier New">+
,h ["decrementstate"] GET $ stateH $ decnshow</font>
<br><font size=2 face="Courier New">+
,h ["viewstate"] GET $ stateH view</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+
,h ["showbashrc_and_inc"] GET $ stateioH incnshow readbashrc
append_state</font>
<br><font size=2 face="Courier New">+
,h ["showbashrc_and_dec"] GET $ stateioH decnshow readbashrc
append_state</font>
<br><font size=2 face="Courier New">+
,h ["showbashrc_and_view"] GET $ stateioH decnshow readbashrc
append_state</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">hunk ./src/HAppS/Examples/HTTP1.hs
82</font>
<br><font size=2 face="Courier New">+append_state s io = io ++ "<BR>State:
" ++ s</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+incnshow = modify (+1) >> get
>>=^ show</font>
<br><font size=2 face="Courier New">+decnshow = modify (\x -> x-1) >>
get >>=^ show </font>
<br><font size=2 face="Courier New">+view = get >>=^ show</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+readbashrc = do</font>
<br><font size=2 face="Courier New">+ home <- getHomeDirectory</font>
<br><font size=2 face="Courier New">+ readfileSafe $ home ++ "/.bashrc"</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+readhttp1 = readfileSafe "./HTTP1.hs"</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+readfileSafe file = catch</font>
<br><font size=2 face="Courier New">+
( readFile file >>=^ format_html ) </font>
<br><font size=2 face="Courier New">+
( \e -> return ( show e ) )</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+stateioH stateaction ioaction combinestio
= \() () -> do</font>
<br><font size=2 face="Courier New">+ stateresult <- stateaction</font>
<br><font size=2 face="Courier New">+ respond $ do </font>
<br><font size=2 face="Courier New">+ ioresult <-
ioaction</font>
<br><font size=2 face="Courier New">+ showresult (combinestio
stateresult ioresult) </font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+stateH stateaction = ok $ \() () ->
stateaction >>= respond </font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+ioshowH :: (Monad m, Show a) =>
IO a -> () -> () -> m (Either Request (IO Result))</font>
<br><font size=2 face="Courier New">+ioshowH ioaction = \() () -> respond
$ do</font>
<br><font size=2 face="Courier New">+ ioresult <- ioaction</font>
<br><font size=2 face="Courier New">+ showresult ioresult </font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+showresult showable = sresult 200
(show showable) </font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">hunk ./src/HAppS/Examples/HTTP1.hs
151</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+format_html xs = concat $ map newlinetobr
xs</font>
<br><font size=2 face="Courier New">+ where newlinetobr '\n'
= "<br>"</font>
<br><font size=2 face="Courier New">+ newlinetobr
x = [x]</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+f >>=^ g = f >>= return
. g</font>
<br><font size=2 face="Courier New">}</font>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<table width=100%>
<tr valign=top>
<td width=40%><font size=1 face="sans-serif"><b>Martin Lütke <prionic@gmx.de></b>
</font>
<br><font size=1 face="sans-serif">Sent by: haskell-cafe-bounces@haskell.org</font>
<p><font size=1 face="sans-serif">09/01/2007 07:04 PM</font>
<td width=59%>
<table width=100%>
<tr valign=top>
<td>
<div align=right><font size=1 face="sans-serif">To</font></div>
<td><font size=1 face="sans-serif">haskell-cafe@haskell.org</font>
<tr valign=top>
<td>
<div align=right><font size=1 face="sans-serif">cc</font></div>
<td>
<tr valign=top>
<td>
<div align=right><font size=1 face="sans-serif">Subject</font></div>
<td><font size=1 face="sans-serif">[Haskell-cafe] Re: wanted: HAppS example
combining state and io</font></table>
<br>
<table>
<tr valign=top>
<td>
<td></table>
<br></table>
<br>
<br>
<br><tt><font size=2>Thomas Hartman <thomas.hartman <at> db.com>
writes:<br>
<br>
> <br>
> <br>
> In the latest happs (darcs pulled, updated<br>
> head is 0.9.1 iirc), I am experimenting with the example file in<br>
src/HAppS/Examples/HTTP1.hs.<br>
> I would like to combine state with io.<br>
> Eventually io will mean stuff like reading from a database, but for
now<br>
> I'm just reading a file.<br>
> The example file HTTP1.hs has an example<br>
> that demonstrates state with macid.<br>
> I added an example that allows you to<br>
> execute arbitrary io.<br>
> I tried, but was unable to, add a handler<br>
> that combines state and io. <br>
> <br>
> , h ["iohandler"] GET $
ioReadFileHandler<br>
> <br>
> , h ["statehandler"] GET $ stateHandler<br>
> <br>
> --, h ["ioandstatehandler"] GET $ ioAndStateHandler<br>
> .....<br>
> -- displays contents of HAPPS.hs in<br>
> current directory<br>
> ioReadFileHandler = iohandler $ readFile<br>
> "./HAppS.hs" <br>
> -- displays incremented state counter<br>
> stateHandler = ok $ \() () -> <br>
> <br>
> modify (+(1::Int)) >> get
>>=<br>
> respond . show<br>
> -- should combine effect of iohandler<br>
> with statehandler<br>
> -- specifically, should display contents<br>
> of HAppS.hs, and under that an incremented state handler<br>
> -- is this possible<br>
> ioAndStateHandler = undefined undefined<br>
> Is mixing state and io possible with<br>
> HAppS? If so, an example showing how to do it would be extremely helpful.<br>
> best, thomas.<br>
> ---This e-mail may contain confidential and/or privileged information.
If you<br>
are not the intended recipient (or have received this e-mail in error)
please<br>
notify the sender immediately and destroy this e-mail. Any unauthorized
copying,<br>
disclosure or distribution of the material in this e-mail is strictly forbidden.<br>
> <br>
> Attachment (https1-whatsnew): application/octet-stream, 1933 bytes<br>
> Attachment (HTTP1.hs): application/octet-stream, 4794 bytes<br>
> <br>
> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> Haskell-Cafe <at> haskell.org<br>
> http://www.haskell.org/mailman/listinfo/haskell-cafe<br>
> <br>
<br>
<br>
I had no trouble getting this handler to work:<br>
<br>
h ["iohandler"] GET $ \() () -> do<br>
modify (+1)<br>
x <- get<br>
respond $ do<br>
cnts <- readFile
"./sometext.txt"<br>
sresult 200 (cnts
++ show x)<br>
<br>
I believe the trick is that you cant mix io INTO the HAppS ServerPart monad.
<br>
But from the ServerPart monad you can RETURN an io action.<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
Haskell-Cafe@haskell.org<br>
http://www.haskell.org/mailman/listinfo/haskell-cafe<br>
</font></tt>
<br>
<br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">---</span><br>
<br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">This e-mail may contain confidential and/or privileged information. If you </span><br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">are not the intended recipient (or have received this e-mail in error) </span><br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">please notify the sender immediately and destroy this e-mail. Any </span><br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">unauthorized copying, disclosure or distribution of the material in this </span><br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">e-mail is strictly forbidden.</span><br>