engine

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: MIT Imports: 24 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AtomPanicError = NewAtom("panic_error")
)
View Source
var ErrMaxVariables = errors.New("maximum number of variables reached")

Functions

func CompareAtomic

func CompareAtomic[T Term](a T, t Term, cmp func(T, T) int, env *Env) int

CompareAtomic compares a custom atomic term of type T with a Term and returns -1, 0, or 1. The order is Variable < Float < Integer < Atom < custom atomic terms < Compound where different types of custom atomic terms are ordered by the Go-syntax representation of the types. It compares values of the same custom atomic term type T by the provided comparison function.

func CompareCompound

func CompareCompound(c Compound, t Term, env *Env) int

CompareCompound compares the Compound with a Term.

func WriteCompound

func WriteCompound(w io.Writer, c Compound, opts *WriteOptions, env *Env) error

WriteCompound outputs the Compound to an io.Writer.

Types

type Atom

type Atom string

Atom is a prolog atom.

func NewAtom

func NewAtom(name string) Atom

NewAtom interns the given string and returns an Atom.

func NewAtomRune

func NewAtomRune(v rune) Atom

func (Atom) Apply

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

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

func (Atom) Compare

func (a Atom) Compare(t Term, env *Env) int

Compare compares the Atom with a Term.

func (Atom) String

func (a Atom) String() string

func (Atom) WriteTerm

func (a Atom) WriteTerm(w io.Writer, opts *WriteOptions, _ *Env) error

WriteTerm outputs the Atom to an io.Writer.

type Compound

type Compound interface {
	Term
	Functor() Atom
	Arity() int
	Arg(n int) Term
}

Compound is a Prolog compound.

type Cont

type Cont func(*Env) *Promise

Cont is a continuation.

type Dict

type Dict interface {
	Compound

	Tag() Term
	All() iter.Seq2[Atom, Term]

	Value(key Atom) (Term, bool)
	At(i int) (Atom, Term, bool)
	Len() int
}

Dict is a term that represents a dictionary.

Dicts are currently represented as a compound term using the functor `dict`. The first argument is the tag. The remaining arguments create an array of sorted key-value pairs.

func NewDict

func NewDict(args []Term) (Dict, error)

NewDict creates a new dictionary (Dict) from the provided arguments (args). It processes the arguments and returns a Dict instance or an error if the arguments are invalid.

The first argument is the tag. The remaining arguments are the key and value pairs.

type Env

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

Env is a mapping from variables to terms.

func NewEnv

func NewEnv() *Env

NewEnv creates an empty environment.

func (*Env) Resolve

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

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

func (*Env) Unify

func (e *Env) Unify(x, y Term) (*Env, bool)

Unify unifies 2 terms.

type Exception

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

Exception is an error represented by a prolog term.

func DomainError

func DomainError(domain, culprit Term, env *Env) Exception

DomainError creates a new domain error exception.

func EvaluationError

func EvaluationError(error Term, env *Env) Exception

EvaluationError creates a new evaluation error exception.

func ExistenceError

func ExistenceError(objectType, culprit Term, env *Env) Exception

ExistenceError creates a new existence error exception.

func InstantiationError

func InstantiationError(env *Env) Exception

InstantiationError returns an instantiation error exception.

func NewException

func NewException(term Term, env *Env) Exception

NewException creates an Exception from a copy of the given Term.

func PermissionError

func PermissionError(operation, permissionType, culprit Term, env *Env) Exception

PermissionError creates a new permission error exception.

func RepresentationError

func RepresentationError(limit Term, env *Env) Exception

RepresentationError creates a new representation error exception.

func ResourceError

func ResourceError(resource Term, env *Env) Exception

ResourceError creates a new resource error exception.

func SyntaxError

func SyntaxError(error Term, env *Env) Exception

SyntaxError creates a new syntax error exception.

func TypeError

func TypeError(typ, culprit Term, env *Env) Exception

TypeError creates a new type error exception.

func (Exception) Error

func (e Exception) Error() string

func (Exception) Term

func (e Exception) Term() Term

Term returns the underlying Term of the Exception.

type Float

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

Float is a prolog "falsy" floating-point number.

The underlying implementation is not based on floating-point, it's a [GDA](https://speleotrove.com/decimal/) compatible implementation to avoid approximation and determinism issues. It uses under the hood a decimal128 with 34 precision digits and must be manipulated using the `decimal128Ctx` only.

