13.2.2.1 Translating JavaScript data to Prolog
This section describes how data from JavaScript is translated into Prolog. The interface is primarily designed for passing JavaScript data as typically used to a natural Prolog representation. In addition a number of classes are provided to create Prolog specific data structures such as strings (as opposed to atoms), variables, compound terms, etc.
- Number
- Translate to a Prolog integer or floating point number.
- BigInt
- Translate to a Prolog integer.
- String
- Translate to a Prolog atom. Use
new Prolog.String(text)
to create a Prolog string. See below. - Boolean
- Translate to one of the Prolog atoms
true
orfalse
. - undefined
- Translate the Prolog atom
undefined
. - null
- Translate the Prolog atom
null
. - Array
- Translate to a Prolog list.
- Objects holding the key
$
:Type - Such objects are converted depending on the value for this key. The
interface defines classes to simplify creating such objects.
- s
- Represent a Prolog string. The key
v
holds the text. May be created usingnew Prolog.string(text)
. May be created usingnew Prolog.String(text)
. - r
- Represent a Prolog rational number. The keys
n
andd
represent the numerator and denominator. For example, to represent1r3
, use {$
:"r",n
:1,d
:3}. May be created usingnew Prolog.Rational(n, d)
, where n and d can be JavaScript numbers or big integers. - t
- Represent a Prolog compound term. The object should hold
exactly one key whose value is an array that holds the argument values.
For example a term
point(1,2)
is constructed using {$
:"t",point
:[1,2]}. May be created usingnew Prolog.Compound(functor, args)
- v
- Represent a variable. If the key
v
is present this identifies the variable. Two variables processed in the same translation with the same identifier represent the same Prolog variable. If thev
key is omitted the variable will be unique. May be created usingnew Prolog.Var(id)
. - l
- Represent a Prolog list. As a JavaScript
Array
we only need this typed object to create a partial list. Thev
key contains the “normal” elements and the keytail
contains the tail of the list. May be created usingnew Prolog.List(array, tail)
.
- Object of class
Object
- Plain JavaScript objects are translated into a Prolog
dict
. Note that JavaScript object keys are always strings and (thus) all dict keys are atoms. This, {1:"one"} is translated into_{'1': one}
. - ArrayBuffer
- Instances of
ArrayBuffer
are translated into a Prolog string that consists of characters in the range 0 ... 255. - Objects of a one class not being
Object
- Instances of non-plain JavaScript objects are translated into a Prolog blob.
Such objects are written as
<js_Class(id)>
. The Prolog interface allows for passing the objects back and calling methods on them. See section 13.3.