[Haskell-cafe] regex libraries: matching operators (=~) and (=~~) are not methods

José Romildo Malaquias j.romildo at gmail.com
Sat Dec 4 10:35:56 CET 2010

On Tue, Nov 30, 2010 at 05:50:02PM -0200, José Romildo Malaquias wrote:
> Hello.
> When learning how to use the many regular expression libraries for
> Haskell, I noticed that the interface API from the regex-base package
> introduces several high level operations that are abstracted from the
> implementations (backends). This is done by means of classes.
> For instance, there are methods for compiling an external representation
> (like a string or a bytestring) into a regular expression. There are
> also methods for matching a regular expression and some text (like a
> string, or a bytestring).
> But the most high level functions for pattern matching using regular
> expressions, (=~) and (=~~), are not defined as methods in a class.
> They are independent functions defined for each backend in the
> corresponding package.
> This prevents one from writing a general function using these operators
> without deliberately choosing a regex backend.
> Why are those operators not defined as methods, like all other relevant
> functions?

I think I have found the reason for that.

Let's consider the operator (=~ ). Its type in the Text.Regex.TDFA is:

( RegexMaker Regex CompOption ExecOption source
, RegexContext Regex source1 target
) =>
source1 -> source -> target

It has two arguments: the text and the regex (in a textual format) used
in matching. They may be both of type String, or ByteString, for
instance. Therefore there is no way to infer which regex engine to use
when the regex text is compiled to an internal format. So the regex
engine cannot be deduced automatically from the arguments. Because of
that each regex backend has to define his own function, and a class is
of no help.


More information about the Haskell-Cafe mailing list