Bug introduced in 10.3 or earlier and persisting through 12.0.0
10.0.2 is not affected.
I am perplexed by the effect of Orderless on pattern matching. Starting from a clean Kernel (i.e. after using Quit), I find the following expected match for function g without any definitions or attributes
MatchQ[g[a[1], b[1]], g[(Y : (a | b))[i__], b[1]]]
(* True *)
If I do the same for a symbol f with Attributes[f] = {Orderless}
Attributes[f] = {Orderless};
MatchQ[f[a[1], b[1]], f[(Y : (a | b))[i__], b[1]]]
(* False *)
it returns False. I would have expected True. If I replace i__ with 1 on the RHS I get True again.
MatchQ[f[a[1], b[1]], f[(Y : (a | b))[1], b[1]]]
(* True *)
The same goes for
MatchQ[f[a[1], b[1]], f[(Y : (a | c))[i__]]
So the issue does not seem to be that b somehow gets matched on the wrong side. Also Orderless seems to prefer the same (flipped) order regardless of whether I change i__ to 1. (And this should be irrelevant anyway since f has orderless.
What am I missing here?
Orderlesssorts the symbols. Have a look atOrderlessPatternSequence. E.g.,MatchQ[f[a[1], b[1]], f[OrderlessPatternSequence[(Y : (a | b))[i__], b[1]]]]might do what you want. – Henrik Schumacher Mar 21 '19 at 11:20MatchQ[f[a[1],b[1]],_[(Y:(a|b))[i__],b[1]]]->True,MatchQ[f[a[1],b[1]],_[b[1],(Y:(a|b))[i__]]]->False,MatchQ[f[b[1],a[1]],_[(Y:(a|b))[i__],b[1]]]->True,MatchQ[f[b[1],a[1]],_[b[1],(Y:(a|b))[i__]]]->False.When pattern is any head it seems to only match the pattern which is canonical order of parts, probably because
– user13892 Mar 21 '19 at 11:58f[b[1],a[1]]has canonical formf[a[1],b[1]].TrueifBlankSequence[](the double underbar) is changed toBlank[]. I do not know why one matches and the other does not. – Daniel Lichtblau Mar 21 '19 at 14:47MatchQ[f[a[], b[]], f[_[___], b[]]]returns false yetMatchQ[f[a[], b[]], f[_[], b[]]]returns true. – WReach Mar 21 '19 at 14:48