[Haskell-beginners] Question about typeclasses

Jason Dusek jason.dusek at gmail.com
Sat Jan 3 04:26:23 EST 2009


<iamkeke at gmail.com>                         2009-01-03T08:17:00Z
> I am bit struggling with understanding the typeclasses usage
> in Text.Regex.Posix (=~) [...] My question is that can I
> understand source1 in the way which is a value has type of
> RegexMaker, Regex, CompOption and ExecOption?

  Prettifying the signature a little bit:

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

  You can read this as:

    The type `source1 -> source -> target` such that both "class
    constraints" are satisfied.

  What is a class contraint and what does it mean for it to be
  satisfied? A class contraint is something like an interface,
  or a generic, or a template. One defines "instances" of
  classes in Haskell, as in Jave one "implements" an interface.
  So what do these particular classes mean? The first one reads:

    We have a way to make a regular expression using the usual
    option specifications (multiline, case insensitive, stuff
    like that -- those are `CompOption` and `ExecOption`).

  The second one reads

    There is a way to match the first value against the second
    to get the desired target value (list of matches, Boolean,
    &c.).

  The "way to..." in each case is a function in the class
  definition -- `makeRegex` in the former case, and `match` in
  the latter.

--
Jason Dusek


More information about the Beginners mailing list