Did you know ... Search Documentation:
Packs (add-ons) for SWI-Prolog

Package "interpolate"

Title:String interpolation
Rating:Not rated. Create the first rating!
Latest version:0.1.0
SHA1 sum:95146f19b4ff6a59bdd3fb39aa9a42a5af00fead
Author:Michael Hendricks <michael@ndrix.org>
Maintainer:Michael Hendricks <michael@ndrix.org>
Packager:Michael Hendricks <michael@ndrix.org>
Home page:https://github.com/mndrix/interpolate
Download URL:https://github.com/mndrix/interpolate/archive/v0.1.0.zip


No reviews. Create the first review!.

Details by download location



:- use_module(library(interpolate)).
main :-
    Name = "Johannson",
    Time = evening,
    writeln('Hello Mr. $Name. Isn''t it a fine $Time?').


Many languages allow one to construct strings using a built-in, miniature template language in which the values of variables are inserted into an otherwise static template. It's often called string interpolation.

This module provides similar functionality for Prolog. It uses the same syntax as Unix shell, Perl, PHP, Tcl, etc. Namely, a local variable name prefixed with $. Interpolation is supported in all of the following string types:

  • single quoted strings (atoms)
  • double quoted strings (depends on double_quotes flag)
  • backquoted strings (string)

Internally the string is constructed with format/2. If a variable's value is a list of codes or a list of chars (single letter atoms), we use the ~s format sequence; otherwise, ~p. This allows compound terms to define their own string representation via the portray/1 hook.

If a string refers to a variable that doesn't exist, that portion of the string is left alone. This behavior may change. In the future it may generate a warning.

Singleton Warnings

Because interpolation hides variables inside of strings, the compiler can't detect that they're being used. This often results in unwanted singleton warnings. You can some choices in how to resolve the problem:

  • prefix the affected variable with an underscore
  • use library(func) string interpolation
  • :- style_check(-singleton).

Once I add quasiquotation support to this module, that will be another option.

Changes in this Version

  • First public release


Using SWI-Prolog 7.1.1 or later:

?- pack_install(interpolate).

This module uses semantic versioning.

Source code available and pull requests accepted at http://github.com/mndrix/interpolate

- Michael Hendricks <michael@ndrix.org>

Contents of pack "interpolate"

Pack contains 6 files holding a total of 7.9K bytes.