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

Can't query lists #16

Closed
manraog opened this issue Jun 7, 2017 · 4 comments
Closed

Can't query lists #16

manraog opened this issue Jun 7, 2017 · 4 comments

Comments

@manraog
Copy link

manraog commented Jun 7, 2017

Hi, this example is working:

from pyswip import Prolog
prolog = Prolog()
prolog.assertz("father(michael,john)")
prolog.assertz("father(michael,gina)")
list(prolog.query("father(michael,X)"))

But if I try the same with a list

from pyswip import Prolog
prolog = Prolog()
prolog.assertz("michael([john,gina])")
list(prolog.query("michael(X)")

The output is:

[{'T': [Atom('333316'), Atom('335364')]}]
@nschllr
Copy link

nschllr commented Oct 18, 2017

I have the same problem:

det(the).
adjective(brown).
noun(fox).
generate_output(X) :- maplist(call,[det,adjective,noun],X).

In swiprolog the query works fine. But if I make a query with

result = list(prolog.query('generate_output(X)'))
print(result)

The output is:

[{'X': [Atom('396421'), Atom('397317'), Atom('393093')]}, ....

I work with swipl 7.6.0-rc2, python 3.6.2 and pyswip 0.2.3.

@manraog did you find a workaround?

@Vimos
Copy link

Vimos commented Apr 10, 2018

I am facing a similar problem,


In [1]: from pyswip import Prolog, registerForeign

In [2]: prolog = Prolog()

In [3]: prolog.consult("geoquery")
Warning: /home/vimos/git/visualjoyce/zoonlp/data/geoquery/geoquery:57:
	Singleton variables: [R]
Warning: /home/vimos/git/visualjoyce/zoonlp/data/geoquery/geoquery:348:
	Local definition of user:subtract/3 overrides weak import from lists

In [4]: for l in open('geoqueries250'):
   ...:     if l.strip():
   ...:         try:
   ...:             a = list(prolog.query("answer{}".format(l.strip().split(', answer')[1])[:-2])) 
   ...:             print(a)
   ...:         except:
   ...:             pass
   ...:         


Answer = [arkansas,canadian,cimarron,colorado,gila,green,neosho,north platte,pecos,red,republican,rio grande,san juan,smoky hill,south platte,washita]
[{'A': Variable(67), 'B': Variable(70), 'C': Variable(73)}]


Answer = [granite peak]
[{'A': Variable(67), 'B': Variable(70)}]

I used the geoquery dataset, the output for a is
[{'A': Variable(67), 'B': Variable(70)}]

I tried to debug, I found that if we run examples from the repo, the printed mapped terms are

/opt/anaconda/anaconda3/bin/python3.6 /home/vimos/git/pyswip/examples/create_term.py
mappedTerms {}
mappedTerms {}
mappedTerms {}
mappedTerms {70: Atom('361349')}
mappedTerms {70: Atom('361349'), 71: Atom('360709'), 69: Functor(373005,2,X,gnu)}
mappedTerms {70: Atom('361349'), 71: Atom('360709'), 69: Functor(373005,2,X,gnu)}
mappedTerms {70: Atom('361349'), 71: Atom('360709'), 69: Functor(373005,2,X,gnu), 73: Atom('361477')}
[{'X': 'gnu', 'Y': 50}]

But for the geoquery case, it will be

/opt/anaconda/anaconda3/bin/python3.6 /home/vimos/git/pyswip/examples/geoquery.py
Warning: /home/vimos/git/visualjoyce/zoonlp/data/geoquery/geoquery:57:
	Singleton variables: [R]
mappedTerms {}


Answer = [arkansas,canadian,cimarron,colorado,gila,green,neosho,north platte,pecos,red,republican,rio grande,san juan,smoky hill,south platte,washita]
mappedTerms {63: []}
mappedTerms {63: []}
mappedTerms {63: []}
mappedTerms {63: [], 66: Atom('405637')}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67)}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073)}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073)}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073), 69: Atom('517893')}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073), 69: Atom('517893'), 70: Variable(70)}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073), 69: Atom('517893'), 70: Variable(70), 68: Functor(373005,2,B,_G1076)}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073), 69: Atom('517893'), 70: Variable(70), 68: Functor(373005,2,B,_G1076)}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073), 69: Atom('517893'), 70: Variable(70), 68: Functor(373005,2,B,_G1076), 72: Atom('505989')}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073), 69: Atom('517893'), 70: Variable(70), 68: Functor(373005,2,B,_G1076), 72: Atom('505989'), 73: Variable(73)}
[{'A': Variable(67), 'B': Variable(70), 'C': Variable(73)}]

After some debuging, the problem of showing Atom may be fixed by changing _unifier function to:

def _unifier(arity, *args):
    assert arity == 2
    #if PL_is_variable(args[0]):
    #    args[0].unify(args[1])

    if hasattr(args[0], 'value'):
        args_0 = args[0].value
    else:
        args_0 = args[0]

    if hasattr(args[1], 'value'):
        args_1 = args[1].value
    else:
        args_1 = args[1]

    if type(args_1) is list:
        for i, item in enumerate(args_1):
            if hasattr(item, 'value'):
                args_1[i] = item.value

    return {args_0: args_1}

But this does not fix the Variable case.
I found a topic on stackoverflow, maybe Variable case is not easy to resolve.

@yuce
Copy link
Owner

yuce commented Jun 9, 2018

This is fixed in the rewrite/refactoring of PySwip in the refactor branch. E.g.,: https://github.com/yuce/pyswip/blob/refactor/tests/test_prolog.py

@yuce yuce closed this as completed Jun 9, 2018
@ShubhamSharma1808
Copy link

Cant access the above link, can you share the updated link @yuce

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

No branches or pull requests

5 participants