It serves as an immutable wrapper over `apd.Decimal`.

func NewFloatFromInt64

func NewFloatFromInt64(i int64) Float

func NewFloatFromString

func NewFloatFromString(s string) (Float, error)

func (Float) Compare

func (f Float) Compare(t Term, env *Env) int

Compare compares the Float with a Term.

func (Float) Eq

func (f Float) Eq(other Float) bool

func (Float) Gt

func (f Float) Gt(other Float) bool

func (Float) Gte

func (f Float) Gte(other Float) bool

func (Float) Lt

func (f Float) Lt(other Float) bool

func (Float) Lte

func (f Float) Lte(other Float) bool

func (Float) Negative

func (f Float) Negative() bool

func (Float) Positive

func (f Float) Positive() bool

func (Float) String

func (f Float) String() string

func (Float) WriteTerm

func (f Float) WriteTerm(w io.Writer, opts *WriteOptions, _ *Env) error

WriteTerm outputs the Float to an io.Writer.

func (Float) Zero

func (f Float) Zero() bool

type HookFunc

type HookFunc func(opcode Opcode, operand Term, env *Env) error

HookFunc is a type for a hook function that is triggered before the VM executes a specific instruction. If the hook function returns an error, the VM halts execution and returns the error.

func CompositeHook

func CompositeHook(fs ...HookFunc) HookFunc

CompositeHook returns a hook function that chains multiple hooks together. The hooks are executed sequentially, and if any hook returns an error, the execution stops.

func DebugHookFn

func DebugHookFn(w io.Writer) HookFunc

DebugHookFn is a function that returns a hook function that prints the executed instruction.

type Integer

type Integer int64

Integer is a prolog integer.

func (Integer) Compare

func (i Integer) Compare(t Term, env *Env) int

Compare compares the Integer with a Term.

func (Integer) WriteTerm

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

WriteTerm outputs the Integer to an io.Writer.

type Lexer

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

Lexer turns runes into tokens.

func (*Lexer) Token

func (l *Lexer) Token() (Token, error)

Token returns the next token.

type ListIterator

type ListIterator struct {
	List         Term
	Env          *Env
	AllowPartial bool
	AllowCycle   bool
	// contains filtered or unexported fields
}

ListIterator is an iterator for a list.

func (*ListIterator) Current

func (i *ListIterator) Current() Term

Current returns the current element.

func (*ListIterator) Err

func (i *ListIterator) Err() error

Err returns an error.

func (*ListIterator) Next

func (i *ListIterator) Next() bool

Next proceeds to the next element of the list and returns true if there's such an element.

func (*ListIterator) Suffix

func (i *ListIterator) Suffix() Term

Suffix returns the rest of the list.

type NextFunc

type NextFunc = func() (PromiseFunc, bool)

NextFunc defines the type of a function that returns the next PromiseFunc in a sequence, along with a boolean indicating whether the returned value is valid.

type Number

type Number interface {
	Term
	// contains filtered or unexported methods
}

Number is a prolog number, either Integer or Float.

type Opcode

type Opcode byte
const (
	OpEnter Opcode = iota
	OpCall
	OpExit
	OpGetConst
	OpPutConst
	OpGetVar
	OpPutVar
	OpGetFunctor
	OpPutFunctor
	OpPop

	OpCut
	OpGetList
	OpPutList
	OpGetPartial
	OpPutPartial
	OpGetDict
	OpPutDict
)

func (Opcode) String

func (op Opcode) String() string

type ParsedVariable

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 {
	Vars []ParsedVariable
	// contains filtered or unexported fields
}

Parser turns bytes into Term.

func NewParser

func NewParser(vm *VM, r io.RuneReader) *Parser

NewParser creates a new parser from the current VM and io.RuneReader.

func (*Parser) More

func (p *Parser) More() bool

More checks if the parser has more tokens to read.

func (*Parser) SetPlaceholder

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

SetPlaceholder 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() (Term, error)

Term parses a term followed by a full stop.

type Predicate0

type Predicate0 func(*VM, Cont, *Env) *Promise

Predicate0 is a predicate of arity 0.

type Predicate1

type Predicate1 func(*VM, Term, Cont, *Env) *Promise

