1.4 Mapping Prolog terms into JSON
In Prolog, solutions to queries are given as bindings which map
variable names into Prolog terms. A programmer using Pengines in a
JavaScript evironment needs to understand how bindings are converted
into JSON. For example, the programmer needs to understand that the
second solution to
append(Xs, Ys, [a,b,c]) is given by the bindings
['Xs'=[a],'Ys'=[b,c]] and that these binding can be
represented in JSON as {"Xs":["a"], "Ys":["b","c"]}.
Pengines defines the following mapping between ground Prolog terms and JSON.
- A Prolog atom is mapped to a JSON string.
- A Prolog number is mapped to a JSON number.
- A Prolog list is mapped to a JSON array.
- The Prolog terms
@(true)and@(false)are mapped to the JSON constantstrueandfalse, respectively. - The Prolog term
@(null)is mapped to the JSON constantnull. - A Prolog term
json(NameValueList), where NameValueList is a list ofName=Valuepairs, is mapped to a JSON object. - Any other complex Prolog term T is mapped to a JSON
object of the form
{"functor": F, "args": A}where F is a string representing the functor of T and A is the list of JSON values representing Ts arguments.