prolog

package
v7.2.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// AtomAs is the term used to indicate the as encoding type option.
	AtomAs = engine.NewAtom("as")
	// AtomAt are terms with principal functor (@)/1 used to represent special values in json objects.
	AtomAt = engine.NewAtom("@")
	// AtomDIDComponents is a term which represents a DID as a compound term `did_components(Method, ID, Path, Query, Fragment)`.
	AtomDIDComponents = engine.NewAtom("did_components")
	// AtomDot is the term used to represent the dot in a list.
	AtomDot = engine.NewAtom(".")
	// AtomEmpty is the term used to represent empty.
	AtomEmpty = engine.NewAtom("")
	// AtomEmptyArray is the term [].
	AtomEmptyArray = engine.NewAtom("[]")
	// AtomEmptyList is the term used to represent an empty list.
	AtomEmptyList = engine.NewAtom("[]")
	// AtomEncoding is the term used to indicate the encoding type option.
	AtomEncoding = engine.NewAtom("encoding")
	// AtomError is the term used to indicate the error.
	AtomError = engine.NewAtom("error")
	// AtomFalse is the term false.
	AtomFalse = engine.NewAtom("false")
	// AtomFragment is the term used to indicate the fragment component.
	AtomFragment = engine.NewAtom("fragment")
	// AtomHex is the term used to indicate the hexadecimal encoding type option.
	AtomHex = engine.NewAtom("hex")
	// AtomJSON are terms with principal functor json/1 used to represent json objects.
	AtomJSON = engine.NewAtom("json")
	// AtomNull is the term null.
	AtomNull = engine.NewAtom("null")
	// AtomOctet is the term used to indicate the byte encoding type option.
	AtomOctet = engine.NewAtom("octet")
	// AtomPadding is the term used to indicate the padding encoding type option.
	AtomPadding = engine.NewAtom("padding")
	// AtomPair are terms with principal functor (-)/2.
	// For example, the term -(A, B) denotes the pair of elements A and B.
	AtomPair = engine.NewAtom("-")
	// AtomPath is the term used to indicate the path component.
	AtomPath = engine.NewAtom("path")
	// AtomQueryValue is the term used to indicate the query value component.
	AtomQueryValue = engine.NewAtom("query_value")
	// AtomSegment is the term used to indicate the segment component.
	AtomSegment = engine.NewAtom("segment")
	// AtomText is the term used to indicate the atom text.
	AtomText = engine.NewAtom("text")
	// AtomTrue is the term true.
	AtomTrue = engine.NewAtom("true")
	// AtomUtf8 is the term used to indicate the UTF-8 encoding type option.
	AtomUtf8 = engine.NewAtom("utf8")
)
View Source
var (
	// AtomTypeAtom is the term used to represent the atom type.
	AtomTypeAtom = engine.NewAtom("atom")
	// AtomTypeByte is the term used to represent the byte type.
	AtomTypeByte = engine.NewAtom("byte")
	// AtomTypeCharacter is the term used to represent the character type.
	// A character type is a single character (e.g. 'a') identified in the Unicode standard.
	AtomTypeCharacter = engine.NewAtom("character")
	// AtomTypeCharacterCode is the term used to represent the character code type.
	// A character code type is a single character identified by its code point (a number) in the Unicode standard.
	AtomTypeCharacterCode = engine.NewAtom("character_code")
	// AtomTypeCharset is the term used to represent the charset type.
	// A charset type is a set of characters identified by its name in the IANA standard.
	AtomTypeCharset = engine.NewAtom("charset")
	// AtomTypeCryptographicAlgorithm is the term used to represent the cryptographic algorithm type.
	AtomTypeCryptographicAlgorithm = engine.NewAtom("cryptographic_algorithm")
	// AtomTypeDID is the term used to represent the DID type.
	// DID type is a compound with the name "did" and 5 arguments which are the components of the DID, in the form of
	// did(Method, ID, Path, Query, Fragment).
	AtomTypeDID = engine.NewAtom("did")
	// AtomTypeHashAlgorithm is the term used to represent the hash algorithm type.
	AtomTypeHashAlgorithm = engine.NewAtom("hash_algorithm")
	// AtomTypeIOMode is the term used to represent the IO mode type.
	// An IO mode specifies the direction of the IO operation represented as an atom.
	// Possible values are: read, write, append.
	AtomTypeIOMode = engine.NewAtom("io_mode")
	// AtomTypeStream is the term used to represent the stream type.
	AtomTypeStream = engine.NewAtom("stream")
	// AtomTypeText is the term used to represent the text type.
	// A text type is either an atom, a list of characters or a list of character codes.
	AtomTypeText = AtomText
	// AtomTypeList is the term used to represent the list type.
	AtomTypeList = engine.NewAtom("list")
	// AtomTypeNumber is the term used to represent the number type.
	AtomTypeNumber = engine.NewAtom("number")
	// AtomTypeOption is the term used to represent the option type.
	// An option is a compound with the name of the option as functor and one term argument which is
	// the value of the option. For instance: opt(v).
	AtomTypeOption = engine.NewAtom("option")
	// AtomTypePair is the term used to indicate the pair type.
	AtomTypePair = engine.NewAtom("pair")
	// AtomTypeJSON is the term used to indicate the json type.
	AtomTypeJSON = AtomJSON
	// AtomTypeURIComponent is the term used to represent the URI component type.
	AtomTypeURIComponent = engine.NewAtom("uri_component")
)
View Source
var (
	// AtomValidEncoding is the atom denoting a valid encoding.
	// The valid encoding atom is a compound with the name of the encoding which is a valid encoding with
	// regard to the predicate where it is used.
	//
	// For instance: valid_encoding(utf8), valid_encoding(hex).
	AtomValidEncoding = engine.NewAtom("encoding")
	// AtomValidEmptyList is the atom denoting a valid empty list.
	AtomValidEmptyList = engine.NewAtom("empty_list")
)
View Source
var (
	// AtomResourceContext is the atom denoting the "context" resource.
	// The context resource is a contextual data that contains all information needed to
	// process a request and produce a response with the blockchain.
	AtomResourceContext = engine.NewAtom("resource_context")
	// AtomResourceModule is the atom denoting the "module" resource.
	// The module resource is the representation of the module with which the interaction is made.
	// The module resource is denoted as a compound with the name of the module.
	AtomResourceModule = engine.NewAtom("resource_module")
	// AtomResourceGas is the atom denoting the "gas" resource.
	AtomResourceGas = engine.NewAtom("gas")
)
View Source
var (
	AtomOperationInput   = engine.NewAtom("input")
	AtomOperationExecute = engine.NewAtom("execute")
)
View Source
var (
	AtomPermissionTypeStream         = engine.NewAtom("stream")
	AtomPermissionForbiddenPredicate = engine.NewAtom("forbidden_predicate")
)
View Source
var AtomObjectTypeSourceSink = engine.NewAtom("source_sink")

