YAP 7.1.0

This file includes the definition of the formatted output predicates. More...

Detailed Description

This file includes the definition of the formatted output predicates.


Class Documentation

◆ format/2

class format/2

format(+ T, :L)

format(+_Stream_+ T, :ListWithArguments)

format(+ T, :ListWithArguments)

Print formatted output to the current output stream The arguments in list L are output according to the string, list of codes or characters, or by the atom T

A control sequence is introduced by a ~ The following control sequences are available in YAP:

  • ~~ Print a single tilde
  • ~a The next argument must be an atom, that will be printed as if by write
  • ~Nc The next argument must be an integer, that will be printed as a character code The number N is the number of times to print the character (default 1)
  • ~Ne
  • ~NE
  • ~Nf
  • ~Ng
  • ~NG The next argument must be a floating point number The float F, the number N and the control code c will be passed to printf as:
printf("%s.Nc", F)

As an example:

?- format("~8e, ~8E, ~8f, ~8g, ~8G~w",
[3.14,3.14,3.14,3.14,3.14,3.14]).
3.140000e+00, 3.140000E+00, 3.140000, 3.14, 3.143.14
  • ~Nd The next argument must be an integer, and N is the number of digits after the decimal point If N is 0 no decimal points will be printed The default is N = 0
?- format("~2d, ~d",[15000, 15000]).
150.00, 15000
  • ~ND Identical to ~Nd, except that commas are used to separate groups of three digits
?- format("~2D, ~D",[150000, 150000]).
1,500.00, 150,000
  • ~i Ignore the next argument in the list of arguments:
?- format('The ~i met the boregrove',[mimsy]).
The met the boregrove
  • ~k Print the next argument with write_canonical:
?- format("Good night ~k",a+[1,2]).
Good night +(a,[1,2])
  • ~Nn Print N newlines (where N defaults to 1)
  • ~NN Print N newlines if at the beginning of the line (where N defaults to 1)
  • ~Nr The next argument must be an integer, and N is interpreted as a radix, such that 2 <= N <= 36 (the default is 8)
?- format("~2r, 0x~16r, ~r",
[150000, 150000, 150000]).
100100100111110000, 0x249f0, 444760

Note that the letters a-z denote digits larger than 9

  • ~NR Similar to ~NR The next argument must be an integer, and N is interpreted as a radix, such that 2 <= N <= 36 (the default is 8)
?- format("~2r, 0x~16r, ~r",
[150000, 150000, 150000]).
100100100111110000, 0x249F0, 444760

The only difference is that letters A-Z denote digits larger than 9

  • ~p Print the next argument with print/1 :
?- format("Good night ~p",a+[1,2]).
Good night a+[1,2]
  • ~q Print the next argument with writeq/1 :
?- format("Good night ~q",'Hello'+[1,2]).
Good night 'Hello'+[1,2]
  • ~Ns The next argument must be a list of character codes.The system then outputs their representation as a string, where N is the maximum number of characters for the string ( N defaults to the length of the string)
?- format("The ~s are ~4s",["woods","lovely"]).
The woods are love
  • ~w Print the next argument with write/1 :
?- format("Good night ~w",'Hello'+[1,2]).
Good night Hello+[1,2]
  • ~W Give the next two arguments to write_term/2 The first is the term to print, and the second is a list of write_term/2 options For example:
format(string(S), '~W', [Term, [singletons(true)]]).

This option is SWI-Prolog specific

The number of arguments, N, may be given as an integer, or it may be given as an extra argument The next example shows a small procedure to write a variable number of a characters:

write_many_as(N) :-
format("~*c",[N,0'a]).

The format/2 built-in also allows for formatted output One can specify column boundaries and fill the intermediate space by a padding character:

  • ~N| Set a column boundary at position N, where N defaults to the current position
  • ~N+ Set a column boundary at N characters past the current position, where N defaults to 8
  • ~Nt Set padding for a column, where N is the fill code (default is SPC)

The next example shows how to align columns and padding We first show left-alignment:

?- format("~n*Hello~16+*~n",[]).
*Hello *

Note that we reserve 16 characters for the column

The following example shows how to do right-alignment:

?- format("*~tHello~16+*~n",[]).
Hello*

The ~t escape sequence forces filling before Hello

We next show how to do centering:

?- format("*~tHello~t~16+*~n",[]).
Hello *

The two ~t escape sequence force filling both before and after Hello Space is then evenly divided between the right and the left sides

  • ~@ Evaluate the next argument as a goal whose standard output is directed to the stream used by format/2

Print formatted output to the current output stream

Print formatted output to the stream Stream

◆ with_output_to/2

class with_output_to/2

with_output_to(+ Ouput,: Goal)

Run Goal as once/1 , while characters written to the current output are sent to Output The predicate was introduced by SWI-Prolog

The example below defines the DCG rule term/3 to insert a term in the output:

term(Term, In, Tail) :-
with_output_to(codes(In, Tail), write(Term)).
?- phrase(term(hello), X).
X = [104, 101, 108, 108, 111]
  • A Stream handle or alias Temporary switch current output to the given stream Redirection using with_output_to/2 guarantees the original output is restored, also if Goal fails or raises an exception See also call_cleanup/2
  • atom(- Atom) Create an atom from the emitted characters Applications should generally avoid creating atoms by breaking and concatenating other atoms as the creation of large numbers of intermediate atoms puts pressure on the atom table and the data-base This may lead to collisions in the hash tables used to implement atoms, and may result in frequent calls to the garbage collector In multi-threaded applications, access to the atom table is controlled by locks This predicate supports creating the therms by expanding difference-list
  • string(- String) Create a string-object, notice that strings are atomic objects
  • codes(- Codes) Create a list of character codes from the emitted characters, similar to atom_codes/2
  • codes(- Codes, - Tail) Create a list of character codes as a difference-list
  • chars(- Chars) Create a list of one-character-atoms codes from the emitted characters, similar to atom_chars/2
  • chars(- Chars, - Tail) Create a list of one-character-atoms as a difference-list

Macro Definition Documentation

◆ TMP_STRING_SIZE

#define TMP_STRING_SIZE   1024

Definition at line 420 of file format.c.

◆ TOO_FEW_ARGUMENTS

#define TOO_FEW_ARGUMENTS (   Needs,
  Has_Repeats 
)
Value:
if (targ > tnum - Needs || Has_Repeats) {\
format_clean_up(sno, sno0, finfo);\
Yap_ThrowError(DOMAIN_ERROR_FORMAT_CONTROL_SEQUENCE, MkIntTerm(fptr-fstr), "command %c in format string %s has no arguments %s", ch,\
fstr, fptr);\
}

Definition at line 443 of file format.c.

Function Documentation

◆ Yap_InitFormat()

void Yap_InitFormat ( void  )

Definition at line 1240 of file format.c.

Macros

#define TMP_STRING_SIZE   1024
 
#define TOO_FEW_ARGUMENTS(Needs, Has_Repeats)
 

Functions

void Yap_InitFormat (void)