https://github.com/rosbit/go-eprolog
An embeddable prolog for golang based on ichiban/prolog. go语言实现的嵌入式prolog.
https://github.com/rosbit/go-eprolog
embedded-prolog golang prolog
Last synced: 28 days ago
JSON representation
An embeddable prolog for golang based on ichiban/prolog. go语言实现的嵌入式prolog.
- Host: GitHub
- URL: https://github.com/rosbit/go-eprolog
- Owner: rosbit
- License: mit
- Created: 2022-07-18T08:15:31.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-08-09T07:40:46.000Z (almost 4 years ago)
- Last Synced: 2025-01-16T00:24:44.873Z (over 1 year ago)
- Topics: embedded-prolog, golang, prolog
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# An embeddable Prolog
[ichiban/prolog](https://github.com/ichiban/prolog) embeddable scripting language for Go.
This package is intended to extend the `ichiban/prolog` with some helper functions and make it simple to calle Prolog from Golang.
### Usage
The package is fully go-getable, so, just type
`go get github.com/rosbit/go-eprolog`
to install.
#### 1. Instantiate a Prolog interpreter
```go
package main
import (
"github.com/rosbit/go-eprolog"
"fmt"
)
func main() {
ctx := pl.NewProlog()
...
}
```
#### 2. Load a Prolog script
Suppose there's a Prolog file named `music.pl` like this:
```prolog
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:
```go
if err := ctx.LoadFile("music.pl"); err != nil {
// error processing
}
```
#### 3. Prepare arguments and variables
```go
// query Who listens to Music
args := []interface{}{pl.PlVar("Who"), pl.PlVar("Music")}
// query Who listens to "bach"
args := []interface{}{pl.PlVar("Who"), "bach"}
// query Which Music "ergou" listens to
args := []interface{}{"ergou", pl.PlVar("Music")}
// check whether "ergou" listens to "bach"
args := []interface{}{"ergou", "bach"}
```
#### 4. Query the goal with arguments and variables
```go
solutions, ok, err := ctx.Query("listen", args...)
```
#### 5. Check the result
```go
// 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](sample/main.go).
### Status
The package is not fully tested, so be careful.
### Contribution
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.