Predicate1 is a predicate of arity 1.

type Predicate2

type Predicate2 func(*VM, Term, Term, Cont, *Env) *Promise

Predicate2 is a predicate of arity 2.

type Predicate3

type Predicate3 func(*VM, Term, Term, Term, Cont, *Env) *Promise

Predicate3 is a predicate of arity 3.

type Predicate4

type Predicate4 func(*VM, Term, Term, Term, Term, Cont, *Env) *Promise

Predicate4 is a predicate of arity 4.

type Predicate5

type Predicate5 func(*VM, Term, Term, Term, Term, Term, Cont, *Env) *Promise

Predicate5 is a predicate of arity 5.

type Predicate6

type Predicate6 func(*VM, Term, Term, Term, Term, Term, Term, Cont, *Env) *Promise

Predicate6 is a predicate of arity 6.

type Predicate7

type Predicate7 func(*VM, Term, Term, Term, Term, Term, Term, Term, Cont, *Env) *Promise

Predicate7 is a predicate of arity 7.

type Predicate8

type Predicate8 func(*VM, Term, Term, Term, Term, Term, Term, Term, Term, Cont, *Env) *Promise

Predicate8 is a predicate of arity 8.

type Promise

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

Promise is a delayed execution that results in (bool, error). The zero value for Promise is equivalent to Bool(false).

func Abolish

func Abolish(vm *VM, pi Term, k Cont, env *Env) *Promise

Abolish removes the procedure indicated by pi from the database.

func AcyclicTerm

func AcyclicTerm(_ *VM, t Term, k Cont, env *Env) *Promise

AcyclicTerm checks if t is acyclic.

func Append

func Append(vm *VM, xs, ys, zs Term, k Cont, env *Env) *Promise

Append succeeds iff zs is the concatenation of lists xs and ys.

func Arg

func Arg(vm *VM, nth, t, arg Term, k Cont, env *Env) *Promise

Arg extracts nth argument of term as arg, or finds the argument position of arg in term as nth.

func Asserta

func Asserta(vm *VM, t Term, k Cont, env *Env) *Promise

Asserta prepends t to the database.

func Assertz

func Assertz(vm *VM, t Term, k Cont, env *Env) *Promise

Assertz appends t to the database.

func AtomChars

func AtomChars(vm *VM, atom, chars Term, k Cont, env *Env) *Promise

AtomChars breaks down atom into list of characters and unifies with chars, or constructs an atom from a list of characters chars and unifies it with atom.

func AtomCodes

func AtomCodes(vm *VM, atom, codes Term, k Cont, env *Env) *Promise

AtomCodes breaks up atom into a list of runes and unifies it with codes, or constructs an atom from the list of runes and unifies it with atom.

func AtomConcat

func AtomConcat(vm *VM, atom1, atom2, atom3 Term, k Cont, env *Env) *Promise

AtomConcat concatenates atom1 and atom2 and unifies it with atom3.

func AtomLength

func AtomLength(vm *VM, atom, length Term, k Cont, env *Env) *Promise

AtomLength counts the runes in atom and unifies the result with length.

func BagOf

func BagOf(vm *VM, template, goal, instances Term, k Cont, env *Env) *Promise

BagOf collects all the solutions of goal as instances, which unify with template. instances may contain duplications.

func Between

func Between(vm *VM, lower, upper, value Term, k Cont, env *Env) *Promise

Between succeeds when lower, upper, and value are all integers, and lower <= value <= upper. If value is a variable, it is unified with successive integers from lower to upper.

func Bool

func Bool(ok bool) *Promise

Bool returns a promise that simply returns (ok, nil).

func Call

func Call(vm *VM, goal Term, k Cont, env *Env) (promise *Promise)

Call executes goal. it succeeds if goal followed by k succeeds. A cut inside goal doesn't affect outside of Call.

func Call1

func Call1(vm *VM, closure, arg1 Term, k Cont, env *Env) *Promise

Call1 succeeds if closure with an additional argument succeeds.

func Call2

func Call2(vm *VM, closure, arg1, arg2 Term, k Cont, env *Env) *Promise

Call2 succeeds if closure with 2 additional arguments succeeds.

func Call3

func Call3(vm *VM, closure, arg1, arg2, arg3 Term, k Cont, env *Env) *Promise

