<!--H3: Section A.1-->
(apx:a.1)=
# Built-in predicates #

*Term manipulation*

|    |    |
|:---|:---|
| `Term1 = Term2` | `Term1` and `Term2` are unified. |
| `Term1 \= Term2` | `Term1` and `Term2` cannot be unified. |
| `Term1 == Term2` | `Term1` and `Term2` are bound to the same term. |
| `Term1 \== Term2` | `Term1` and `Term2` are bound to different terms. |
| `var(V)` | `V` is an unbound variable. |
| `arg(N,T,A)` | `A` is the `N`-th argument of term `T`. |
| `functor(T,F,N)` | `T` is a term with functor `F` and arity `N`. |
| `Term =.. List` | `List` is a list starting with the functor of `Term`, followed by its arguments. |
| `varsin(Term,Vs)` | `Vs` is a list of the variables in `Term`. |
| `numbervars(T,N,M)` | the variables in term `T` are instantiated to terms of the form `'$VAR'(` *n* `)`, where *n* is an integer which has a different value for each distinct variable. The variables will be numbered starting from `N`, and `M` is the next unused number.|

+++

*Database manipulation*

|    |    |
|:---|:---|
| `assert(Clause)` | `Clause` is added at the end of the database (can also be a single atom, without a body). |
| `asserta(Clause)` | `Clause` is added at the *top* of the database. |
| `clause(H,B)` | `H:-B` is a clause in the database (`H` must be instantiated). |
| `retract(Clause)` | the first clause unifying with `Clause` is removed from the database. |
| `retractall(Head)` | all clauses with head unifying with `Head` are removed from the database. |
| `kill(Predicate)` | all clauses defining `Predicate` are removed from the database. |
| `save(File)` | save the clauses in the database to `File`. |
| `consult(File)` | load the clauses in `File` into the database. |
| `op(P,Type,Name)` | declare an operator `Name` with priority `P` (a number between 0 and 1200, lower priority binds stronger); `Type` is `fx` or `fy` for prefix, `xfx`, `xfy` or `yfx` for infix, and `xf` or `yf` for postfix. |

+++

*Control*

|    |    |
|:---|:---|
| `call(Goal)` | call `Goal` (must be instantiated to a goal). |
| `not(Goal)` | `Goal` is not provable (must be instantiated to a goal).<br>`not/1` could be defined as <ul><li><tt>not(Goal):-call(Goal),!,fail.</tt></li><li><tt>not(Goal).</tt></li></ul> |
| `fail`, `false` | forces failure. |
| `true`, `otherwise` | always succeeds.<br>`true/0` could be defined as <ul><li><tt>true.</tt></li></ul> |
| `repeat` | succeeds indefinitely many times on backtracking.<br>`repeat/0` could be defined as <ul><li><tt>repeat.</tt></li><li><tt>repeat:-repeat.</tt></li></ul> |
| `findall(X,G,L)` | `L` is a list of `X`'s, one for each solution of `G` (succeeds with the empty list if no solutions are found). |
| `bagof(X,G,L)` | `L` is a list of `X`'s, one for each solution of `G`, which may be preceded by existential variables (fails if no solutions are found). |
| `setof(X,G,L)` | as `bagof/3`, but `L` is a sorted list without duplicates. |
| `forall(G,C)` | for all the solutions of `G`, `C` is true.<br>`forall/2` could be defined as <ul><li><tt>forall(G,C):-not((G,not(C))).</tt></li></ul> |

+++

*Interaction*

|    |    |
|:---|:---|
| `read(Term)` | `Term` is instantiated to the next line typed by the user (must be a Prolog term). |
| `write(Term)` | write `Term` to the screen. |
| `tab(N)` | write `N` spaces to the screen. |
| `nl` | write a newline to the screen. |
| `get(C)` | `C` is ASCII code of next character typed by the user. |
| `put(C)` | write character with ASCII code `C` to the screen. |
| `tell(File)` | redirect output to `File`. |
| `told` | stop redirecting output. |
| `see(File)` | redirect input from `File`. |
| `seen`. | stop redirecting input. |
