Merge ssh://ssh.dcc.fc.up.pt:31064/home/vsc/yap

This commit is contained in:
Vitor Santos Costa
2018-05-22 21:40:22 +01:00
16 changed files with 803 additions and 1009 deletions

View File

@@ -200,7 +200,7 @@
int getRealNumber(char *c, double *number);
int getIntNumber(char *c, int *number);
inline int getPosNumber(char *c, int *number);
//inline int getPosNumber(char *c, int *number);
int IsRealNumber(char *c);
int IsPosNumber(const char *c);
int IsNumber(const char *c);

View File

@@ -188,12 +188,13 @@
//#include <stdio.h>
//#include <stdlib.h>
#include <unistd.h>
#include "simplecudd.h"
#include "problogmath.h"
#include <signal.h>
#include <time.h>
#define VERSION "2.0.1"
#define PROBLOGBDD_VERSION "2.0.1"
#ifndef max
@@ -1550,7 +1551,7 @@ int argtype(const char *arg) {
}
void printhelp(int argc, char **arg) {
fprintf(stderr, "\n\nProbLogBDD Tool Version: %s\n\n", VERSION);
fprintf(stderr, "\n\nProbLogBDD Tool Version: %s\n\n", PROBLOGBDD_VERSION);
fprintf(stderr, "SimpleCUDD library (www.cs.kuleuven.be/~theo/tools/simplecudd.html)\n");
fprintf(stderr, "SimpleCUDD was developed at Katholieke Universiteit Leuven(www.kuleuven.be)\n");
fprintf(stderr, "Copyright Katholieke Universiteit Leuven 2008\n");

View File

@@ -400,7 +400,7 @@ int simpleNamedBDDtoDot(DdManager *manager, namedvars varmap, DdNode *bdd,
// Cudd_AutodynEnable(mana, CUDD_REORDER_EXACT);
// Cudd_ReduceHeap(manager, CUDD_REORDER_SIFT, 1);
ret = Cudd_DumpDot(manager, 1, f, varmap.vars, NULL, fd);
ret = Cudd_DumpDot(manager, 1, f, (const char *const *)varmap.vars, NULL, fd);
fclose(fd);
return ret;
}

View File

@@ -7,8 +7,7 @@
YAP_Term TermErrStream, TermOutStream;
static int py_put(int sno, int ch)
{
static int py_put(int sno, int ch) {
// PyObject *pyw; // buffer
// int pyw_kind;
// PyObject *pyw_data;
@@ -16,13 +15,13 @@ static int py_put(int sno, int ch)
if (st->user_name == TermOutStream) {
term_t tg = python_acquire_GIL();
PySys_WriteStdout("%C", ch);
python_release_GIL(tg);
return ch;
python_release_GIL(tg);
return ch;
}
if (st->user_name == TermErrStream) {
term_t tg = python_acquire_GIL();
PySys_WriteStderr("%C", ch);
python_release_GIL(tg);
term_t tg = python_acquire_GIL();
PySys_WriteStderr("%C", ch);
python_release_GIL(tg);
return ch;
}
char s[2];
@@ -33,8 +32,7 @@ static int py_put(int sno, int ch)
PyObject_CallMethodObjArgs(st->u.private_data, PyUnicode_FromString("write"),
PyUnicode_FromString(s), NULL);
python_release_GIL(g0);
if ((err = PyErr_Occurred()))
{
if ((err = PyErr_Occurred())) {
PyErr_SetString(
err,
"Error in put\n"); // %s:%s:%d!\n", __FILE__, __FUNCTION__, __LINE__);
@@ -43,7 +41,8 @@ static int py_put(int sno, int ch)
}
VFS_t pystream;
static void *py_open(VFS_t *me, const char *name, const char *io_mode, int sno) {
static void *py_open(VFS_t *me, const char *name, const char *io_mode,
int sno) {
#if HAVE_STRCASESTR
if (strcasestr(name, "/python/") == name)
name += strlen("/python/");
@@ -52,34 +51,36 @@ VFS_t pystream;
name += strlen("/python/");
#endif
term_t ctk = python_acquire_GIL();
PyObject *pystream = string_to_python(name, true, NULL);
if (pystream == NULL || pystream == Py_None) {
python_release_GIL(ctk);
return NULL;
}
PyObject *pystream = string_to_python(name, true, NULL);
if (pystream == NULL || pystream == Py_None) {
python_release_GIL(ctk);
return NULL;
}
StreamDesc *st = YAP_RepStreamFromId(sno);
st->name = YAP_LookupAtom(name);
if (strcmp(name,"sys.stdout") == 0) {
if (strcmp(name, "sys.stdout") == 0) {
st->user_name = TermOutStream;
} else if(strcmp(name,"sys.stderr") == 0) {
} else if (strcmp(name, "sys.stderr") == 0) {
st->user_name = TermErrStream;
} else {
st->user_name = YAP_MkAtomTerm(st->name);
}
// we assume object is already open, so there is no need to open it.
st->u.private_data = pystream;
if (PyCallable_Check(pystream))
st->u.private_data = PyObject_Call(pystream, PyTuple_New(0), NULL);
else
st->u.private_data = pystream;
st->vfs = me;
python_release_GIL(ctk);
python_release_GIL(ctk);
return st;
}
static bool py_close(int sno) {
StreamDesc *st = YAP_RepStreamFromId(sno);
if (strcmp(st->name,"sys.stdout") &&
strcmp(st->name,"sys.stderr")) {
Py_XDECREF(st->u.private_data);
if (strcmp(st->name, "sys.stdout") && strcmp(st->name, "sys.stderr")) {
Py_XDECREF(st->u.private_data);
}
st->u.private_data = NULL;
st->u.private_data = NULL;
st->vfs = NULL;
return true;
@@ -87,19 +88,20 @@ static bool py_close(int sno) {
static bool getLine(int inp) {
char *myrl_line = NULL;
StreamDesc *rl_instream = YAP_RepStreamFromId(inp);
term_t ctk = python_acquire_GIL();
fprintf(stderr,"in");
PyObject*prompt = PyUnicode_FromString( "?- "),
*msg = PyUnicode_FromString(" **input** ");
StreamDesc *rl_instream = YAP_RepStreamFromId(inp);
term_t ctk = python_acquire_GIL();
fprintf(stderr, "in");
PyObject *prompt = PyUnicode_FromString("?- "),
*msg = PyUnicode_FromString(" **input** ");
/* window of vulnerability opened */
myrl_line = PyUnicode_AsUTF8(PyObject_CallFunctionObjArgs(rl_instream->u.private_data,msg,prompt,NULL));
myrl_line = PyUnicode_AsUTF8(PyObject_CallFunctionObjArgs(
rl_instream->u.private_data, msg, prompt, NULL));
python_release_GIL(ctk);
rl_instream->u.irl.ptr = rl_instream->u.irl.buf = (const unsigned char*)myrl_line;
rl_instream->u.irl.ptr = rl_instream->u.irl.buf =
(const unsigned char *)myrl_line;
myrl_line = NULL;
return true;
}
static int py_getc(int sno) {
StreamDesc *s = YAP_RepStreamFromId(sno);
@@ -117,7 +119,7 @@ static int py_getc(int sno) {
} else {
return EOF;
}
return ch;
return ch;
}
/**
@@ -151,23 +153,22 @@ static int py_peek(int sno) {
return ch;
}
static int64_t py_seek(int sno, int64_t where, int how) {
StreamDesc *g0 = YAP_RepStreamFromId(sno);
term_t s0 = python_acquire_GIL();
term_t s0 = python_acquire_GIL();
PyObject *fseek = PyObject_GetAttrString(g0->u.private_data, "seek");
PyObject *pyr = PyObject_CallFunctionObjArgs(fseek, PyLong_FromLong(where),
PyLong_FromLong(how), NULL);
python_release_GIL(s0);
return PyLong_AsLong(pyr);
python_release_GIL(s0);
return PyLong_AsLong(pyr);
}
static void py_flush(int sno) {
StreamDesc *s = YAP_GetStreamFromId(sno);
term_t tg = python_acquire_GIL();
term_t tg = python_acquire_GIL();
PyObject *flush = PyObject_GetAttrString(s->u.private_data, "flush");
PyObject_CallFunction(flush, NULL);
python_release_GIL(tg);
python_release_GIL(tg);
}
#if 0

View File

@@ -20,7 +20,6 @@
% ]
%% ).
:- [library(hacks)].
:- reexport(library(yapi)).
:- use_module(library(lists)).
:- use_module(library(maplist)).
@@ -274,4 +273,21 @@ close_events( Self ) :-
fail.
close_events( _ ).
:- if( current_prolog_flag(apple, true) ).
:- putenv( 'LC_ALL', 'en_us:UTF-8').
plot_inline :-
X := self.inline_plotting,
nb_setval(inline, X ),
X = true,
!,
:= (
import( matplotlib ),
matplotlib.use( `nbagg` )
).
:- endif.
%:- ( start_low_level_trace ).

View File

@@ -19,231 +19,24 @@
#
# Variable search order:
# 1. Attempt to locate and set R_COMMAND
# - If unsuccessful, generate error and prompt user to manually set R_COMMAND
# If unsuccessful, generate error and prompt user to manually set R_COMMAND
# 2. Use R_COMMAND to set R_HOME
# 3. Locate other libraries in the priority:
# 1. Within a user-built instance of R at R_HOME
# 2. Within an installed instance of R
# 3. Within external system libraries
#
if (R_LIBRARIES AND R_INCLUDE_DIR)
set_package_properties(R PROPERTIES
DESCRIPTION "The R Project for Statistical Computing."
URL "https://www.r-project.org/")
find_program (
R_COMMAND
NAMES R r
)
if (R_COMMAND)
# find the R binary
MESSAGE(STATUS "Looking for R executable")
IF(NOT R_EXECUTABLE)
FIND_PROGRAM(R_EXECUTABLE R)
IF(R_EXECUTABLE-NOTFOUND)
MESSAGE(FATAL_ERROR "Could NOT find R (TODO: name option)")
ELSE(R_EXECUTABLE-NOTFOUND)
MESSAGE(STATUS "Using R at ${R_EXECUTABLE}")
ENDIF(R_EXECUTABLE-NOTFOUND)
ENDIF(NOT R_EXECUTABLE)
# find R_HOME
MESSAGE(STATUS "Looking for R_HOME")
IF(NOT R_HOME)
EXECUTE_PROCESS(
COMMAND ${R_EXECUTABLE} "--slave" "--no-save" "-e" "cat(R.home())"
OUTPUT_VARIABLE R_HOME)
ENDIF(NOT R_HOME)
IF(NOT R_HOME)
MESSAGE(FATAL_ERROR "Could NOT determine R_HOME (probably you misspecified the location of R)")
ELSE(NOT R_HOME)
MESSAGE(STATUS "R_HOME is ${R_HOME}")
ENDIF(NOT R_HOME)
# find R include dir
MESSAGE(STATUS "Looking for R include files")
IF(NOT R_INCLUDEDIR)
IF(WIN32 OR APPLE) # This version of the test will not work with R < 2.9.0, but the other version (in the else part) will not work on windows or apple (but we do not really need to support ancient versions of R, there).
EXECUTE_PROCESS(
COMMAND ${R_EXECUTABLE} "--slave" "--no-save" "-e" "cat(R.home('include'))"
OUTPUT_VARIABLE R_INCLUDEDIR)
ELSE(WIN32 OR APPLE)
EXECUTE_PROCESS(
COMMAND ${R_EXECUTABLE} CMD sh -c "echo -n $R_INCLUDE_DIR"
OUTPUT_VARIABLE R_INCLUDEDIR)
ENDIF(WIN32 OR APPLE)
ELSE(NOT R_INCLUDEDIR)
MESSAGE(STATUS "Location specified by user")
ENDIF(NOT R_INCLUDEDIR)
IF(NOT R_INCLUDEDIR)
SET(R_INCLUDEDIR ${R_HOME}/include)
MESSAGE(STATUS "Not findable via R. Guessing")
ENDIF(NOT R_INCLUDEDIR)
MESSAGE(STATUS "Include files should be at ${R_INCLUDEDIR}. Checking for R.h")
IF(NOT R_H)
FIND_FILE(R_H
R.h
PATHS ${R_INCLUDEDIR}
NO_DEFAULT_PATH)
ENDIF(NOT R_H)
IF(NOT R_H)
MESSAGE(FATAL_ERROR "Not found")
ELSE(NOT R_H)
MESSAGE(STATUS "Found at ${R_H}")
GET_FILENAME_COMPONENT(R_INCLUDEDIR ${R_H}
PATH)
ENDIF(NOT R_H)
# check for existence of libR.so
IF(NOT LIBR_SO)
MESSAGE(STATUS "Checking for existence of R shared library")
FIND_LIBRARY(LIBR_SO
R
PATHS ${R_HOME}/lib ${R_SHAREDLIBDIR} ${R_HOME}/bin
NO_DEFAULT_PATH)
endif(NOT LIBR_SO)
IF(NOT LIBR_SO)
MESSAGE(FATAL_ERROR "Not found. Make sure the location of R was detected correctly, above, and R was compiled with the --enable-shlib option")
ELSE(NOT LIBR_SO)
MESSAGE(STATUS "Exists at ${LIBR_SO}")
GET_FILENAME_COMPONENT(R_SHAREDLIBDIR ${LIBR_SO}
PATH)
SET(R_USED_LIBS R)
ENDIF(NOT LIBR_SO)
# for at least some versions of R, we seem to have to link against -lRlapack. Else loading some
# R packages will fail due to unresolved symbols, or we can't link against -lR.
# However, we can't do this unconditionally,
# as this is not available in some configurations of R
MESSAGE(STATUS "Checking whether we should link against Rlapack library")
FIND_LIBRARY(LIBR_LAPACK
Rlapack
PATHS ${R_SHAREDLIBDIR}
NO_DEFAULT_PATH)
IF(NOT LIBR_LAPACK)
MESSAGE(STATUS "No, it does not exist in ${R_SHAREDLIBDIR}")
ELSE(NOT LIBR_LAPACK)
MESSAGE(STATUS "Yes, ${LIBR_LAPACK} exists")
SET(R_USED_LIBS ${R_USED_LIBS} Rlapack)
IF(WIN32 OR APPLE)
ELSE(WIN32 OR APPLE)
# needed when linking to Rlapack on linux for some unknown reason.
# apparently not needed on windows (let's see, when it comes back to bite us, though)
# and compiling on windows is hard enough even without requiring libgfortran, too.
SET(R_USED_LIBS ${R_USED_LIBS} gfortran)
ENDIF(WIN32 OR APPLE)
ENDIF(NOT LIBR_LAPACK)
# for at least some versions of R, we seem to have to link against -lRlapack. Else loading some
# R packages will fail due to unresolved symbols, or we can't link against -lR.
# However, we can't do this unconditionally,
# as this is not available in some configurations of R
MESSAGE(STATUS "Checking whether we should link against Rblas library")
FIND_LIBRARY(LIBR_BLAS
Rblas
PATHS ${R_SHAREDLIBDIR}
NO_DEFAULT_PATH)
IF(NOT LIBR_BLAS)
MESSAGE(STATUS "No, it does not exist in ${R_SHAREDLIBDIR}")
ELSE(NOT LIBR_BLAS)
MESSAGE(STATUS "Yes, ${LIBR_BLAS} exists")
SET(R_USED_LIBS ${R_USED_LIBS} Rblas)
ENDIF(NOT LIBR_BLAS)
# find R package library location
IF(WIN32)
SET(PATH_SEP ";")
ELSE(WIN32)
SET(PATH_SEP ":")
ENDIF(WIN32)
MESSAGE(STATUS "Checking for R package library location to use")
IF(NOT R_LIBDIR)
EXECUTE_PROCESS(
COMMAND ${R_EXECUTABLE} "--slave" "--no-save" "-e" "cat(paste(unique (c(.Library.site, .Library)), collapse='${PATH_SEP}'))"
OUTPUT_VARIABLE R_LIBDIR)
ELSE(NOT R_LIBDIR)
MESSAGE(STATUS "Location specified by user")
ENDIF(NOT R_LIBDIR)
# strip whitespace
STRING(REGEX REPLACE "[ \n]+"
"" R_LIBDIR
"${R_LIBDIR}")
# strip leading colon(s)
STRING(REGEX REPLACE "^${PATH_SEP}+"
"" R_LIBDIR
"${R_LIBDIR}")
# strip trailing colon(s)
STRING(REGEX REPLACE "${PATH_SEP}+$"
"" R_LIBDIR
"${R_LIBDIR}")
# find first path
STRING(REGEX REPLACE "${PATH_SEP}"
" " R_LIBDIR
"${R_LIBDIR}")
IF(NOT R_LIBDIR)
MESSAGE(STATUS "Not reliably determined or specified. Guessing.")
SET(R_LIBDIR ${R_HOME}/library)
ENDIF(NOT R_LIBDIR)
SET(R_LIBDIRS ${R_LIBDIR})
SEPARATE_ARGUMENTS(R_LIBDIRS)
SET(R_LIBDIR)
FOREACH(CURRENTDIR ${R_LIBDIRS})
IF(NOT USE_R_LIBDIR)
IF(EXISTS ${CURRENTDIR})
SET(R_LIBDIR ${CURRENTDIR})
SET(USE_R_LIBDIR 1)
ELSE(EXISTS ${CURRENTDIR})
MESSAGE(STATUS "${CURRENTDIR} does not exist. Skipping")
ENDIF(EXISTS ${CURRENTDIR})
ENDIF(NOT USE_R_LIBDIR)
ENDFOREACH(CURRENTDIR ${R_LIBDIRS})
IF(NOT EXISTS ${R_LIBDIR})
MESSAGE(FATAL_ERROR "No existing library location found")
ELSE(NOT EXISTS ${R_LIBDIR})
MESSAGE(STATUS "Will use ${R_LIBDIR}")
ENDIF(NOT EXISTS ${R_LIBDIR})
endif()
#macro_optional_find_package (R ON)
if (R_INCLUDEDIR AND R_LIBDIR)
add_feature_info(R yes "Real")
set (REAL_SOURCES
real.c
)
set (REAL_PL
real.pl
)
add_to_group( REAL_PL pl_library)
URL "https://www.r-project.org/")
add_lib(real ${REAL_SOURCES})
target_link_libraries (real ${R_LIBRARIES} libYap)
include_directories (
${CMAKE_CURRENT_BINARY_DIR}
${R_INCLUDEDIR}
${CMAKE_CURRENT_BINARY_DIR}
${R_INCLUDE_DIR}
)
list (APPEND CMAKE_REQUIRED_INCLUDES
@@ -251,12 +44,8 @@ include_directories (
${R_INCLUDE_DIR}
)
add_lib(real ${REAL_SOURCES})
link_directories(${R_LIBDIR})
target_link_libraries (real R libYap)
check_include_files( "stdio.h;R.h" HAVE_R_H )
check_include_files( "R.h;Rembedded.h" HAVE_R_EMBEDDED_H )
check_include_files( "R.h,;Rembedded.h" HAVE_R_EMBEDDED_H )
check_include_files( "Rembedded.h;Rinterface.h" HAVE_R_INTERFACE_H )
configure_file ("rconfig.h.cmake" "rconfig.h" )

View File

@@ -63,8 +63,8 @@ Solver::Solver() :
Solver::~Solver()
{
for (int i = 0; i < learnts.size(); i++) free(learnts[i]);
for (int i = 0; i < clauses.size(); i++) free(clauses[i]);
for (int i = 0; i < learnts.size(); i++) std::free(learnts[i]);
for (int i = 0; i < clauses.size(); i++) std::free(clauses[i]);
}
@@ -163,7 +163,7 @@ void Solver::detachClause(Clause& c) {
void Solver::removeClause(Clause& c) {
detachClause(c);
free(&c); }
std::free(&c); }
bool Solver::satisfied(const Clause& c) const {

View File

@@ -147,7 +147,7 @@ template<class V>
Clause* Clause_new(const V& ps, bool learnt) {
assert(sizeof(Lit) == sizeof(uint32_t));
assert(sizeof(float) == sizeof(uint32_t));
void* mem = malloc(sizeof(Clause) + sizeof(uint32_t)*(ps.size()));
void* mem = std::malloc(sizeof(Clause) + sizeof(uint32_t)*(ps.size()));
return new (mem) Clause(ps, learnt); }
/*_________________________________________________________________________________________________
|

View File

@@ -27,7 +27,7 @@ OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWA
//=================================================================================================
// Automatically resizable arrays
//
// NOTE! Don't use this vector on datatypes that cannot be re-located in memory (with realloc)
// NOTE! Don't use this vector on datatypes that cannot be re-located in memory (with std::realloc)
template<class T>
class vec {
@@ -79,9 +79,9 @@ public:
// Stack interface:
#if 1
void push (void) { if (sz == cap) { cap = imax(2, (cap*3+1)>>1); data = (T*)realloc(data, cap * sizeof(T)); } new (&data[sz]) T(); sz++; }
//void push (const T& elem) { if (sz == cap) { cap = imax(2, (cap*3+1)>>1); data = (T*)realloc(data, cap * sizeof(T)); } new (&data[sz]) T(elem); sz++; }
void push (const T& elem) { if (sz == cap) { cap = imax(2, (cap*3+1)>>1); data = (T*)realloc(data, cap * sizeof(T)); } data[sz++] = elem; }
void push (void) { if (sz == cap) { cap = imax(2, (cap*3+1)>>1); data = (T*)std::realloc(data, cap * sizeof(T)); } new (&data[sz]) T(); sz++; }
//void push (const T& elem) { if (sz == cap) { cap = imax(2, (cap*3+1)>>1); data = (T*)std::realloc(data, cap * sizeof(T)); } new (&data[sz]) T(elem); sz++; }
void push (const T& elem) { if (sz == cap) { cap = imax(2, (cap*3+1)>>1); data = (T*)std::realloc(data, cap * sizeof(T)); } data[sz++] = elem; }
void push_ (const T& elem) { assert(sz < cap); data[sz++] = elem; }
#else
void push (void) { if (sz == cap) grow(sz+1); new (&data[sz]) T() ; sz++; }
@@ -106,7 +106,7 @@ void vec<T>::grow(int min_cap) {
if (min_cap <= cap) return;
if (cap == 0) cap = (min_cap >= 2) ? min_cap : 2;
else do cap = (cap*3+1) >> 1; while (cap < min_cap);
data = (T*)realloc(data, cap * sizeof(T)); }
data = (T*)std::realloc(data, cap * sizeof(T)); }
template<class T>
void vec<T>::growTo(int size, const T& pad) {
@@ -127,7 +127,7 @@ void vec<T>::clear(bool dealloc) {
if (data != NULL){
for (int i = 0; i < sz; i++) data[i].~T();
sz = 0;
if (dealloc) free(data), data = NULL, cap = 0; } }
if (dealloc) std::free(data), data = NULL, cap = 0; } }
#endif