From ac863a56ff48fb6d8697d62e73b13e04c2f7a648 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 1 Feb 2023 10:10:53 +0100 Subject: [PATCH] add "utiliser-swipl-en-cpp.tgz" --- TP1/Prolog/AidesCPP/1.cpp | 19 +++++++++++++++++++ TP1/Prolog/AidesCPP/2-f.pl | 3 +++ TP1/Prolog/AidesCPP/2.cpp | 17 +++++++++++++++++ TP1/Prolog/AidesCPP/3-f.pl | 3 +++ TP1/Prolog/AidesCPP/3.cpp | 22 ++++++++++++++++++++++ TP1/Prolog/AidesCPP/4.cpp | 22 ++++++++++++++++++++++ TP1/Prolog/AidesCPP/empty.pl | 0 TP1/Prolog/AidesCPP/fac.pl | 8 ++++++++ TP1/Prolog/TODO.md | 6 ++++-- 9 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 TP1/Prolog/AidesCPP/1.cpp create mode 100644 TP1/Prolog/AidesCPP/2-f.pl create mode 100644 TP1/Prolog/AidesCPP/2.cpp create mode 100644 TP1/Prolog/AidesCPP/3-f.pl create mode 100644 TP1/Prolog/AidesCPP/3.cpp create mode 100644 TP1/Prolog/AidesCPP/4.cpp create mode 100644 TP1/Prolog/AidesCPP/empty.pl create mode 100644 TP1/Prolog/AidesCPP/fac.pl diff --git a/TP1/Prolog/AidesCPP/1.cpp b/TP1/Prolog/AidesCPP/1.cpp new file mode 100644 index 0000000..6cf6a3a --- /dev/null +++ b/TP1/Prolog/AidesCPP/1.cpp @@ -0,0 +1,19 @@ +#include +#include + +// après avoir installé swi-prolog (et peut être aussi swi-prolog-devel) +// pour pouvoir appeler Prolog à partir du C/C++ +// https://www.swi-prolog.org/pldoc/man?section=calling-prolog-from-c + +// pour avoir les paramètres +// pkg-config swipl --cflags +// pkg-config swipl --libs + +// et selon les paramètres précédents, pour compiler +// g++ 1.cpp -I /opt/local/lib/swipl/include -L/opt/local/lib/swipl/lib/arm64-darwin -lswipl + +int main(int _ac, char** _av) { + PL_initialise(_ac, _av); + printf("ok\n"); + return 0; +} diff --git a/TP1/Prolog/AidesCPP/2-f.pl b/TP1/Prolog/AidesCPP/2-f.pl new file mode 100644 index 0000000..9d6f9d1 --- /dev/null +++ b/TP1/Prolog/AidesCPP/2-f.pl @@ -0,0 +1,3 @@ +f(1). +f(2). +f(3). \ No newline at end of file diff --git a/TP1/Prolog/AidesCPP/2.cpp b/TP1/Prolog/AidesCPP/2.cpp new file mode 100644 index 0000000..1cceca0 --- /dev/null +++ b/TP1/Prolog/AidesCPP/2.cpp @@ -0,0 +1,17 @@ +#include +#include + +// swipl-ld -ld g++ -goal true 2.cpp 2-f.pl +// attention, recompile nécessaire si 2-f.pl change + +int main(int _ac, char** _av) { + PL_initialise(_ac, _av); + + PlTermv pl_args2(1); + PlQuery q0("f", pl_args2); + while( q0.next_solution()) { + printf("---> %s\n", (char *)pl_args2[0]); + } + + return 0; +} diff --git a/TP1/Prolog/AidesCPP/3-f.pl b/TP1/Prolog/AidesCPP/3-f.pl new file mode 100644 index 0000000..f7047ce --- /dev/null +++ b/TP1/Prolog/AidesCPP/3-f.pl @@ -0,0 +1,3 @@ +f(1). +f(2). +f(10). \ No newline at end of file diff --git a/TP1/Prolog/AidesCPP/3.cpp b/TP1/Prolog/AidesCPP/3.cpp new file mode 100644 index 0000000..cd2dcd8 --- /dev/null +++ b/TP1/Prolog/AidesCPP/3.cpp @@ -0,0 +1,22 @@ +#include +#include + +// swipl-ld -ld g++ -goal true 3.cpp empty.pl +// on retire 3-f.pl de la compile pour le charger à chq execution +// charger empty.pl (qui est vide) permet de ne pas avoir le message de bienvenu de Prolog + +int main(int _ac, char** _av) { + PL_initialise(_ac, _av); + + PlTermv pl_args(1); + pl_args[0] = "3-f.pl"; + PlCall("consult", pl_args); + + PlTermv pl_args2(1); + PlQuery q0("f", pl_args2); + while( q0.next_solution()) { + printf("---> %s\n", (char *)pl_args2[0]); + } + + return 0; +} diff --git a/TP1/Prolog/AidesCPP/4.cpp b/TP1/Prolog/AidesCPP/4.cpp new file mode 100644 index 0000000..1c557f8 --- /dev/null +++ b/TP1/Prolog/AidesCPP/4.cpp @@ -0,0 +1,22 @@ +#include +#include + +// swipl-ld -ld g++ -goal true 4.cpp empty.pl + +int main(int _ac, char** _av) { + printf("using fac.pl to compute 10!\n"); + PL_initialise(_ac, _av); + + PlTermv pl_args(1); + pl_args[0] = "fac.pl"; + PlCall("consult", pl_args); + + PlTermv pl_args2(2); + pl_args2[0] = 10; + PlQuery q0("fac", pl_args2); + while( q0.next_solution()) { + printf("---> %d\n", (int)pl_args2[1]); + } + + return 0; +} diff --git a/TP1/Prolog/AidesCPP/empty.pl b/TP1/Prolog/AidesCPP/empty.pl new file mode 100644 index 0000000..e69de29 diff --git a/TP1/Prolog/AidesCPP/fac.pl b/TP1/Prolog/AidesCPP/fac.pl new file mode 100644 index 0000000..4a579f0 --- /dev/null +++ b/TP1/Prolog/AidesCPP/fac.pl @@ -0,0 +1,8 @@ +fac(1,1):- !. +fac(X,F):- + X > 1, + Y is X - 1, + fac( Y, Next ), + F is X * Next. + +% ?- fac(3, X). \ No newline at end of file diff --git a/TP1/Prolog/TODO.md b/TP1/Prolog/TODO.md index f7f32c3..beaef90 100644 --- a/TP1/Prolog/TODO.md +++ b/TP1/Prolog/TODO.md @@ -2,5 +2,7 @@ qui lance le résolveur - [ ] Faire le résolveur -Le prof propose d'utiliser C/C++ pour utiliser Prolog avec `swi-prolog-devel` -(header : `SWI-Prolog.h` ou `SWI-cpp.h`) +Le prof propose d'[utiliser C/C++ pour utiliser Prolog](./AidesCPP/) +avec `swi-prolog-devel` +([header](https://www.swi-prolog.org/pldoc/man?section=foreignlink) : +`SWI-Prolog.h` ou `SWI-cpp.h`)