I’ve come up with the following simple convention for moving between Prolog and Json that Postgresql and web browsers can parse.
Terms = [cell(1, 1, x), cell(1, 2, x), cell(1, 3, o),
cell(2, 1, b), cell(2, 2, o), cell(2, 3, b),
cell(3, 1, b), cell(3, 2, o), cell(3, 3, x),
control(white)].
Json = [["cell", 1, 1, "x"], ["cell", 1, 2, "x"], ["cell", 1, 3, "o"],
["cell", 2, 1, "b"], ["cell", 2, 2, "o"], ["cell", 2, 3, "b"],
["cell", 3, 1, "b"], ["cell", 3, 2, "o"], ["cell", 3, 3, "x"],
["control", "white"]]
My code to do this translation looks like so:
atom_json(true, true) :- !.
atom_json(false, false) :- !.
atom_json(null, null) :- !.
atom_json(Number, Number) :- number(Number), !.
atom_json(Atom, String) :- atom_string(Atom, String).
jsonify(AtomList, JsonArray) :-
maplist(atom_json, AtomList, JsonArray).
json_terms(Json, Terms) :-
ground(Terms), !,
maplist(=.., Terms, List),
maplist(jsonify, List, Json).
json_terms(Json, Terms) :-
maplist(jsonify, List, Json),
maplist(=.., Terms, List).
I guess if need to convert Prolog rules to Json for some reason, something more complex would be needed. But this seems the easiest way to convert Prolog facts to something other languages can read and write.