ML-KULeuven / problog

ProbLog is a Probabilistic Logic Programming Language for logic programs with probabilities.
https://dtai.cs.kuleuven.be/problog/
317 stars 35 forks source link

bug in String library: str2lst? #84

Closed Mezzenilium closed 2 years ago

Mezzenilium commented 2 years ago

I try to use the predicates of the String library of Problog2, but it does not work or I have not understood the way to properly use them : %:- use_module(library(lists)). :- use_module(library(string)).

  o1(L) :- L = str2lst("aaaa, zzzz, eeee, rrrr").
  o2(S) :- S = lst2str(["aaa","dddd","fffff"]).
  o3(T) :- T = join('',["aaa","dddd","fffff"]).

  query(o1(S)).
  query(o2(S)).
  query(o3(S)).

gives

  o1(str2lst("aaaa, zzzz, eeee, rrrr")):    1         
  o2(lst2str(["aaa", "dddd", "fffff"])):    1         
  o3(join('',["aaa", "dddd", "fffff"])):    1

Originally posted by @Mezzenilium in https://github.com/ML-KULeuven/problog/issues/79#issuecomment-1103724787

Mezzenilium commented 2 years ago

I have made some progress in the use of the string library. I manage to have lst2str, join and concat working. But str2lst (test1) gives "An unexpected error has occurred." .

:- use_module(library(string)).

  atome1('aaaa zzzz eeee rrrr').
  test1(L) :- atome1(A), str2lst(A, L).

  atome2(['aaa','dddd','fffff']).
  test2(L) :- atome2(A), lst2str(A, L). 

  sep(',').
  atome3(['aaa','dddd','fffff']).
  test3(L) :- sep(S),atome3(A), join(S, A, L). 

  atome4(['aaa','dddd','fffff']).
  test4(L) :- atome3(A), concat(A, L). 

  %query(test1(V)).
  query(test2(U)).
  query(test3(T)).
  query(test4(X)).
VincentDerk commented 2 years ago

That might be a bug... @rmanhaeve What do you think?

:- use_module(library(string)).

test1(L) :- str2lst('aaaa zzzz eeee rrrr', L).
test2(L) :- lst2str(['aaa','dddd','fffff'], L).
test3(L) :- join(',', ['aaa','dddd','fffff'], L).
test4(L) :- concat(['aaa','dddd','fffff'], L).

query(test1(_)).
%query(test2(_)).
%query(test3(_)).
%query(test4(_)).

yields

  File ".../problog/problog/extern.py", line 110, in _convert_output
    return list2term(a)
  File ".../problog/problog/logic.py", line 119, in list2term
    for e in reversed(lst):
TypeError: 'map' object is not reversible

An unexpected error has occurred.

The current definition of str2lst (from string.py)

@problog_export("+str", "-list")
def str2lst(string):
    return map(Term, map(make_safe, string))

should probably be

@problog_export("+str", "-list")
def str2lst(string):
    return list(map(Term, map(make_safe, string)))

(notice return function), because right now it returns a map-function rather than a list. And since it expects to get a list ("-list"), it tries to convert the python list into a prolog list (here) which crashes because it instead received a map-function that is not reversible.

Mezzenilium commented 2 years ago

I have modified the string.py file with the statement return list(map(Term, map(make_safe, string))). My test examples run smoothly now and str2lst transforms a string in a list of its characters.

Thank you for your support.