From 70a43ece1d8629b34a10820a665512641bbd1596 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Mon, 18 Mar 2019 14:47:29 +0000 Subject: [PATCH] jupyter --- CMakeLists.txt | 4 +- CXX/yapa.hh | 48 +++++++++++++++++ CXX/yapi.cpp | 17 ++++++ CXX/yapt.hh | 53 ++----------------- info/meta.yaml | 2 +- packages/gecode/dev/code-generator.py | 2 +- packages/gecode/dev/extractor/Makefile | 2 +- packages/gecode/gecode6-common.icc | 12 ++--- packages/gecode/gecode6_yap.cc | 22 ++++---- packages/python/swig/prolog/yapi.yap | 12 ++--- packages/python/swig/yap4py/yapi.py | 4 +- .../yap_kernel/yap_ipython/core/usage.py | 4 +- .../python/yap_kernel/yap_ipython/yapi.py | 3 +- packages/swig/CMakeLists.txt | 3 +- 14 files changed, 105 insertions(+), 83 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d96b4c0d0..9d7fbeed1 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -590,7 +590,7 @@ ENDIF (WITH_PYTHON) IF (WITH_R) find_host_package(LibR) add_subDIRECTORY(packages/real) -ENDIF (WITH_R) + ENDIF (WITH_R) include(Sources) @@ -811,7 +811,7 @@ endif () if (WITH_JAVA) #detect java setup, as it is shared between different installations. - find_package(Java COMPONENTS Runtime Development) + find_package(Java COMPONENTS Development Runtime) # find_package(Java COMPONENTS Development) # find_package(Java COMPONENTS Runtime) #find_package(JavaLibs) diff --git a/CXX/yapa.hh b/CXX/yapa.hh index 8d0454326..c4f76e465 100644 --- a/CXX/yapa.hh +++ b/CXX/yapa.hh @@ -118,6 +118,54 @@ public: }; +/** + * @brief YAPFunctor represents Prolog functors Name/Arity + */ +class X_API YAPFunctor : public YAPProp { + friend class YAPApplTerm; + friend class YAPTerm; + friend class YAPPredicate; + friend class YAPQuery; + Functor f; + /// Constructor: receives Prolog functor and casts it to YAPFunctor + /// + /// Notice that this is designed for internal use only. + inline YAPFunctor(Functor ff) { f = ff; } + +public: + /// Constructor: receives name as an atom, plus arity + /// + /// This is the default method, and the most popular + YAPFunctor(YAPAtom at, uintptr_t arity) { f = Yap_MkFunctor(at.a, arity); } + + /// Constructor: receives name as a string plus arity + /// + /// Notice that this is designed for ISO-LATIN-1 right now + /// Note: Python confuses the 3 constructors, + /// use YAPFunctorFromString + inline YAPFunctor(const char *s, uintptr_t arity, bool isutf8 = true) { + f = Yap_MkFunctor(Yap_LookupAtom(s), arity); + } + /// Constructor: receives name as a wide string plus arity + /// + /// Notice that this is designed for UNICODE right now + /// + /// Note: Python confuses the 3 constructors, + /// use YAPFunctorFromWideString + inline YAPFunctor(const wchar_t *s, uintptr_t arity) { + CACHE_REGS f = Yap_MkFunctor(UTF32ToAtom(s PASS_REGS), arity); + } + /// Getter: extract name of functor as an atom + /// + /// this is for external usage. + YAPAtom name(void) { return YAPAtom(NameOfFunctor(f)); } + + /// Getter: extract arity of functor as an unsigned integer + /// + /// this is for external usage. + uintptr_t arity(void) { return ArityOfFunctor(f); } +}; + #endif /* YAPA_HH */ /// @} diff --git a/CXX/yapi.cpp b/CXX/yapi.cpp index fe3a3789c..df03d1e4b 100644 --- a/CXX/yapi.cpp +++ b/CXX/yapi.cpp @@ -411,6 +411,23 @@ std::vector YAPPairTerm::listToArray() { return o; } +std::vector YAPPairTerm::listToVector() { + Term *tailp; + Term t1 = gt(); + Int l = Yap_SkipList(&t1, &tailp); + if (l < 0) { + throw YAPError(SOURCE(), TYPE_ERROR_LIST, (t), nullptr); + } + std::vector o = *new std::vector(l); + int i = 0; + Term t = gt(); + while (t != TermNil) { + o[i++] = YAPTerm(HeadOfTerm(t)); + t = TailOfTerm(t); + } + return o; +} + YAP_tag_t YAPTerm::tag() { Term tt = gt(); if (IsVarTerm(tt)) { diff --git a/CXX/yapt.hh b/CXX/yapt.hh index bf8abb188..c5eecaa5e 100644 --- a/CXX/yapt.hh +++ b/CXX/yapt.hh @@ -2,6 +2,10 @@ * @file yapt.hh */ +#ifndef X_API +#define X_API +#endif + /** * @defgroup yap-cplus-term-handling Term Handling in the YAP interface. * @@ -240,54 +244,6 @@ public: inline bool initialized() { return t != 0; }; }; -/** - * @brief YAPFunctor represents Prolog functors Name/Arity - */ -class X_API YAPFunctor : public YAPProp { - friend class YAPApplTerm; - friend class YAPTerm; - friend class YAPPredicate; - friend class YAPQuery; - Functor f; - /// Constructor: receives Prolog functor and casts it to YAPFunctor - /// - /// Notice that this is designed for internal use only. - inline YAPFunctor(Functor ff) { f = ff; } - -public: - /// Constructor: receives name as an atom, plus arity - /// - /// This is the default method, and the most popular - YAPFunctor(YAPAtom at, uintptr_t arity) { f = Yap_MkFunctor(at.a, arity); } - - /// Constructor: receives name as a string plus arity - /// - /// Notice that this is designed for ISO-LATIN-1 right now - /// Note: Python confuses the 3 constructors, - /// use YAPFunctorFromString - inline YAPFunctor(const char *s, uintptr_t arity, bool isutf8 = true) { - f = Yap_MkFunctor(Yap_LookupAtom(s), arity); - } - /// Constructor: receives name as a wide string plus arity - /// - /// Notice that this is designed for UNICODE right now - /// - /// Note: Python confuses the 3 constructors, - /// use YAPFunctorFromWideString - inline YAPFunctor(const wchar_t *s, uintptr_t arity) { - CACHE_REGS f = Yap_MkFunctor(UTF32ToAtom(s PASS_REGS), arity); - } - /// Getter: extract name of functor as an atom - /// - /// this is for external usage. - YAPAtom name(void) { return YAPAtom(NameOfFunctor(f)); } - - /// Getter: extract arity of functor as an unsigned integer - /// - /// this is for external usage. - uintptr_t arity(void) { return ArityOfFunctor(f); } -}; - /** * @brief Compound Term */ @@ -371,6 +327,7 @@ public: bool nil() { return gt() == TermNil; } YAPPairTerm cdr() { return YAPPairTerm(TailOfTerm(gt())); } std::vector listToArray(); + std::vector listToVector(); }; /** diff --git a/info/meta.yaml b/info/meta.yaml index d75d2ba34..8a51f5bbd 100644 --- a/info/meta.yaml +++ b/info/meta.yaml @@ -1,6 +1,6 @@ package: name: yap4py - version: 6.4.0 + version: 6.5.0 requirements: ignore_prefix_files: diff --git a/packages/gecode/dev/code-generator.py b/packages/gecode/dev/code-generator.py index 0e9f463ae..31925dce8 100755 --- a/packages/gecode/dev/code-generator.py +++ b/packages/gecode/dev/code-generator.py @@ -694,7 +694,7 @@ class CCDescriptor(object): print('YAP_UserCPredicate("gecode_constraint_%s", gecode_constraint_%s, %d);' \ % (self.api, self.api, len(self.argtypes))) -GECODE_VERSION = None +GECODE_VERSION = "6.1.1" def gecode_version(): #import pdb; pdb.set_trace() diff --git a/packages/gecode/dev/extractor/Makefile b/packages/gecode/dev/extractor/Makefile index 1f73ba27a..0221be9e2 100644 --- a/packages/gecode/dev/extractor/Makefile +++ b/packages/gecode/dev/extractor/Makefile @@ -1,5 +1,5 @@ GECODEDIR := $(shell g++ $(CPPFLAGS) $(CXXFLAGS) -H -E gecodedir.hh 2>&1 >/dev/null | grep gecode/kernel.hh | awk '{print $$2}' | sed 's|/kernel.hh||') -GECODEDIR=/usr/local/opt/gecode/include/gecode +GECODEDIR=/usr/include/gecode GECODECONFIG := $(GECODEDIR)/support/config.hpp GECODEVERSION := $(shell cat $(GECODECONFIG) | egrep '\' | awk '{print $$3}' | sed 's/"//g') PROTOTYPES = ../gecode-prototypes-$(GECODEVERSION).hh diff --git a/packages/gecode/gecode6-common.icc b/packages/gecode/gecode6-common.icc index 19a257192..50df988f7 100644 --- a/packages/gecode/gecode6-common.icc +++ b/packages/gecode/gecode6-common.icc @@ -353,27 +353,27 @@ namespace generic_gecode else return ikaboom("too late to create vars"); } - int new_svar(int glbMin, int glbMax, int lubMin, int lubMax, + int new_svar(int glbMin, int glbMax, int lub, unsigned int cardMin=0, unsigned int cardMax=Set::Limits::card) { - SetVar v(*this, glbMin, glbMax, lubMin, lubMax, cardMin, cardMax); + SetVar v(*this, glbMin, glbMax, lub, cardMin, cardMax); return _new_svar(v); } - int new_ssvar(int glbMin, int glbMax, IntSet lubMin, IntSet lubMax, + int new_ssvar(int glbMin, int glbMax, IntSet lub, unsigned int cardMin=0, unsigned int cardMax=Set::Limits::card) { - SetVar v(*this, glbMin, glbMax, lubMin, lubMax, cardMin, cardMax); + SetVar v(*this, glbMin, glbMax, lub, cardMin, cardMax); return _new_svar(v); } - int new_ssvar(IntSet glb, int lubMin, int lubMax, + int new_ssvar(IntSet glb, int lub, unsigned int cardMin=0, unsigned int cardMax=Set::Limits::card) { - SetVar v(*this, glb, lubMin, lubMax, cardMin, cardMax); + SetVar v(*this, glb, lub, cardMin, cardMax); return _new_svar(v); } diff --git a/packages/gecode/gecode6_yap.cc b/packages/gecode/gecode6_yap.cc index a58ddfd3e..dd1937481 100644 --- a/packages/gecode/gecode6_yap.cc +++ b/packages/gecode/gecode6_yap.cc @@ -825,10 +825,10 @@ return BOOL_VAL_RND(Rnd()); int GlbMin = YAP_IntOfTerm(YAP_ARG3); int GlbMax = YAP_IntOfTerm(YAP_ARG4); int LubMin = YAP_IntOfTerm(YAP_ARG5); - int LubMax = YAP_IntOfTerm(YAP_ARG6); + int LubMax = YAP_IntOfTerm(YAP_ARG6); //ignore int CardMin= YAP_IntOfTerm(YAP_ARG7); int CardMax= YAP_IntOfTerm(YAP_ARG8); - int idx = space->new_svar(GlbMin,GlbMax,LubMin,LubMax,CardMin,CardMax); + int idx = space->new_svar(GlbMin,GlbMax,LubMin,CardMin,CardMax); return YAP_Unify(result, YAP_MkIntTerm(idx)); } @@ -839,9 +839,9 @@ return BOOL_VAL_RND(Rnd()); int GlbMin = YAP_IntOfTerm(YAP_ARG3); int GlbMax = YAP_IntOfTerm(YAP_ARG4); int LubMin = YAP_IntOfTerm(YAP_ARG5); - int LubMax = YAP_IntOfTerm(YAP_ARG6); + int LubMax = YAP_IntOfTerm(YAP_ARG6); //ignore int CardMin= YAP_IntOfTerm(YAP_ARG7); - int idx = space->new_svar(GlbMin,GlbMax,LubMin,LubMax,CardMin); + int idx = space->new_svar(GlbMin,GlbMax,LubMin,CardMin); return YAP_Unify(result, YAP_MkIntTerm(idx)); } @@ -852,8 +852,8 @@ return BOOL_VAL_RND(Rnd()); int GlbMin = YAP_IntOfTerm(YAP_ARG3); int GlbMax = YAP_IntOfTerm(YAP_ARG4); int LubMin = YAP_IntOfTerm(YAP_ARG5); - int LubMax = YAP_IntOfTerm(YAP_ARG6); - int idx = space->new_svar(GlbMin,GlbMax,LubMin,LubMax); + int LubMax = YAP_IntOfTerm(YAP_ARG6); //ignore? + int idx = space->new_svar(GlbMin,GlbMax,LubMin); return YAP_Unify(result, YAP_MkIntTerm(idx)); } @@ -863,10 +863,10 @@ return BOOL_VAL_RND(Rnd()); GenericSpace* space = gecode_Space_from_term(YAP_ARG2); IntSet Glb = gecode_IntSet_from_term(YAP_ARG3); int LubMin = YAP_IntOfTerm(YAP_ARG4); - int LubMax = YAP_IntOfTerm(YAP_ARG5); + int LubMax = YAP_IntOfTerm(YAP_ARG5);// int CardMin = YAP_IntOfTerm(YAP_ARG6); int CardMax = YAP_IntOfTerm(YAP_ARG7); - int idx = space->new_ssvar(Glb,LubMin,LubMax,CardMin,CardMax); + int idx = space->new_ssvar(Glb,LubMin/* ,lubmax */,CardMin,CardMax); return YAP_Unify(result, YAP_MkIntTerm(idx)); } @@ -890,7 +890,7 @@ return BOOL_VAL_RND(Rnd()); IntSet Glb = gecode_IntSet_from_term(YAP_ARG3); int LubMin = YAP_IntOfTerm(YAP_ARG4); int LubMax = YAP_IntOfTerm(YAP_ARG5); - int idx = space->new_ssvar(Glb,LubMin,LubMax); + int idx = space->new_ssvar(Glb,LubMin/* ,lubmax */); return YAP_Unify(result, YAP_MkIntTerm(idx)); } @@ -903,7 +903,7 @@ return BOOL_VAL_RND(Rnd()); IntSet Lub = gecode_IntSet_from_term(YAP_ARG5); int CardMin = YAP_IntOfTerm(YAP_ARG6); int CardMax = YAP_IntOfTerm(YAP_ARG7); - int idx = space->new_ssvar(GlbMin,GlbMax,Lub,Lub,CardMin,CardMax); + int idx = space->new_ssvar(GlbMin,GlbMax,Lub,CardMin,CardMax); return YAP_Unify(result, YAP_MkIntTerm(idx)); } @@ -915,7 +915,7 @@ return BOOL_VAL_RND(Rnd()); int GlbMax = YAP_IntOfTerm(YAP_ARG4); IntSet Lub = gecode_IntSet_from_term(YAP_ARG5); int CardMin = YAP_IntOfTerm(YAP_ARG6); - int idx = space->new_ssvar(GlbMin,GlbMax,Lub,Lub,CardMin); + int idx = space->new_ssvar(GlbMin,GlbMax,Lub,CardMin); return YAP_Unify(result, YAP_MkIntTerm(idx)); } diff --git a/packages/python/swig/prolog/yapi.yap b/packages/python/swig/prolog/yapi.yap index baf97b55a..167b12773 100644 --- a/packages/python/swig/prolog/yapi.yap +++ b/packages/python/swig/prolog/yapi.yap @@ -79,16 +79,14 @@ python_query( Caller, String, Bindings ) :- output(Caller, Bindings). output( Caller, Bindings ) :- -fail, - Answer := {}, - % start_low_level_trace, + Caller.answer := {}, + /* % start_low_level_trace, foldl(ground_dict(answer), Bindings, [], Ts), term_variables( Ts, Hidden), foldl(bv, Hidden , 0, _), - maplist(into_dict(Answer),Ts), - Caller.answer := Answer, - fail. - + */ maplist(into_dict(answer),Bindings), + := print(answer)}, + Caller.answer := answer. output( _, Bindings ) :- write_query_answer( Bindings ), diff --git a/packages/python/swig/yap4py/yapi.py b/packages/python/swig/yap4py/yapi.py index c25157075..11fcdb724 100644 --- a/packages/python/swig/yap4py/yapi.py +++ b/packages/python/swig/yap4py/yapi.py @@ -1,4 +1,5 @@ import readline +import copy from yap4py.yap import * from yap4py.systuples import * from os.path import join, dirname @@ -76,11 +77,10 @@ class Query (YAPQuery): return self.port == "fail" or self.port == "exit" def __next__(self): - self.answer = {} if self.port == "fail" or self.port == "exit": raise StopIteration() if self.next(): - return self.answer + return copy.deepcopy(self.answer) raise StopIteration() def name( name, arity): diff --git a/packages/python/yap_kernel/yap_ipython/core/usage.py b/packages/python/yap_kernel/yap_ipython/core/usage.py index 5b010188f..287b47429 100644 --- a/packages/python/yap_kernel/yap_ipython/core/usage.py +++ b/packages/python/yap_kernel/yap_ipython/core/usage.py @@ -333,9 +333,9 @@ The following magic functions are currently available: """ -default_banner_parts = ["Python %s\n"%sys.version.split("\n")[0], +default_banner_parts = ["YAP %s\n"%sys.version.split("\n")[0], "Type 'copyright', 'credits' or 'license' for more information\n" , - "yap_ipython {version} -- An enhanced Interactive Python. Type '?' for help.\n".format(version=release.version), + "yap_ipython {version} -- An enhanced Interactive Prolog for Jupyter. Type '?' for help.\n".format(version=release.version), ] default_banner = ''.join(default_banner_parts) diff --git a/packages/python/yap_kernel/yap_ipython/yapi.py b/packages/python/yap_kernel/yap_ipython/yapi.py index 8623a54e2..5ebf94fa5 100644 --- a/packages/python/yap_kernel/yap_ipython/yapi.py +++ b/packages/python/yap_kernel/yap_ipython/yapi.py @@ -563,7 +563,8 @@ class YAPRun(InteractiveShell): self.answers = [] for answer in self.query: print( answer ) - self.answers += [copy.deepcopy(answer)] + self.answers += [answer] + print( self.answers) self.iterations += 1 self.os = None diff --git a/packages/swig/CMakeLists.txt b/packages/swig/CMakeLists.txt index 5d116096d..44d5ec390 100644 --- a/packages/swig/CMakeLists.txt +++ b/packages/swig/CMakeLists.txt @@ -13,7 +13,8 @@ set (SOURCES yap.i) if (ANDROID) add_subdirectory(android) else(ANDROID) -# add_subdirectory(java) + add_subdirectory(R) + add_subdirectory(java) endif(ANDROID) set_property( DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS SWIGYAP=1)