term

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotANumber = errors.New("not a number")

Functions

func Compare

func Compare(a, b Interface, env *Env) int64

func Contains

func Contains(t, s Interface, env *Env) bool

Contains checks if t contains s.

func Write added in v0.3.1

func Write(w io.Writer, t Interface, opts WriteTermOptions, env *Env) error

Write outputs one of the external representations of the term.

Types

type Atom

type Atom string

Atom is a prolog atom.

func (Atom) Apply added in v0.3.0

func (a Atom) Apply(args ...Interface) Interface

Apply returns a Compound which Functor is the Atom and Args are the arguments. If the arguments are empty, then returns itself.

func (Atom) String

func (a Atom) String() string

func (Atom) Unify

func (a Atom) Unify(t Interface, occursCheck bool, env *Env) (*Env, bool)

Unify unifies the atom with t.

func (Atom) Unparse added in v0.3.1

func (a Atom) Unparse(emit func(syntax.Token), opts WriteTermOptions, _ *Env)

Unparse emits tokens that represent the atom.

type Compound

type Compound struct {
	Functor Atom
	Args    []Interface
}

Compound is a prolog compound.

func (*Compound) String

func (c *Compound) String() string

func (*Compound) Unify

func (c *Compound) Unify(t Interface, occursCheck bool, env *Env) (*Env, bool)

Unify unifies the compound with t.

func (*Compound) Unparse added in v0.3.1

func (c *Compound) Unparse(emit func(syntax.Token), opts WriteTermOptions, env *Env)

Unparse emits tokens that represent the compound.

type DoubleQuotes added in v0.3.0

type DoubleQuotes int
const (
	DoubleQuotesCodes DoubleQuotes = iota
	DoubleQuotesChars
	DoubleQuotesAtom
)

func (DoubleQuotes) String added in v0.3.0

func (d DoubleQuotes) String() string

type Env

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

Env is a mapping from variables to terms.

func NewEnv added in v0.3.0

func NewEnv() *Env

NewEnv creates an empty environment.

func (*Env) Bind added in v0.3.0

func (e *Env) Bind(k Variable, v Interface) *Env

Bind adds a new entry to the environment.

func (*Env) FreeVariables

func (e *Env) FreeVariables(ts ...Interface) Variables

FreeVariables extracts variables in the given terms.

func (*Env) Lookup

func (e *Env) Lookup(k Variable) (Interface, bool)

Lookup returns a term that the given variable is bound to.

func (*Env) Resolve

func (e *Env) Resolve(t Interface) Interface

Resolve follows the variable chain and returns the first non-variable term or the last free variable.

func (*Env) Simplify

func (e *Env) Simplify(t Interface) Interface

Simplify trys to remove as many variables as possible from term t.

type EofAction

type EofAction int
const (
	EofActionEOFCode EofAction = iota
	EofActionError
	EofActionReset
)

type Float

type Float float64

Float is a prolog floating-point number.

func (Float) String

func (f Float) String() string

func (Float) Unify

func (f Float) Unify(t Interface, occursCheck bool, env *Env) (*Env, bool)

Unify unifies the float with t.

func (Float) Unparse added in v0.3.1

func (f Float) Unparse(emit func(syntax.Token), _ WriteTermOptions, _ *Env)

Unparse emits tokens that represent the float.

type Integer

type Integer int64

Integer is a prolog integer.

func (Integer) String

func (i Integer) String() string

func (Integer) Unify

func (i Integer) Unify(t Interface, occursCheck bool, env *Env) (*Env, bool)

Unify unifies the integer with t.

func (Integer) Unparse added in v0.3.1

func (i Integer) Unparse(emit func(token syntax.Token), _ WriteTermOptions, _ *Env)

Unparse emits tokens that represent the integer.

func (Integer) WriteTerm

func (i Integer) WriteTerm(w io.Writer, _ WriteTermOptions, _ *Env) error

WriteTerm writes the integer into w.

type Interface

type Interface interface {
	fmt.Stringer
	Unify(Interface, bool, *Env) (*Env, bool)
	Unparse(func(syntax.Token), WriteTermOptions, *Env)
}

Interface is a prolog term.

func Cons

func Cons(car, cdr Interface) Interface

Cons returns a list consists of a first element car and the rest cdr.

func List

func List(ts ...Interface) Interface

List returns a list of ts.

func ListRest

func ListRest(rest Interface, ts ...Interface) Interface

ListRest returns a list of ts followed by rest.

func Rulify

func Rulify(t Interface, env *Env) Interface

Rulify returns t if t is in a form of P:-Q, t:-true otherwise.

func Set

func Set(ts ...Interface) Interface

Set returns a list of ts which elements are unique.

