Janus does not like (X,Y) notation for tuples

I’m using: SWI-Prolog version 9.2.7, janus_swi 1.4.0

I want the code to:

#!/usr/bin/python

import janus_swi as janus

janus.consult("x", r"""
point((X,Y)) :-
  between(0,1,X),
  between(0,1,Y).
""") 

print(list(janus.query("point(Z)")))

But what I’m getting is:
janus_swi.janus.PrologError: ‘$c_call_prolog’/0: Domain error: py_term' expected, found 0,0’

The following works correct:

#!/usr/bin/python

import janus_swi as janus

janus.consult("x", r"""
point((X-Y)) :-
  between(0,1,X),
  between(0,1,Y).
""") 

print(list(janus.query("point(Z)")))

Thank you

Prolog (a,b,c,d) is a bad choice as it is a term ','(a, ','(b, ','(c,d))), which is not an array as what Python tuples are. Also () is illegal syntax. So, we represent tuples as -(a,b,c,d), exploiting Prolog’s common pair notation for the common binary tuples: a-b.

We had long discussions, but nobody came up with anything better … These things happen if you need to map data between languages.

Thank you, I will stick to a-b notation.
btw Python itself is ambiguous about tuples:

Good luck with that, A-B has some problems with associativity.
It doesn’t really model the LISP cons, car and cdr:

?- 1-2-3 = A-B.
A = 1-2,
B = 3.

?- (1,2,3) = (A,B).
A = 1,
B = (2, 3).

So I am not sure whether not supporting (',')/2
in Janus makes any sense at all.

That is a nested pair. The Python tuple (1,2,3) takes the Prolog shape -(1,2,3). There was a clear agreement that a tuple should map on a compound term and a lot of discussion about the functor name.

1 Like

You missed the “-”

I searched for “tuple” in the discourse site here and found this post which then had a link to this other thread.

Not I think Jan W. is literally suggesting to choose
compounds for tuples. So that we have:

  • -(1,2) or 1-2:
    A binary tuple.

  • -(1,2,3):
    A ternary tuple.

  • Etc…

Yes, exactly. You also missed the 0-tuple and the singleton, so

- % or maybe -()
-(1)
-(1,2)
...

Interestingly, when I just search the wider internets for “list vs tuple” all top hits take me to Python-specific sources. This one for example:

Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences. Tuples have structure, lists have order.

This proposal is not ideal but it looks really close, and it’s somewhat clean Prolog. The big discussion was about the name of the functor, not about the concept itself. Is there any potential problems with picking the -, since it is so overloaded with meaning? But then again, a Prolog pair 1-2 is supported by a few useful built-ins, although keysort/2 is can now be expressed in terms of sort/4 in SWI-Prolog so…