term

package
v0.0.0-...-a28e2a2 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2017 License: MIT Imports: 10 Imported by: 6

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

View Source
const (
	VariableType = iota
	FloatType
	IntegerType
	AtomType
	CompoundType

	// odd man out
	ErrorType
)

Possible term types, in order according to ISO §7.2

Variables

View Source
var AlreadyBound = fmt.Errorf("Variable was already bound")

Returned by Bind() if the variable in question has already been bound to a value.

View Source
var CantUnify error = Errorf("Can't unify the given terms")

Returned by Unify() if the unification fails

View Source
var NotBound = fmt.Errorf("Variable is not bound")

Returned by Value() and ByName() if the variable in question has no bindings yet.

Functions

func ArithmeticEval2

func ArithmeticEval2(first, second Term) (Number, Number, error)

func IsAtom

func IsAtom(t Term) bool

Returns true if term t is an atom

func IsCallable

func IsCallable(t Term) bool

Returns true if term t is an atom or compound term.

func IsClause

func IsClause(t Term) bool

IsClause returns true if the term is like 'Head :- Body', otherwise false

func IsCompound

func IsCompound(t Term) bool

Returns true if term t is a compound term.

func IsDirective

func IsDirective(t Term) bool

Returns true if term t is a directive like `:- foo.`

func IsEmptyList

func IsEmptyList(t Term) bool

func IsError

func IsError(t Term) bool

Returns true if term t is an error term.

func IsFloat

func IsFloat(t Term) bool

Returns true if term t is an floating point number

func IsInteger

func IsInteger(t Term) bool

Returns true if term t is an integer

func IsList

func IsList(t Term) bool

func IsNumber

func IsNumber(t Term) bool

Returns true if term is a number (integer, float, etc)

func IsRational

func IsRational(t Term) bool

Returns true if term t is a rational number

func IsString

func IsString(t Term) bool

func IsVariable

func IsVariable(t Term) bool

Returns true if term t is a variable.

func NumberCmp

func NumberCmp(a, b Number) int

Compare two Golog numbers. Returns

-1 if a <  b
 0 if a == b
+1 if a > b

func Precedes

func Precedes(a, b Term) bool

Precedes returns true if the first argument 'term-precedes' the second argument according to ISO §7.2

func PrettyList

func PrettyList(t Term) string

func PrettyString

func PrettyString(t Term) string

func QuoteFunctor

func QuoteFunctor(name string) string

QuoteFunctor returns a canonical representation of a term's name by quoting characters that require quoting

func RawString

func RawString(t Term) string

func UnificationHash

func UnificationHash(terms []Term, n uint, preparation bool) uint64

func Variables

func Variables(t Term) ps.Map

Variables returns a ps.Map whose keys are human-readable variable names and those values are *Variable used inside term t.

Types

type Atom

type Atom string

func (*Atom) Arguments

func (self *Atom) Arguments() []Term

func (*Atom) Arity

func (self *Atom) Arity() int

func (*Atom) Indicator

func (self *Atom) Indicator() string

func (*Atom) Name

func (self *Atom) Name() string

func (*Atom) ReplaceVariables

func (self *Atom) ReplaceVariables(env Bindings) Term

func (*Atom) String

func (self *Atom) String() string

func (*Atom) Type

func (self *Atom) Type() int

func (*Atom) Unify

func (a *Atom) Unify(e Bindings, b Term) (Bindings, error)

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
}

func NewBindings

func NewBindings() Bindings

NewBindings returns a new, empty bindings value.

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 Body

func Body(t Term) Callable

Body returns a term's second argument. Panics if there isn't one

func Head(t Term) Callable

Head returns a term's first argument. Panics if there isn't one

func NewAtom

func NewAtom(name string) Callable

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

func NewAtomFromLexeme(possiblyQuotedName string) Callable

Unlikely to be useful outside of the parser

func NewCallable

func NewCallable(functor string, arguments ...Term) Callable

NewCallable creates a new term (or atom) with the given functor and optional arguments

func NewTermFromLexeme

func NewTermFromLexeme(possiblyQuotedName string, arguments ...Term) Callable

Unlikely to be useful outside of the parser

type Compound

type Compound struct {
	Func string
	Args []Term
	// contains filtered or unexported fields
}

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

func (self *Compound) Arguments() []Term

func (*Compound) Arity

func (self *Compound) Arity() int

func (*Compound) Indicator

func (self *Compound) Indicator() string

func (*Compound) MightUnify

func (a *Compound) MightUnify(b *Compound) bool

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

func (self *Compound) Name() string

func (*Compound) ReplaceVariables

func (self *Compound) ReplaceVariables(env Bindings) Term

func (*Compound) String

func (self *Compound) String() string

func (*Compound) Type

func (self *Compound) Type() int

func (*Compound) Unify

func (a *Compound) Unify(e Bindings, x Term) (Bindings, error)

func (*Compound) Univ

func (self *Compound) Univ() []Term

Univ is just like =../2 in ISO Prolog

type Error

type Error struct {
	// contains filtered or unexported fields
}

func (*Error) Arguments

func (self *Error) Arguments() []Term

func (*Error) Arity

func (self *Error) Arity() int

func (*Error) Functor

func (self *Error) Functor() string

func (*Error) Indicator

func (self *Error) Indicator() string

func (*Error) ReplaceVariables

func (self *Error) ReplaceVariables(env Bindings) Term

func (*Error) String

func (self *Error) String() string

func (*Error) Type

func (self *Error) Type() int

func (*Error) Unify

func (a *Error) Unify(e Bindings, b Term) (Bindings, error)

type Float

type Float float64

func NewFloat64

func NewFloat64(f float64) *Float

func (*Float) Float64

func (self *Float) Float64() float64

implement Number interface

func (*Float) Indicator

func (self *Float) Indicator() string

func (*Float) LosslessInt

func (self *Float) LosslessInt() (*big.Int, bool)

func (*Float) LosslessRat

func (self *Float) LosslessRat() (*big.Rat, bool)

func (*Float) ReplaceVariables

func (self *Float) ReplaceVariables(env Bindings) Term

func (*Float) String

func (self *Float) String() string

func (*Float) Type

func (self *Float) Type() int

func (*Float) Unify

func (a *Float) Unify(e Bindings, b Term) (Bindings, error)

func (*Float) Value

func (self *Float) Value() float64

type Integer

type Integer big.Int

Integer represents an unbounded, signed integer value

func NewCode

func NewCode(c rune) *Integer

NewCode returns an integer whose value is the character code if the given rune.

func (*Integer) Code

func (self *Integer) Code() rune

treat this integer as a character code. should be a method on a Code interface someday

func (*Integer) Float64

func (self *Integer) Float64() float64

implement Number interface

func (*Integer) Indicator

func (self *Integer) Indicator() string

func (*Integer) LosslessInt

func (self *Integer) LosslessInt() (*big.Int, bool)

func (*Integer) LosslessRat

func (self *Integer) LosslessRat() (*big.Rat, bool)

func (*Integer) ReplaceVariables

func (self *Integer) ReplaceVariables(env Bindings) Term

func (*Integer) String

func (self *Integer) String() string

func (*Integer) Type

func (self *Integer) Type() int

func (*Integer) Unify

func (a *Integer) Unify(e Bindings, b Term) (Bindings, error)

func (*Integer) Value

func (self *Integer) Value() *big.Int

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

func ArithmeticAdd(a, b Number) (Number, error)

Add two Golog numbers returning the result as a new Golog number

func ArithmeticDivide

func ArithmeticDivide(a, b Number) (Number, error)

Divide two Golog numbers returning the result as a new Golog number. The return value uses the most precise internal type possible.

func ArithmeticEval

func ArithmeticEval(t0 Term) (Number, error)

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

func ArithmeticMinus(a, b Number) (Number, error)

Subtract two Golog numbers returning the result as a new Golog number

func ArithmeticMultiply

func ArithmeticMultiply(a, b Number) (Number, error)

Multiply two Golog numbers returning the result as a new Golog number

func NewBigInt

func NewBigInt(val *big.Int) Number

helper for when a big.Int is already available

func NewFloat

func NewFloat(text string) Number

func NewInt

func NewInt(text string) Number

NewInt parses an integer's string representation to create a new integer value. Panics if the string's is not a valid integer

func NewInt64

func NewInt64(i int64) Number

helper for when an int64 is already available

type Rational

type Rational big.Rat

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 NewBigRat

func NewBigRat(r *big.Rat) *Rational

Constructs a new Rational value from a big.Rat value

func NewRational

func NewRational(text string) (*Rational, bool)

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

func (self *Rational) Float64() float64

implement Number interface

func (*Rational) Indicator

func (self *Rational) Indicator() string

func (*Rational) LosslessInt

func (self *Rational) LosslessInt() (*big.Int, bool)

func (*Rational) LosslessRat

func (self *Rational) LosslessRat() (*big.Rat, bool)

func (*Rational) ReplaceVariables

func (self *Rational) ReplaceVariables(env Bindings) Term

func (*Rational) String

func (self *Rational) String() string

func (*Rational) Type

func (self *Rational) Type() int

func (*Rational) Unify

func (a *Rational) Unify(e Bindings, b Term) (Bindings, error)

func (*Rational) Value

func (self *Rational) Value() *big.Rat

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 ListToSlice(t Term) (result []Term)

func NewCodeList

func NewCodeList(s string) Term

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

func NewCodeListFromDoubleQuotedString(s string) Term

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

func NewError(message string, eme *lex.Eme) Term

Returns an error term. These are used internally for error handling and may disappear in the future.

func NewTermList

func NewTermList(terms []Term) Term

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

func ProperListToTermSlice(x Term) []Term

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

func RenameVariables(t Term) Term

RenameVariables returns a new term like t with all variables replaced by fresh ones.

func SliceToList

func SliceToList(ts []Term) (result Term)

type TermSlice

type TermSlice []Term

Implement sort.Interface for []Term

func (*TermSlice) Len

func (self *TermSlice) Len() int

func (*TermSlice) Less

func (self *TermSlice) Less(i, j int) bool

func (*TermSlice) Swap

func (self *TermSlice) Swap(i, j int)

type Variable

type Variable struct {
	Name string
	// contains filtered or unexported fields
}

A Prolog logic variable. See ISO §6.1.2(a)

func NewVar

func NewVar(name string) *Variable

Creates a new logic variable with the given name.

func (*Variable) Arguments

func (self *Variable) Arguments() []Term

func (*Variable) Arity

func (self *Variable) Arity() int

func (*Variable) Functor

func (self *Variable) Functor() string

func (*Variable) Id

func (self *Variable) Id() int64

Id returns a unique identifier for this variable

func (*Variable) Indicator

func (self *Variable) Indicator() string

func (*Variable) ReplaceVariables

func (self *Variable) ReplaceVariables(env Bindings) Term

func (*Variable) String

func (self *Variable) String() string

func (*Variable) Type

func (self *Variable) Type() int

func (*Variable) Unify

func (a *Variable) Unify(e Bindings, b Term) (Bindings, error)

func (*Variable) WithNewId

func (self *Variable) WithNewId() *Variable

Jump to

Keyboard shortcuts

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