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

Problem while printing result of query #100

Open
fpopineau opened this issue Jul 29, 2020 · 1 comment
Open

Problem while printing result of query #100

fpopineau opened this issue Jul 29, 2020 · 1 comment

Comments

@fpopineau
Copy link

fpopineau commented Jul 29, 2020

Hi there,

Thanks for this amazing module.

I think there is a problem with the way terms are printed.
I run some Prolog code from Python, querying Prolog with:

H = pyswip.Variable()
induce = pyswip.Functor('induce', 1)
q = Query(induce(H))
q.nextSolution()
print(H.value)

I get a result like this for H:

[Functor(319757,2,
 [Functor(9048461,3,_10560234,_10560236,_10560238), Functor(9044237,2,_10560234,_10560238),
  Functor(9040141,2,_10560238,_10560236), Functor(9036045,2,_10560238,_10560274),
  Functor(9036045,2,_10560236,_10560286), Functor(1724685,2,_10560274,_10560286)],
 [Functor(188685,2,_10560234,unit), Functor(188685,2,_10560236,unit), Functor(188685,2,_10560238,unit),
  Functor(188685,2,_10560286,time), Functor(188685,2,_10560274,time)])]

where I should get:

[[unconformity(A,C,B),above(A,B),touch(B,C),has_period(B,D),has_period(C,E),D\=E]/[A:unit,C:unit,B:unit,E:time,D:time]]

Strangely, it I print H.value[0].name, I have the correct name for the functor.

This is with Python 3.7, SwiPL 8.0.3, PySwip 0.2.10 under Windows 10.

Regards,

@rindPHI
Copy link

rindPHI commented May 21, 2021

I'm using the following function for converting the output to a String that I could feed back into Prolog:

def pyswip_output_to_str(inp) -> Union[str, int, List]:
    if type(inp) is bytes:
        inp: bytes
        return f'"{inp.decode("utf-8")}"'
    elif type(inp) is int:
        inp: int
        return inp
    elif type(inp) is pyswip.easy.Variable:
        inp: pyswip.easy.Variable
        if inp.chars is None:
            return f"_{inp.handle}"
        else:
            return inp.chars
    elif type(inp) is pyswip.easy.Atom:
        inp: pyswip.easy.Atom
        return f"'{inp.value}'"
    elif type(inp) is list:
        inp: List
        return [pyswip_output_to_str(child) for child in inp]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants