Search code examples
prologoperator-overloadingiso-prolog

Prolog: declaring an operator


I have defined ! (factorial) function and registered it as arithmetic function and an operator, so that I can execute: A is 6!.

Now I'd like to define !! (factorial of odd numbers), but the same way - writing clauses, registering arithmetic_function and operator, calling A is 7!! - results in SyntaxError: Operator expected

How should I, if possible, register !! operator ?

Yes, I realize, ! is normally the cut.


Solution

  • ! is a so-called solo character, you cannot have two in a row. If it were not, you could not write for example:

    c :- !.
    

    but would instead have to write:

    c :- ! .
    

    because "!." would otherwise be interpreted as a single token.

    Also, if you let ! be an operator, both versions are invalid syntax (yes, SWI still accepts it, but for example GNU Prolog does not). You need to write:

    c :- (!).
    

    because operators that are operands need to be bracketed. Instead of !, use for example "f" and "ff", or fitting Unicode characters for your use case.