[m-users.] Pattern matching in Mercury

Mark Brown mark at mercurylang.org
Sun Oct 26 23:46:11 AEDT 2014


Hi Oleg,

On Sun, Oct 26, 2014 at 9:44 PM, Left Right <olegsivokon at gmail.com> wrote:
> If I may add my 2 cents... I started looking into Mercury about half a
> year ago, then I realized that there are things I don't quite
> understand about the language. I think that I found a good book, even
> though it's about Prolog, it explained the formalisms used to build
> the language and the consequences of using these formalisms. I'm
> talking about The Art of Prolog by Sterling and Shapiro. I can't point
> you to the exact chapter in the book, where it talks about unifying
> (something that you call pattern matching). But, to put it simply:
> pattern matching and unifying aren't the same thing. They also
> describe different things (so they aren't two different tools to do
> the same task, they also accomplish different tasks).

Yes, this is true. Operationally, Mercury unifications cover the
following cases:

    - testing if values are equal
    - assigning values
    - constructing terms from sub-terms
    - deconstructing terms back into sub-terms

Pattern matching of tree-structures corresponds to only the last of these.

Conversely, pattern matching can also be done on sequences rather than
trees, and standard unification cannot express this.

(It's worth mentioning that Mercury unifications must be well-moded,
so they are strictly less expressive than Prolog ones.)

Cheers,
Mark.



More information about the users mailing list