engine

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2021 License: MIT Imports: 14 Imported by: 39

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFunctionSet = FunctionSet{
	Unary: map[term.Atom]func(term.Interface, *term.Env) (term.Interface, error){
		"-":        unaryNumber(func(i int64) int64 { return -1 * i }, func(n float64) float64 { return -1 * n }),
		"abs":      unaryFloat(math.Abs),
		"atan":     unaryFloat(math.Atan),
		"ceiling":  unaryFloat(math.Ceil),
		"cos":      unaryFloat(math.Cos),
		"exp":      unaryFloat(math.Exp),
		"sqrt":     unaryFloat(math.Sqrt),
		"sign":     unaryNumber(sgn, sgnf),
		"float":    unaryFloat(func(n float64) float64 { return n }),
		"floor":    unaryFloat(math.Floor),
		"log":      unaryFloat(math.Log),
		"sin":      unaryFloat(math.Sin),
		"truncate": unaryFloat(math.Trunc),
		"round":    unaryFloat(math.Round),
		"\\":       unaryInteger(func(i int64) int64 { return ^i }),
	},
	Binary: map[term.Atom]func(term.Interface, term.Interface, *term.Env) (term.Interface, error){
		"+":   binaryNumber(func(i, j int64) int64 { return i + j }, func(n, m float64) float64 { return n + m }),
		"-":   binaryNumber(func(i, j int64) int64 { return i - j }, func(n, m float64) float64 { return n - m }),
		"*":   binaryNumber(func(i, j int64) int64 { return i * j }, func(n, m float64) float64 { return n * m }),
		"/":   binaryFloat(func(n float64, m float64) float64 { return n / m }),
		"//":  binaryInteger(func(i, j int64) int64 { return i / j }),
		"rem": binaryInteger(func(i, j int64) int64 { return i % j }),
		"mod": binaryInteger(func(i, j int64) int64 { return (i%j + j) % j }),
		"**":  binaryFloat(math.Pow),
		">>":  binaryInteger(func(i, j int64) int64 { return i >> j }),
		"<<":  binaryInteger(func(i, j int64) int64 { return i << j }),
		"/\\": binaryInteger(func(i, j int64) int64 { return i & j }),
		"\\/": binaryInteger(func(i, j int64) int64 { return i | j }),
	},
}

DefaultFunctionSet is a FunctionSet with builtin functions.

Functions

func Arg

