Prolog text

The CSProlog.Syntax package reads and writes Prolog text. It adds extension methods to PrologEngine, in the CSProlog namespace.

Consulting text

Consult reads the clauses and directives in the text, and adds the clauses to the database.

engine.Consult("""
    :- dynamic(counter/1).
    counter(0).
    greeting --> [hello], name.
    name --> [world].
    """);
  • A directive, :- Goal., runs once while the text is read. If it fails, Consult throws a PrologException with the ball directive_failed(Goal).
  • :- initialization(Goal). runs the goal after the whole text is read.
  • DCG rules, written with -->, are turned into ordinary clauses. Use them with phrase/2 and phrase/3.
  • Each call of Consult adds more clauses. Consulting the same text twice adds the clauses twice.

Queries written as text

Solve with a string runs a query written as Prolog text. The final period can be left out. Each solution reads the values of the variables by their names.

foreach (var solution in engine.Solve("member(X, [a, b])."))
{
    Console.WriteLine(solution["X"]);  // a, then b
    Console.WriteLine(solution);       // X = a, then X = b
}
  • VariableNames lists the names of the variables in the query. Names that start with an underscore are left out.
  • ToString() shows the values like a Prolog top level, such as X = 1, Y = f(_G1), or true when there is nothing to show.
  • SolveAsync with a string does the same without blocking the UI.

Syntax errors

A syntax error throws a PrologSyntaxException. It is a PrologException, and it also has Line and Column, which tell where the error starts.

try
{
    engine.Consult("p(1).\np(2");
}
catch (PrologSyntaxException exception)
{
    Console.WriteLine($"{exception.Line}:{exception.Column}"); // 2:4
}

Clauses read before the error stay in the database.

Operators

The usual operators of ISO Prolog are defined from the start. op/3 defines more, and they apply to the text that is read after it. Each engine has its own operators.

:- op(700, xfx, likes).
john likes mary.

Writing terms

FormatTerm writes a term as Prolog text, with the operators of the engine. The text can be read back to the same term. A float is written with the shortest digits that read back to the same value, and always with a fraction, such as 2.0, 0.1, and 1.0e20.

var term = new Compound("+", new Integer(1), new Atom("a b"));
Console.WriteLine(engine.FormatTerm(term));                 // 1+'a b'
Console.WriteLine(engine.FormatTerm(term, quoted: false));  // 1+a b

write/1 and the other output predicates write to Console.Out. SetOutput changes where they write to.

var output = new StringWriter();
engine.SetOutput(output);
engine.Solve("write(hello), nl.").ToList();

Tokens

  • Variables start with an uppercase letter or _, followed by letters, digits, or _, all in ASCII.
  • Unquoted atoms can also use other Unicode letters, such as café.
  • Integers can be written as 42, -7, 0x1F, 0o17, 0b101, and 0'a (a character code).
  • Floats are written with digits on both sides of the dot, such as 1.5, -0.25, 1.0e10, and 1.5E-3. 1e10 and .5 are not floats.
  • The meaning of double-quoted text such as "abc" depends on the double_quotes flag. By default (codes) it is a list of character codes, [97, 98, 99]. With chars it is a list of one-character atoms, [a, b, c], and with atom it is the atom abc. Escapes and doubled quotes work as in quoted atoms. A directive such as :- set_prolog_flag(double_quotes, chars). changes how the text after it is read.
An unhandled error has occurred. Reload 🗙