<s>
The	O
syntax	O
and	O
semantics	B-Application
of	O
Prolog	B-Language
,	O
a	O
programming	O
language	O
,	O
are	O
the	O
sets	O
of	O
rules	O
that	O
define	O
how	O
a	O
Prolog	B-Language
program	O
is	O
written	O
and	O
how	O
it	O
is	O
interpreted	O
,	O
respectively	O
.	O
</s>
<s>
The	O
rules	O
are	O
laid	O
out	O
in	O
ISO	O
standard	O
ISO/IEC	B-Language
13211	I-Language
although	O
there	O
are	O
differences	O
in	O
the	O
Prolog	B-Language
implementations	I-Language
.	O
</s>
<s>
Prolog	B-Language
is	O
dynamically	O
typed	O
.	O
</s>
<s>
It	O
is	O
composed	O
of	O
a	O
sequence	O
of	O
characters	O
that	O
is	O
parsed	B-Language
by	O
the	O
Prolog	B-Language
reader	O
as	O
a	O
single	O
unit	O
.	O
</s>
<s>
Atoms	O
are	O
usually	O
bare	O
words	O
in	O
Prolog	B-Language
code	O
,	O
written	O
with	O
no	O
special	O
syntax	O
.	O
</s>
<s>
Numbers	O
can	O
be	O
floats	B-Algorithm
or	O
integers	O
.	O
</s>
<s>
Many	O
Prolog	B-Language
implementations	I-Language
also	O
provide	O
unbounded	O
integers	O
and	O
rational	O
numbers	O
.	O
</s>
<s>
A	O
variable	O
can	O
become	O
instantiated	O
(	O
bound	O
to	O
equal	O
a	O
specific	O
term	O
)	O
via	O
unification	B-Algorithm
.	O
</s>
<s>
Unlike	O
other	O
variables	O
,	O
the	O
underscore	O
does	O
not	O
represent	O
the	O
same	O
value	O
everywhere	O
it	O
occurs	O
within	O
a	O
predicate	B-Algorithm
definition	O
.	O
</s>
<s>
Strings	O
:	O
A	O
sequence	O
of	O
characters	O
surrounded	O
by	O
quotes	O
is	O
equivalent	O
to	O
a	O
list	O
of	O
(	O
numeric	B-Algorithm
)	O
character	B-General_Concept
codes	I-General_Concept
,	O
generally	O
in	O
the	O
local	O
character	B-General_Concept
encoding	I-General_Concept
or	O
Unicode	O
if	O
the	O
system	O
supports	O
Unicode	O
.	O
</s>
<s>
Prolog	B-Language
programs	O
describe	O
relations	O
,	O
defined	O
by	O
means	O
of	O
clauses	O
.	O
</s>
<s>
Pure	O
Prolog	B-Language
is	O
restricted	O
to	O
Horn	B-Application
clauses	I-Application
,	O
a	O
Turing-complete	B-Algorithm
subset	O
of	O
first-order	O
predicate	B-Algorithm
logic	O
.	O
</s>
<s>
A	O
rule	O
's	O
body	O
consists	O
of	O
calls	O
to	O
predicates	B-Algorithm
,	O
which	O
are	O
called	O
the	O
rule	O
's	O
goals	O
.	O
</s>
<s>
The	O
built-in	O
predicate	B-Algorithm
,	O
/2	O
(	O
meaning	O
a	O
2-arity	O
operator	O
with	O
name	O
,	O
)	O
denotes	O
conjunction	O
of	O
goals	O
,	O
and	O
;	O
/2	O
denotes	O
disjunction	O
.	O
</s>
<s>
The	O
built-in	O
predicate	B-Algorithm
true/0	O
is	O
always	O
true	O
.	O
</s>
<s>
Execution	O
of	O
a	O
Prolog	B-Language
program	O
is	O
initiated	O
by	O
the	O
user	O
's	O
posting	O
of	O
a	O
single	O
goal	O
,	O
called	O
the	O
query	O
.	O
</s>
<s>
Logically	O
,	O
the	O
Prolog	B-Language
engine	O
tries	O
to	O
find	O
a	O
resolution	O
refutation	O
of	O
the	O
negated	O
query	O
.	O
</s>
<s>
The	O
resolution	O
method	O
used	O
by	O
Prolog	B-Language
is	O
called	O
SLD	B-Application
resolution	I-Application
.	O
</s>
<s>
Operationally	O
,	O
Prolog	B-Language
's	O
execution	O
strategy	O
can	O
be	O
thought	O
of	O
as	O
a	O
generalization	O
of	O
function	O
calls	O
in	O
other	O
languages	O
,	O
one	O
difference	O
being	O
that	O
multiple	O
clause	O
heads	O
can	O
match	O
a	O
given	O
call	O
.	O
</s>
<s>
This	O
execution	O
strategy	O
is	O
called	O
chronological	O
backtracking	B-Algorithm
.	O
</s>
<s>
enumerates	O
all	O
valid	O
answers	O
on	O
backtracking	B-Algorithm
.	O
</s>
<s>
Iterative	O
algorithms	O
can	O
be	O
implemented	O
by	O
means	O
of	O
recursive	O
predicates	B-Algorithm
.	O
</s>
<s>
Prolog	B-Language
systems	O
typically	O
implement	O
a	O
well-known	O
optimization	O
technique	O
called	O
tail	B-Language
call	I-Language
optimization	I-Language
(	O
TCO	O
)	O
for	O
deterministic	O
predicates	B-Algorithm
exhibiting	O
tail	B-Language
recursion	I-Language
or	O
,	O
more	O
generally	O
,	O
tail	B-Language
calls	I-Language
:	O
A	O
clause	O
's	O
stack	O
frame	O
is	O
discarded	O
before	O
performing	O
a	O
call	O
in	O
a	O
tail	O
position	O
.	O
</s>
<s>
Therefore	O
,	O
deterministic	O
tail-recursive	B-Language
predicates	B-Algorithm
are	O
executed	O
with	O
constant	O
stack	O
space	O
,	O
like	O
loops	O
in	O
other	O
languages	O
.	O
</s>
<s>
A	O
cut	B-Application
(	O
!	O
)	O
</s>
<s>
inside	O
a	O
rule	O
will	O
prevent	O
Prolog	B-Language
from	O
backtracking	B-Algorithm
any	O
predicates	B-Algorithm
behind	O
the	O
cut	B-Application
:	O
</s>
<s>
Anonymous	O
variables	O
_	O
are	O
never	O
bound	O
to	O
a	O
value	O
and	O
can	O
be	O
used	O
multiple	O
times	O
in	O
a	O
predicate	B-Algorithm
.	O
</s>
<s>
The	O
built-in	O
Prolog	B-Language
predicate	B-Algorithm
\+/1	O
provides	O
negation	B-Application
as	I-Application
failure	I-Application
,	O
which	O
allows	O
for	O
non-monotonic	O
reasoning	O
.	O
</s>
<s>
is	O
evaluated	O
as	O
follows	O
:	O
Prolog	B-Language
attempts	O
to	O
prove	O
the	O
illegal(X )	O
.	O
</s>
<s>
Procedurally	O
,	O
however	O
,	O
it	O
is	O
often	O
important	O
to	O
take	O
into	O
account	O
Prolog	B-Language
's	O
execution	O
strategy	O
,	O
either	O
for	O
efficiency	O
reasons	O
,	O
or	O
due	O
to	O
the	O
semantics	B-Application
of	O
impure	O
built-in	O
predicates	B-Algorithm
for	O
which	O
the	O
order	O
of	O
evaluation	O
matters	O
.	O
</s>
<s>
Also	O
,	O
as	O
Prolog	B-Language
interpreters	O
try	O
to	O
unify	O
clauses	O
in	O
the	O
order	O
they	O
're	O
provided	O
,	O
failing	O
to	O
give	O
a	O
correct	O
ordering	O
can	O
lead	O
to	O
infinite	O
recursion	O
,	O
as	O
in	O
:	O
</s>
<s>
There	O
is	O
a	O
special	O
notation	O
called	O
definite	B-Application
clause	I-Application
grammars	I-Application
(	O
DCGs	O
)	O
.	O
</s>
<s>
A	O
rule	O
defined	O
via	O
-->/	O
2	O
instead	O
of	O
:-/2	O
is	O
expanded	O
by	O
the	O
preprocessor	O
(	O
expand_term/2	O
,	O
a	O
facility	O
analogous	O
to	O
macros	O
in	O
other	O
languages	O
)	O
according	O
to	O
a	O
few	O
straightforward	O
rewriting	O
rules	O
,	O
resulting	O
in	O
ordinary	O
Prolog	B-Language
clauses	O
.	O
</s>
<s>
Most	O
notably	O
,	O
the	O
rewriting	O
equips	O
the	O
predicate	B-Algorithm
with	O
two	O
additional	O
arguments	O
,	O
which	O
can	O
be	O
used	O
to	O
implicitly	O
thread	O
state	O
around	O
,	O
analogous	O
to	O
monads	O
in	O
other	O
languages	O
.	O
</s>
<s>
DCGs	O
are	O
often	O
used	O
to	O
write	O
parsers	B-Language
or	O
list	O
generators	O
,	O
as	O
they	O
also	O
provide	O
a	O
convenient	O
interface	O
to	O
list	O
differences	O
.	O
</s>
<s>
A	O
larger	O
example	O
will	O
show	O
the	O
potential	O
of	O
using	O
Prolog	B-Language
in	O
parsing	B-Language
.	O
</s>
<s>
This	O
can	O
be	O
written	O
in	O
Prolog	B-Language
using	O
DCGs	O
,	O
corresponding	O
to	O
a	O
predictive	O
parser	O
with	O
one	O
token	O
look-ahead	O
:	O
</s>
<s>
This	O
code	O
defines	O
a	O
relation	O
between	O
a	O
sentence	O
(	O
given	O
as	O
a	O
list	O
of	O
tokens	O
)	O
and	O
its	O
abstract	B-Data_Structure
syntax	I-Data_Structure
tree	I-Data_Structure
(	O
AST	O
)	O
.	O
</s>
<s>
The	O
AST	O
is	O
represented	O
using	O
Prolog	B-Language
terms	O
and	O
can	O
be	O
used	O
to	O
apply	O
optimizations	O
,	O
to	O
compile	O
such	O
expressions	O
to	O
machine-code	O
,	O
or	O
to	O
directly	O
interpret	O
such	O
statements	O
.	O
</s>
<s>
As	O
is	O
typical	O
for	O
the	O
relational	O
nature	O
of	O
predicates	B-Algorithm
,	O
these	O
definitions	O
can	O
be	O
used	O
both	O
to	O
parse	B-Language
and	O
generate	O
sentences	O
,	O
and	O
also	O
to	O
check	O
whether	O
a	O
given	O
tree	O
corresponds	O
to	O
a	O
given	O
list	O
of	O
tokens	O
.	O
</s>
<s>
Using	O
iterative	B-Algorithm
deepening	I-Algorithm
for	O
fair	O
enumeration	O
,	O
each	O
arbitrary	O
but	O
fixed	O
sentence	O
and	O
its	O
corresponding	O
AST	O
will	O
be	O
generated	O
eventually	O
:	O
</s>
