Yield Prolog




Query Editor


Report Bugs

Sourceforge Project
Yield Prolog lets you embed Prolog programs directly in Python, C# [1] or Javascript [2] by using the yield keyword. For example, here is the classic "uncle" predicate in Prolog:

uncle(Person, Uncle) :-
parent(Person, Parent),
brother(Parent, Uncle).

(This says that a person has an uncle if the person has a parent and that parent has a brother.) And here it is from the Yield Prolog compiler:

def uncle(Person, Uncle):
Parent = Variable()
for l1 in parent(Person, Parent):
for l2 in brother(Parent, Uncle):
yield False
IEnumerable<bool> uncle(object Person, object Uncle) {
Variable Parent = new Variable();
foreach (bool l1 in parent(Person, Parent)) {
foreach (bool l2 in brother(Parent, Uncle))
yield return false;
function* uncle(Person, Uncle) {
var Parent = new Variable();
for (let l1 of parent(Person, Parent)) {
for (let l2 of brother(Parent, Uncle))
yield false;

As you can see, the flow of the code in Yield Prolog is similar to Prolog. The Tutorial explains how these examples work, without expecting you to know Prolog. And the benchmarks show that Yield Prolog in C# can be faster than efficient Prolog systems like Yap Prolog and XSB.

Yield Prolog is made possible by the yield keyword, which automatically creates iterators that you can nest, combined with Yield Prolog's Variable class which can unify a variable with other values (just like in Prolog). There is no "API" standing between your code and Yield Prolog, because you just use the yield keyword to make "iterator functions" wherever you need them. Yield Prolog is part of your code, which can mix Prolog-style predicates directly with ordinary arrays, file I/O, GUI calls and all your own classes. Because it lets you mix these, Yield Prolog unifies the declarative and procedural programming models.

1. Tested with C# .NET 2008+ and Mono 2.0+.
2. Yield Prolog for Javascript is supported in Firefox and Seamonkey, which fully support the yield keyword in EcmaScript 3.1.