<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">+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; ,h [&quot;showbashrc&quot;] GET $ ioshowH readbashrc</font>
<br><font size=2 face="Courier New">+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; ,h [&quot;showhttp1&quot;] GET $ ioshowH readhttp1</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; ,h [&quot;incrementstate&quot;] GET $ stateH $ incnshow</font>
<br><font size=2 face="Courier New">+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; ,h [&quot;decrementstate&quot;] GET $ stateH $ decnshow</font>
<br><font size=2 face="Courier New">+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; ,h [&quot;viewstate&quot;] GET $ stateH view</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; ,h [&quot;showbashrc_and_inc&quot;] GET $ stateioH incnshow readbashrc
append_state</font>
<br><font size=2 face="Courier New">+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; ,h [&quot;showbashrc_and_dec&quot;] GET $ stateioH decnshow readbashrc
append_state</font>
<br><font size=2 face="Courier New">+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; ,h [&quot;showbashrc_and_view&quot;] 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 ++ &quot;&lt;BR&gt;State:
&quot; ++ s</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+incnshow = modify (+1) &gt;&gt; get
&gt;&gt;=^ show</font>
<br><font size=2 face="Courier New">+decnshow = modify (\x -&gt; x-1) &gt;&gt;
get &gt;&gt;=^ show &nbsp;</font>
<br><font size=2 face="Courier New">+view = get &gt;&gt;=^ 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">+ &nbsp;home &lt;- getHomeDirectory</font>
<br><font size=2 face="Courier New">+ &nbsp;readfileSafe $ home ++ &quot;/.bashrc&quot;</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+readhttp1 = readfileSafe &quot;./HTTP1.hs&quot;</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">+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; ( readFile file &gt;&gt;=^ format_html ) </font>
<br><font size=2 face="Courier New">+ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; ( \e -&gt; return ( show e ) )</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+stateioH stateaction ioaction combinestio
= \() () -&gt; do</font>
<br><font size=2 face="Courier New">+ &nbsp; stateresult &lt;- stateaction</font>
<br><font size=2 face="Courier New">+ &nbsp; respond $ do </font>
<br><font size=2 face="Courier New">+ &nbsp; &nbsp; &nbsp;ioresult &lt;-
ioaction</font>
<br><font size=2 face="Courier New">+ &nbsp; &nbsp; &nbsp;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 $ \() () -&gt;
stateaction &gt;&gt;= respond </font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+ioshowH :: (Monad m, Show a) =&gt;
IO a -&gt; () -&gt; () -&gt; m (Either Request (IO Result))</font>
<br><font size=2 face="Courier New">+ioshowH ioaction = \() () -&gt; respond
$ do</font>
<br><font size=2 face="Courier New">+ &nbsp; ioresult &lt;- ioaction</font>
<br><font size=2 face="Courier New">+ &nbsp; 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">+ &nbsp;where &nbsp;newlinetobr '\n'
= &quot;&lt;br&gt;&quot;</font>
<br><font size=2 face="Courier New">+ &nbsp; &nbsp; &nbsp; &nbsp; newlinetobr
x = [x]</font>
<br><font size=2 face="Courier New">+</font>
<br><font size=2 face="Courier New">+f &gt;&gt;=^ g = f &gt;&gt;= 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 &lt;prionic@gmx.de&gt;</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 &lt;thomas.hartman &lt;at&gt; db.com&gt;
writes:<br>
<br>
&gt; <br>
&gt; <br>
&gt; In the latest happs (darcs pulled, updated<br>
&gt; head is 0.9.1 iirc), I am experimenting with the example file in<br>
src/HAppS/Examples/HTTP1.hs.<br>
&gt; I would like to combine state with io.<br>
&gt; Eventually io will mean stuff like reading from a database, but for
now<br>
&gt; I'm just reading a file.<br>
&gt; The example file HTTP1.hs has an example<br>
&gt; that demonstrates state with macid.<br>
&gt; I added an example that allows you to<br>
&gt; execute arbitrary io.<br>
&gt; I tried, but was unable to, add a handler<br>
&gt; that combines state and io. <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>
&gt; &nbsp; &nbsp;, &nbsp;h [&quot;iohandler&quot;] &nbsp; &nbsp;GET $
ioReadFileHandler<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>
&gt; &nbsp; &nbsp;, &nbsp;h [&quot;statehandler&quot;] GET $ stateHandler<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>
&gt; &nbsp; &nbsp;--, &nbsp;h [&quot;ioandstatehandler&quot;] GET $ ioAndStateHandler<br>
&gt; .....<br>
&gt; -- displays contents of HAPPS.hs in<br>
&gt; current directory<br>
&gt; ioReadFileHandler = iohandler $ readFile<br>
&gt; &quot;./HAppS.hs&quot; <br>
&gt; -- displays incremented state counter<br>
&gt; stateHandler = ok $ \() () -&gt; <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modify (+(1::Int)) &gt;&gt; get
&gt;&gt;=<br>
&gt; respond . show<br>
&gt; -- should combine effect of iohandler<br>
&gt; with statehandler<br>
&gt; -- specifically, should display contents<br>
&gt; of HAppS.hs, and under that an incremented state handler<br>
&gt; -- is this possible<br>
&gt; ioAndStateHandler = undefined undefined<br>
&gt; Is mixing state and io possible with<br>
&gt; HAppS? If so, an example showing how to do it would be extremely helpful.<br>
&gt; best, thomas.<br>
&gt; ---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>
&gt; <br>
&gt; Attachment (https1-whatsnew): application/octet-stream, 1933 bytes<br>
&gt; Attachment (HTTP1.hs): application/octet-stream, 4794 bytes<br>
&gt; <br>
&gt; _______________________________________________<br>
&gt; Haskell-Cafe mailing list<br>
&gt; Haskell-Cafe &lt;at&gt; haskell.org<br>
&gt; http://www.haskell.org/mailman/listinfo/haskell-cafe<br>
&gt; <br>
<br>
<br>
I had no trouble getting this handler to work:<br>
<br>
 &nbsp; &nbsp;h [&quot;iohandler&quot;] GET $ \() () -&gt; do<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modify (+1)<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x &lt;- get<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;respond $ do<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cnts &lt;- readFile
&quot;./sometext.txt&quot;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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>