add "utiliser-swipl-en-cpp.tgz"
This commit is contained in:
parent
7eaf2d68e1
commit
ac863a56ff
9 changed files with 98 additions and 2 deletions
19
TP1/Prolog/AidesCPP/1.cpp
Normal file
19
TP1/Prolog/AidesCPP/1.cpp
Normal file
|
@ -0,0 +1,19 @@
|
|||
#include <cstdio>
|
||||
#include <SWI-Prolog.h>
|
||||
|
||||
// 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;
|
||||
}
|
3
TP1/Prolog/AidesCPP/2-f.pl
Normal file
3
TP1/Prolog/AidesCPP/2-f.pl
Normal file
|
@ -0,0 +1,3 @@
|
|||
f(1).
|
||||
f(2).
|
||||
f(3).
|
17
TP1/Prolog/AidesCPP/2.cpp
Normal file
17
TP1/Prolog/AidesCPP/2.cpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
#include <cstdio>
|
||||
#include <SWI-cpp.h>
|
||||
|
||||
// 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;
|
||||
}
|
3
TP1/Prolog/AidesCPP/3-f.pl
Normal file
3
TP1/Prolog/AidesCPP/3-f.pl
Normal file
|
@ -0,0 +1,3 @@
|
|||
f(1).
|
||||
f(2).
|
||||
f(10).
|
22
TP1/Prolog/AidesCPP/3.cpp
Normal file
22
TP1/Prolog/AidesCPP/3.cpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include <cstdio>
|
||||
#include <SWI-cpp.h>
|
||||
|
||||
// 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;
|
||||
}
|
22
TP1/Prolog/AidesCPP/4.cpp
Normal file
22
TP1/Prolog/AidesCPP/4.cpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include <cstdio>
|
||||
#include <SWI-cpp.h>
|
||||
|
||||
// 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;
|
||||
}
|
0
TP1/Prolog/AidesCPP/empty.pl
Normal file
0
TP1/Prolog/AidesCPP/empty.pl
Normal file
8
TP1/Prolog/AidesCPP/fac.pl
Normal file
8
TP1/Prolog/AidesCPP/fac.pl
Normal file
|
@ -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).
|
|
@ -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`)
|
||||
|
|
Reference in a new issue