11.3.82 foreign/[2,3] [hook]


:- discontiguous foreign/2, foreign/3.

foreign(+Routine, +ForeignSpec)

foreign(+Routine, +Language, +ForeignSpec)

Describes the interface between Prolog and the foreign Routine. Used by load_foreign_resource/1.


atom, must be nonvar

An atom that names a foreign code Routine.

atom, must be nonvar

An atom that names the Language in which Routine is written. Can only be c.

foreign_spec, must be ground

A term of the form PredName(Argspec, Argspec, ...) as described in Conversions between Prolog Arguments and C Types.


The user has to define a foreign/[2,3] fact for every foreign function that is to be called from Prolog. Note that Routine does not have to be the same as PredicateName. Arguments are passed to the foreign function as specified in ForeignSpec.

specifies that an argument is to be passed to the foreign function.
specifies that an argument is to be received from the foreign function.
argument is used to obtain the return value of a foreign function call. At most one “return value” argument can be specified.

The foreign/[2,3] facts are used only in the context of a load_foreign_resource/1 command and can be removed once the foreign files are loaded.

Contrary to most hook predicates which reside in the user module, foreign/[2,3] facts will only be looked up in the source module of the loading command.

See Also

load_foreign_resource/1, Calling C from Prolog.

Send feedback on this subject.