English 中文(简体)
":=" and "=>" in Mercury
原标题:

I recently came across this code example in Mercury:

append(X,Y,Z) :-
  X == [],
  Z := Y.
append(X,Y,Z) :-
  X => [H | T],
  append(T,Y,NT),
  Z <= [H | NT].

Being a Prolog programmer, I wonder: what s the difference between a normal unification = and the := or => which are use here?

In the Mercury reference, these operators get different priorities, but they don t explain the difference.

最佳回答

First let s re-write the code using indentation:

append(X, Y, Z) :-
    X == [],
    Z := Y.
append(X, Y, Z) :-
    X => [H | T],
    append(T, Y, NT),
    Z <= [H | NT].

You seem to have to indent all code by four spaces, which doesn t seem to work in comments, my comments above should be ignored (I m not able to delete them).

The code above isn t real Mercury code, it is pseudo code. It doesn t make sense as real Mercury code because the <= and => operators are used for typeclasses (IIRC), not unification. Additionally, I haven t seen the := operator before, I m not sure what is does.

In this style of pseudo code (I believe) that the author is trying to show that := is an assignment type of unification where X is assigned the value of Y. Similarly => is showing a deconstruction of X and <= is showing a construction of Z. Also == shows an equality test between X and the empty list. All of these operations are types of unification. The compiler knows which type of unification should be used for each mode of the predicate. For this code the mode that makes sense is append(in, in, out)

Mercury is different from Prolog in this respect, it knows which type of unification to use and therefore can generate more efficient code and ensure that the program is mode-correct.

One more thing, the real Mercury code for this pseudo code would be:

:- pred append(list(T)::in, list(T)::in, list(T)::out) is det.

append(X, Y, Z) :-
    X = [],
    Z = Y.
append(X, Y, Z) :-
    X = [H | T],
    append(T, Y, NT),
    Z = [H | NT].

Note that every unification is a = and a predicate and mode declaration has been added.

问题回答

In concrete Mercury syntax the operator := is used for field updates.

Maybe we are not able to use such operators like := <= => == in recent Mercury release, but actually these operators are specialized unification, according to the description in Nancy Mazur s thesis. => stands for deconstruction e.g. X => f(Y1, Y2, ..., Yn) where X is input and all Yn is output. It s semidet. <= is on the contrary, and is det. == is used in the situation where both sides are ground, and it is semidet. := is just like regular assigning operator in any other language, and it s det. In older papers I even see that they use == instead of => to perform a deconstruction. (I think my English is awful = x =)





相关问题
Help with prolog s clauses

emissionOf(alpha). emissionOf(beta). detected(proton), detected(electron) :- emissionOf(alpha), emissionOf(beta). I m facing the problem that for some (probably obvious) reason Prolog doesn t accept ...

A question about logic and the Curry-Howard correspondence

Could you please explain me what is the basic connection between the fundamentals of logical programming and the phenomenon of syntactic similarity between type systems and conventional logic?

Can you do Logic Programming in Scala?

I read somewhere that Pattern Matching like that supported by the match/case feature in Scala was actually borrowed from Logic languages like Prolog. Can you use Scala to elegantly solve problems like ...

Relational/Logic Programming in Python?

I m a longtime python developer and recently have been introduced to Prolog. I love the concept of using relationship rules for certain kinds of tasks, and would like to add this to my repertoire. ...

Embedded Prolog Interpreter/Compiler for Java

I m working on an application in Java, that needs to do some complex logic rule deductions as part of its functionality. I d like to code my logic deductions in Prolog or some other logic/constraint ...

热门标签