Call3 succeeds if closure with 3 additional arguments succeeds.

func Call4

func Call4(vm *VM, closure, arg1, arg2, arg3, arg4 Term, k Cont, env *Env) *Promise

Call4 succeeds if closure with 4 additional arguments succeeds.

func Call5

func Call5(vm *VM, closure, arg1, arg2, arg3, arg4, arg5 Term, k Cont, env *Env) *Promise

Call5 succeeds if closure with 5 additional arguments succeeds.

func Call6

func Call6(vm *VM, closure, arg1, arg2, arg3, arg4, arg5, arg6 Term, k Cont, env *Env) *Promise

Call6 succeeds if closure with 6 additional arguments succeeds.

func Call7

func Call7(vm *VM, closure, arg1, arg2, arg3, arg4, arg5, arg6, arg7 Term, k Cont, env *Env) *Promise

Call7 succeeds if closure with 7 additional arguments succeeds.

func CallNth

func CallNth(vm *VM, goal, nth Term, k Cont, env *Env) *Promise

CallNth succeeds iff goal succeeds and nth unifies with the number of re-execution. See http://www.complang.tuwien.ac.at/ulrich/iso-prolog/call_nth

func Catch

func Catch(vm *VM, goal, catcher, recover Term, k Cont, env *Env) *Promise

Catch calls goal. If an exception is thrown and unifies with catcher, it calls recover.

func CharCode

func CharCode(vm *VM, char, code Term, k Cont, env *Env) *Promise

CharCode converts a single-rune Atom char to an Integer code, or vice versa.

func CharConversion

func CharConversion(vm *VM, inChar, outChar Term, k Cont, env *Env) *Promise

CharConversion registers a character conversion from inChar to outChar, or remove the conversion if inChar = outChar.

func Clause

func Clause(vm *VM, head, body Term, k Cont, env *Env) *Promise

Clause unifies head and body with H and B respectively where H :- B is in the database.

func Close

func Close(vm *VM, streamOrAlias, options Term, k Cont, env *Env) *Promise

Close closes a stream specified by streamOrAlias.

func Compare

func Compare(vm *VM, order, term1, term2 Term, k Cont, env *Env) *Promise

Compare compares term1 and term2 and unifies order with <, =, or >.

func Consult

func Consult(vm *VM, files Term, k Cont, env *Env) *Promise

Consult executes Prolog texts in files.

func CopyTerm

func CopyTerm(vm *VM, in, out Term, k Cont, env *Env) *Promise

CopyTerm clones in as out.

func CurrentCharConversion

func CurrentCharConversion(vm *VM, inChar, outChar Term, k Cont, env *Env) *Promise

CurrentCharConversion succeeds iff a conversion from inChar to outChar is defined.

func CurrentInput

func CurrentInput(vm *VM, stream Term, k Cont, env *Env) *Promise

CurrentInput unifies stream with the current input stream.

func CurrentOp

func CurrentOp(vm *VM, priority, specifier, op Term, k Cont, env *Env) *Promise

CurrentOp succeeds if operator is defined with priority and specifier.

func CurrentOutput

func CurrentOutput(vm *VM, stream Term, k Cont, env *Env) *Promise

CurrentOutput unifies stream with the current output stream.

func CurrentPredicate

func CurrentPredicate(vm *VM, pi Term, k Cont, env *Env) *Promise

CurrentPredicate matches pi with a predicate indicator of the user-defined procedures in the database.

func CurrentPrologFlag

func CurrentPrologFlag(vm *VM, flag, value Term, k Cont, env *Env) *Promise

CurrentPrologFlag succeeds iff flag is set to value.

func Delay

func Delay(k ...PromiseFunc) *Promise

Delay delays an execution of k. Should be used with reasonable quantity of k, otherwise prefer DelaySeq.

func DelaySeq

func DelaySeq(next NextFunc) *Promise

DelaySeq delays an execution of a sequence of promises.

func Equal

func Equal(_ *VM, e1, e2 Term, k Cont, env *Env) *Promise

Equal succeeds iff e1 equals to e2.

func Error

func Error(err error) *Promise

Error returns a promise that simply returns (false, err).

func ExpandTerm

func ExpandTerm(vm *VM, term1, term2 Term, k Cont, env *Env) *Promise

