Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧠 Logic: json_prolog array serialization issue #398

Closed
amimart opened this issue Jun 23, 2023 · 5 comments · Fixed by #399
Closed

🧠 Logic: json_prolog array serialization issue #398

amimart opened this issue Jun 23, 2023 · 5 comments · Fixed by #399
Labels
bug 🐞 bugs and defects released urgent 🚨 urgent issues that require work ASAP

Comments

@amimart
Copy link
Member

amimart commented Jun 23, 2023

Issue description

When unifying a json array through a Prolog empty list to its string representation, it outputs to the string "[]" instead of an empty json array: [].

How to reproduce

This can easily reproduced with a Ask query using the CLI:

> okp4d query logic ask "json_prolog(X, [])." -ojson | jq -r '.answer.results[-1].substitutions[-1].term.name'
'"[]"'

We can observe the same results specifying the '[]' atom instead of an empty list:

> okp4d query logic ask "json_prolog(X, [])." -ojson | jq -r '.answer.results[-1].substitutions[-1].term.name'
'"[]"'

The reason under this behavior is related to the equality between an empty list and the '[]' atom at the interpreter level:

> okp4d query logic ask "[] = '[]'." -ojson | jq -r '.answer.success'
true

First Analysis

An issue has been opened on the interpreter: ichiban/prolog#296.

It seems this behavior is expected, the SWI-PROLOG implementation having its specific empty list type being not standard.

Should the Prolog interpreter in the logic module be aligned on the standard or the SWI-PROLOG implementation? And then, how do we address the JSON array marshaling?

@amimart amimart added bug 🐞 bugs and defects urgent 🚨 urgent issues that require work ASAP labels Jun 23, 2023
@amimart
Copy link
Member Author

amimart commented Jun 23, 2023

@bdeneux @ccamel I propose to respect to Prolog standard and not head towards SWI-PROLOG "unconventional" empty list consideration.

We still need a solution though, amending the original specification.

A json(Attributes) predicate was introduced to manage JSON objects (e.g. {"b": "a", "a":"c"} => json([a-c,b-a])), may I suggest a similar predicate to handle JSON arrays, like json_array(Elements)? It would be consistent with the way we manage objects. Moreover, the original issue is only related to empty arrays so we can support both.

As an example, the following Prolog:

json([
    empty-json_array([]),
    filled-[with, something],
    alsoFilled-json_array([0, 1])
])

What are your thoughts?

Would represents:

{
    "empty": [],
    "filled": [
        "with",
        "something"
    ],
    "alsoFilled": [
        0,
        1
    ]
}

@bdeneux
Copy link
Contributor

bdeneux commented Jun 23, 2023

@amimart Ok that seems a good solution we could use to allow empty list in json. Maybe to make more consistency we should rename the json(Attributes) predicate to json_obj(Attributes) or json_object(Attributes).

We have another solution, is to create a specific atom to represent an empty list, like null (@(null)) or boolean value (@(true)), we could create the atom @([]). But this one is not common so I don't know what is the best solution...

@ccamel
Copy link
Member

ccamel commented Jun 23, 2023

I have a deep feeling, whose origin eludes me, that the choice of using the functor @ with [] as argument would be the most appropriate for designating the special value that is the empty list.

@amimart
Copy link
Member Author

amimart commented Jun 23, 2023

I have a deep feeling, whose origin eludes me, that the choice of using the functor @ with [] as argument would be the most appropriate for designating the special value that is the empty list.

@ccamel Yeah it seems simple, just add an @ in the case of an empty array, I'm ok with that :)

@bot-anik
Copy link
Member

🎉 This issue has been resolved in version 5.0.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 bugs and defects released urgent 🚨 urgent issues that require work ASAP
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants