2016-09-21 20:55:09 +01:00
|
|
|
|
2016-07-31 16:09:21 +01:00
|
|
|
import yap
|
2016-08-18 08:13:55 +01:00
|
|
|
import sys
|
2016-07-31 16:09:21 +01:00
|
|
|
|
|
|
|
# this class is not being used
|
|
|
|
# we rely on namedtuples instead.
|
2016-08-18 08:13:55 +01:00
|
|
|
|
|
|
|
|
2016-07-31 16:09:21 +01:00
|
|
|
class T(tuple):
|
|
|
|
|
|
|
|
"""Represents a non-interned Prolog atom"""
|
|
|
|
def __new__(self, s, tple):
|
|
|
|
self.tuple.__new__(self, tple)
|
|
|
|
self.name = s
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "yapex.T(" + self.name + " , " + tuple.__repr__(self) + ")"
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.name) + str(self.tuple)
|
|
|
|
|
|
|
|
|
2016-09-23 03:53:42 +01:00
|
|
|
|
2016-07-31 16:09:21 +01:00
|
|
|
def query_prolog(engine, s):
|
2016-09-23 03:53:42 +01:00
|
|
|
|
|
|
|
def answer( q ):
|
2016-09-23 07:21:42 +01:00
|
|
|
try:
|
|
|
|
return q.next()
|
|
|
|
except Exception as e:
|
|
|
|
print( e.args[1] )
|
|
|
|
return False
|
2016-09-23 03:53:42 +01:00
|
|
|
|
2016-07-31 16:09:21 +01:00
|
|
|
q = engine.query(s)
|
|
|
|
ask = True
|
2016-09-23 03:53:42 +01:00
|
|
|
while answer(q):
|
2016-07-31 16:09:21 +01:00
|
|
|
vs = q.namedVarsCopy()
|
|
|
|
if vs:
|
|
|
|
i = 0
|
|
|
|
for eq in vs:
|
|
|
|
name = eq[0]
|
|
|
|
bind = eq[1]
|
|
|
|
if bind.isVar():
|
|
|
|
var = yap.YAPAtom('$VAR')
|
|
|
|
f = yap.YAPFunctor(var, 1)
|
|
|
|
bind.unify(yap.YAPApplTerm(f, (name)))
|
|
|
|
else:
|
|
|
|
i = bind.numberVars(i, True)
|
|
|
|
print(name.text() + " = " + bind.text())
|
|
|
|
print("yes")
|
|
|
|
if q.deterministic():
|
|
|
|
q.close()
|
|
|
|
return
|
|
|
|
if ask:
|
|
|
|
s = input("more(;/y), all(!/a), no ?").lstrip()
|
|
|
|
if s.startswith(';') or s.startswith('y'):
|
|
|
|
continue
|
|
|
|
elif s.startswith('#'):
|
|
|
|
exec(s.lstrip('#'))
|
|
|
|
elif s.startswith('a'):
|
|
|
|
ask = False
|
|
|
|
else:
|
|
|
|
break
|
2016-09-21 20:55:09 +01:00
|
|
|
print("No (more) answers")
|
|
|
|
q.close()
|
2016-07-31 16:09:21 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
def live():
|
|
|
|
engine = yap.YAPEngine()
|
|
|
|
loop = True
|
|
|
|
while loop:
|
|
|
|
try:
|
|
|
|
s = input("?- ")
|
|
|
|
if not s:
|
|
|
|
loop = False
|
|
|
|
query_prolog(engine, s)
|
|
|
|
except SyntaxError as err:
|
|
|
|
print("Syntax Error error: {0}".format(err))
|
|
|
|
except EOFError:
|
|
|
|
return
|
|
|
|
except RuntimeError as err:
|
|
|
|
print("YAP Execution Error: {0}".format(err))
|
|
|
|
except ValueError:
|
|
|
|
print("Could not convert data to an integer.")
|
|
|
|
except:
|
|
|
|
print("Unexpected error:", sys.exc_info()[0])
|
|
|
|
raise
|
2016-09-21 20:55:09 +01:00
|
|
|
engine.close()
|
2016-07-31 16:09:21 +01:00
|
|
|
#
|
|
|
|
# initialize engine
|
|
|
|
# engine = yap.YAPEngine();
|
|
|
|
# engine = yap.YAPEngine(yap.YAPParams());
|
2016-09-21 20:55:09 +01:00
|
|
|
live()
|