Functions

func AreGround

func AreGround(terms []engine.Term, env *engine.Env) bool

func AssertAtom

func AssertAtom(term engine.Term, env *engine.Env) (engine.Atom, error)

AssertAtom resolves a term and attempts to convert it into an engine.Atom if possible. If conversion fails, the function returns the empty atom and the error.

func AssertByte

func AssertByte(term engine.Term, env *engine.Env) (byte, error)

AssertByte resolves a term and attempts to convert it into a byte if possible. If conversion fails, the function returns the zero value and the error.

func AssertCharacter

func AssertCharacter(term engine.Term, env *engine.Env) (rune, error)

AssertCharacter resolves a term and attempts to convert it into an engine.Atom if possible. If conversion fails, the function returns the empty atom and the error.

func AssertCharacterCode

func AssertCharacterCode(term engine.Term, env *engine.Env) (rune, error)

AssertCharacterCode resolves a term and attempts to convert it into a rune if possible. If conversion fails, the function returns the zero value and the error.

func AssertIsGround

func AssertIsGround(term engine.Term, env *engine.Env) (engine.Term, error)

AssertIsGround resolves a term and returns it if it is ground. If the term is not ground, the function returns nil and the instantiation error.

func AssertList

func AssertList(term engine.Term, env *engine.Env) (engine.Term, error)

AssertList resolves a term as a list and returns it as a engine.Compound. If conversion fails, the function returns nil and the error.

func AssertPair

func AssertPair(term engine.Term, env *engine.Env) (engine.Term, engine.Term, error)

AssertPair resolves a term as a pair and returns the pair components. If conversion fails, the function returns nil and the error.

func AssertURIComponent

func AssertURIComponent(term engine.Term, env *engine.Env) (util.URIComponent, error)

AssertURIComponent resolves a term as a URI component and returns it as an URIComponent.

func AtomToString

func AtomToString(term engine.Term, env *engine.Env) (string, error)

AtomToString try to convert a given atom to a string.

func ByteListTermToBytes

func ByteListTermToBytes(term engine.Term, env *engine.Env) ([]byte, error)

ByteListTermToBytes try to convert a given list of bytes into native golang []byte.

func BytesToByteListTerm

func BytesToByteListTerm(in []byte) engine.Term

