prolog

package module
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2021 License: MIT Imports: 9 Imported by: 32

README

prolog - the only reasonable scripting engine for Go

Go Reference Actions Status Go Report Card codecov

What is this?

ichiban/prolog is an embeddable scripting language for Go. Unlike any other scripting engines, ichiban/prolog implements logic programming language Prolog.

  • Easy to reason about: based on first-order logic
  • Easy to adopt: database/sql-like Go API
  • Intelligent: full-featured Prolog implementation
  • Highly customizable: sandboxing, custom predicates

ichiban/prolog vs otto vs go-lua

prolog otto go-lua
Language ISO Prolog ECMA Script Lua
Paradigm 🎓 Logic Object-oriented Object-oriented
Go API 😻 database/sql-like original original
Declarative
Sandboxing

Getting started

Install latest version
go get -u github.com/ichiban/prolog@latest
Usage
Instantiate an interpreter
p := prolog.New(nil, nil)

Or, if you want to expose standard input/output to your interpreter:

p := prolog.New(os.Stdin, os.Stdout)

Or, if you want a secure interpreter without any builtin predicates:

// See examples/sandboxing/main.go for details.
p := new(prolog.Interpreter)
Load a Prolog program
if err := p.Exec(`
	:- [abc].              % Load file 'abc' or 'abc.pl'.

	:- ['/path/to/abc'].   % Load file '/path/to/abc' or '/path/to/abc.pl'.
	                       % You need to quote to contain / in the path.

	:- [library(dcg)].     % Load library 'library(dcg)'.
	                       % See examples/dcg/main.go for a complete example.

	human(socrates).       % This is a fact.
	mortal(X) :- human(X). % This is a rule.
`); err != nil {
	panic(err)
}
Run the Prolog program
// Prolog program invocation takes a form of query.
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
}

License

Distributed under the MIT license. See LICENSE for more information.

Contributing

See ARCHITECTURE.md for architecture details.

  1. Fork it (https://github.com/ichiban/prolog/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("closed")

ErrClosed indicates the Solutions are already closed and unable to perform the operation.

Functions

func Register added in v0.5.0

func Register(name string, library func(*Interpreter) error)

Register registers a library. if there's a library with the same name registered, it panics.

Types

type Interpreter added in v0.2.0

type Interpreter struct {
	engine.State
}

Interpreter is a Prolog interpreter. The zero value is a valid interpreter without any predicates/operators defined.

func New added in v0.2.0

func New(in io.Reader, out io.Writer) *Interpreter

New creates a new Prolog interpreter with predefined predicates/operators.

func (*Interpreter) Exec added in v0.2.0

func (i *Interpreter) Exec(query string, args ...interface{}) error

Exec executes a prolog program.

func (*Interpreter) ExecContext added in v0.3.0

func (i *Interpreter) ExecContext(ctx context.Context, query string, args ...interface{}) error

ExecContext executes a prolog program with context.

func (*Interpreter) Query added in v0.2.0

func (i *Interpreter) Query(query string, args ...interface{}) (*Solutions, error)

Query executes a prolog query and returns *Solutions.

func (*Interpreter) QueryContext added in v0.3.0

func (i *Interpreter) QueryContext(ctx context.Context, query string, args ...interface{}) (*Solutions, error)

QueryContext executes a prolog query and returns *Solutions with context.

type Solutions added in v0.2.0

type Solutions struct {
	// contains filtered or unexported fields
}

Solutions is the result of a query. Everytime the Next method is called, it searches for the next solution. By calling the Scan method, you can retrieve the content of the solution.

func (*Solutions) Close added in v0.2.0

func (s *Solutions) Close() error

Close closes the Solutions and terminates the search for other solutions.

func (*Solutions) Err added in v0.2.0

func (s *Solutions) Err() error

Err returns the error if exists.

func (*Solutions) Next added in v0.2.0

func (s *Solutions) Next() bool

Next prepares the next solution for reading with the Scan method. It returns true if it finds another solution, or false if there's no further solutions or if there's an error.

func (*Solutions) Scan added in v0.2.0

func (s *Solutions) Scan(dest interface{}) error

Scan copies the variable values of the current solution into the specified struct/map.

func (*Solutions) Vars added in v0.2.0

func (s *Solutions) Vars() []string

Vars returns variable names.

Directories

Path Synopsis
cmd
1pl
examples
dcg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL