Search code examples
prologpredicate

SWI-Prolog: Write predicate union(A,B,C) in form C = A ∪ B


Is their some way in SWI-Prolog to write predicates with three variables for example union(A,B,C) in the following form C = A ∪ B. For predicates with two variables I know their are operators to do that, but I am not sure if their is something similar in that case.


Solution

  • No.

    Not directly. Prolog only supports defining unary operators (prefix/suffix operators such as -- 32 or 32 ++, both of which correspond to '--'/1 or '++'/1) and infix operators (e.g. X is Y which corresponds to is/2).

    If you look at the operator definitions and precedences, you would need to define your union operator as an infix operator with a precedence of less than 700.

    Then, reading a term like x = y ∪ z would yield '='( x , '∪'(y,z) ).

    Another way to do it would be to write a DCG (definite clause grammar) to parse the text as desired. See this tutorial: https://www.metalevel.at/prolog/dcg