CSProlog.Cli 7.0.0

dotnet tool install --global CSProlog.Cli --version 7.0.0
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local CSProlog.Cli --version 7.0.0
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=CSProlog.Cli&version=7.0.0
                    
nuke :add-package CSProlog.Cli --version 7.0.0
                    

C#Prolog NuGet Package unit tests

A Prolog interpreter written in C#, for .NET. It is published on NuGet as CSProlog.

About v7

C#Prolog v7 is a complete rewrite, written from scratch.

It keeps the name C#Prolog, but it is a different library from v6 and earlier. The engine, the term types, and the C# API were designed again for modern .NET. It does not contain the code of the old implementation, and it is not compatible with the old API. Code that uses v6 or earlier needs changes to move to v7.

The old implementation is not gone. Its source code and its whole commit history, including the work of John Pool, stay available on the vnext/master branch and in the other branches of this repository. Its documents are kept in the legacy folder of this branch.

The same is true for the CSProlog package on NuGet. Version 7 and later of the package contain this new implementation, and version 6 and earlier contain the old one. If you update the CSProlog package from version 6 to version 7, expect to change your code.

Features

  • A core engine with the main ISO Prolog builtin predicates, such as control constructs, catch/3 and throw/1, arithmetic, findall/3, bagof/3, setof/3, assertz/1, retract/1, atom predicates, and list predicates.
  • A separate package that reads and writes Prolog text, with user-defined operators (op/3) and DCG rules (-->).
  • A C# API to build terms and run queries, without writing Prolog text.
  • Queries that can be cancelled with a CancellationToken.
  • SolveAsync, which gives control back to the caller from time to time. On a single thread, such as in Blazor WebAssembly, the UI keeps working during a long query.
  • Integers of any size, and floating point numbers.
  • A command line tool, csprolog, with an interactive Prolog console.

Packages

Package What it contains
CSProlog A package that brings in both CSProlog.Core and CSProlog.Syntax. Start with this one.
CSProlog.Core The engine, the term types, and the builtin predicates.
CSProlog.Syntax Reading and writing Prolog text, Consult, and queries written as text.
CSProlog.Cli The csprolog command line tool (a .NET tool). See Command line tool.

If you build terms only in C# and never read Prolog text, CSProlog.Core alone is enough.

Documentation

The C#Prolog site has the documents, a playground where you can try Prolog in your browser, and a tutorial for people who are new to Prolog.

Installation

Add the CSProlog package to your project.

dotnet add package CSProlog

The packages target .NET 11. They are designed to work in Blazor WebAssembly too.

Getting started

Run a query written as Prolog text.

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
}

Build terms and run a query in C#, without Prolog text.

using CSProlog;

var engine = new PrologEngine();
engine.AddClause(new Compound("human", new Atom("socrates")));

var who = new Var("Who");
foreach (var solution in engine.Solve(new Compound("human", who)))
{
    Console.WriteLine(solution[who]); // socrates
}

Run a query without blocking the UI.

await foreach (var solution in engine.SolveAsync("between(1, 3, X)."))
{
    Console.WriteLine(solution); // X = 1, X = 2, X = 3
}

Command line tool

CSProlog.Cli is a .NET tool with an interactive Prolog console, like the top level of other Prolog systems. Run it without installing it.

dnx CSProlog.Cli

Or install it, and run the csprolog command.

dotnet tool install -g CSProlog.Cli
csprolog

Type a query that ends with a period. After each answer, press ; for the next answer, or Enter to stop.

?- member(X, [a, b]).
X = a ;
X = b ;
false.
  • Files given on the command line are consulted first, such as csprolog family.pl. In the console, consult(family). and [family]. consult a file too.
  • With --input-mode consult, the console adds what you type to the database, and shows what was added. Input that starts with ?- is still a query.
  • The arrow keys bring back earlier input. The history is saved in ~/.csprolog/history.
  • Ctrl+C stops a running query. halt. ends the console.
  • dnx itself also has options such as --version and --help. To pass them to the tool, put -- before them, such as dnx CSProlog.Cli -- --version.

Building and testing

The projects target .NET 11. To build and run all tests, run these commands at the repository root.

dotnet build
dotnet test

Design

DESIGN-DECISIONS.md records the design decisions of the rewrite, the open questions, and the known limitations.

History and acknowledgments

C#Prolog was first written by John Pool of Amersfoort, the Netherlands, who improved it from 2007 to 2015. It grew into a reliable and fairly fast interpreter, with a command line interface, DCG, XML and JSON predicates, SQL predicates, and much more. Version 4.1, released in September 2015, was his last version.

In 2015, J. Sakamoto imported C#Prolog 4.1 into this GitHub repository and continued to maintain it. Versions 5 and 6 were published on NuGet as the CSProlog package, with support for .NET Standard and .NET Core.

Many people helped along the way. Josh Ahlstrom (@poolofthought) added the cross-platform console interpreter for .NET Core, unit tests, and many fixes. @michelvosje added the installation guide and the release notes to the documentation. Many more people reported issues and sent ideas and pull requests. See the contributors page of this repository.

v7 is a new implementation, but it would not exist without all this work. Many thanks to John Pool, who created C#Prolog and shared it with everyone, and to all the contributors of the earlier versions.

License

The rewrite is licensed under the Mozilla Public License 2.0. The old implementation was licensed under the GNU LGPL v3 (see legacy/LICENSE).

The CSProlog.Cli package contains third-party libraries. See the third party notices for their licenses.

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last Updated
7.0.0 0 12/5/2026

v.7.0.0
- Feature: Rewrote C#Prolog from scratch for .NET 11, now under the Mozilla Public License 2.0. The API is not compatible with version 6 and earlier.
- Obsolete: Removed many predicates of version 6, such as the SQL, XML, JSON, and date and time predicates. Version 7 starts with the core of ISO Prolog.
- Feature: Split the library into CSProlog.Core (the engine) and CSProlog.Syntax (reading and writing Prolog text). The CSProlog package brings in both.
- Feature: Added a C# API to build terms and run queries without writing Prolog text.
- Feature: Added SolveAsync, which keeps the UI working during a long query, such as in Blazor WebAssembly.
- Feature: Added cancellation of a running query with a CancellationToken.
- Feature: Provided the main ISO Prolog predicates for control, exceptions, collecting solutions, the database, atoms, and lists.
- Feature: Provided integers of any size, and floating point numbers with functions such as sqrt, sin, round, and pi.
- Feature: Provided reading and writing of Prolog text, with user-defined operators (op/3) and DCG rules.
- Feature: Added the double_quotes flag, which reads text such as "abc" as character codes (default), chars, or an atom.
- Feature: Added the occurs check, which can be turned on and off from C# or with set_prolog_flag/2.
- Feature: Added tracing with trace/0, spy/1, and the Tracer property of the engine, and the listing/0 and listing/1 predicates.
- Feature: Added the csprolog command line tool (the CSProlog.Cli package), a Prolog console that can consult files and trace step by step.
- Feature: Added a website with documents, a playground, and a Learn Prolog tutorial (https://jsakamoto.github.io/CSharpProlog/).

To see all the change logs, please visit the following URL.
- https://github.com/jsakamoto/CSharpProlog/blob/vnext7/main/RELEASE-NOTES.txt