BytesToByteListTerm try to convert a given golang []byte into a list of bytes.

func CharacterCodeListTermToString

func CharacterCodeListTermToString(term engine.Term, env *engine.Env) (string, error)

CharacterCodeListTermToString try to convert a given list of character codes to a string. The character codes must be between 0 and 0x10ffff (i.e. a Rune).

func CharacterListTermToString

func CharacterListTermToString(term engine.Term, env *engine.Env) (string, error)

CharacterListTermToString try to convert a given list of characters to a string. Characters is a list of atoms, each representing a single character.

func Decode

func Decode(value engine.Term, bs []byte, encoding engine.Atom, env *engine.Env) (string, error)

Decode decodes the given byte slice with the given encoding. value is used for error reporting (the culprit).

func Encode

func Encode(value engine.Term, str string, encoding engine.Atom, env *engine.Env) ([]byte, error)

Encode encodes the given string with the given encoding. value is used for error reporting (the culprit).

func ErrorTerm

func ErrorTerm(err error) engine.Term

ErrorTerm returns a term representing the given error, suitable for use in the syntax_error/2 predicate. TODO: to be improved with specific error types.

func ExtractJSONTerm

func ExtractJSONTerm(term engine.Compound, env *engine.Env) (map[string]engine.Term, error)

ExtractJSONTerm is a utility function that would extract all attribute of a JSON object that is represented in prolog with the `json` atom.

This function will ensure the json atom follow our json object representation in prolog.

A JSON object is represented like this :

``` json([foo-bar]) ```

That give a JSON object: `{"foo": "bar"}` Returns the map of all attributes with its term value.

func GetOption

func GetOption(name engine.Atom, options engine.Term, env *engine.Env) (engine.Term, error)

GetOption returns the value of the first option with the given name in the given options. An option is a compound with the given name as functor and one argument which is a term, for instance `opt(v)`. The options are either a list of options or an option. If no option is found nil is returned.

func GetOptionAsAtomWithDefault

func GetOptionAsAtomWithDefault(
	name engine.Atom, options engine.Term, defaultValue engine.Term, env *engine.Env,
) (engine.Atom, error)

GetOptionAsAtomWithDefault is a helper function that returns the value of the first option with the given name in the given options.

func GetOptionWithDefault

func GetOptionWithDefault(
	name engine.Atom, options engine.Term, defaultValue engine.Term, env *engine.Env,
) (engine.Term, error)

GetOptionWithDefault returns the value of the first option with the given name in the given options, or the given default value if no option is found.

func IsEmptyList

func IsEmptyList(term engine.Term, env *engine.Env) bool

IsEmptyList returns true if the given term is an empty list.

func IsGround

func IsGround(term engine.Term, env *engine.Env) bool

IsGround returns true if the given term holds no free variables.

func IsList

func IsList(term engine.Term, env *engine.Env) bool

IsList returns true if the given term is a list.

func JSONBool

func JSONBool(b bool) engine.Term

JSONBool returns the compound term @(true) if b is true, otherwise @(false).

func JSONEmptyArray

func JSONEmptyArray() engine.Term

JSONEmptyArray returns is the compound term @([]). It is used to represent the empty array in json objects.

func JSONNull

func JSONNull() engine.Term

JSONNull returns the compound term @(null). It is used to represent the null value in json objects.

func ListHead

func ListHead(list engine.Term, env *engine.Env) engine.Term

ListHead returns the first element of the given list.

func ListIterator

func ListIterator(list engine.Term, env *engine.Env) (engine.ListIterator, error)

ListIterator returns a list iterator.

func OctetListTermToString

func OctetListTermToString(term engine.Term, env *engine.Env) (string, error)

OctetListTermToString try to convert a given list of bytes to a string. It's the same as CharacterCodeListTermToString, but expects the list to contain bytes. It's equivalent to the prolog encoding 'octet'.

func PredicateMatches

func PredicateMatches(this string) func(string) bool

PredicateMatches returns a function that matches the given predicate against the given other predicate. If the other predicate contains a slash, it is matched as is. Otherwise, the other predicate is matched against the first part of the given predicate. For example:

  • matchPredicate("foo/0")("foo/0") -> true
  • matchPredicate("foo/0")("foo/1") -> false
  • matchPredicate("foo/0")("foo") -> true
  • matchPredicate("foo/0")("bar") -> false

The function is curried, and is a binary relation that is reflexive, associative (but not commutative).

func ResourceContext

func ResourceContext() engine.Term

ResourceContext returns a term representing the context resource.

func ResourceGas

func ResourceGas(descriptor string, consumed types.Gas, limit types.Gas) engine.Term