ExpandTerm transforms term1 according to term_expansion/2 and DCG rules then unifies with term2.

func Failure

func Failure(*Env) *Promise

Failure is a continuation that leads to false.

func FindAll

func FindAll(vm *VM, template, goal, instances Term, k Cont, env *Env) *Promise

FindAll collects all the solutions of goal as instances, which unify with template. instances may contain duplications.

func FlushOutput

func FlushOutput(vm *VM, streamOrAlias Term, k Cont, env *Env) *Promise

FlushOutput sends any buffered output to the stream.

func Functor

func Functor(vm *VM, t, name, arity Term, k Cont, env *Env) *Promise

Functor extracts the name and arity of term, or unifies term with an atomic/compound term of name and arity with fresh variables as arguments.

func GetByte

func GetByte(vm *VM, streamOrAlias, inByte Term, k Cont, env *Env) *Promise

GetByte reads a byte from the stream represented by streamOrAlias and unifies it with inByte.

func GetChar

func GetChar(vm *VM, streamOrAlias, char Term, k Cont, env *Env) *Promise

GetChar reads a character from the stream represented by streamOrAlias and unifies it with char.

func GetDict3

func GetDict3(vm *VM, keyPath Term, dict Term, result Term, cont Cont, env *Env) *Promise

GetDict3 return the value associated with keyPath. keyPath is either a single key or a term Key1/Key2/.... Each key is either an atom, small integer or a variable. While Dict.Key (see Op3) throws an existence error, this function fails silently if a key does not exist in the target dict.

func GreaterThan

func GreaterThan(_ *VM, e1, e2 Term, k Cont, env *Env) *Promise

GreaterThan succeeds iff e1 is greater than e2.

func GreaterThanOrEqual

func GreaterThanOrEqual(_ *VM, e1, e2 Term, k Cont, env *Env) *Promise

GreaterThanOrEqual succeeds iff e1 is greater than or equal to e2.

func Halt

func Halt(_ *VM, n Term, k Cont, env *Env) *Promise

Halt exits the process with exit code of n.

func Is

func Is(vm *VM, result, expression Term, k Cont, env *Env) *Promise

Is evaluates expression and unifies the result with result.

func KeySort

func KeySort(vm *VM, pairs, sorted Term, k Cont, env *Env) *Promise

KeySort succeeds if sorted is a sorted list of pairs based on their keys.

func Length

func Length(vm *VM, list, length Term, k Cont, env *Env) *Promise

Length succeeds iff list is a list of length.

func LessThan

func LessThan(_ *VM, e1, e2 Term, k Cont, env *Env) *Promise

LessThan succeeds iff e1 is less than e2.

func LessThanOrEqual

func LessThanOrEqual(_ *VM, e1, e2 Term, k Cont, env *Env) *Promise

LessThanOrEqual succeeds iff e1 is less than or equal to e2.

func Negate

func Negate(vm *VM, goal Term, k Cont, env *Env) *Promise

Negate calls goal and returns false if it succeeds. Otherwise, invokes the continuation.

func NotEqual

func NotEqual(_ *VM, e1, e2 Term, k Cont, env *Env) *Promise

NotEqual succeeds iff e1 doesn't equal to e2.

func Nth0

func Nth0(vm *VM, n, list, elem Term, k Cont, env *Env) *Promise

Nth0 succeeds if elem is the n-th element of list, counting from 0.

func Nth1

func Nth1(vm *VM, n, list, elem Term, k Cont, env *Env) *Promise

Nth1 succeeds if elem is the n-th element of list, counting from 1.

func NumberChars

func NumberChars(vm *VM, num, chars Term, k Cont, env *Env) *Promise

NumberChars breaks up an atom representation of a number num into a list of characters and unifies it with chars, or constructs a number from a list of characters chars and unifies it with num.

func NumberCodes

func NumberCodes(vm *VM, num, codes Term, k Cont, env *Env) *Promise

NumberCodes breaks up an atom representation of a number num into a list of runes and unifies it with codes, or constructs a number from a list of runes codes and unifies it with num.

func Op

func Op(vm *VM, priority, specifier, op Term, k Cont, env *Env) *Promise

Op defines operator with priority and specifier, or removes when priority is 0.

func Op3

func Op3(vm *VM, dict, function, result Term, cont Cont, env *Env) *Promise

