7

This Prolog program prints Hello:

main :-
    write('Hello'), nl.

:- main.

I changed (:-)/1 to (?-)/1:

main :-
    write('Hello'), nl.

?- main.

This produces exactly the same result. This also prints Hello.

So what's the difference between (:-)/1 and (?-)/1?

Flux
  • 9,805
  • 5
  • 46
  • 92

3 Answers3

4

The argument of term with principal functor (:-)/1 is called a directive. In the Prolog standard, it is defined in 7.4.2 Directives.

In addition, 5.5.5 states:

A processor may support one or more additional directive indicators (7.4.2) as an implementation specific feature.

So, in some Prolog systems, (?-)/1 is available as additional directive indicator.

mat
  • 40,498
  • 3
  • 51
  • 78
  • 1
    In the case of SWI Prolog, are `:-` and `?-` interchangeable? – Flux Oct 24 '18 at 10:00
  • 2
    Yes, you can see that `?-(D)` is internally treated exactly as `:-(D)`: https://github.com/SWI-Prolog/swipl-devel/blob/99b4a92c93077a0ab9e04d6e7549c29fc8d3c57d/boot/init.pl#L2660 However, it is common (and standard) practice to use `(:-)/1` for directives. This works in all Prolog systems. Note that you should use the `initialization/1` directive to portably execute Prolog code. For example, write `:- initialization main.` – mat Oct 24 '18 at 10:03
  • 1
    Interestingly, in SWI and Gnu Prologs, the `?-` operator doesn't work with the `include/1` directive. – lurker Oct 24 '18 at 18:07
3

In ISO Prolog, :- is used for directives like operator declarations only. The ?- operator is also defined but no meaning is given to it.

These operators stem from the DEC system 10 Prolog of ~1978 where they were called command and question respectively. While :- p(X). just tested for the success of p(X) during consulting, ?- p(X). showed an actual answer and prompted for further answers. So you got some interaction while loading your files which was abandoned in subsequent systems making both operators behave in the same way.

Since DEC10, many systems print ?- as a prompt for the top level loop, reminding that the next term read in will be interpreted and answered as a question. Some systems go even further and add the interpreter prompt | in front of it.

false
  • 10,264
  • 13
  • 101
  • 209
2

Both :- and ?- are specified as prefix operators in the ISO Prolog Core standard:

Priority   Specifier   Operator(s)
...
1200       fx          :- ?-

The ?- prefix operator is there mainly historical reasons. Its semantics is the same as the :- prefix operator although I didn't find a statement clarifying it in the standard besides the one mentioned in Markus's answer.

Paulo Moura
  • 18,373
  • 3
  • 23
  • 33