ichiban/prolog
is an embeddable ISO Prolog interpreter in Go.
database/sql
-like Go APIprolog | otto | go-lua | |
---|---|---|---|
Language | ISO Prolog | ECMA Script | Lua |
Paradigm | š Logic | Object-oriented | Object-oriented |
Go API | š» database/sql -like |
original | original |
Declarative | ā | ā | ā |
Sandboxing | ā | ā | ā |
go get -u github.com/ichiban/prolog
p := prolog.New(os.Stdin, os.Stdout) // Or `prolog.New(nil, nil)` if you don't need user_input/user_output.
Or, if you want a sandbox interpreter without any built-in predicates:
// See examples/sandboxing/main.go for details.
p := new(prolog.Interpreter)
if err := p.Exec(`
human(socrates). % This is a fact.
mortal(X) :- human(X). % This is a rule.
`); err != nil {
panic(err)
}
Similar to database/sql
, you can use placeholder ?
to insert Go data as Prolog data.
if err := p.Exec(`human(?).`, "socrates"); err != nil { // Same as p.Exec(`human("socrates").`)
panic(err)
}
sols, err := p.Query(`mortal(?).`, "socrates") // Same as p.Query(`mortal("socrates").`)
if err != nil {
panic(err)
}
defer sols.Close()
// Iterates over solutions.
for sols.Next() {
fmt.Printf("Yes.\n") // ==> Yes.
}
// Check if an error occurred while querying.
if err := sols.Err(); err != nil {
panic(err)
}
Or, if you want to query for the variable values for each solution:
sols, err := p.Query(`mortal(Who).`)
if err != nil {
panic(err)
}
defer sols.Close()
// Iterates over solutions.
for sols.Next() {
// Prepare a struct with fields which name corresponds with a variable in the query.
var s struct {
Who string
}
if err := sols.Scan(&s); err != nil {
panic(err)
}
fmt.Printf("Who = %s\n", s.Who) // ==> Who = socrates
}
// Check if an error occurred while querying.
if err := sols.Err(); err != nil {
panic(err)
}
ichiban/prolog
adheres the ISO standard and comes with the ISO predicates as well as the Prologue for Prolog and DCG predicates.
See the Wiki for the directives and the built-in predicates.
1pl
is an experimental top level command for testing the default language and its compliance to the ISO standard.
You can install it with go install
:
go install github.com/ichiban/prolog/cmd/1pl@latest
Then, you can enter the top level with 1pl
:
$(go env GOPATH)/bin/1pl [<file>...]
Distributed under the MIT license. See LICENSE
for more information.
See ARCHITECTURE.md
for architecture details.
We would like to extend our thanks to the following individuals for their contributions to this project:
We are grateful for the support and contributions of everyone involved in this project. Arigatou gozaimasu!