Op3 primarily evaluates "./2" terms within Dict expressions. If the provided Function is an atom, the function checks for the corresponding key in the Dict, raising an exception if the key is missing. For compound terms, it interprets Function as a call to a predefined set of functions, processing it accordingly.

func Open

func Open(vm *VM, sourceSink, mode, stream, options Term, k Cont, env *Env) *Promise

Open opens SourceSink in mode and unifies with stream.

func PeekByte

func PeekByte(vm *VM, streamOrAlias, inByte Term, k Cont, env *Env) *Promise

PeekByte peeks a byte from the stream represented by streamOrAlias and unifies it with inByte.

func PeekChar

func PeekChar(vm *VM, streamOrAlias, char Term, k Cont, env *Env) *Promise

PeekChar peeks a rune from the stream represented by streamOrAlias and unifies it with char.

func Phrase

func Phrase(vm *VM, grBody, s0, s Term, k Cont, env *Env) *Promise

Phrase succeeds if the difference list of s0-s satisfies the grammar rule of grBody.

func PutByte

func PutByte(vm *VM, streamOrAlias, byt Term, k Cont, env *Env) *Promise

PutByte outputs an integer byte to a stream represented by streamOrAlias.

func PutChar

func PutChar(vm *VM, streamOrAlias, char Term, k Cont, env *Env) *Promise

PutChar outputs char to the stream represented by streamOrAlias.

func PutDict3

func PutDict3(vm *VM, new Term, dictIn Term, dictOut Term, cont Cont, env *Env) *Promise

PutDict3 evaluates to a new dict where the key-values in dictIn replace or extend the key-values in the original dict.

new is either a dict or list of attribute-value pairs using the syntax Key:Value, Key=Value, Key-Value or Key(Value)

func ReadTerm

func ReadTerm(vm *VM, streamOrAlias, out, options Term, k Cont, env *Env) *Promise

ReadTerm reads from the stream represented by streamOrAlias and unifies with stream.

func Repeat

func Repeat(_ *VM, k Cont, env *Env) *Promise

Repeat repeats the continuation until it succeeds.

func Retract

func Retract(vm *VM, t Term, k Cont, env *Env) *Promise

Retract removes the first clause that matches with t.

func SetInput

func SetInput(vm *VM, streamOrAlias Term, k Cont, env *Env) *Promise

SetInput sets streamOrAlias as the current input stream.

func SetOf

func SetOf(vm *VM, template, goal, instances Term, k Cont, env *Env) *Promise

SetOf collects all the solutions of goal as instances, which unify with template. instances don't contain duplications.

func SetOutput

func SetOutput(vm *VM, streamOrAlias Term, k Cont, env *Env) *Promise

SetOutput sets streamOrAlias as the current output stream.

func SetPrologFlag

func SetPrologFlag(vm *VM, flag, value Term, k Cont, env *Env) *Promise

SetPrologFlag sets flag to value.

func SetStreamPosition

func SetStreamPosition(vm *VM, streamOrAlias, position Term, k Cont, env *Env) *Promise

SetStreamPosition sets the position property of the stream represented by streamOrAlias.

func SkipMaxList

func SkipMaxList(vm *VM, skip, max, list, suffix Term, k Cont, env *Env) *Promise

SkipMaxList iterates over list up to max elements and unifies the number of skipped elements with skip and the rest with suffix.

func Sort

func Sort(vm *VM, list, sorted Term, k Cont, env *Env) *Promise

Sort succeeds if sorted list of elements of list unifies with sorted.

func StreamProperty

func StreamProperty(vm *VM, stream, property Term, k Cont, env *Env) *Promise

StreamProperty succeeds iff the stream represented by stream has the stream property.

func SubAtom

func SubAtom(vm *VM, atom, before, length, after, subAtom Term, k Cont, env *Env) *Promise

SubAtom unifies subAtom with a sub atom of length which appears with before runes preceding it and after runes following it.

func SubsumesTerm

func SubsumesTerm(_ *VM, general, specific Term, k Cont, env *Env) *Promise

SubsumesTerm succeeds if general and specific are unifiable without binding variables in specific.

func Succ

func Succ(vm *VM, x, s Term, k Cont, env *Env) *Promise

Succ succeeds if s is the successor of non-negative integer x.

func Success