type Operator

type Operator struct {
	Priority  Integer // 1 ~ 1200
	Specifier OperatorSpecifier
	Name      Atom
}

Operator is an operator definition.

type OperatorSpecifier added in v0.3.1

type OperatorSpecifier uint8
const (
	OperatorSpecifierNone OperatorSpecifier = iota
	OperatorSpecifierFX
	OperatorSpecifierFY
	OperatorSpecifierXF
	OperatorSpecifierYF
	OperatorSpecifierXFX
	OperatorSpecifierXFY
	OperatorSpecifierYFX
)

func (OperatorSpecifier) Term added in v0.3.1

func (s OperatorSpecifier) Term() Interface

type Operators

type Operators []Operator

Operators are a list of operators sorted in a descending order of precedence.

type ParsedVariable added in v0.3.0

type ParsedVariable struct {
	Name     Atom
	Variable Variable
	Count    int
}

ParsedVariable is a set of information regarding a variable in a parsed term.

type Parser

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

Parser turns bytes into Interface.

func NewParser

func NewParser(input *bufio.Reader, charConversions map[rune]rune, opts ...ParserOption) *Parser

NewParser creates a Parser.

func (*Parser) More

func (p *Parser) More() bool

More checks if the parser has more tokens to read.

func (*Parser) Number

func (p *Parser) Number() (Interface, error)

Number parses a number term.

func (*Parser) Replace

func (p *Parser) Replace(placeholder Atom, args ...interface{}) error

Replace registers placeholder and its arguments. Every occurrence of placeholder will be replaced by arguments. Mismatch of the number of occurrences of placeholder and the number of arguments raises an error.

func (*Parser) Term

func (p *Parser) Term() (Interface, error)

Term parses a term followed by a full stop.

type ParserOption added in v0.3.0

type ParserOption func(p *Parser)

ParserOption is option for NewParser.

func WithDoubleQuotes added in v0.3.0

func WithDoubleQuotes(quotes DoubleQuotes) ParserOption

WithDoubleQuotes sets how Parser handles double quotes.

func WithOperators added in v0.3.0

func WithOperators(operators *Operators) ParserOption

WithOperators sets operators for Parser.

func WithParsedVars added in v0.3.0

func WithParsedVars(vars *[]ParsedVariable) ParserOption

WithParsedVars sets where Parser to store information regarding parsed variables.

type Stream

type Stream struct {
	Source io.Reader
	Sink   io.Writer
	Closer io.Closer

	Mode       StreamMode
	Alias      Atom
	EofAction  EofAction
	Reposition bool
	StreamType StreamType
}

Stream is a prolog stream.

func (*Stream) String

func (s *Stream) String() string

func (*Stream) Unify

func (s *Stream) Unify(t Interface, occursCheck bool, env *Env) (*Env, bool)

Unify unifies the stream with t.

func (*Stream) Unparse added in v0.3.1

func (s *Stream) Unparse(emit func(syntax.Token), _ WriteTermOptions, _ *Env)

Unparse emits tokens that represent the stream.

type StreamMode

type StreamMode int
const (
	StreamModeRead StreamMode = iota
	StreamModeWrite
	StreamModeAppend
)

type StreamType

type StreamType int
const (
	StreamTypeText StreamType = iota
	StreamTypeBinary
)

type UnexpectedTokenError added in v0.3.0

type UnexpectedTokenError struct {
	ExpectedKind syntax.TokenKind
	ExpectedVals []string
	Actual       syntax.Token
	History      []syntax.Token
}

func (UnexpectedTokenError) Error added in v0.3.0

func (e UnexpectedTokenError) Error() string

type Variable

type Variable string

Variable is a prolog variable.

func NewVariable

func NewVariable() Variable

func (Variable) Generated added in v0.4.0

func (v Variable) Generated() bool

func (Variable) String

func (v Variable) String() string

func (Variable) Unify

func (v Variable) Unify(t Interface, occursCheck bool, env *Env) (*Env, bool)

Unify unifies the variable with t.

func (Variable) Unparse added in v0.3.1

func (v Variable) Unparse(emit func(token syntax.Token), opts WriteTermOptions, env *Env)

Unparse emits tokens that represent the variable.

type Variables added in v0.3.0

type Variables []Variable

func (Variables) Except added in v0.3.0

func (vs Variables) Except(ws Variables) Variables

func (Variables) Terms added in v0.3.0

func (vs Variables) Terms() []Interface

type WriteTermOptions

type WriteTermOptions struct {
	Quoted      bool
	Ops         Operators
	NumberVars  bool
	Descriptive bool

	Priority int
}

WriteTermOptions describes options to write terms.

Jump to

Keyboard shortcuts

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