logic

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: 5 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

View Source
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

func Eq(t1, t2 Term) bool

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

func FormatAtom(text string) string

Formats an atom, escaping and quoting the text if necessary.

func FormatString

func FormatString(text []rune) string

FormatString formats runes as a Prolog string with proper escaping.

func IsInt

func IsInt(text string) bool

IsInt returns whether text is a valid value for an Int.

An int must contain only (Latin) digit letters.

func IsQuotedAtom

func IsQuotedAtom(text string) bool

Returns whether text must be quoted as an atom.

func IsVar

func IsVar(text string) bool

IsVar returns whether text is a valid name for a Var.

A var must begin with an uppercase letter or an underscore, and the other letters must be identifier letters (e.g., letter, digit or underscore).

func Less

func Less(t1, t2 Term) bool

Less returns the order between t1 and t2, following the standard of terms.

The order of terms is: Vars < Ints < Atoms < Comps < List < Assoc < Dict

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.

func NewAssoc

func NewAssoc(key, val Term) *Assoc

NewAssoc returns an assoc with provided key and value.

func (*Assoc) Eq

func (t *Assoc) Eq(other *Assoc) bool

Eq returns whether this assoc is equal to another.

func (*Assoc) Less

func (t *Assoc) Less(other *Assoc) bool

Less returns whether this assoc is less than another.

Assocs are compared first by key, than by value.

func (*Assoc) String

func (t *Assoc) String() string

type AssocSet

type AssocSet []*Assoc

AssocSet is a set of assocs, implemented as a sorted array.

func NewAssocSet

func NewAssocSet(as []*Assoc) (AssocSet, error)

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'

func (Atom) Eq

func (t Atom) Eq(other Atom) bool

Eq returns whether this atom is equal to another.

func (Atom) Less

func (t Atom) Less(other Atom) bool

Less returns whether this atom is less than another, in lexicographic order.

func (Atom) String

func (t Atom) String() string

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.

func NewClause

func NewClause(head Term, body ...Term) *Clause

NewClause returns a clause with the provided head and terms as body.

func (*Clause) String

func (c *Clause) String() string

func (*Clause) ToClause

func (r *Clause) ToClause() *Clause

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

func DCGExpandComp(c *Comp, l0, l1 Term) *Comp

DCGExpandComp expands a DCG goal to be called with the provided initial and final states.

func NewComp

func NewComp(functor string, terms ...Term) *Comp

NewComp creates a compound term.

func (*Comp) Eq

func (t *Comp) Eq(other *Comp) bool

Eq returns whether this comp is equal to another.

func (*Comp) Indicator

func (c *Comp) Indicator() Indicator

Indicator returns the functor's indicator.

func (*Comp) Less

func (t *Comp) Less(other *Comp) bool

Less returns whether this comp is less than another.

Comps are first compared by arity, then by functor, then by args pairwise.

func (*Comp) String

func (t *Comp) String() string

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
}

func NewDCG

func NewDCG(head Term, body ...DCGTerm) *DCG

NewDCG returns a definite clause grammar with the provided head and terms as body.

func (*DCG) String

func (dcg *DCG) String() string

func (*DCG) ToClause

func (dcg *DCG) ToClause() *Clause

ToClause converts a DCG into a regular clause.

type DCGGoals

type DCGGoals []Term

DCGGoals is a list of logic terms present between braces.

func (DCGGoals) String

func (ts DCGGoals) String() string

type DCGTerm

type DCGTerm interface {
	fmt.Stringer
	// contains filtered or unexported methods
}

DCGTerm represents elements that are accepted within a DCG body.

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.

func (*Dict) Eq

func (t *Dict) Eq(other *Dict) bool

Eq returns whether this dict is equal to another.

func (*Dict) Less

func (t *Dict) Less(other *Dict) bool

Less returns whether this dict is less than another.

Dicts are compared in lexicographic order, with keys sorted.

func (*Dict) String

func (t *Dict) String() string

func (*Dict) Tail

func (d *Dict) Tail() Term

Tail returns a new dict excluding the first (minimum) element.

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.

func (Int) Eq

func (t Int) Eq(other Int) bool

Eq returns whether this int is equal to another.

func (Int) Less

func (t Int) Less(other Int) bool

Less returns whether this int is less than another.

func (Int) String

func (t Int) String() string

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) Eq

func (t *List) Eq(other *List) bool

Eq returns whether this list is equal to another.

func (*List) Less

func (t *List) Less(other *List) bool

Less returns whether this list is less than another.

Lists are compared lexicographically.

func (*List) Slice

func (l *List) Slice(n int) Term

Slice returns a new list starting from the n-th term, inclusive.

func (*List) String

func (t *List) String() string

type Ptr

type Ptr struct {
	// Value is an arbitrary Go pointer.
	Value interface{}
}

Ptr is an atomic term containing a Go pointer.

func (Ptr) Eq

func (t Ptr) Eq(other Ptr) bool

Eq returns whether the ptr contents are equal to another.

func (Ptr) Less

func (t Ptr) Less(other Ptr) bool

Less returns whether this ptr is less than another (meaningless comparison).

func (Ptr) String

func (t Ptr) String() string

type Rule

type Rule interface {
	// Normalize rule to a Clause.
	ToClause() *Clause
}

Rule is the sum type of Clause and DCG.

type Term

type Term interface {
	fmt.Stringer
	// contains filtered or unexported methods
}

Term is a representation of a logic term.

func NewDict

func NewDict(assocs ...*Assoc) Term

NewDict returns a dict with the provided assocs and EmptyDict as parent.

It panics if there are duplicate keys in assocs.

func NewIncompleteDict

func NewIncompleteDict(assocs []*Assoc, parent Term) Term

NewIncompleteDict returns a dict with the provided assocs and parent.

It panics if there are duplicate keys in assocs.

func NewIncompleteList

func NewIncompleteList(terms []Term, tail Term) Term

NewIncompleteList creates a List with the provided terms and tail.

func NewList

func NewList(terms ...Term) Term

NewList creates a List with the provided terms and EmptyList as 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

func NewVar(name string) Var

NewVar creates a new var.

It panics if the name doesn't start with an uppercase letter or an underscore.

func Vars

func Vars(term Term) []Var

Vars returns a set with all term variables, in insertion order.

func (Var) Eq

func (t Var) Eq(other Var) bool

Eq returns whether this var is equal to another.

func (Var) Less

func (t Var) Less(other Var) bool

Less returns whether this var is less than another, in lexicographic order.

func (Var) String

func (t Var) String() string

func (Var) WithSuffix

func (x Var) WithSuffix(suffix int) Var

WithSuffix creates a new var with the same name and provided suffix. Used to generate vars from the same template.

Jump to

Keyboard shortcuts

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