admin管理员组

文章数量:1292358

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 *>)

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 *>)

Share edited Feb 13 at 8:55 RandomB asked Feb 13 at 8:46 RandomBRandomB 3,7491 gold badge26 silver badges40 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 6
(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 ($>).

amalloy explained where you've gone wrong, but let me just get into one small bit of your question:

Is it a problem of the library[...]?

It's not, and it can't be. The fact that *> discards the result(s) from the left operand is guaranteed by its type.

class Functor f => Applicative f where
   ...
   (*>) :: f a -> f b -> f b

The polymorphic type variable a only appears in a "negative" position in the function type, with nothing to connect it to types in the result. So *> doesn't "know" anything about that type and can't use values of that type at all (except to force them).

本文标签: haskellWhy does the implementation of *gt return something from the left argumentStack Overflow