Documentation ¶
Overview ¶
Represent and unify Prolog terms. Along with golog.Machine, term.Term is one of the most important data types in Golog. It provides a Go representation of Prolog terms. Terms represent Prolog code, Prolog queries and Prolog results.
The current term API is messy and will definitely change in the future.
Index ¶
- Constants
- Variables
- func ArithmeticEval2(first, second Term) (Number, Number, error)
- func IsAtom(t Term) bool
- func IsCallable(t Term) bool
- func IsClause(t Term) bool
- func IsCompound(t Term) bool
- func IsDirective(t Term) bool
- func IsEmptyList(t Term) bool
- func IsError(t Term) bool
- func IsFloat(t Term) bool
- func IsInteger(t Term) bool
- func IsList(t Term) bool
- func IsNumber(t Term) bool
- func IsRational(t Term) bool
- func IsString(t Term) bool
- func IsVariable(t Term) bool
- func NumberCmp(a, b Number) int
- func Precedes(a, b Term) bool
- func PrettyList(t Term) string
- func PrettyString(t Term) string
- func QuoteFunctor(name string) string
- func RawString(t Term) string
- func UnificationHash(terms []Term, n uint, preparation bool) uint64
- func Variables(t Term) ps.Map
- type Atom
- func (self *Atom) Arguments() []Term
- func (self *Atom) Arity() int
- func (self *Atom) Indicator() string
- func (self *Atom) Name() string
- func (self *Atom) ReplaceVariables(env Bindings) Term
- func (self *Atom) String() string
- func (self *Atom) Type() int
- func (a *Atom) Unify(e Bindings, b Term) (Bindings, error)
- type Bindings
- type Callable
- type Compound
- func (self *Compound) Arguments() []Term
- func (self *Compound) Arity() int
- func (self *Compound) Indicator() string
- func (a *Compound) MightUnify(b *Compound) bool
- func (self *Compound) Name() string
- func (self *Compound) ReplaceVariables(env Bindings) Term
- func (self *Compound) String() string
- func (self *Compound) Type() int
- func (a *Compound) Unify(e Bindings, x Term) (Bindings, error)
- func (self *Compound) Univ() []Term
- type Error
- func (self *Error) Arguments() []Term
- func (self *Error) Arity() int
- func (self *Error) Functor() string
- func (self *Error) Indicator() string
- func (self *Error) ReplaceVariables(env Bindings) Term
- func (self *Error) String() string
- func (self *Error) Type() int
- func (a *Error) Unify(e Bindings, b Term) (Bindings, error)
- type Float
- func (self *Float) Float64() float64
- func (self *Float) Indicator() string
- func (self *Float) LosslessInt() (*big.Int, bool)
- func (self *Float) LosslessRat() (*big.Rat, bool)
- func (self *Float) ReplaceVariables(env Bindings) Term
- func (self *Float) String() string
- func (self *Float) Type() int
- func (a *Float) Unify(e Bindings, b Term) (Bindings, error)
- func (self *Float) Value() float64
- type Integer
- func (self *Integer) Code() rune
- func (self *Integer) Float64() float64
- func (self *Integer) Indicator() string
- func (self *Integer) LosslessInt() (*big.Int, bool)
- func (self *Integer) LosslessRat() (*big.Rat, bool)
- func (self *Integer) ReplaceVariables(env Bindings) Term
- func (self *Integer) String() string
- func (self *Integer) Type() int
- func (a *Integer) Unify(e Bindings, b Term) (Bindings, error)
- func (self *Integer) Value() *big.Int
- type Number
- func ArithmeticAdd(a, b Number) (Number, error)
- func ArithmeticDivide(a, b Number) (Number, error)
- func ArithmeticEval(t0 Term) (Number, error)
- func ArithmeticMinus(a, b Number) (Number, error)
- func ArithmeticMultiply(a, b Number) (Number, error)
- func NewBigInt(val *big.Int) Number
- func NewFloat(text string) Number
- func NewInt(text string) Number
- func NewInt64(i int64) Number
- type Rational
- func (self *Rational) Float64() float64
- func (self *Rational) Indicator() string
- func (self *Rational) LosslessInt() (*big.Int, bool)
- func (self *Rational) LosslessRat() (*big.Rat, bool)
- func (self *Rational) ReplaceVariables(env Bindings) Term
- func (self *Rational) String() string
- func (self *Rational) Type() int
- func (a *Rational) Unify(e Bindings, b Term) (Bindings, error)
- func (self *Rational) Value() *big.Rat
- type Term
- func ListToSlice(t Term) (result []Term)
- func NewCodeList(s string) Term
- func NewCodeListFromDoubleQuotedString(s string) Term
- func NewError(message string, eme *lex.Eme) Term
- func NewTermList(terms []Term) Term
- func ProperListToTermSlice(x Term) []Term
- func RenameVariables(t Term) Term
- func SliceToList(ts []Term) (result Term)
- type TermSlice
- type Variable
- func (self *Variable) Arguments() []Term
- func (self *Variable) Arity() int
- func (self *Variable) Functor() string
- func (self *Variable) Id() int64
- func (self *Variable) Indicator() string
- func (self *Variable) ReplaceVariables(env Bindings) Term
- func (self *Variable) String() string
- func (self *Variable) Type() int
- func (a *Variable) Unify(e Bindings, b Term) (Bindings, error)
- func (self *Variable) WithNewId() *Variable
Constants ¶
const ( VariableType = iota FloatType IntegerType AtomType CompoundType // odd man out ErrorType )
Possible term types, in order according to ISO §7.2
Variables ¶
var AlreadyBound = fmt.Errorf("Variable was already bound")
Returned by Bind() if the variable in question has already been bound to a value.
var CantUnify error = Errorf("Can't unify the given terms")
Returned by Unify() if the unification fails
var NotBound = fmt.Errorf("Variable is not bound")
Returned by Value() and ByName() if the variable in question has no bindings yet.
Functions ¶
func IsDirective ¶
Returns true if term t is a directive like `:- foo.`
func IsEmptyList ¶
func Precedes ¶
Precedes returns true if the first argument 'term-precedes' the second argument according to ISO §7.2
func PrettyList ¶
func PrettyString ¶
func QuoteFunctor ¶
QuoteFunctor returns a canonical representation of a term's name by quoting characters that require quoting
Types ¶
type Bindings ¶
type Bindings interface { // Bind returns a new Environment, like the old one, but with the variable // bound to its new value; error is AlreadyBound if the variable had a // value previously. Bind(*Variable, Term) (Bindings, error) // ByName is similar to Value() but it searches for a variable // binding by using that variable's name. Names can be ambiguous so // use with caution. ByName(string) (Term, error) // ByName_ is like ByName() but panics on error. ByName_(string) Term // Resolve follows bindings recursively until a term is found for // which no binding exists. If you want to know the value of a // variable, this is your best bet. Resolve(*Variable) (Term, error) // Resolve_ is like Resolve() but panics on error. Resolve_(*Variable) Term // Size returns the number of variable bindings in this environment. Size() int // Value returns the value of a bound variable; error is NotBound if // the variable is free. Value(*Variable) (Term, error) // WithNames returns a new bindings with human-readable names attached // for convenient lookup. Panics if names have already been attached. WithNames(ps.Map) Bindings }
type Callable ¶
type Callable interface { Term // Name returns the term's name. Some people might call this the term's // functor, but there's ambiguity surrounding that word in the Prolog // community (some use it for Name/Arity pairs). Name() string // Arity returns the number of arguments a term has. An atom has 0 arity. Arity() int // Arguments returns a slice of this term's arguments, if any Arguments() []Term }
Callable represents either an atom or a compound term. This is the terminology used by callable/1 in many Prologs.
func NewAtom ¶
NewAtom creates a new atom with the given name. This is just a 0-arity compound term, for now. Eventually, it will have an optimized implementation.
func NewAtomFromLexeme ¶
Unlikely to be useful outside of the parser
func NewCallable ¶
NewCallable creates a new term (or atom) with the given functor and optional arguments
func NewTermFromLexeme ¶
Unlikely to be useful outside of the parser
type Compound ¶
ISO calls this a "compound term" see §6.1.2(e) We currently use this type to cover atoms defined in §6.1.2(b) by treating atoms as compound terms with 0 arity.
func (*Compound) MightUnify ¶
Returns true if a and b might unify. This is an optimization for times when a and b are frequently unified with other compound terms. For example, goals and clause heads.
func (*Compound) ReplaceVariables ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
func (*Error) ReplaceVariables ¶
type Integer ¶
Integer represents an unbounded, signed integer value
func (*Integer) Code ¶
treat this integer as a character code. should be a method on a Code interface someday
func (*Integer) ReplaceVariables ¶
type Number ¶
type Number interface { Term // Float64 gives a floating point representation of this number. The // representation inherits all weaknesses of the float64 data type. Float64() float64 // LosslessInt returns a big.Int representation of this number, if // possible. If int64 can't represent the number perfectly, returns // false. LosslessInt() (*big.Int, bool) // LosslessRat returns a big.Rational representation of this number, // if possible. If a big.Rational can't represent the number perfectly, // returns false. Floats never convert to rationals. LosslessRat() (*big.Rat, bool) }
Number represents either an integer or a floating point number. This interface is convenient when working with arithmetic
func ArithmeticAdd ¶
Add two Golog numbers returning the result as a new Golog number
func ArithmeticDivide ¶
Divide two Golog numbers returning the result as a new Golog number. The return value uses the most precise internal type possible.
func ArithmeticEval ¶
Evaluate an arithmetic expression to produce a number. This is conceptually similar to Prolog: X is Expression. Returns false if the expression cannot be evaluated (unbound variables, in)
func ArithmeticMinus ¶
Subtract two Golog numbers returning the result as a new Golog number
func ArithmeticMultiply ¶
Multiply two Golog numbers returning the result as a new Golog number
type Rational ¶
Rational is a specialized, internal representation of floats. The goal is to facilitate more accurate numeric computations than floats allow. This isn't always possible, but it's a helpful optimization in many practical circumstances.
func NewRational ¶
NewRational parses a rational's string representation to create a new rational value. Panics if the string's is not a valid rational
func (*Rational) ReplaceVariables ¶
type Term ¶
type Term interface { // ReplaceVariables replaces any internal variables with the values // to which they're bound. Unbound variables are left as they are ReplaceVariables(Bindings) Term // String provides a string representation of a term String() string // Type indicates whether this term is an atom, number, compound, etc. // ISO §7.2 uses the word "type" to descsribe this idea. Constants are // defined for each type. Type() int // Indicator() provides a "predicate indicator" representation of a term Indicator() string // Unifies the invocant and another term in the presence of an // environment. // On succes, returns a new environment with additional variable // bindings. On failure, returns CantUnify error along with the // original environment Unify(Bindings, Term) (Bindings, error) }
Term represents a single Prolog term which might be an atom, a compound structure, an integer, etc. Many methods on Term will be replaced with functions in the future. The Term interface is also likely to be split into several smaller interfaces like Atomic, Number, etc.
func ListToSlice ¶
func NewCodeList ¶
NewCodeList returns a compound term consisting of the character codes of the given string. The internal representation may eventually optimize for storing character codes.
func NewCodeListFromDoubleQuotedString ¶
NewCodeList constructs a list of character codes from a string. The string should include opening and closing " characters. Nominally, the resulting term is just a chain of cons cells ('.'/2), but it might actually be a more efficient implementation under the hood.
func NewError ¶
Returns an error term. These are used internally for error handling and may disappear in the future.
func NewTermList ¶
NewTermList returns a list term consisting of each term from the slice. A future implementation may optimize the data structure that's returned.
func ProperListToTermSlice ¶
Converts a '.'/2 list terminated in []/0 into a slice of the associated terms. Panics if the argument is not a proper list.
func RenameVariables ¶
RenameVariables returns a new term like t with all variables replaced by fresh ones.