Documentation ¶
Overview ¶
Package logic implements the interface for a logic engine, with terms and solvers.
A logic term can fall in one of three categories:
* atomic: a term that represents an immutable value.
* variable: a term that represents an unbound, yet-to-be-resolved term.
* complex: a term that contains other terms, recursively.
A logic program is composed of clauses of the form 'head :- term1, term2.', that must be read as "head holds if term1 and term2 holds". A clause with no terms in the body is called a fact.
There is a clause with special syntax called DCG, for Definite Clause Grammar. They are used to describe lists, and have the form 'head --> dcg1, {term1, term2}, dcg2, [literal list].' This is then translated to a regular clause that accepts a difference list that satisfies the grammar and embedded clause restrictions.
Index ¶
- Variables
- func Eq(t1, t2 Term) bool
- func FormatAtom(text string) string
- func FormatString(text []rune) string
- func IsInt(text string) bool
- func IsQuotedAtom(text string) bool
- func IsVar(text string) bool
- func Less(t1, t2 Term) bool
- type Assoc
- type AssocSet
- type Atom
- type Clause
- type Comp
- type DCG
- type DCGGoals
- type DCGTerm
- type Dict
- type Indicator
- type Int
- type List
- type Ptr
- type Rule
- type Term
- type Var
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // AnonymousVar represents a variable to be ignored. AnonymousVar = NewVar("_") // EmptyList is an atom representing an empty list. EmptyList = Atom{"[]"} // EmptyDict is an atom representing an empty dict. EmptyDict = Atom{"{}"} )
Functions ¶
func Eq ¶
Eq returns whether t1 and t2 are identical terms.
Note that this only takes into account the structure of terms, not whether any binding may make them identical.
func FormatAtom ¶
Formats an atom, escaping and quoting the text if necessary.
func FormatString ¶
FormatString formats runes as a Prolog string with proper escaping.
func IsInt ¶
IsInt returns whether text is a valid value for an Int.
An int must contain only (Latin) digit letters.
func IsQuotedAtom ¶
Returns whether text must be quoted as an atom.
Types ¶
type Assoc ¶
type Assoc struct { // Key is the association key Key Term // Val is the association value Val Term // contains filtered or unexported fields }
Assoc is a complex term, representing an association pair.
type AssocSet ¶
type AssocSet []*Assoc
AssocSet is a set of assocs, implemented as a sorted array.
func NewAssocSet ¶
NewAssocSet returns an AssocSet with the provided assocs.
It returns an error if there are any duplicate keys.
type Atom ¶
type Atom struct { // Name is the identifier for an atom. Name string }
Atom is an atomic term representing a symbol.
Example ¶
fmt.Println(Atom{"a123"}, Atom{"space-> <-"}, Atom{"Upper"}, Atom{"123"})
Output: a123 'space-> <-' 'Upper' '123'
type Clause ¶
type Clause struct { // Head is the consequent of a clause. May be Atom or Comp. Head Term // Body is the antecedent of a clause. May be Atom, Var, Comp or Assoc. Body []Term // contains filtered or unexported fields }
Clause is the representation of a logic rule. Note that Clause is not a Term, so it can't be used within complex terms.
type Comp ¶
type Comp struct { // Functor is the primary identifier of a comp. Functor string // Args is the list of terms within this term. Args []Term // contains filtered or unexported fields }
Comp is a complex term, representing an immutable compound term.
func DCGExpandComp ¶
DCGExpandComp expands a DCG goal to be called with the provided initial and final states.
type DCG ¶
type DCG struct { // Head is the grammar rule name. May be Atom or Comp. Head Term // Body is the sequence of terminal sequences (vars and lists), // nonterminals (atoms and comps) and goals (braced goals). Body []DCGTerm // contains filtered or unexported fields }
type Dict ¶
type Dict struct { // Set of associations of this dict. Assocs AssocSet // Parent is the representation of another dict with additional // values to this dict. Note that the keys in Assocs override the // ones in Parent. It usually is another dict, an unbound var, or // the empty dict. Parent Term // contains filtered or unexported fields }
Dict is a complex term, representing a set of associations with unique keys.
type Indicator ¶
type Indicator struct { // Name is the compound term's functor. Name string // Arity is the compound term's number of args. Arity int }
Indicator is a notation for a comp, usually shown as functor/arity, e.g., f/2.
type Int ¶
type Int struct { // Value is the (immutable) value of an int. Value int }
Int is an atomic term representing an integer.
type List ¶
type List struct { // Terms are the contents of a list. Terms []Term // Tail is the continuation of a list, which is usually another // list, the empty list, or an unbound var. Tail Term // contains filtered or unexported fields }
List is a complex term, representing an ordered sequence of terms.
func (*List) Less ¶
Less returns whether this list is less than another.
Lists are compared lexicographically.
type Ptr ¶
type Ptr struct {
// Value is an arbitrary Go pointer.
Value interface{}
}
Ptr is an atomic term containing a Go pointer.
type Rule ¶
type Rule interface { // Normalize rule to a Clause. ToClause() *Clause }
Rule is the sum type of Clause and DCG.
type Term ¶
Term is a representation of a logic term.
func NewDict ¶
NewDict returns a dict with the provided assocs and EmptyDict as parent.
It panics if there are duplicate keys in assocs.
func NewIncompleteDict ¶
NewIncompleteDict returns a dict with the provided assocs and parent.
It panics if there are duplicate keys in assocs.
func NewIncompleteList ¶
NewIncompleteList creates a List with the provided terms and tail.
type Var ¶
type Var struct { // Name is the identifier for a var. Name string // contains filtered or unexported fields }
Var is a variable term.
func NewVar ¶
NewVar creates a new var.
It panics if the name doesn't start with an uppercase letter or an underscore.
func (Var) WithSuffix ¶
WithSuffix creates a new var with the same name and provided suffix. Used to generate vars from the same template.