2016-07-31 16:09:21 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-08-25 07:26:11 +01:00
|
|
|
from ipykernel.ipkernel import IPythonKernel
|
2016-07-31 16:09:21 +01:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import signal
|
|
|
|
import yap
|
2016-08-30 18:20:31 +01:00
|
|
|
# import ipdb
|
|
|
|
# ipdb,set_trace()
|
2016-08-25 07:26:11 +01:00
|
|
|
|
|
|
|
kernel_json = {
|
|
|
|
"argv": [sys.executable,
|
2016-08-30 18:20:31 +01:00
|
|
|
"-m", "yap_kernel",
|
|
|
|
"-f", "{connection_file}"],
|
|
|
|
"display_name": " YAP-6.3",
|
2016-08-25 07:26:11 +01:00
|
|
|
"language": "prolog",
|
|
|
|
"name": "yap_kernel",
|
|
|
|
}
|
2016-07-31 16:09:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def eprint(*args, **kwargs):
|
|
|
|
print(*args, file=sys.stderr, **kwargs)
|
|
|
|
|
2016-08-30 18:20:31 +01:00
|
|
|
|
2016-08-25 07:26:11 +01:00
|
|
|
class YAPKernel(IPythonKernel):
|
2016-07-31 16:09:21 +01:00
|
|
|
implementation = 'MetaKernel YAP'
|
|
|
|
implementation_version = '1.0'
|
|
|
|
language = 'text'
|
|
|
|
language_version = '0.1'
|
2016-08-30 18:20:31 +01:00
|
|
|
banner = "YAP-6.3"
|
2016-07-31 16:09:21 +01:00
|
|
|
language_info = {
|
|
|
|
'mimetype': 'text/prolog',
|
|
|
|
'name': 'text',
|
|
|
|
# ------ If different from 'language':
|
2016-08-25 07:26:11 +01:00
|
|
|
'codemirror_mode': {
|
2016-07-31 16:09:21 +01:00
|
|
|
"version": 2,
|
|
|
|
"name": "prolog"
|
2016-08-25 07:26:11 +01:00
|
|
|
},
|
|
|
|
'pygments_lexer': 'prolog',
|
2016-08-30 18:20:31 +01:00
|
|
|
'version': "0.0.1",
|
2016-07-31 16:09:21 +01:00
|
|
|
'file_extension': '.yap',
|
|
|
|
}
|
|
|
|
|
2016-08-26 06:13:50 +01:00
|
|
|
def init_yap(self, **kwargs):
|
2016-08-30 18:20:31 +01:00
|
|
|
# Signal handlers are inherited by
|
|
|
|
# forked processes,
|
|
|
|
# and we can't easily
|
|
|
|
# reset it from the subprocess. Since kernelapp
|
|
|
|
# ignores SIGINT except in
|
|
|
|
# message handlers, we need to temporarily
|
|
|
|
# reset the SIGINT handler
|
|
|
|
# here so that yap and its children are interruptible.
|
2016-07-31 16:09:21 +01:00
|
|
|
sig = signal.signal(signal.SIGINT, signal.SIG_DFL)
|
|
|
|
try:
|
|
|
|
self.engine = yap.YAPEngine()
|
|
|
|
self.q = None
|
|
|
|
self.engine.query("load_files(library(python), [])").command()
|
2016-08-18 08:13:55 +01:00
|
|
|
self.engine.query("load_files(library(jupyter), [])").command()
|
2016-08-25 07:26:11 +01:00
|
|
|
banner = "YAP6-3 Kernel"
|
2016-07-31 16:09:21 +01:00
|
|
|
self.olines = banner
|
|
|
|
finally:
|
2016-08-30 18:20:31 +01:00
|
|
|
signal.signal(signal.SIGINT, sig)
|
2016-07-31 16:09:21 +01:00
|
|
|
|
2016-08-26 06:13:50 +01:00
|
|
|
def __init__(self, **kwargs):
|
2016-08-30 18:20:31 +01:00
|
|
|
super(YAPKernel, self).__init__(**kwargs)
|
|
|
|
self.init_yap(**kwargs)
|
2016-08-26 06:13:50 +01:00
|
|
|
self.shell.run_cell = self.yap_run_cell
|
|
|
|
|
2016-07-31 16:09:21 +01:00
|
|
|
def get_usage(self):
|
|
|
|
return "This is the YAP kernel."
|
|
|
|
|
2016-08-30 18:20:31 +01:00
|
|
|
def yap_run_cell(self, s, store_history=False, silent=False,
|
|
|
|
shell_futures=True):
|
2016-07-31 16:09:21 +01:00
|
|
|
|
|
|
|
if not self.q:
|
|
|
|
self.q = self.engine.query(s)
|
|
|
|
if self.q.next():
|
2016-08-18 08:13:55 +01:00
|
|
|
myvs = self.q.namedVarsCopy()
|
|
|
|
if myvs:
|
2016-07-31 16:09:21 +01:00
|
|
|
i = 0
|
2016-08-18 08:13:55 +01:00
|
|
|
for peq in myvs:
|
|
|
|
name = peq[0]
|
|
|
|
bind = peq[1]
|
2016-07-31 16:09:21 +01:00
|
|
|
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 self.q.deterministic():
|
|
|
|
self.closeq()
|
|
|
|
return
|
|
|
|
print("No (more) answers")
|
|
|
|
self.closeq()
|
|
|
|
return
|
|
|
|
|
2016-08-30 18:20:31 +01:00
|
|
|
def closeq(self):
|
2016-07-31 16:09:21 +01:00
|
|
|
if self.q:
|
|
|
|
self.q.close()
|
|
|
|
self.q = None
|