prolog

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2021 License: MIT Imports: 13 Imported by: 32

README

ichiban/prolog

A minimal prolog interpreter based on ZIP.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFunctionSet = FunctionSet{
	Unary: map[Atom]func(Term) (Term, 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[Atom]func(Term, Term) (Term, 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 }),
	},
}

Functions

func Arg

func Arg(arg, term, value Term, k func() (bool, error)) (bool, error)

func AtomChars

func AtomChars(atom, chars Term, k func() (bool, error)) (bool, error)

func AtomCodes

func AtomCodes(atom, codes Term, k func() (bool, error)) (bool, error)

func AtomConcat

func AtomConcat(atom1, atom2, atom3 Term, k func() (bool, error)) (bool, error)

func AtomLength

func AtomLength(atom, integer Term, k func() (bool, error)) (bool, error)

func CharCode

func CharCode(char, code Term, k func() (bool, error)) (bool, error)

func Compare

func Compare(order, term1, term2 Term, k func() (bool, error)) (bool, error)

func Contains

func Contains(t, s Term) bool

Contains checks if t contains s.

func CopyTerm

func CopyTerm(in, out Term, k func() (bool, error)) (bool, error)

func Cut

func Cut(k func() (bool, error)) (bool, error)

func Done

func Done() (bool, error)

Done terminates a continuation chain.

func Each

func Each(list Term, f func(elem Term) error) error

Each iterates over list.

func Functor

func Functor(term, name, arity Term, k func() (bool, error)) (bool, error)

func NumberChars

func NumberChars(num, chars Term, k func() (bool, error)) (bool, error)

func NumberCodes

func NumberCodes(num, chars Term, k func() (bool, error)) (bool, error)

func Repeat

func Repeat(k func() (bool, error)) (bool, error)

func SubAtom

func SubAtom(atom, before, length, after, subAtom Term, k func() (bool, error)) (bool, error)

func Throw

func Throw(t Term, _ func() (bool, error)) (bool, error)

func TypeAtom

func TypeAtom(t Term, k func() (bool, error)) (bool, error)

func TypeCompound

func TypeCompound(t Term, k func() (bool, error)) (bool, error)

func TypeFloat

func TypeFloat(t Term, k func() (bool, error)) (bool, error)

func TypeInteger

func TypeInteger(t Term, k func() (bool, error)) (bool, error)

func TypeVar

func TypeVar(t Term, k func() (bool, error)) (bool, error)

func Unify

func Unify(t1, t2 Term, k func() (bool, error)) (bool, error)

func UnifyWithOccursCheck

func UnifyWithOccursCheck(t1, t2 Term, k func() (bool, error)) (bool, error)

func Univ

func Univ(term, list Term, k func() (bool, error)) (bool, error)

Types

type Atom

type Atom string

Atom is a prolog atom.

func (Atom) Copy

func (a Atom) Copy() Term

Copy simply returns the atom.

func (Atom) String

func (a Atom) String() string

func (Atom) Unify

func (a Atom) Unify(t Term, occursCheck bool) bool

Unify unifies the atom with t.

func (Atom) WriteTerm

func (a Atom) WriteTerm(w io.Writer, opts WriteTermOptions) error

WriteTerm writes the atom into w.

type Compound

type Compound struct {
	Functor Atom
	Args    []Term
}

Compound is a prolog compound.

func (*Compound) Copy

func (c *Compound) Copy() Term

Copy returns a fresh compound with copies of arguments.

func (*Compound) String

func (c *Compound) String() string

func (*Compound) Unify

func (c *Compound) Unify(t Term, occursCheck bool) bool

Unify unifies the compound with t.

func (*Compound) WriteTerm

func (c *Compound) WriteTerm(w io.Writer, opts WriteTermOptions) error

WriteTerm writes the compound into w.

type Engine

type Engine struct {
	EngineState
}

Engine is a Prolog interpreter. The zero value for Engine is a valid interpreter without any builtin predicates.

func NewEngine

func NewEngine(in io.Reader, out io.Writer) (*Engine, error)

NewEngine creates an Engine with user_input, user_output and builtin predicates. It is encouraged user_input to be *bufio.Reader since it enables a wide range of input operations.

func (*Engine) Describe

func (e *Engine) Describe(v *Variable) string

Describe stringifies the given variable in the format of 'V = f(a, b)'.

func (*Engine) Exec

func (e *Engine) Exec(s string) error

Exec executes a prolog program.

func (*Engine) Query

func (e *Engine) Query(s string, cb func(vars []*Variable) bool) (bool, error)

Query executes a prolog query and calls the callback function for each solution. Returning true from the callback halts the iteration.

func (*Engine) Register0

func (e *Engine) Register0(name string, p func(func() (bool, error)) (bool, error))

Register0 registers a predicate of arity 0.

func (*Engine) Register1

func (e *Engine) Register1(name string, p func(Term, func() (bool, error)) (bool, error))

Register1 registers a predicate of arity 1.

func (*Engine) Register2

