ut

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2018 License: MIT Imports: 8 Imported by: 0

README

GoDoc Go Report Card Build Status

ut

Package ut implements "Yet Another Efficient Unification Algorithm" by Alin Suciu (https://arxiv.org/abs/cs/0603080v1).

The unification algorithm is at the core of the logic programming paradigm, the first unification algorithm being developed by Robinson. More efficient algorithms were developed later by Martelli and, Montanari. Here yet another efficient unification algorithm centered on a specific data structure, called the Unification Table.

x, y := "p(Z,h(Z,W),f(W))", "p(f(X),h(Y,f(a)),Y)"
mgu := ut.Unify(x, y)
fmt.Println("W = " + mgu("W"))
fmt.Println("X = " + mgu("X"))
fmt.Println("Y = " + mgu("Y"))
fmt.Println("Z = " + mgu("Z"))

// Output:
// W = f(a)
// X = f(a)
// Y = f(f(a))
// Z = f(f(a))

x, y = "f(X1,g(X2,X3),X2,b)", "f(g(h(a,X5),X2),X1,h(a,X4),X4)"
mgu = ut.Unify(x, y)
fmt.Println("X1 = " + mgu["X1"])
fmt.Println("X2 = " + mgu["X2"])
fmt.Println("X3 = " + mgu["X3"])
fmt.Println("X4 = " + mgu["X4"])
fmt.Println("X5 = " + mgu["X5"])

// Output:
// X1 = g(h(a,b),h(a,b))
// X2 = h(a,b)
// X3 = h(a,b)
// X4 = b
// X5 = b

Documentation

Overview

Package ut implements "Yet Another Efficient Unification Algorithm" by Alin Suciu (https://arxiv.org/abs/cs/0603080v1). The unification algorithm is at the core of the logic programming paradigm, the first unification algorithm being developed by Robinson. More efficient algorithms were developed later by Martelli and, Montanari. Here yet another efficient unification algorithm centered on a specific data structure, called the Unification Table.

Index

Examples

Constants

View Source
const (
	EOF      = -(iota + 1) // reached end of source
	Atom                   // a Prolog atom, possibly quoted
	Comment                // a comment
	Float                  // a floating point number
	Functor                // an atom used as a predicate functor
	FullStop               // "." ending a term
	Int                    // an integer
	String                 // a double-quoted string
	Variable               // a Prolog variable
	Void                   // the special "_" variable
)

The result of Scan is one of these tokens or a Unicode character.

Variables

View Source
var (
	// VAR stands for variable type
	VAR = func(t rune) bool {
		return t == Variable
	}

	// STR stands for constants or composite terms
	STR = func(t rune) bool {
		return t == Atom ||
			t == Float ||
			t == Int ||
			t == String ||
			t == Void ||
			t == Functor
	}
)

Functions

func Unify

func Unify(x, y string) map[string]string

Unify returns a unification maps with VAR bindings. Also see ut.MGU for particular terms.

Example
x, y := "f(X1,g(X2,X3),X2,b)", "f(g(h(a,X5),X2),X1,h(a,X4),X4)"
mgu := Unify(x, y)
fmt.Println("X1 = " + mgu["X1"])
fmt.Println("X2 = " + mgu["X2"])
fmt.Println("X3 = " + mgu["X3"])
fmt.Println("X4 = " + mgu["X4"])
fmt.Println("X5 = " + mgu["X5"])
Output:

X1 = g(h(a,b),h(a,b))
X2 = h(a,b)
X3 = h(a,b)
X4 = b
X5 = b

Types

type Entry

type Entry struct {
	Term       string
	Functor    string
	Components []int
	Type       rune
}

Entry stands for Unification Table Entry

func (*Entry) Arity

func (e *Entry) Arity() int

Arity is the arity of the term; for variables and constants, it is 0.

type Token

type Token struct {
	Type       rune
	Term       string
	Functor    string
	Components []string
}

Token encapsulating its type, content and related components.

func Tokenize

func Tokenize(terms ...string) []*Token

Tokenize scans and classifies prolog terms.

type UT

type UT struct {
	// Lookup table (term) -> (index)
	Lookup   map[string]int
	Entries  []*Entry
	Bindings map[int]int
}

UT stands for Unification Table

func New

func New(tokens []*Token) (ut *UT)

New creates a new Unification Table.

func (*UT) MGU

func (ut *UT) MGU(term string) string

MGU returns The Most General Unifier as a string for a given term. It dereferences bindings and term componenets.

func (*UT) Unify

func (ut *UT) Unify(ix, iy int) bool

Unify tries to calculate MGU (Most General Unifier)

Jump to

Keyboard shortcuts

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