Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Types of Operators



In prolog, we can define and categorise various operators by their arity(number of arguments on the operator) and the position of the operator with respect to the arguments of the operator.

  • Infix Operators − Most of the operators are infix operators where operator is in between the two arguments.

    Example

    • +

    • -

    • *

    • /

    • =

    • is

    • <

    • >

    • <=

    • >=

    Usage

    A + B, X = Y.
    
  • Prefix Operators − Prefix operators comes before the arguments. Unary operators are prefix operators.

    Example

    • - (Unary minus)

    • \+ (Not Provable)

    Usage

    -5, \+ goal
    

Operator Properties

When multiple operators are used in an expression, prolog determines the order of execution of operators as per their precedence and by use of associativity.

Precedence

Precedence determines the binding capacity of an operator with respect to other operator. For example * and / have higher precedence over + and -. Owing to precedence, A + B * C is interpreted by Prolog as A + (B * C).

Associativity

When operators of same precedence are grouped then we can use following rules of associativity.

  • xfx − In case of non-associative infix, we should use brackets to avoid error. For example A op B op C will throw an error.

  • xfy − In case of right-associative infix, Prolog will treat A op B op C as A op (B op C). Right-associative infix is useful in case of list concatenation and exponentiation operations.

  • yfx − In case of left-associative infix, Prolog will treat A op B op C as (A op B) op C). Left-associative infix is useful in case of arithmetic operators like +, -, * , /.

  • fx − In case of non-associative prefix, Prolog allows only single operator to appear.

  • yf − In case of right-associative prefix, Prolog allows multiple operator to stack. For example --X.

Advertisements