func (e *Engine) Register2(name string, p func(Term, Term, func() (bool, error)) (bool, error))

Register2 registers a predicate of arity 2.

func (*Engine) Register3

func (e *Engine) Register3(name string, p func(Term, Term, Term, func() (bool, error)) (bool, error))

Register3 registers a predicate of arity 3.

func (*Engine) Register4

func (e *Engine) Register4(name string, p func(Term, Term, Term, Term, func() (bool, error)) (bool, error))

Register4 registers a predicate of arity 4.

func (*Engine) Register5

func (e *Engine) Register5(name string, p func(Term, Term, Term, Term, Term, func() (bool, error)) (bool, error))

Register5 registers a predicate of arity 5.

type EngineState

type EngineState struct {
	// BeforeHalt is a hook which gets triggered right before halt/0 or halt/1.
	BeforeHalt []func()
	// contains filtered or unexported fields
}

EngineState is an internal state of Engine, a subject to many builtin predicates.

func (*EngineState) Abolish

func (e *EngineState) Abolish(t Term, k func() (bool, error)) (bool, error)

func (*EngineState) Asserta

func (e *EngineState) Asserta(t Term, k func() (bool, error)) (bool, error)

func (*EngineState) Assertz

func (e *EngineState) Assertz(t Term, k func() (bool, error)) (bool, error)

func (*EngineState) BagOf

func (e *EngineState) BagOf(template, goal, bag Term, k func() (bool, error)) (bool, error)

func (*EngineState) Call

func (e *EngineState) Call(goal Term, k func() (bool, error)) (bool, error)

func (*EngineState) Catch

func (e *EngineState) Catch(goal, catcher, recover Term, k func() (bool, error)) (bool, error)

func (*EngineState) CharConversion

func (e *EngineState) CharConversion(char1, char2 Term, k func() (bool, error)) (bool, error)

func (*EngineState) Clause

func (e *EngineState) Clause(head, body Term, k func() (bool, error)) (bool, error)

func (*EngineState) Close

func (e *EngineState) Close(stream, options Term, k func() (bool, error)) (bool, error)

func (*EngineState) CurrentCharConversion

func (e *EngineState) CurrentCharConversion(char1, char2 Term, k func() (bool, error)) (bool, error)

func (*EngineState) CurrentInput

func (e *EngineState) CurrentInput(stream Term, k func() (bool, error)) (bool, error)

func (*EngineState) CurrentOp

func (e *EngineState) CurrentOp(precedence, typ, name Term, k func() (bool, error)) (bool, error)

func (*EngineState) CurrentOutput

func (e *EngineState) CurrentOutput(stream Term, k func() (bool, error)) (bool, error)

func (*EngineState) CurrentPredicate

func (e *EngineState) CurrentPredicate(pf Term, k func() (bool, error)) (bool, error)

func (*EngineState) CurrentPrologFlag

func (e *EngineState) CurrentPrologFlag(flag, value Term, k func() (bool, error)) (bool, error)

func (*EngineState) FlushOutput

func (e *EngineState) FlushOutput(stream Term, k func() (bool, error)) (bool, error)

func (*EngineState) GetByte

func (e *EngineState) GetByte(stream, byt Term, k func() (bool, error)) (bool, error)

func (*EngineState) GetCode

func (e *EngineState) GetCode(stream, code Term, k func() (bool, error)) (bool, error)

func (*EngineState) Halt

func (e *EngineState) Halt(n Term, k func() (bool, error)) (bool, error)

func (*EngineState) Op

func (e *EngineState) Op(precedence, typ, name Term, k func() (bool, error)) (bool, error)

func (*EngineState) Open

func (e *EngineState) Open(filename, mode, stream, options Term, k func() (bool, error)) (bool, error)

func (*EngineState) PeekByte

func (e *EngineState) PeekByte(stream, byt Term, k func() (bool, error)) (bool, error)

func (*EngineState) PeekCode

func (e *EngineState) PeekCode(stream, code Term, k func() (bool, error)) (bool, error)

func (*EngineState) PutByte

func (e *EngineState) PutByte(stream, byt Term, k func() (bool, error)) (bool, error)

func (*EngineState) PutCode

func (e *EngineState) PutCode(stream, code Term, k func() (bool, error)) (bool, error)

func (*EngineState) ReadTerm

func (e *EngineState) ReadTerm(stream, term, options Term, k func() (bool, error)) (bool, error)

func (*EngineState) Retract

func (e *EngineState) Retract(t Term, k func() (bool, error)) (bool, error)

func (*EngineState) SetInput

func (e *EngineState) SetInput(stream Term, k func() (bool, error)) (bool, error)

func (*EngineState) SetOf

func (e *EngineState) SetOf(template, goal, bag Term, k func() (bool, error)) (bool, error)

func (*EngineState) SetOutput

func (e *EngineState) SetOutput(stream Term, k func() (bool, error)) (bool, error)

func (*EngineState) SetPrologFlag

