Getting started
Install the package
Add the CSProlog package to your project. The packages target .NET 11.
dotnet add package CSPrologYour first query
Create an engine, consult some Prolog text, and run a query.
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
}Consult adds the clauses in the text to the database of the engine. You can call it as many times as you want, and each call adds more clauses.
Solve runs a query and returns its solutions one by one. Moving to the next item of the foreach loop backtracks into the query and finds the next solution. solution["Who"] reads the value of the variable Who.
Load a whole program
Consult accepts any number of clauses, so you can also load a whole program at once.
engine.Consult("""
parent(tom, bob).
parent(bob, ann).
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
""");
var solution = engine.Solve("grandparent(tom, Who).").First();
Console.WriteLine(solution); // Who = annToString() of a solution gives all the values, in the same form as a Prolog top level shows them.
Try it without writing code
- The playground on the home page runs C#Prolog in your browser.
- The command line tool gives you an interactive Prolog console in your terminal.