solver

package
v0.0.0-...-7d3eea6 Latest Latest
Warning

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

Go to latest
Published: May 28, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

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

type Solution map[logic.Var]logic.Term

Solution (or bindings) is a substitution from vars to terms that produce a valid predicate.

func (Solution) String

func (s Solution) String() string

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 New

func New(text string) (*Solver, error)

New creates a Solver from parsing and compiling the provided program.

func NewSolverFromRules

func NewSolverFromRules(rules []logic.Rule) (*Solver, error)

NewFromClauses is like New, with already parsed clauses.

func (*Solver) Query

func (solver *Solver) Query(text string) (<-chan Solution, func())

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

func (solver *Solver) QueryTerms(terms ...logic.Term) (<-chan Solution, func())

QueryTerms is like Query, with already parsed terms.

func (*Solver) SetDebug

func (solver *Solver) SetDebug(filename string)

SetDebug sets a file to output execution information for the internal machine.

func (*Solver) SetIterLimit

func (solver *Solver) SetIterLimit(limit int)

SetIterLimit sets a maximum number of iterations to the internal machine.

Jump to

Keyboard shortcuts

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