A small Prolog interpreter in MoonBit: SLD resolution with iterative deepening, cut, negation-as-failure, arithmetic, and list built-ins.
Dependencies
///|
test {
let program_text =
#|parent(john, mary).
#|parent(mary, ann).
#|parent(mary, tom).
#|ancestor(X, Y) :- parent(X, Y).
#|ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
let p = Program::parse(program_text) catch { e => fail(e.message()) }
assert_eq(p.query("ancestor(john, X)").answer_lines(), [
"X = mary", "X = ann", "X = tom",
])
}///|
test {
let p = Program::parse("") catch { e => fail(e.message()) }
assert_eq(p.query("X = f(Y), Y = 2").answer_lines(), ["X = f(2), Y = 2"])
assert_eq(p.query("X is 1 + 2 * 3").answer_lines(), ["X = 7"])
assert_eq(p.query("append(X, Y, [1, 2])").answer_lines(), [
"X = [], Y = [1, 2]", "X = [1], Y = [2]", "X = [1, 2], Y = []",
])
}///|
test {
let p = Program::parse("") catch { e => fail(e.message()) }
let r = p.query("(X = 1; X = 2), write(X), nl")
assert_eq(r.output, "1\n2\n")
assert_eq(r.answer_lines(), ["X = 1", "X = 2"])
}$ moon run cmd/prolog -- --query "member(X, [a, b, c])"
X = a
X = b
X = cpub struct Options {
max_depth : Int
ilds : Bool
max_steps : Int
max_solutions : Int
}fn Program::query_with(self : Program, goal : String, opts : Options) -> QueryResult raise PrologErrorpub struct QueryResult {
solutions : Array[Bindings]
vars : Array[String]
output : String
completion : Completion
}Install
Download zipA small Prolog interpreter in MoonBit: SLD resolution with iterative deepening, cut, negation-as-failure, arithmetic, and list built-ins.
Dependencies