C#Prolog
Prolog for .NET
Prolog is a programming language, and C#Prolog is an interpreter for it, written in C#. Version 7 is rewritten from scratch. Embed Prolog in your C# programs, run it in the browser with Blazor WebAssembly, or use it from the command line.
dotnet add package CSPrologNever used Prolog?
Tell the computer what is true
Welcome to logic programming, a curious world where you don't write the steps. You state facts and rules, and then you ask questions.
const parents = [["tom", "bob"], ["bob", "ann"]];
function grandchildren(name) {
const result = [];
for (const [parent, child] of parents) {
if (parent !== name) continue;
for (const [other, grandchild] of parents) {
if (other === child) result.push(grandchild);
}
}
return result;
}
grandchildren("tom"); // ["ann"]% Fact: Tom is a parent of Bob
parent(tom, bob).
% Fact: Bob is a parent of Ann
parent(bob, ann).
% Rule: a parent of a parent is a grandparent
grandparent(X, Z) :-
parent(X, Y),
parent(Y, Z).
% Question: who is a grandchild of Tom?
?- grandparent(tom, Who).
Who = ann
And the same rule answers the opposite question too. Ask ?- grandparent(Who, ann).
and you get Who = tom.
Learn Prolog from zero Run every example in the page, all the way to the Game of Life.
Try it in your browser
This playground runs C#Prolog with Blazor WebAssembly, right in this page. Nothing is sent to a server.
Welcome to the C#Prolog playground. The database is empty.
Features
ISO Prolog builtins
Control constructs, exceptions, integer and float arithmetic, findall/3, bagof/3, setof/3, assert and retract, atoms, and lists.
Made for C#
Build terms and run queries with a small C# API. Get each solution as you move through the results with foreach.
Runs in the browser
SolveAsync gives control back from time to time, so a long query never freezes a Blazor WebAssembly page.
Prolog text
Consult Prolog programs, run queries written as text, define operators with op/3, and write grammars with DCG.
Safe to stop
Every query can be cancelled with a CancellationToken. The occurs check can be turned on when you need it.
Command line tool
An interactive console with line editing and history, which can also consult Prolog files.
Prolog in a few lines of C#
Consult Prolog text, and run a query. Each solution reads the values of the variables by their names. You can also build terms in C#, without writing Prolog text at all.
using CSProlog;
var engine = new PrologEngine();
engine.Consult("human(socrates).");
engine.Consult("human(plato).");
engine.Consult("mortal(X) :- human(X).");
foreach (var solution in engine.Solve("mortal(Who)."))
{
Console.WriteLine(solution["Who"]); // socrates, then plato
}A Prolog console in your terminal
The csprolog command is a .NET tool with an interactive Prolog console, like the top level of other Prolog systems.
Run it right away with dnx, without installing it.
$ dnx CSProlog.Cli
?- member(X, [a, b]).
X = a ;
X = b ;
false.Thanks to John Pool
C#Prolog was first written by John Pool, who improved it from 2007 to 2015. Version 7 is a new implementation, but it would not exist without his work and the work of all the contributors of the earlier versions.