func Arg(nth, t, arg term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func AtomChars

func AtomChars(atom, chars term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.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(atom, codes term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.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(atom1, atom2, atom3 term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

AtomConcat concatenates atom1 and atom2 and unifies it with atom3.

func AtomLength

func AtomLength(atom, length term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func CharCode

func CharCode(char, code term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func Compare

func Compare(order, term1, term2 term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func CopyTerm

func CopyTerm(in, out term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

CopyTerm clones in as out.

func Each

func Each(list term.Interface, f func(elem term.Interface) error, env term.Env) error

Each iterates over list.

func Failure

func Failure(_ term.Env) *nondet.Promise

func Functor

func Functor(t, name, arity term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.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 NumberChars

func NumberChars(num, chars term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.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(num, codes term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.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 Repeat

func Repeat(k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

Repeat enforces k until it returns true.

func SubAtom

func SubAtom(atom, before, length, after, subAtom term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func Success

func Success(_ term.Env) *nondet.Promise

func Throw

func Throw(ball term.Interface, _ func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

Throw throws ball as an exception.

func TypeAtom

func TypeAtom(t term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

TypeAtom checks if t is an atom.

func TypeCompound

func TypeCompound(t term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

TypeCompound checks if t is a compound term.

func TypeFloat

func TypeFloat(t term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

TypeFloat checks if t is a floating-point number.

func TypeInteger

func TypeInteger(t term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

TypeInteger checks if t is an integer.

func TypeVar

func TypeVar(t term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

TypeVar checks if t is a variable.

func Unify

func Unify(t1, t2 term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func UnifyWithOccursCheck

func UnifyWithOccursCheck(t1, t2 term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func Univ

func Univ(t, list term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.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.

Types

type Exception

type Exception struct {
	Term term.Interface
}

Exception is an error represented by a prolog term.

func (*Exception) Error

func (e *Exception) Error() string

type FunctionSet

type FunctionSet struct {
	Unary  map[term.Atom]func(x term.Interface, env *term.Env) (term.Interface, error)
	Binary map[term.Atom]func(x, y term.Interface, env *term.Env) (term.Interface, error)
}

FunctionSet is a set of unary/binary functions.

func (FunctionSet) Equal

func (fs FunctionSet) Equal(lhs, rhs term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

Equal succeeds iff lhs equals to rhs.

func (FunctionSet) GreaterThan

func (fs FunctionSet) GreaterThan(lhs, rhs term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

GreaterThan succeeds iff lhs is greater than rhs.

func (FunctionSet) GreaterThanOrEqual

func (fs FunctionSet) GreaterThanOrEqual(lhs, rhs term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

GreaterThanOrEqual succeeds iff lhs is greater than or equal to rhs.

func (FunctionSet) Is

func (fs FunctionSet) Is(result, expression term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

Is evaluates expression and unifies the result with result.

func (FunctionSet) LessThan

func (fs FunctionSet) LessThan(lhs, rhs term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

LessThan succeeds iff lhs is less than rhs.

func (FunctionSet) LessThanOrEqual

func (fs FunctionSet) LessThanOrEqual(lhs, rhs term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

LessThanOrEqual succeeds iff lhs is less than or equal to rhs.

func (FunctionSet) NotEqual

func (fs FunctionSet) NotEqual(lhs, rhs term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

NotEqual succeeds iff lhs doesn't equal to rhs.

type VM

type VM struct {
	OnCall, OnExit, OnFail, OnRedo func(pi string, args term.Interface, env term.Env)

	UnknownWarning func(procedure string)

	Operators       term.Operators
	CharConversions map[rune]rune
	// 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) Abolish

func (vm *VM) Abolish(pi term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

Abolish removes the procedure indicated by pi from the database.

func (*VM) Asserta

func (vm *VM) Asserta(t term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

Asserta prepends t to the database.

func (*VM) Assertz

func (vm *VM) Assertz(t term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

Assertz appends t to the database.

func (*VM) BagOf

func (vm *VM) BagOf(template, goal, instances term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) Call

func (vm *VM) Call(goal term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) Catch

func (vm *VM) Catch(goal, catcher, recover term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) CharConversion

func (vm *VM) CharConversion(inChar, outChar term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) Clause

func (vm *VM) Clause(head, body term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) Close

func (vm *VM) Close(streamOrAlias, options term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

Close closes a stream specified by streamOrAlias.

func (*VM) Conjunction

func (vm *VM) Conjunction(p, q term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

Conjunction executes the given goals p then q. Note that ,/2 in clause body (H :- B1, B2, ..., Bn.) is compiled and doesn't involve this method.

func (*VM) CurrentCharConversion

func (vm *VM) CurrentCharConversion(inChar, outChar term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) CurrentInput

func (vm *VM) CurrentInput(stream term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

CurrentInput unifies stream with the current input stream.

func (*VM) CurrentOp

func (vm *VM) CurrentOp(priority, specifier, operator term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

CurrentOp succeeds if operator is defined with priority and specifier.

func (*VM) CurrentOutput

func (vm *VM) CurrentOutput(stream term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

CurrentOutput unifies stream with the current output stream.

func (*VM) CurrentPredicate

func (vm *VM) CurrentPredicate(pi term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) CurrentPrologFlag

func (vm *VM) CurrentPrologFlag(flag, value term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

CurrentPrologFlag succeeds iff flag is set to value.

func (*VM) DescribeTerm

func (vm *VM) DescribeTerm(t term.Interface, env term.Env) string

func (*VM) Disjunction

func (vm *VM) Disjunction(p, q term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

func (*VM) Dynamic

func (vm *VM) Dynamic(pi term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

func (*VM) FlushOutput

func (vm *VM) FlushOutput(streamOrAlias term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

FlushOutput sends any buffered output to the stream.

func (*VM) GetByte

func (vm *VM) GetByte(streamOrAlias, inByte term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) GetChar

func (vm *VM) GetChar(streamOrAlias, char term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) Halt

func (vm *VM) Halt(n term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

Halt exits the process with exit code of n.

func (*VM) IfThenElse

func (vm *VM) IfThenElse(if_, then_, else_ term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

func (*VM) Negation

func (vm *VM) Negation(goal term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

func (*VM) Op

func (vm *VM) Op(priority, specifier, operator term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) Open

func (vm *VM) Open(SourceSink, mode, stream, options term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

Open opens SourceSink in mode and unifies with stream.

func (*VM) PeekByte

func (vm *VM) PeekByte(streamOrAlias, inByte term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) PeekChar

func (vm *VM) PeekChar(streamOrAlias, char term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) PutByte

func (vm *VM) PutByte(streamOrAlias, byt term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) PutCode

func (vm *VM) PutCode(streamOrAlias, code term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

PutCode outputs code to the stream represented by streamOrAlias.

func (*VM) ReadTerm

func (vm *VM) ReadTerm(streamOrAlias, out, options term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) Register0

func (vm *VM) Register0(name string, p func(func(term.Env) *nondet.Promise, *term.Env) *nondet.Promise)

Register0 registers a predicate of arity 0.

func (*VM) Register1

func (vm *VM) Register1(name string, p func(term.Interface, func(term.Env) *nondet.Promise, *term.Env) *nondet.Promise)

Register1 registers a predicate of arity 1.

func (*VM) Register2

func (vm *VM) Register2(name string, p func(term.Interface, term.Interface, func(term.Env) *nondet.Promise, *term.Env) *nondet.Promise)

Register2 registers a predicate of arity 2.

func (*VM) Register3

func (vm *VM) Register3(name string, p func(term.Interface, term.Interface, term.Interface, func(term.Env) *nondet.Promise, *term.Env) *nondet.Promise)

Register3 registers a predicate of arity 3.

func (*VM) Register4

func (vm *VM) Register4(name string, p func(term.Interface, term.Interface, term.Interface, term.Interface, func(term.Env) *nondet.Promise, *term.Env) *nondet.Promise)

Register4 registers a predicate of arity 4.

func (*VM) Register5

Register5 registers a predicate of arity 5.

func (*VM) Retract

func (vm *VM) Retract(t term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

Retract removes a clause which matches with t.

func (*VM) SetInput

func (vm *VM) SetInput(streamOrAlias term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

SetInput sets streamOrAlias as the current input stream.

func (*VM) SetOf

func (vm *VM) SetOf(template, goal, instances term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) SetOutput

func (vm *VM) SetOutput(streamOrAlias term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

SetOutput sets streamOrAlias as the current output stream.

func (*VM) SetPrologFlag

func (vm *VM) SetPrologFlag(flag, value term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

SetPrologFlag sets flag to value.

func (*VM) SetStreamPosition

func (vm *VM) SetStreamPosition(streamOrAlias, position term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) SetUserInput

func (vm *VM) SetUserInput(r io.Reader)

SetUserInput sets the given reader as a stream with an alias of user_input.

func (*VM) SetUserOutput

func (vm *VM) SetUserOutput(w io.Writer)

SetUserOutput sets the given writer as a stream with an alias of user_output.

func (*VM) StreamProperty

func (vm *VM) StreamProperty(streamOrAlias, property term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

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

func (*VM) WriteTerm

func (vm *VM) WriteTerm(streamOrAlias, t, options term.Interface, k func(term.Env) *nondet.Promise, env *term.Env) *nondet.Promise

WriteTerm outputs term to stream with options.

Jump to

Keyboard shortcuts

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