I have a question about this program in PROLOG that they left me, it is about that by means of a predicate like: show. show me all the content of the knowledge base that is flight, but the truth is that I have not found much about how to do it other than that you could use the dynamic function that prolog brings by default, but it does not work for me.
mostrar:- dynamic(vuelo/3).
vuelo(nueva_york, chicago,1000).
vuelo(chicago, denver,1000).
vuelo(nueva_york, toronto,800).
vuelo(nueva_york, denver,1900).
% 1.- Hacer una regla donde visualice todos los viajes.
% 2.- Hacer una regla que elimine todos los viajes de un origen.
vuelade:-write('desde '),read(A),
write('a: '), read(B), encontrar(A,B).
encontrar(A,B):- ruta(A,B,D), write('La distancia es de '),write(D), write(' Km.'),!, nl.
ruta(A,B,C):- es_vuelo(A,B,C).
ruta(_,_,D):- !,write('no, hay ruta para ese destino '), nl, D=0, fail, !.
es_vuelo(T,T2,D):- vuelo(T,T2,D), write(T).
agregar:-write('desde: '),read(Desde),write('hacia: '),read(H),
write('distancia: '),read(Dis),
assert(vuelo(Desde,H,Dis)).
borrar :- write('Desde: '),read(Desde), retract(vuelo(Desde,_,_)), !,
write('vuelo Borrado').
borrar :- write('No existe este vuelo').
borratodo:- abolish(vuelo/3), write('vuelos Borrados').
Anyway this is my program I hope you can guide me on how to do it.
Based on what you defined in the predicate
mostrar
, you could use findall to get the flight information. For example:Result:
On the other hand, the dynamic/1 predicate is used to tell the Prolog interpreter that the definition of a predicate can change during execution.