func (e *EngineState) SetPrologFlag(flag, value Term, k func() (bool, error)) (bool, error)

func (*EngineState) SetStreamPosition

func (e *EngineState) SetStreamPosition(stream, pos Term, k func() (bool, error)) (bool, error)

func (*EngineState) StreamProperty

func (e *EngineState) StreamProperty(stream, property Term, k func() (bool, error)) (bool, error)

func (*EngineState) WriteTerm

func (e *EngineState) WriteTerm(stream, term, options Term, k func() (bool, error)) (bool, error)

type Exception

type Exception struct {
	Term
}

func (*Exception) Error

func (e *Exception) Error() string

type Float

type Float float64

Float is a prolog floating-point number.

func (Float) Copy

func (f Float) Copy() Term

Copy simply returns the float.

func (Float) String

func (f Float) String() string

func (Float) Unify

func (f Float) Unify(t Term, occursCheck bool) bool

Unify unifies the float with t.

func (Float) WriteTerm

func (f Float) WriteTerm(w io.Writer, _ WriteTermOptions) error

WriteTerm writes the float into w.

type FunctionSet

type FunctionSet struct {
	Unary  map[Atom]func(x Term) (Term, error)
	Binary map[Atom]func(x, y Term) (Term, error)
}

func (FunctionSet) Equal

func (fs FunctionSet) Equal(lhs, rhs Term, k func() (bool, error)) (bool, error)

func (FunctionSet) GreaterThan

func (fs FunctionSet) GreaterThan(lhs, rhs Term, k func() (bool, error)) (bool, error)

func (FunctionSet) GreaterThanOrEqual

func (fs FunctionSet) GreaterThanOrEqual(lhs, rhs Term, k func() (bool, error)) (bool, error)

func (FunctionSet) Is

func (fs FunctionSet) Is(lhs, rhs Term, k func() (bool, error)) (bool, error)

func (FunctionSet) LessThan

func (fs FunctionSet) LessThan(lhs, rhs Term, k func() (bool, error)) (bool, error)

func (FunctionSet) LessThanOrEqual

func (fs FunctionSet) LessThanOrEqual(lhs, rhs Term, k func() (bool, error)) (bool, error)

func (FunctionSet) NotEqual

func (fs FunctionSet) NotEqual(lhs, rhs Term, k func() (bool, error)) (bool, error)

type Integer

type Integer int64

Integer is a prolog integer.

func (Integer) Copy

func (i Integer) Copy() Term

Copy simply returns the integer.

func (Integer) String

func (i Integer) String() string

func (Integer) Unify

func (i Integer) Unify(t Term, occursCheck bool) bool

Unify unifies the integer with t.

func (Integer) WriteTerm

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

WriteTerm writes the integer into w.

type Operator

type Operator struct {
	Precedence Integer // 1 ~ 1200
	Type       Atom
	Name       Atom
}

Operator is an operator definition.

type Operators

type Operators []Operator

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

type Parser

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

Parser turns bytes into Term.

func NewParser

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

NewParser creates a Parser.

func (*Parser) Clause

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

Clause parses a clause, term followed by a full stop.

func (*Parser) Term

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

Term parses a term.

type Stream

type Stream struct {
	io.Reader
	io.Writer
	io.Closer
	// contains filtered or unexported fields
}

Stream is a prolog stream.

func (*Stream) Copy

func (s *Stream) Copy() Term

Copy simply returns the stream.

func (*Stream) String

func (s *Stream) String() string

func (*Stream) Unify

func (s *Stream) Unify(t Term, occursCheck bool) bool

Unify unifies the stream with t.

func (*Stream) WriteTerm

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

WriteTerm writes the stream into w.

type Term

type Term interface {
	fmt.Stringer
	WriteTerm(io.Writer, WriteTermOptions) error
	Unify(Term, bool) bool
	Copy() Term
}

Term is a prolog term.

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 ListRest

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

ListRest returns a list of ts prepended by rest.

func PrincipalFunctor

func PrincipalFunctor(name Atom, arity Integer) Term

PrincipalFunctor returns a compound of name/arity.

func Resolve

func Resolve(t Term) Term

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

func Rulify

func Rulify(t Term) Term

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

func Set

func Set(ts ...Term) Term

Set returns a list of ts which elements are unique.

type Variable

type Variable struct {
	Name string
	Ref  Term
}

Variable is a prolog variable.

func (*Variable) Copy

func (v *Variable) Copy() Term

Copy returns a fresh variable with a copy of Ref.

func (*Variable) String

func (v *Variable) String() string

func (*Variable) Unify

func (v *Variable) Unify(t Term, occursCheck bool) bool

Unify unifies the variable with t.

func (*Variable) WriteTerm

func (v *Variable) WriteTerm(w io.Writer, opts WriteTermOptions) error

WriteTerm writes the variable into w.

type WriteTermOptions

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

WriteTermOptions describes options to write terms.

Directories

Path Synopsis
cmd
1pl

Jump to

Keyboard shortcuts

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