func Success(*Env) *Promise

Success is a continuation that leads to true.

func TermVariables

func TermVariables(vm *VM, term, vars Term, k Cont, env *Env) *Promise

TermVariables succeeds if vars unifies with a list of variables in term.

func Throw

func Throw(_ *VM, ball Term, _ Cont, env *Env) *Promise

Throw throws ball as an exception.

func TypeAtom

func TypeAtom(_ *VM, t Term, k Cont, env *Env) *Promise

TypeAtom checks if t is an atom.

func TypeCompound

func TypeCompound(_ *VM, t Term, k Cont, env *Env) *Promise

TypeCompound checks if t is a compound term.

func TypeFloat

func TypeFloat(_ *VM, t Term, k Cont, env *Env) *Promise

TypeFloat checks if t is a floating-point number.

func TypeInteger

func TypeInteger(_ *VM, t Term, k Cont, env *Env) *Promise

TypeInteger checks if t is an integer.

func TypeVar

func TypeVar(_ *VM, t Term, k Cont, env *Env) *Promise

TypeVar checks if t is a variable.

func Unify

func Unify(_ *VM, x, y Term, k Cont, env *Env) *Promise

Unify unifies x and y without occurs check (i.e., X = f(X) is allowed).

func UnifyWithOccursCheck

func UnifyWithOccursCheck(_ *VM, x, y Term, k Cont, env *Env) *Promise

UnifyWithOccursCheck unifies x and y with occurs check (i.e., X = f(X) is not allowed).

func Univ

func Univ(vm *VM, t, list Term, k Cont, env *Env) *Promise

Univ constructs list as a list which first element is the functor of term and the rest is the arguments of term, or construct a compound from list as term.

func WriteTerm

func WriteTerm(vm *VM, streamOrAlias, t, options Term, k Cont, env *Env) *Promise

WriteTerm outputs term to stream with options.

func (*Promise) Force

func (p *Promise) Force(ctx context.Context) (ok bool, err error)

Force enforces the delayed execution and returns the result. (i.e. trampoline)

type PromiseFunc

type PromiseFunc = func(context.Context) *Promise

PromiseFunc defines the type of a function that returns a promise.

type Stream

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

Stream is a prolog stream.

func NewInputBinaryStream

func NewInputBinaryStream(r io.Reader) *Stream

NewInputBinaryStream creates a new input binary stream backed by the given io.Reader.

func NewInputTextStream

func NewInputTextStream(r io.Reader) *Stream

NewInputTextStream creates a new input text stream backed by the given io.Reader.

func NewOutputBinaryStream

func NewOutputBinaryStream(w io.Writer) *Stream

NewOutputBinaryStream creates a new output binary stream backed by the given io.Writer.

func NewOutputTextStream

func NewOutputTextStream(w io.Writer) *Stream

NewOutputTextStream creates a new output text stream backed by the given io.Writer.

func (*Stream) Close

func (s *Stream) Close() error

Close closes the underlying source/sink.

func (*Stream) Compare

func (s *Stream) Compare(t Term, env *Env) int

Compare compares the Stream with a Term.

func (*Stream) Flush

func (s *Stream) Flush() error

Flush flushes the buffered output to the sink.

func (*Stream) Name

func (s *Stream) Name() string

Name returns the stream's name. If the underlying source/sink doesn't have a name, returns "".

func (*Stream) ReadByte

func (s *Stream) ReadByte() (byte, error)

ReadByte reads a byte from the underlying source. It throws an error if the stream is not an input binary stream.

func (*Stream) ReadRune

func (s *Stream) ReadRune() (r rune, size int, err error)

ReadRune reads the next rune from the underlying source. It throws an error if the stream is not an input text stream.

func (*Stream) Seek

func (s *Stream) Seek(offset int64, whence int) (int64, error)

Seek sets the offset to the underlying source/sink.

func (*Stream) UnreadByte

func (s *Stream) UnreadByte() error

func (*Stream) UnreadRune

func (s *Stream) UnreadRune() error

func (*Stream) WriteByte

func (s *Stream) WriteByte(c byte) error

WriteByte writes the byte c to the underlying sink. It throws an error if the stream is not an output binary stream,.

func (*Stream) WriteRune

func (s *Stream) WriteRune(r rune) (size int, err error)

