[Haskell-beginners] Pure Regex.PCRE Possible? (PLEAC, Fix Style)
stephen.tetley at gmail.com
Sat Dec 19 12:05:36 EST 2009
Under the hood PCRE.regex uses withCStringLen which has the type:
withCStringLen :: String -> (CStringLen -> IO a) -> IO a
You can see the code here:
So the implementation is impure - it is built on impure code.
In the Real Word Haskell book, there is another implementation of a
PCRE interface, here the authors decided that their use of PCRE is
pure. The functions they have that use side effects - allocating
memory and freeing it - use the side effects entirely locally inside
each function body and the side effects never leak out of any
The implementation from the book is here:
Potentially the author of Text.Regex.PCRE could have decided that the
use of PCRE was pure and gone some way to hiding the IO type of
withCStringLen and similar functions (withCStringLen already
encapsulates its side effects). But as a design decision - if you are
using IO it is entirely reasonable to keep within IO even if what you
are doing appears pure (and personally I would choose to use the IO
version as I like to know when a library I'm using is performing side
2009/12/19 Volkan YAZICI <yazicivo at ttmail.com>:
> I'm trying to implement FixStyle program of PLEAC for Haskell.
> (It's missing in the Haskell version of PLEAC.) For this purpose, I
> needed regular expressions. (Now, I have two problems!) Despite, my
> code is, IMHO, quite purely functional, Regex.PCRE.String functions
> totally mess up the design with impure functions they introduce even for
> really simple string matching stuff.
> If you'd check out the sources, you'll see below impure functions.
> transDictRegex :: IO PCRE.Regex
> matchRegex :: String -> IO (String, String, String)
> translate :: String -> IO String
> However, if I'm not mistaken, above functions _should_ be absolutely
> pure. Is it possible to encapsulate impure PCRE functions into their
> pure equivalents?
> BTW, why are PCRE functions impure?
>  http://pleac.sourceforge.net/pleac_perl/strings.html
>  http://pleac.sourceforge.net/
>  http://hpaste.org/fastcgi/hpaste.fcgi/view?id=14422
> Beginners mailing list
> Beginners at haskell.org
More information about the Beginners