1 An overview of Pengines
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • Pengines: Web Logic Programming Made Easy
        • An overview of Pengines
          • Pengine references
          • Pengine by examples
          • Making predicates available to clients
          • Mapping Prolog terms into JSON
          • Pengine settings

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 constants true and false, respectively.
  • The Prolog term @(null) is mapped to the JSON constant null.
  • A Prolog term json(NameValueList), where NameValueList is a list of Name=Value pairs, 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.