WriteRune writes the rune r to the underlying sink. It throws an error if the stream is not an output binary stream.

func (*Stream) WriteTerm

func (s *Stream) WriteTerm(w io.Writer, _ *WriteOptions, _ *Env) error

WriteTerm outputs the Stream to an io.Writer.

type Term

type Term interface {
	WriteTerm(w io.Writer, opts *WriteOptions, env *Env) error
	Compare(t Term, env *Env) int
}

Term is a prolog term.

func CharList

func CharList(s string) Term

CharList returns a character list.

func CodeList

func CodeList(s string) Term

CodeList returns a character code list.

func Cons

func Cons(car, cdr Term) Term

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

func List

func List(ts ...Term) Term

List returns a list of ts.

func PartialList

func PartialList(tail Term, ts ...Term) Term

PartialList returns a list of ts followed by tail.

type Token

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

Token is a smallest meaningful unit of prolog program.

func (Token) String

func (t Token) String() string

type VM

type VM struct {
	// Unknown is a callback that is triggered when the VM reaches to an unknown predicate while current_prolog_flag(unknown, warning).
	Unknown func(name Atom, args []Term, env *Env)

	// FS is a file system that is referenced when the VM loads Prolog texts e.g. ensure_loaded/1.
	// It has no effect on open/4 nor open/3 which always access the actual file system.
	FS fs.FS
	// contains filtered or unexported fields
}

VM is the core of a Prolog interpreter. The zero value for VM is a valid VM without any builtin predicates.

func (*VM) Arrive

func (vm *VM) Arrive(name Atom, args []Term, k Cont, env *Env) (promise *Promise)

Arrive is the entry point of the VM.

func (*VM) ClearHook

func (vm *VM) ClearHook()

ClearHook removes the installed hook function from the VM.

func (*VM) Compile

func (vm *VM) Compile(ctx context.Context, s string, args ...interface{}) error

Compile compiles the Prolog text and updates the DB accordingly.

func (*VM) InstallHook

func (vm *VM) InstallHook(f HookFunc)

InstallHook sets the given hook function in the VM.

func (*VM) Register0

func (vm *VM) Register0(name Atom, p Predicate0)

Register0 registers a predicate of arity 0.

func (*VM) Register1

func (vm *VM) Register1(name Atom, p Predicate1)

Register1 registers a predicate of arity 1.

func (*VM) Register2

func (vm *VM) Register2(name Atom, p Predicate2)

Register2 registers a predicate of arity 2.

func (*VM) Register3

func (vm *VM) Register3(name Atom, p Predicate3)

Register3 registers a predicate of arity 3.

func (*VM) Register4

func (vm *VM) Register4(name Atom, p Predicate4)

Register4 registers a predicate of arity 4.

func (*VM) Register5

func (vm *VM) Register5(name Atom, p Predicate5)

Register5 registers a predicate of arity 5.

func (*VM) Register6

func (vm *VM) Register6(name Atom, p Predicate6)

Register6 registers a predicate of arity 6.

func (*VM) Register7

func (vm *VM) Register7(name Atom, p Predicate7)

Register7 registers a predicate of arity 7.

func (*VM) Register8

func (vm *VM) Register8(name Atom, p Predicate8)

Register8 registers a predicate of arity 8.

func (*VM) ResetEnv

func (vm *VM) ResetEnv()

ResetEnv is used to reset all global variable

func (*VM) SetMaxVariables

func (vm *VM) SetMaxVariables(n uint64)

SetMaxVariables sets the maximum number of variables that the VM can create. Zero value mean no limits

func (*VM) SetUserInput

func (vm *VM) SetUserInput(s *Stream)

SetUserInput sets the given stream as user_input.

func (*VM) SetUserOutput

func (vm *VM) SetUserOutput(s *Stream)

SetUserOutput sets the given stream as user_output.

type Variable

type Variable int64

Variable is a prolog variable.

func NewVariable

func NewVariable() Variable

NewVariable creates a new anonymous variable.

func (Variable) Compare

func (v Variable) Compare(t Term, env *Env) int

func (Variable) WriteTerm

func (v Variable) WriteTerm(w io.Writer, opts *WriteOptions, env *Env) error

type WriteOptions

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

WriteOptions specify how the Term writes itself.

Jump to

Keyboard shortcuts

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