- Reference manual
- Built-in Predicates
- Notation of Predicate Descriptions
- Character representation
- Loading Prolog source files
- Editor Interface
- List the program, predicates or clauses
- Verify Type of a Term
- Comparison and Unification of Terms
- Control Predicates
- Meta-Call Predicates
- Delimited continuations
- Exception handling
- Handling signals
- DCG Grammar rules
- Declaring predicate properties
- Examining the program
- Input and output
- Status of streams
- Primitive character I/O
- Term reading and writing
- Analysing and Constructing Terms
- Analysing and Constructing Atoms
- Localization (locale) support
- Character properties
- Character Conversion
- Misc arithmetic support predicates
- Built-in list operations
- Finding all Solutions to a Goal
- Formatted Write
- Global variables
- Terminal Control
- Operating System Interaction
- File System Interaction
- User Top-level Manipulation
- Creating a Protocol of the User Interaction
- Debugging and Tracing Programs
- Obtaining Runtime Statistics
- Execution profiling
- Memory Management
- Windows DDE interface
- Built-in Predicates
- Reference manual
The current version of SWI-Prolog provides two formatted write predicates. The‘writef' family (writef/1, writef/2, swritef/3), is compatible with Edinburgh C-Prolog and should be considered deprecated. The‘format' family (format/1, format/2, format/3), was defined by Quintus Prolog and currently available in many Prolog systems, although the details vary.
- Equivalent to
writef(Atom, ).See writef/2 for details.
- [deprecated]writef(+Format, +Arguments)
- Formatted write. Format is an atom whose characters will be
Format may contain certain special character sequences which
specify certain formatting and substitution actions. Arguments
provides all the terms required to be output.
Escape sequences to generate a single special character:
Output a newline character (see also nl/[0,1])
Output a line separator (same as
Output a carriage return character (ASCII 13)
Output the ASCII character TAB (9)
where <nnn> is an integer (1-3 digits); the character with code <nnn> is output (NB : <nnn> is read as decimal)
\\are interpreted differently when character escapes are in effect. See section 184.108.40.206.
Escape sequences to include arguments from Arguments. Each time a % escape sequence is found in Format the next argument from Arguments is formatted according to the specification.
print/1 the next item (mnemonic: term)
write/1 the next item
writeq/1 the next item
Write the term, ignoring operators. See also write_term/2. Mnemonic: old Edinburgh display/1
print/1 the next item (identical to
Put the next item as a character (i.e., it is a character code)
Write the next item N times where N is the second item (an integer)
Write the next item as a String (so it must be a list of characters)
Perform a ttyflush/0 (no items used)
Write the next item Centered in N columns
Write the next item Left justified in N columns
Write the next item Right justified in N columns. N is a decimal number with at least one digit. The item must be an atom, integer, float or string.
- [deprecated]swritef(-String, +Format, +Arguments)
- Equivalent to writef/2,
but “writes'' the result on String instead of the
current output stream. Example:
?- swritef(S, '%15L%w', ['Hello', 'World']). S = "Hello World"
- [deprecated]swritef(-String, +Format)
- Equivalent to
swritef(String, Format, ).
The format family of predicates is the most versatile and portable135Unfortunately not covered by any standard. way to produce textual output.
- Defined as‘
format(Format) :- format(Format, ).’. See format/2 for details.
- format(+Format, :Arguments)
- Format is an atom, list of character codes, or a Prolog
Arguments provides the arguments required by the format
specification. If only one argument is required and this single argument
is not a list, the argument need not be put in a list. Otherwise the
arguments are put in a list.
Special sequences start with the tilde (
), followed by an optional numeric argument, optionally followed by a colon modifier (:), 136The colon modifiers is a SWI-Prolog extension, proposed by Richard O'Keefe. followed by a character describing the action to be undertaken. A numeric argument is either a sequence of digits, representing a positive decimal number, a sequence
‘<character>, representing the character code value of the character (only useful for
~t) or a asterisk (
), in which case the numeric argument is taken from the next argument of the argument list, which should be a positive integer. E.g., the following three examples all pass 46 (
?- format('~w ~46t ~w~72|~n', ['Title', 'Page']). ?- format('~w ~`.t ~w~72|~n', ['Title', 'Page']). ?- format('~w ~*t ~w~72|~n', ['Title', 46, 'Page']).
Numeric conversion (
G) accept an arithmetic expression as argument. This is introduced to handle rational numbers transparently (see section 220.127.116.11). The floating point conversions allow for unlimited precision for printing rational numbers in decimal form. E.g., the following will write as many 3's as you want by changing the‘50'.
?- format('~50f', [10 rdiv 3]). 3.33333333333333333333333333333333333333333333333333
Output the tilde itself.
Output the next argument, which must be an atom. This option is equivalent to w, except that it requires the argument to be an atom.
Interpret the next argument as a character code and add it to the output. This argument must be a valid Unicode character code. Note that the actually emitted bytes are defined by the character encoding of the output stream and an exception may be raised if the output stream is not capable of representing the requested Unicode character. See section 2.19.1 for details.
Output next argument as a decimal number. It should be an integer. If a numeric argument is specified, a dot is inserted argument positions from the right (useful for doing fixed point arithmetic with integers, such as handling amounts of money).
The colon modifier (e.g.,
~:d) causes the number to be printed according to the locale of the output stream. See section 4.23.
Same as d, but makes large values easier to read by inserting a comma every three digits left or right of the dot. This is the same as
~:d, but using the fixed English locale.
Output next argument as a floating point number in exponential notation. The numeric argument specifies the precision. Default is 6 digits. Exact representation depends on the C library function printf(). This function is invoked with the format
Equivalent to e, but outputs a capital E to indicate the exponent.
Floating point in non-exponential notation. The numeric argument defines the number of digits right of the decimal point. If the colon modifier (:) is used, the float is formatted using conventions from the current locale, which may define the decimal point as well as grouping of digits left of the decimal point.
Floating point in e or f notation, whichever is shorter.
Floating point in E or f notation, whichever is shorter.
Ignore next argument of the argument list. Produces no output.
Emit a decimal number using Prolog digit grouping (the underscore,
_). The argument describes the size of each digit group. The default is 3. See also section 18.104.22.168. For example:
?- A is 1<<100, format('~10I', [A]). 1_2676506002_2822940149_6703205376
Give the next argument to write_canonical/1.
Output a newline character.
Only output a newline if the last character output on this stream was not a newline. Not properly implemented yet.
Give the next argument to print/1.
Give the next argument to writeq/1.
Print integer in radix numeric argument notation. Thus
~16rprints its argument hexadecimal. The argument should be in the range [2, ... , 36]. Lowercase letters are used for digits above 9. The colon modifier may be used to form locale-specific digit groups.
Same as r, but uses uppercase letters for digits above 9.
Output text from a list of character codes or a string (see string/1 and section 5.2) from the next argument.137The s modifier also accepts an atom for compatibility. This is deprecated due to the ambiguity of
Interpret the next argument as a goal and execute it. Output written to the
current_outputstream is inserted at this place. Goal is called in the module calling format/3. This option is not present in the original definition by Quintus, but supported by some other Prolog systems.
All remaining space between 2 tab stops is distributed equally over
~tstatements between the tab stops. This space is padded with spaces by default. If an argument is supplied, it is taken to be the character code of the character used for padding. This can be used to do left or right alignment, centering, distributing, etc. See also
~+to set tab stops. A tab stop is assumed at the start of each line.
Set a tab stop on the current position. If an argument is supplied set a tab stop on the position of that argument. This will cause all
~t’s to be distributed between the previous and this tab stop.
Set a tab stop (as
~|) relative to the last tab stop or the beginning of the line if no tab stops are set before the
~+. This constructs can be used to fill fields. The partial format sequence below prints an integer right-aligned and padded with zeros in 6 columns. The ... sequences in the example illustrate that the integer is aligned in 6 columns regardless of the remainder of the format specification.
format('...~|~`0t~d~6+...', [..., Integer, ...])
Give the next argument to write/1.
Give the next two arguments to write_term/2. For example,
format('~W', [Term, [numbervars(true)]]). This option is SWI-Prolog specific.
simple_statistics :- <obtain statistics> % left to the user format('~tStatistics~t~72|~n~n'), format('Runtime: ~`.t ~2f~34| Inferences: ~`.t ~D~72|~n', [RunT, Inf]), ....
Statistics Runtime: .................. 3.45 Inferences: .......... 60,345
- format(+Output, +Format, :Arguments)
- As format/2,
but write the output on the given Output. The de-facto
standard only allows Output to be a stream. The SWI-Prolog
implementation allows all valid arguments for
versions defined sformat/3 . These predicates have been moved to the
library(backcomp). For example:
?- format(atom(A), '~D', ). A = '1,000,000'
- format_predicate(+Char, +Head)
- If a sequence
~c(tilde, followed by some character) is found, the format/3 and friends first check whether the user has defined a predicate to handle the format. If not, the built-in formatting rules described above are used. Char is either a character code or a one-character atom, specifying the letter to be (re)defined. Head is a term, whose name and arity are used to determine the predicate to call for the redefined formatting character. The first argument to the predicate is the numeric argument of the format command, or the atom
defaultif no argument is specified. The remaining arguments are filled from the argument list. The example below defines
~Tto print a timestamp in ISO8601 format (see format_time/3). The subsequent block illustrates a possible call.
:- format_predicate('T', format_time(_Arg,_Time)). format_time(_Arg, Stamp) :- must_be(number, Stamp), format_time(current_output, '%FT%T%z', Stamp).
?- get_time(Now), format('Now, it is ~T~n', [Now]). Now, it is 2012-06-04T19:02:01+0200 Now = 1338829321.6620328.
- current_format_predicate(?Code, ?:Head)
- True when
Code is handled by the user-defined predicate specified by Head.