trealla-prolog is a compact, efficient Prolog interpreter written in C.
This package is intended to provide a wrapper to interact trealla with application written in golang.
With some helper functions, go-trealla makes it simple to calle Prolog from Golang, and go-trallea can be
treated as an embeddable Prolog.
The package is fully go-getable, so, just type
go get github.com/rosbit/go-trealla
to install.
package main
import (
"github.com/rosbit/go-trealla"
"fmt"
)
func main() {
ctx, err := trealla.NewTrealla("/path/to/trealla/exe") // linux executable trealla-prolog(tpl) can be downloaded from releases.
if err != nil {
// error processing
}
...
}Suppose there's a Prolog file named music.pl like this:
listen(ergou, bach).
listen(ergou, beethoven).
listen(ergou, mozart).
listen(xiaohong, mj).
listen(xiaohong, dylan).
listen(xiaohong, bach).
listen(xiaohong, beethoven).one can load the script like this:
if err := ctx.LoadFile("music.pl"); err != nil {
// error processing
} // query Who listens to Music
args := []interface{}{trealla.PlVar("Who"), trealla.PlVar("Music")}
// solution #1: map[string]interface {}{"Music":"bach", "Who":"ergou"}
// solution #2: map[string]interface {}{"Music":"beethoven", "Who":"ergou"}
// solution #3: map[string]interface {}{"Music":"mozart", "Who":"ergou"}
// solution #4: map[string]interface {}{"Music":"mj", "Who":"xiaohong"}
// solution #5: map[string]interface {}{"Music":"dylan", "Who":"xiaohong"}
// solution #6: map[string]interface {}{"Music":"bach", "Who":"xiaohong"}
// solution #7: map[string]interface {}{"Music":"beethoven", "Who":"xiaohong"}
// query Who listens to "bach"
args := []interface{}{trealla.PlVar("Who"), "bach"}
// solution #1: map[string]interface {}{"Who":"ergou"}
// solution #2: map[string]interface {}{"Who":"xiaohong"}
// query Which Music "ergou" listens to
args := []interface{}{"ergou", trealla.PlVar("Music")}
// solution #1: map[string]interface {}{"Music":"bach"}
// solution #2: map[string]interface {}{"Music":"beethoven"}
// solution #3: map[string]interface {}{"Music":"mozart"}
// check whether "ergou" listens to "bach"
args := []interface{}{"ergou", "bach"}
// true solutions, ok, err := ctx.Query("listen", args...) // error checking
if err != nil {
// error processing
return
}
// proving checking with result `false`
if !ok {
// the result is false
return
}
// proving checking with result `true`
if solutions == nil {
// the result is true
return
}
// solutions processing
for sol := range solutions {
fmt.Printf("solution: %#v\n", sol)
}The full usage sample can be found sample/main.go.
The package is not fully tested, so be careful.
Pull requests are welcome! Also, if you want to discuss something send a pull request with proposal and changes.
Convention: fork the repository and make changes on your fork in a feature branch.