0

I am following the tau-prolog tutorial, and I run into this error:

throw(error(existence_error(procedure,/(fruits_in,2)),/(top_level,0)))

My index.html is

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Hello, Tau Prolog!</title>
</head>

<body>

    <script src="tau-prolog.js"></script>
    <script src="main.js"></script>
</body>

</html>

main.js is

let session = pl.create();
session.consult("                   \
    % load lists module                          \
    :- use_module(library(lists)).               \
                                                 \
    % fruit/1                                    \
    fruit(apple). fruit(pear). fruit(banana).    \
                                                 \
    % fruits_in/2                                \
    fruits_in(Xs, X) :- member(X, Xs), fruit(X). \
", {
    success: () => { console.log("success") },
    error: (err) => { console.log('error: ', err); }
});

session.query("fruits_in([carrot, apple, banana, broccoli], X).", {
    success: (goal) => { console.log('Query success. Goal: ', goal) },
    error: (err) => { console.log('error: ', err) }
});

session.answer({
    success: (answer) => {
        console.log(answer); // {X/apple}
        session.answer({
            success: (answer) => {
                console.log(answer); // {X/banana}
            },
            error: (err) => { console.log(`answer error: ${err}`) },
            fail: () => { console.log('no more answers') },
            limit: () => { console.log('limit') }
        });
    },
    error: (err) => { console.log(`answer error: ${err}`) },
    fail: () => { console.log('no more answers') },
    limit: () => { console.log('limit') }
})

and the console output is

here

I am not sure where to go from here, and I'd be grateful for someone pointing out the beginner's error I've made!

*** UPDATE

If I change the session.consult from using " and \, to using the new `` template string syntax, this code works!

new session.consult:

session.consult(`
    % load lists module 
    :- use_module(library(lists)).     
                                                 
    % fruit/1                                    
    fruit(apple). fruit(pear). fruit(banana).    
                                                 
    % fruits_in/2                                
    fruits_in(Xs, X) :- member(X, Xs), fruit(X). 
`, {
    success: () => { console.log("success") },
    error: (err) => { console.log('error: ', err); }
});

So the new question is: "Why does that make a difference?"

Thanks - again!

user2162871
  • 409
  • 3
  • 10

1 Answers1

0

OK - the process is asynchronous, and answer needs to execute after query which needs to execute after consult, so nested function calls.

It's in the docs... sigh.

And this SO post covered it. Sorry for wasting folks' time.

user2162871
  • 409
  • 3
  • 10