ResourceGas returns a term representing the gas resource with the given descriptor, consumed and limit at the given context.

func ResourceModule

func ResourceModule(module string) engine.Term

ResourceModule returns a term representing the module resource with the given name.

func StringToAtom

func StringToAtom(s string) engine.Atom

StringToAtom converts a string to an atom.

func StringToByteListTerm

func StringToByteListTerm(s string) engine.Term

StringToByteListTerm converts a string (utf8) to a term representing a list of bytes. This is equivalent to the prolog encoding 'text'.

func StringToCharacterCodeListTerm

func StringToCharacterCodeListTerm(s string) engine.Term

StringToCharacterCodeListTerm converts a string to a term representing a list of character codes.

func StringToCharacterListTerm

func StringToCharacterListTerm(s string) engine.Term

StringToCharacterListTerm converts a string to a term representing a list of characters.

func StringToOctetListTerm

func StringToOctetListTerm(s string, env *engine.Env) (engine.Term, error)

StringToOctetListTerm converts a string (utf8) to a term representing a list of bytes. This is the same as StringToCharacterCodeListTerm, but it returns an error when a rune is greater than 0xff. This is equivalent to the prolog encoding 'octet'.

func TermHexToBytes

func TermHexToBytes(term engine.Term, env *engine.Env) ([]byte, error)

TermHexToBytes try to convert an hexadecimal encoded atom to native golang []byte.

func TextTermToString

func TextTermToString(term engine.Term, env *engine.Env) (string, error)

TextTermToString try to convert a given Text term to a string. Text is an instantiated term which represents text as: an atom, a list of character codes, or list of characters.

func Tuple

func Tuple(args ...engine.Term) engine.Term

Tuple is a predicate which unifies the given term with a tuple of the given arity.

func UnifyFunctional

func UnifyFunctional(
	in,
	out []engine.Term,
	options engine.Term,
	forwardConverter ConvertFunc,
	backwardConverter ConvertFunc,
	env *engine.Env,
) (bool, *engine.Env, error)

UnifyFunctional is a generic unification which unifies a set of input terms with a set of output terms, using the given conversion functions maintaining the function's relationship.

The aim of this function is to simplify the implementation of a wide range of predicates which are essentially functional, like hash functions, encoding functions, etc.

The semantic of the unification is as follows:

  1. first all the variables are resolved
  2. if there's variables in the input and the output, the conversion is not possible and a not sufficiently instantiated error is returned.
  3. if there's no variables in the input, then the conversion is attempted from the input to the output and the result is unified with the output.
  4. if there's no variables in the output, then the conversion is attempted from the output to the input and the result is unified with the input.

The following table summarizes the behavior, where: - fi = fully instantiated (i.e. no variables) - !fi = not fully instantiated (i.e. at least one variable)

| input | output | result | |-------|--------|--------------------------------------| | !fi | !fi | error: not sufficiently instantiated | | fi | !fi | unify(forward(input), output) | | fi | fi | unify(forward(input), output) | | !fi | fi | unify(input,backward(output)) |

Conversion functions may produce an error in scenarios where the conversion is unsuccessful or infeasible due to the inherent characteristics of the function's relationship, such as the absence of a one-to-one correspondence (e.g. hash functions).

func UnifyFunctionalPredicate

func UnifyFunctionalPredicate(
	in,
	out []engine.Term,
	options engine.Term,
	forwardConverter ConvertFunc,
	backwardConverter ConvertFunc,
	cont engine.Cont,
	env *engine.Env,
) *engine.Promise

UnifyFunctionalPredicate is the predicate version of UnifyFunctional returning a promise.

func UnwrapSDKContext

func UnwrapSDKContext(ctx context.Context, env *engine.Env) (sdk.Context, error)

UnwrapSDKContext retrieves a Context from a context.Context instance attached with WrapSDKContext.

func ValidEmptyList

func ValidEmptyList() engine.Term

func ValidEncoding

func ValidEncoding(encoding string) engine.Term

ValidEncoding returns a term representing the valid encoding with the given name.

func WithError

func WithError(exception engine.Exception, err error, env *engine.Env) engine.Exception

WithError adds the error term to the exception term if possible. TODO: wait for ichiban/prolog to offer a better way to do this.

Types

type ConvertFunc

type ConvertFunc func(value []engine.Term, options engine.Term, env *engine.Env) ([]engine.Term, error)

ConvertFunc is a function mapping a domain which is a list of terms with a codomain which is a set of terms. Domains and co-domains can have different cardinalities. options is a list of options that can be used to parameterize the conversion. All the terms provided are fully instantiated (i.e. no variables).

Jump to

Keyboard shortcuts

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