
Haskell I/O and XPath - adamretter
https://blog.adamretter.org.uk/haskell-io-and-xpath/
======
lmm
This feels like cargo culting. The point of IO isn't the RealWorld stuff
(that's an implementation technique, and not even really true these days as IO
offers all sorts of other facilities), the point is that it's a way to get the
language's type system to help you distinguish between accidental control flow
and essential control flow (
[http://www.lihaoyi.com/post/WhatsFunctionalProgrammingAllAbo...](http://www.lihaoyi.com/post/WhatsFunctionalProgrammingAllAbout.html)
). Without the type system it's a lot of ceremony for no benefit.

~~~
adamretter
I do agree that the RealWorld stuff is there to enforce control flow, but that
is exactly one of the things that we needed. I also realise that the modern
GHC optimizes such things away, so there is no RealWorld!

Regards implementation, as the type system available in XQuery is much more
limited than that of Haskell, I had to choose some mechanism to represent an
IO. Actually representing it as a state transforming function that operates on
the RealWorld, as was done in the design of the IO Monad for Haskell, seems to
work pretty well.

I tried to follow closely and adapt both: 1) Section 7.3 "Monadic I/O" from "A
History of Haskell", Hudak, Hughes, Peyton Jones and Wadler 2007 -
[https://www.microsoft.com/en-us/research/wp-
content/uploads/...](https://www.microsoft.com/en-us/research/wp-
content/uploads/2016/07/history.pdf) 2) Section 4 "Implementing monadic I/O"
from "Imperative functional programming", Wadler and Peyton Jones 1992 -
[https://www.microsoft.com/en-us/research/wp-
content/uploads/...](https://www.microsoft.com/en-us/research/wp-
content/uploads/1993/01/imperative.pdf)

~~~
lmm
I appreciate that your implementation follows the Haskell one. But the point
of IO isn't the implementation, it's to make it easier to maintain code that
interacts with the real world. That's the part that feels cargo-culty to me
(having gone down the same path in Python once): if the type system can't
actually enforce that your monads are composed correctly then you pay the same
cost you would in Haskell but don't get the main benefit.

------
ToJans
On a side note: IMHO this article contains one of the most accessible
explanations I have seen about monads.

~~~
HelloNurse
It's an outstandingly well-written introduction, but it only goes so far; a
more formal treatment (an inevitably longer one, off topic in a discussion of
I/O) would be needed to understand what, exactly, can be done with monads and
how they differ from other types.

------
paulddraper
Good post, and clever technique.

Site feedback: That blue bar at the top make it looks like your page hasn't
loaded. If I want to see how far I've read, I will just look to the right side
at the scrollbar.

------
peteretep
Aha, nice! I have wondered about doing something similar before.

As part of a module on XML for my MSc I decided to go maximum overkill, and
created in XSLT a simple key-value lookup, a breadth-first node search
algorithm, and a simple TAP-based test suite to make sure it all worked
properly. Almost looked at seeing how much of the Functional Data Structures
book I could cram into XSLT for my dissertation before choosing something a
little less ambitious.

------
mark_l_watson
That is a good writeup, a fun read! I love Haskell but I tend to use a small
subset of the language. This means that I am effective when working in my own
Haskell projects but reading and understanding other people’s code takes time
and effort.

------
kreetx
Wow! Great article, and interesting info on Philip Wadler!

------
crb002
It's never a good day when I have to pull out AWK and XSLT because that's all
the server supports.

------
agumonkey
didn't know Wadler was part of xquery et al .. interesting.

good read

