0

I want to use Tau Prolog together with JavaScript on a Node Server, following this tutorial, which works well. I need to convert the answers in a JavaScript compatible format, especially lists to arrays, or objects and vice versa.

I changed the Prolog program and goal in a way that it returns a list

Program: test(t, [64,65,100,120]).

Goal: test(t, X).

which returns with

console.log(session.format_answer(answer));

X = [64,65,100,120]

too the console. How can I get only the list and assign it to a js variable?

I tried

answer.lookup("X")

which gives me

Term {
ref: 1258,
id: '.',
args: [
Num { is_float: false, value: 64 },
Term { ref: 1257, id: '.', args: [Array], indicator: './2' }
],
indicator: './2'
}

Which is not very comfortable to access. The args array seems to be the list. I managed to get single list elements with

console.log(answer.links.X.args[1].args[1].args);

to get the third list element.
What is the best way to use complex answers and lists in JavaScript?

false
  • 10,264
  • 13
  • 101
  • 209
Carsten H
  • 831
  • 15
  • 32
  • This might be of value: Tau-prolog examples [like](http://tau-prolog.org/examples/likes). If that works and as I don't plan to write an elaborated answer, the OP is free to elaborate and post as an answer. For others, let the OP earn some extra points. – Guy Coder Dec 18 '21 at 11:52
  • I will say that learning Prolog effectively is hard. Learning to interface Prolog with another language is harder. Learning to use Prolog, interface with another language and then interface as a web site is a black art. – Guy Coder Dec 18 '21 at 11:56
  • Thanks @GuyCoder, I got the mentioned `answer.lookup("X");` from this example. The basic mechanics are understood but not how to process more complex answers e.g. with lists, correctly. The likes example returns just one item an I can use its value. No problem to process it, but in my example I get back a list `X = [64,65,100,120]`. How do I go on from here. I managed to get the values from the list, as described above with `answer.links.X.args[1]` etc. but I guess there is a better way. – Carsten H Dec 18 '21 at 13:09
  • First I have not installed Tau Prolog and don't plan on it at the moment as I have way to many items I am juggling. Second, without seeing your entire code we are having to guess at it. It would be easier if you posted your code here or on GitHub or something and provided a link. continued. – Guy Coder Dec 18 '21 at 13:42
  • Third, I use SWI-Prolog and try to exclusively pass such information as JSON. After working with [Cytoscape.js](https://js.cytoscape.org/) and seeing how JSON can be used exclusively for such ([ref](https://js.cytoscape.org/#cy.json)) and then learning how to convert SWI-Prolog dicts to JSON ([json_write_dict/2](https://www.swi-prolog.org/pldoc/doc_for?object=json_write_dict/2)), life became much easier. – Guy Coder Dec 18 '21 at 13:45
  • Does this [answer](https://stackoverflow.com/a/60913321/1243762) help? If not you note in your questions which previous SO answers you looked at and why they don't help with your particular situation. – Guy Coder Dec 18 '21 at 13:57
  • Thanks again, I read every Tau Prolog related answer and all of them do not fit because thy are related to answers with atoms like `X = banana` not lists like `X= [64,65,100,120]` which I want to convert to JS arrays. I have the formatted answer of my goal so this part is not the problem, just how to extract the list from the answer in a way that I can easily convert it. – Carsten H Dec 19 '21 at 21:31
  • Sounds like you are the one on the cutting edge and will have to write or update the manual. :) – Guy Coder Dec 19 '21 at 22:28

1 Answers1

1

Just for completeness, I replicate this answer here.

You can write a function to transform Prolog lists to arrays:

function fromList(xs) {
    var arr = [];
    while(pl.type.is_term(xs) && xs.indicator === "./2") {
        arr.push(xs.args[0]);
        xs = xs.args[1];
    }
    if(pl.type.is_term(xs) && xs.indicator === "[]/0")
        return arr;
    return null;
}

Example:

var session = pl.create();
session.query("X = [1,2,3].");
session.answer(a => console.log(fromList(a.lookup("X")))); // [ {...}, {...}, {...} ]

Note that elements in array are still Tau Prolog objects.