Documentation
¶
Overview ¶
Package solver contains methods to execute logic programs and list their solutions.
Example ¶
package main import ( "fmt" "github.com/brunokim/logic-engine/solver" ) func main() { s, _ := solver.New(` % Natural number definition in terms of successor s(X). nat(0). nat(s(X)) :- nat(X). % Adding A+B=Sum add(0, Sum, Sum). add(s(A), B, s(Sum)) :- add(A, B, Sum). % Multiplying A*B=Product mul(0, _, 0). mul(s(A), B, Product) :- mul(A, B, Partial), add(B, Partial, Product). `) solutions, _ := s.Query(` mul(s(s(0)), s(s(s(0))), Y), % 2*3=Y add(s(0), X, Y), % 1+X=Y `) for solution := range solutions { fmt.Println(solution) } // You can reuse the same solver object for a different query. solutions, _ = s.Query(` add(A, B, s(s(s(0)))) % A+B=3 `) for solution := range solutions { fmt.Println(solution) } }
Output: X = s(s(s(s(s(0))))), Y = s(s(s(s(s(s(0)))))) A = 0, B = s(s(s(0))) A = s(0), B = s(s(0)) A = s(s(0)), B = s(0) A = s(s(s(0))), B = 0
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Solution ¶
Solution (or bindings) is a substitution from vars to terms that produce a valid predicate.
type Solver ¶
type Solver struct { // Err stores the error that terminated a query. Err error // contains filtered or unexported fields }
Solver provides an asynchronous interface for enumerating the solutions of a logic query.
func NewSolverFromRules ¶
NewFromClauses is like New, with already parsed clauses.
func (*Solver) Query ¶
Query returns an (unbuffered) channel of solutions for the provided query, and a cancel function to interrupt the execution.
As soon as a value is read from the channel, the next solution starts to be computed. If there's no need for additional solutions, you should call cancel().
The last error found is stored in solver.Err.
func (*Solver) QueryTerms ¶
QueryTerms is like Query, with already parsed terms.
func (*Solver) SetDebug ¶
SetDebug sets a file to output execution information for the internal machine.
func (*Solver) SetIterLimit ¶
SetIterLimit sets a maximum number of iterations to the internal machine.