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

problog_export does not support type annotated functions #24

Closed
XVilka opened this issue Aug 8, 2019 · 4 comments
Closed

problog_export does not support type annotated functions #24

XVilka opened this issue Aug 8, 2019 · 4 comments

Comments

@XVilka
Copy link
Contributor

XVilka commented Aug 8, 2019

I have a code like this:

#!/usr/bin/env python3
from __future__ import unicode_literals
from typing import List
from problog.extern import problog_export

# -----------------------------------------------------------------------------
# ProbLog exports

# instruction_string(Architecture, Bits, Address, Bytes, Instruction).
@problog_export('+str', '+int', '+int', '+list', '-str')
def instruction_string(architecture : str, bits : int, address : int, bytelist : List) -> str:
    return "blah"

But if I ran instruction_string("x86", 32, 0, [142, 142, 142], A). it fails miserably:

λ query(instruction_string("x86", 32, 0, [142,142,142], A)).                                                                                                                                                                                 
Traceback (most recent call last):
  File "./PrologShell.py", line 123, in <module>
    repl(ProblogDb)
  File "./PrologShell.py", line 94, in repl
    print_status(ProblogDb)
  File "./PrologShell.py", line 64, in print_status
    result = get_evaluatable().create_from(ProblogDb).evaluate()
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/core.py", line 150, in create_from
    return ProbLog.convert(obj, cls, **kwdargs)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/core.py", line 120, in convert
    next_obj = path[0](current_obj, path[1](**kwdargs), **kwdargs)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/engine.py", line 54, in ground
    return ground_default(model, target, **kwdargs)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/engine.py", line 74, in ground_default
    propagate_evidence=propagate_evidence, labels=labels)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/engine.py", line 492, in ground_all
    self.ground_queries(db, target, queries)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/engine.py", line 453, in ground_queries
    target = self.ground(db, query, target, label=label)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/engine.py", line 301, in ground
    target, results = self._ground(db, term, target, silent_fail=False, **kwdargs)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/engine.py", line 409, in _ground
    results = self.execute(clause_node, database=db, target=gp, context=context, **kwdargs)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/engine_stack.py", line 521, in execute
    is_root=is_root, **kwdargs)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/engine_stack.py", line 180, in eval
    return exec_func(node_id=node_id, node=node, **kwdargs)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/engine_stack.py", line 992, in eval_extern
    return self.eval_builtin(node=SimpleBuiltIn(node.function), **kwdargs)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/engine_stack.py", line 995, in eval_builtin
    return self.eval_default(EvalBuiltIn, **kwdargs)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/engine_stack.py", line 999, in eval_default
    cleanup, actions = node()  # Evaluate the node
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/engine_stack.py", line 2181, in __call__
    current_clause=self.current_clause)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/engine_stack.py", line 2227, in __call__
    results = self.base_function(*args, **kwdargs)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/extern.py", line 138, in _wrapped_function
    bound = check_mode(args, list(self._extract_callmode()), funcname, **kwdargs)
  File "/home/user/.penv/lib64/python3.7/site-packages/problog/engine_builtin.py", line 611, in check_mode
    raise CallModeError(functor, args, accepted, location=location)
problog.engine_builtin.CallModeError: Invalid argument types for call to 'instruction_string/5': arguments: ("x86", 32, 0, [142, 142, 142], X1), expected: (atom, integer, integer, fixed_list, var) or (atom, integer, integer, fixed_list, atom).
@anton3s
Copy link
Collaborator

anton3s commented Aug 8, 2019

The Python 'str' type is mapped to the Prolog 'atom' type.
The expression "x86" is not a valid atom, you should use 'x86' instead.
As far as I remember Prolog strings (i.e. double quoted) are not supported in ProbLog (yet).

@XVilka
Copy link
Contributor Author

XVilka commented Aug 8, 2019

Changed the syntax to 'x86', but now get a different error:

raise ValueError("Function has keyword-only parameters or annotations)

@anton3s
Copy link
Collaborator

anton3s commented Aug 8, 2019

It seems problog_export expect positional arguments on the exported Python function.
Adding type annotations apparently changes them to keyword parameters.

@anton3s anton3s changed the title exporting Python functions to ProbLog doesn't work problog_export does not support type annotated functions Aug 8, 2019
@XVilka
Copy link
Contributor Author

XVilka commented Aug 8, 2019

Thanks! Now it works. Created two separate issues:

@XVilka XVilka closed this as completed Aug 8, 2019
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

2 participants