Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions python/sandbox/jetta/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def jetta(j_space_id: str, code: str, url=None):
raise JettaServerError(r['messages'])
if r['type'] == 'java.lang.Integer':
r['result'] = int(r['result'])
return r['result']
# NOTE: disambiguation is needed if java.util.ArrayList is used
# as a grounded result instead of non-deterministic result
return r['result'] if r['type'] == 'java.util.ArrayList' \
else [r['result']]

def _err_msg(expr, msg):
if not isinstance(expr, Atom):
Expand All @@ -54,13 +57,16 @@ def jetta_unwrap_atom(j_space_a: Atom, code_a: Atom,
url = url_a.get_object().value
try:
result = jetta(j_space, code_a, url)
return [Atoms.UNIT if result is None else ValueAtom(result)]
# NOTE: handling symbols and expressions will be needed at some point
return [Atoms.UNIT if r is None else ValueAtom(r) for r in result]
except JettaServerError as e:
return _err_msg(code_a, e)
#return [E(S('Error'), ValueAtom(code),
# E(S('JettaCompileError'), ValueAtom(str(e))))]

def compile(metta: MeTTa, j_space_a, func_a, arity=None):
code = ""
# Get the function name
j_space = j_space_a.get_object().content
if arity is not None:
arity = arity.get_object().content
Expand All @@ -70,6 +76,15 @@ def compile(metta: MeTTa, j_space_a, func_a, arity=None):
return _err_msg(func_a, "compile expects a function name")
else:
func = repr(func_a)

# Get annotations (if any)
annotations = metta.space().query(
E(S('@'), S(func), V('$ann'))
)
for a in annotations:
code += f"(@ {func} {repr(a['$ann'])})\n"

# Get the type
typ = metta.space().query(
E(S(':'), S(func), V('t'))
)
Expand All @@ -86,17 +101,17 @@ def compile(metta: MeTTa, j_space_a, func_a, arity=None):
else:
typ = typ[0]['t']
arity = len(typ.get_children()) - 2
typ = f"(: {func} {repr(typ)})"
typ = f"(: {func} {repr(typ)})\n"
code += typ

f_args = E(S(func), *[V(f'x{i}') for i in range(arity)])
res = metta.space().query(
E(S('='), f_args, V('__r'))
)
res = list(res)
assert len(res) == 1, "Functions with one equality are allowed for now"
code = "(= " + repr(f_args) + "\n " +\
repr(res[0]['__r']) + ")"
code = typ + "\n" + code
code += "(= " + repr(f_args) + "\n " +\
repr(res[0]['__r']) + ")\n"
# TODO: check if compilation is successful
jetta(j_space, code)
#TODO: doesn't work for passing expressions (e.g. lambdas)
Expand Down
3 changes: 3 additions & 0 deletions python/sandbox/jetta/test_basic_jetta.metta
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
; creating a space for repetitive use
!(bind! &jspace (new-jetta-space))

;!(jetta &jspace (+ 1 2)) ; TODO
;!(metta (+ 1 2) Atom &jspace) ; TODO

; passing text code of a typed function definition
!(jetta &jspace
"(: foo (-> Int Int Int))
Expand Down
30 changes: 30 additions & 0 deletions python/sandbox/jetta/test_nondet.metta
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
!(import! &self compile)

!(bind! &jspace (new-jetta-space))
!(jetta &jspace "
(@ foo multivalued)
(: foo (-> Int))
(= (foo) (seq 1 2 3))

(@ bar multivalued)
(: bar (-> Int))
(= (bar) (+ 1 (if (>= (foo) 2) (+ 1 (foo)) 0)))")
!(assertEqualToResult
(jetta &jspace "(foo)")
(1 2 3))
!(assertEqualToResult
(jetta &jspace "(bar)")
(1 3 4 5 3 4 5))

(@ goo multivalued)
(: goo (-> Int))
(= (goo) (seq 1 2 3))
; FIXME: `(= (goo) (+ 1 (seq 1 2 3)))` doesn't work
!(compile &jspace goo)
;(@ doo multivalued)
(: doo (-> Int Int))
(= (doo $x) (+ $x (goo)))
!(compile &jspace doo)
!(assertEqualToResult
(doo-gnd 1)
(2 3 4))
Loading