https://github.com/facticiusvir/backtraq
https://github.com/facticiusvir/backtraq
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/facticiusvir/backtraq
- Owner: FacticiusVir
- Created: 2016-11-04T19:01:51.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-04-17T07:33:11.000Z (about 6 years ago)
- Last Synced: 2025-08-16T08:41:10.846Z (10 months ago)
- Language: C#
- Size: 80.1 KB
- Stars: 13
- Watchers: 3
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# BacktraQ
BacktraQ is a framework for running Prolog-like queries with backtracking, choicepoints & variable unification within managed C#.
The library has no external dependencies and runs as pure managed code, to allow it to be used in limited/sandbox domains like Unity3D.
## Usage
BacktraQ has two stages - building queries, and running them. Building queries is designed to look like a native extension to C# syntax, so logic program code is inline with regular imperative statements. Various operators have been overloaded to support this - for example, `<=` is used as the unification operator.
```c#
// Create unbound variables
var a = new Var();
var b = new Var();
// Create query as "unify a with b, and unify b with 123"
var query = a <= b & b <= 123;
```
The query doesn't execute and no variables are set/unified until you test for success:
```c#
if (query.Succeeds())
{
Console.WriteLine($"a = {a}");
Console.WriteLine($"b = {b}");
}
```
If a query may have multiple solutions, it can be iterated over and the value of each variable will update:
```c#
// Create unbound variable
var a = new Var();
// Create query as "unify a with 'a', or 'b', or 'c'"
var query = a <= 'a' | a <= 'b' | a <= 'c';
foreach (var result in query)
{
Console.WriteLine($"a = {a}");
}
```
## Contributing
Pull requests are welcome!
## License
[MIT](https://choosealicense.com/licenses/mit/)