<div dir="ltr">The problem is non-strict evaluation aka "laziness". "return" doesn't force evaluation of the pure expression you give to it (actual I/O involving its value generally would, but return just puts a wrapper around it), so its evaluation is forced later outside of your code to catch it, when the value is actually examined.<div><br></div><div>In extreme cases you end up using deepseq's rnf to force full evaluation, but here it's enough to force evaluation to WHNF with ($!) before handing the result off to return. With a lazy ByteString you might need rnf, since ($!) would only force part of the structure of the list of chunks but you would need to force all of the chunks' contents to trigger the decode exception.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Mar 4, 2019 at 3:27 PM Kees Bleijenberg <<a href="mailto:K.Bleijenberg@lijbrandt.nl">K.Bleijenberg@lijbrandt.nl</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div lang="NL"><div class="gmail-m_1428190607071173671WordSection1"><p class="MsoNormal"><span lang="EN-US" style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">David, <u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">I tried your first suggestion $!. Nothing changed.<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">When I tried ‘Right <$> evaluate (TE.decodeUtf8 bufferStrict)’ success. handleException catches the exception. <u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">I don’t understand why. Maybe the documentation for the evaluate function below has to do with it:<u></u><u></u></span></p><p style="margin:0cm 0cm 0.0001pt;background:white"><span lang="EN-US" style="font-size:10pt;font-family:Arial,sans-serif;color:black">There is a subtle difference between </span><code><span style="font-size:10pt;color:black"><a href="http://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Exception-Base.html#v:evaluate" title="Control.Exception.Base" target="_blank"><span lang="EN-US" style="color:rgb(171,105,84)">evaluate</span></a></span></code><code><span lang="EN-US" style="font-size:10pt;color:black"> x</span></code><span lang="EN-US" style="font-size:10pt;font-family:Arial,sans-serif;color:black"> and </span><code><span style="font-size:10pt;color:black"><a href="http://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Monad.html#v:return" title="Control.Monad" target="_blank"><span lang="EN-US" style="color:rgb(171,105,84)">return</span></a></span></code><code><span lang="EN-US" style="font-size:10pt;color:black"> </span></code><code><span style="font-size:10pt;color:black"><a href="http://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#v:-36--33-" title="Prelude" target="_blank"><span lang="EN-US" style="color:rgb(171,105,84)">$!</span></a></span></code><code><span lang="EN-US" style="font-size:10pt;color:black"> x</span></code><span lang="EN-US" style="font-size:10pt;font-family:Arial,sans-serif;color:black">, analogous to the difference between </span><code><span style="font-size:10pt;color:black"><a href="http://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Exception-Base.html#v:throwIO" title="Control.Exception.Base" target="_blank"><span lang="EN-US" style="color:rgb(171,105,84)">throwIO</span></a></span></code><span lang="EN-US" style="font-size:10pt;font-family:Arial,sans-serif;color:black"> and </span><code><span style="font-size:10pt;color:black"><a href="http://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Exception-Base.html#v:throw" title="Control.Exception.Base" target="_blank"><span lang="EN-US" style="color:rgb(171,105,84)">throw</span></a></span></code><span lang="EN-US" style="font-size:10pt;font-family:Arial,sans-serif;color:black">. If the lazy value </span><code><span lang="EN-US" style="font-size:10pt;color:black">x</span></code><span lang="EN-US" style="font-size:10pt;font-family:Arial,sans-serif;color:black"> throws an exception, </span><code><span style="font-size:10pt;color:black"><a href="http://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Monad.html#v:return" title="Control.Monad" target="_blank"><span lang="EN-US" style="color:rgb(171,105,84)">return</span></a></span></code><code><span lang="EN-US" style="font-size:10pt;color:black"> </span></code><code><span style="font-size:10pt;color:black"><a href="http://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#v:-36--33-" title="Prelude" target="_blank"><span lang="EN-US" style="color:rgb(171,105,84)">$!</span></a></span></code><code><span lang="EN-US" style="font-size:10pt;color:black"> x</span></code><span lang="EN-US" style="font-size:10pt;font-family:Arial,sans-serif;color:black"> will fail to return an </span><code><span style="font-size:10pt;color:black"><a href="http://hackage.haskell.org/package/base-4.12.0.0/docs/System-IO.html#t:IO" title="System.IO" target="_blank"><span lang="EN-US" style="color:rgb(171,105,84)">IO</span></a></span></code><code><span style="font-size:10pt;color:black"> </span></code><span lang="EN-US" style="font-size:10pt;font-family:Arial,sans-serif;color:black">action and will throw an exception instead. </span><code><span style="font-size:10pt;color:black"><a href="http://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Exception-Base.html#v:evaluate" title="Control.Exception.Base" target="_blank"><span lang="EN-US" style="color:rgb(171,105,84)">evaluate</span></a></span></code><code><span lang="EN-US" style="font-size:10pt;color:black"> x</span></code><span lang="EN-US" style="font-size:10pt;font-family:Arial,sans-serif;color:black">, on the other hand, always produces an </span><code><span style="font-size:10pt;color:black"><a href="http://hackage.haskell.org/package/base-4.12.0.0/docs/System-IO.html#t:IO" title="System.IO" target="_blank"><span lang="EN-US" style="color:rgb(171,105,84)">IO</span></a></span></code><span lang="EN-US" style="font-size:10pt;font-family:Arial,sans-serif;color:black"> action; that action will throw an exception upon <em><span style="font-family:Arial,sans-serif">execution</span></em> iff </span><code><span lang="EN-US" style="font-size:10pt;color:black">x</span></code><span lang="EN-US" style="font-size:10pt;font-family:Arial,sans-serif;color:black"> throws an exception upon <em><span style="font-family:Arial,sans-serif">evaluation</span></em>.<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">I don’t fully understand this, but evaluate works. Thanks!<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">Kees<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></p><p class="MsoNormal" style="line-height:14.25pt;background:rgb(30,30,30)"><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(220,220,170)">readCDFile</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> </span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(86,156,214)">::</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> </span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(86,156,214)">FilePath</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> </span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(86,156,214)">-></span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> </span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(86,156,214)">FilePath</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> </span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(86,156,214)">-></span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> </span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(86,156,214)">IO</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> (</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(86,156,214)">Either</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> </span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(86,156,214)">String</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> </span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(86,156,214)">T</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)">.</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(86,156,214)">Text</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)">)<u></u><u></u></span></p><p class="MsoNormal" style="line-height:14.25pt;background:rgb(30,30,30)"><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)">readCDFile baseDir fn = </span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(197,134,192)">do</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"><u></u><u></u></span></p><p class="MsoNormal" style="line-height:14.25pt;background:rgb(30,30,30)"><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> catch ( </span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(197,134,192)">do</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> <u></u><u></u></span></p><p class="MsoNormal" style="line-height:14.25pt;background:rgb(30,30,30)"><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> buffer <- B.readFile (combine baseDir fn) </span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(106,153,85)">--reads strict the whole file</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"><u></u><u></u></span></p><p class="MsoNormal" style="line-height:14.25pt;background:rgb(30,30,30)"><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> </span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(86,156,214)">let</span><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> bufferStrict = B.toStrict buffer<u></u><u></u></span></p><p class="MsoNormal" style="margin-left:35.4pt;text-indent:35.4pt;line-height:14.25pt;background:rgb(30,30,30)"><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)">return $ Right $! TE.decodeUtf8 bufferStrict -- this doesn’t work <u></u><u></u></span></p><p class="MsoNormal" style="margin-left:35.4pt;text-indent:35.4pt;line-height:14.25pt;background:rgb(30,30,30)"><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)">Right <$> evaluate (TE.decodeUtf8 bufferStrict) –- this does <u></u><u></u></span></p><p class="MsoNormal" style="margin-left:35.4pt;text-indent:35.4pt;line-height:14.25pt;background:rgb(30,30,30)"><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)">liftM Right $ evaluate (TE.decodeUtf8 bufferStrict) – this works too <u></u><u></u></span></p><p class="MsoNormal" style="line-height:14.25pt;background:rgb(30,30,30)"><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"> ) exceptionHandler<u></u><u></u></span></p><p class="MsoNormal" style="line-height:14.25pt;background:rgb(30,30,30)"><span lang="EN-US" style="font-size:10.5pt;font-family:"Courier New";color:rgb(212,212,212)"><u></u> <u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></p><p class="MsoNormal"><b><span lang="EN-US" style="font-size:11pt;font-family:Calibri,sans-serif">Van:</span></b><span lang="EN-US" style="font-size:11pt;font-family:Calibri,sans-serif"> David Fox [mailto:<a href="mailto:dsf@seereason.com" target="_blank">dsf@seereason.com</a>] <br><br></span><span lang="EN-US"><u></u><u></u></span></p><div><div><div><div><p class="MsoNormal"><span lang="EN-US">This fixes it by forcing the evaluation of the decode where it can be caught:<u></u><u></u></span></p></div><div><p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p></div><div><p class="MsoNormal">return $ Right $! TE.decodeUtf8 bufferStrict<u></u><u></u></p></div><div><p class="MsoNormal"><u></u> <u></u></p></div><div><p class="MsoNormal">or <u></u><u></u></p></div><div><p class="MsoNormal"><u></u> <u></u></p></div><div><p class="MsoNormal">Right <$> evaluate (TE.decodeUtf8 bufferStrict)<u></u><u></u></p></div><div><p class="MsoNormal"><u></u> <u></u></p></div><div><p class="MsoNormal"><span style="color:rgb(31,73,125)"><snip></span><u></u><u></u></p></div></div></div></div></div><div id="gmail-m_1428190607071173671DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><br> <table style="border-top:1px solid rgb(211,212,222)">
<tbody><tr>
<td style="width:55px;padding-top:18px"><a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient" target="_blank"><img src="https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif" alt="" width="46" height="29" style="width: 46px; height: 29px;"></a></td>
<td style="width:470px;padding-top:17px;color:rgb(65,66,78);font-size:13px;font-family:Arial,Helvetica,sans-serif;line-height:18px">Virusvrij. <a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient" style="color:rgb(68,83,234)" target="_blank">www.avast.com</a> </td>
</tr>
</tbody></table>
<a href="#m_1428190607071173671_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2" width="1" height="1"> </a></div></div>_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>brandon s allbery kf8nh</div><div><a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a></div></div></div></div></div>