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,Consultthrows aPrologExceptionwith the balldirective_failed(Goal). :- initialization(Goal).runs the goal after the whole text is read.- DCG rules, written with
-->, are turned into ordinary clauses. Use them withphrase/2andphrase/3. - Each call of
Consultadds 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
}VariableNameslists 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 asX = 1, Y = f(_G1), ortruewhen there is nothing to show.SolveAsyncwith 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 bwrite/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, and0'a(a character code). - Floats are written with digits on both sides of the dot, such as
1.5,-0.25,1.0e10, and1.5E-3.1e10and.5are not floats. - The meaning of double-quoted text such as
"abc"depends on thedouble_quotesflag. By default (codes) it is a list of character codes,[97, 98, 99]. Withcharsit is a list of one-character atoms,[a, b, c], and withatomit is the atomabc. 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.