Search code examples
haskell

Why does the implementation of *> return something from the left argument?


I try to use a library regex-applicative.

My code is:

nicBeg = ((psym $ not . isSpace) $> (few anySym <> " adapter ")) *> some anySym
result = match nicBeg "abcd adapter NIC 1"

and I see that result is Just "bcd adapter NIC 1".

But the documentation of *> tells:

*> :: forall (f :: Type -> Type) a b. Applicative f => f a -> f b -> f b

Defined in ‘GHC.Base’ (base-4.16.4.0)
Sequence actions, discarding the value of the first argument.

So, the question is: why does "bcd adapter " part exist in the result and not only "NIC 1" (it was not discarded)? Is it a problem of the library or I am wrong somewhere?

PS. The library has a concept of "greedy" RE expressions.

PS. It's easy to get an expected result: ((psym $ not . isSpace) *> (few anySym <> " adapter ")) *> some anySym (ie, to replace $> with *>)


Solution

  • (psym $ not . isSpace) $> (few anySym <> " adapter ")
    

    is a very weird expression. I'm not sure what you want it to mean, but because ($>) is a flipped version of (<$), it means "match the regular expression (psym $ not . isSpace), and then replace the value you parsed with (few anySym <> " adapter ")". Importantly, (few anySym <> " adapter ") is not being used as a regular expression at all. It is the value associated with parsing a single non-space character. Then, of course, *> some anySym throws away that value, just as you expect, and tries to match the remaining string (i.e., "bcd adapter NIC 1") against some anySym, with predictable results.

    So in the end you are not confused about (*>) at all, but about ($>).