Documentation ¶
Index ¶
- Variables
- type Interpreter
- func (i *Interpreter) Exec(query string, args ...interface{}) error
- func (i *Interpreter) ExecContext(ctx context.Context, query string, args ...interface{}) error
- func (i *Interpreter) Query(query string, args ...interface{}) (*Solutions, error)
- func (i *Interpreter) QueryContext(ctx context.Context, query string, args ...interface{}) (*Solutions, error)
- func (i *Interpreter) QuerySolution(query string, args ...interface{}) *Solution
- func (i *Interpreter) QuerySolutionContext(ctx context.Context, query string, args ...interface{}) *Solution
- type Solution
- type Solutions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("closed")
ErrClosed indicates the Solutions are already closed and unable to perform the operation.
var ErrNoSolutions = errors.New("no solutions")
ErrNoSolutions indicates there's no solutions for the query.
Functions ¶
This section is empty.
Types ¶
type Interpreter ¶ added in v0.2.0
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.
Example (Placeholders) ¶
p := New(nil, os.Stdout) _ = p.Exec(`my_atom(?).`, "foo") sols, _ := p.Query(`my_atom(A), atom(A), write(A), nl.`) sols.Next() _ = p.Exec(`my_int(?, ?, ?, ?, ?).`, int8(1), int16(1), int32(1), int64(1), 1) sols, _ = p.Query(`my_int(I, I, I, I, I), integer(I), write(I), nl.`) sols.Next() _ = p.Exec(`my_float(?, ?).`, float32(1), float64(1)) sols, _ = p.Query(`my_float(F, F), float(F), write(F), nl.`) sols.Next() _ = p.Exec(`my_atom_list(?).`, []string{"foo", "bar", "baz"}) sols, _ = p.Query(`my_atom_list(As), maplist(atom, As), write(As), nl.`) sols.Next() _ = p.Exec(`my_int_list(?).`, []int{1, 2, 3}) sols, _ = p.Query(`my_int_list(Is), maplist(integer, Is), write(Is), nl.`) sols.Next() _ = p.Exec(`my_float_list(?).`, []float64{1, 2, 3}) sols, _ = p.Query(`my_float_list(Fs), maplist(float, Fs), write(Fs), nl.`) sols.Next()
Output: foo 1 1.0 [foo, bar, baz] [1, 2, 3] [1.0, 2.0, 3.0]
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.
Example (Placeholders) ¶
p := New(nil, os.Stdout) sols, _ := p.Query(`A = ?, atom(A), write(A), nl.`, "foo") sols.Next() sols, _ = p.Query(`(I, I, I, I, I) = (?, ?, ?, ?, ?), integer(I), write(I), nl.`, int8(1), int16(1), int32(1), int64(1), 1) sols.Next() sols, _ = p.Query(`(F, F) = (?, ?), float(F), write(F), nl.`, float32(1), float64(1)) sols.Next() sols, _ = p.Query(`L = ?, maplist(atom, L), write(L), nl.`, []string{"foo", "bar", "baz"}) sols.Next() sols, _ = p.Query(`L = ?, maplist(integer, L), write(L), nl.`, []int{1, 2, 3}) sols.Next() sols, _ = p.Query(`L = ?, maplist(float, L), write(L), nl.`, []float64{1, 2, 3}) sols.Next()
Output: foo 1 1.0 [foo, bar, baz] [1, 2, 3] [1.0, 2.0, 3.0]
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.
func (*Interpreter) QuerySolution ¶ added in v0.6.0
func (i *Interpreter) QuerySolution(query string, args ...interface{}) *Solution
QuerySolution executes a Prolog query for the first solution.
func (*Interpreter) QuerySolutionContext ¶ added in v0.6.0
func (i *Interpreter) QuerySolutionContext(ctx context.Context, query string, args ...interface{}) *Solution
QuerySolutionContext executes a Prolog query with context.
type Solution ¶ added in v0.6.0
type Solution struct {
// contains filtered or unexported fields
}
Solution is the single result of a query.
func (*Solution) Err ¶ added in v0.6.0
Err returns an error that occurred while querying for the Solution, if any.
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
Close closes the Solutions and terminates the search for other solutions.
func (*Solutions) Next ¶ added in v0.2.0
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
Scan copies the variable values of the current solution into the specified struct/map.
Example ¶
p := New(nil, nil) sols, _ := p.Query(`A = foo, I = 42, F = 3.14.`) for sols.Next() { var s struct { A string I int F float64 } _ = sols.Scan(&s) fmt.Printf("A = %s\n", s.A) fmt.Printf("I = %d\n", s.I) fmt.Printf("F = %.2f\n", s.F) }
Output: A = foo I = 42 F = 3.14
Example (List) ¶
p := New(nil, nil) sols, _ := p.Query(`Atoms = [foo, bar], Integers = [1, 2], Floats = [1.1, 2.1], Mixed = [foo, 1, 1.1].`) for sols.Next() { var s struct { Atoms []string Integers []int64 Floats []float64 Mixed []interface{} } _ = sols.Scan(&s) fmt.Printf("Atoms = %s\n", s.Atoms) fmt.Printf("Integers = %d\n", s.Integers) fmt.Printf("Floats = %.1f\n", s.Floats) fmt.Printf("Mixed = %v\n", s.Mixed) }
Output: Atoms = [foo bar] Integers = [1 2] Floats = [1.1 2.1] Mixed = [foo 1 1.1]
Example (Tag) ¶
p := New(nil, nil) sols, _ := p.Query(`A = foo, I = 42, F = 3.14.`) for sols.Next() { var s struct { Atom string `prolog:"A"` Integer int `prolog:"I"` Float float64 `prolog:"F"` } _ = sols.Scan(&s) fmt.Printf("Atom = %s\n", s.Atom) fmt.Printf("Integer = %d\n", s.Integer) fmt.Printf("Float = %.2f\n", s.Float) }
Output: Atom = foo Integer = 42 Float = 3.14