Laboratorio 4: PROLOG |
---------------------------------------
Para comenzar a trabajar con el Amzi Explorer debemos lanzar un listener(Listener/Start).
Crear un nuevo fichero donde cargaremos las reglas. Una vez creado consultarlo en el listener.
Si lo modificamos podemos reconsultar posteriormente. En el Listener ya podemos hacer consultas.
En los ejemplos se aconseja utilizar el depurador para entender mejor el modo de funcionamiento de PROLOG.
---------------------------------------------------------
Ejemplo 1:
likes(john,fish).
likes(john,mary).
likes(mary,book).
likes(john,book).
Ejemplo 2:
room(kitchen).
room(office).
room(hall).
room('dining room').
room(cellar).
door(office, hall).
door(kitchen, office).
door(hall, 'dining room').
door(kitchen, cellar).
door('dining room', kitchen).
location(desk, office).
location(apple, kitchen).
location(flashlight, desk).
location('washing machine', cellar).
location(nani, 'washing machine').
location(broccoli, kitchen).
location(crackers, kitchen).
location(computer, office).
turned_off(flashlight).
here(kitchen).
----------------------------------------------------------
Ejemplo 1:
?-likes(john,money).
?-likes(mary,book).
Ejemplo 2:
?- room(office).
?- room(attic).
?- location(apple, kitchen).
?- location(kitchen, apple).
?- door(office, hall).
?- door(hall,office).
----------------------------------------------------------
Ejemplo 1:
likes(john, X).
Ejemplo 2:
?- room(X).
?- location(Thing, Place).
?- location(Thing, kitchen).
-----------------------------------------------------------
Ejemplo 1:
?- likes(joe,mary),likes(mary,joe).
Ejemplo 2:
?- door(kitchen, R),location(T,R).
-------------------------------------------------------
?- location(X, kitchen), write(X) ,nl, fail.
-----------------------------------------------------------
Ejemplo 1:
reina(rhodri, 844,878).
reina(anarawd,878,916).
reina(cadwallon,985,999).
reina(buba, 1000,1020).
reina(bob,1010,1200).
reina(tom,1200,1999).
gobierna(X,Y):-reina(X,A,B),Y >= A,Y =< B.
Ejemplo 2:
poblacion(usa,203).
poblacion(india,548).
poblacion(china,800).
poblacion(brasil,108).
area(usa,3).
area(india,1).
area(china,4).
area(brasil,3).
densidad(X,Y) :-
poblacion(X,P),
area(X,A),
Y is P/A.
------------------------------------------------------
connect(X,Y) :- door(X,Y).
connect(X,Y) :- door(Y,X).
?- connect(kitchen, office).
?- connect(office, kitchen).
?- connect(X,Y).
list_things(Place) :-
location(X, Place),
tab(2),
write(X),
nl,
fail.
--------------------------------------------------------------
move(Place):-
retract(here(X)),
asserta(here(Place)).
can_go(Place):-
here(X),
connect(X, Place).
goto(Place):-
can_go(Place),
move(Place).
-------------------------------------------------------
location_s(object(candle, red, small, 1), kitchen).
location_s(object(apple, red, small, 1), kitchen).
location_s(object(apple, green, small, 1), kitchen).
location_s(object(table, blue, big, 50), kitchen).
?- location_s(X, kitchen).
?- location_s(object(X, red, _, _), kitchen).
----------------------------------------------------------
location(envelope, desk).
location(stamp, envelope).
location(key, envelope).
is_contained_in(T1,T2) :- location(T1,T2).
is_contained_in(T1,T2) :-
location(X,T2),
is_contained_in(T1,X).
?- is_contained_in(X, office).
-----------------------------------------------------------
p([1,2,3]).
p([the,cat,sat,[on,the,mat]]).
?- p([X|Y]).
?- p([_,_,_,[_|X] ]).
member(X,[X|_]).
member(X,[_|Y]):-member(X,Y).
longitud([],0).
longitud([H|T],N) :- longitud(T,N1), N is N1 + 1.
listlen(L,N) :- lenacc(L,0,N).
lenacc([],A,A).
lenacc([H|T],A,N) :- A1 is A +1, lenacc(T,A1,N).
append([],X,X).
append([H|T1],X,[H|T2]) :- append(T1,X,T2).
change(you,i).
change(are,[am,not]).
change(french,german).
change(do,no).
change(X,X).
alter([],[]).
alter([H|T], [X |Y]) :- change(H,X),alter(T,Y).
---------------------------------------------------
data(one).
data(two).
data(three).
cut_test_a(X) :- data(X).
cut_test_a('last clause').
?-cut_test_a(X), write(X), nl, fail.
cut_test_b(X) :- data(X), !.
cut_test_b('last clause').
?- cut_test_b(X), write(X), nl, fail.
cut_test_c(X,Y) :-
data(X),
!,
data(Y).
cut_test_c('last clause').
?- cut_test_c(X,Y), write(X-Y), nl, fail.
-------------------------------------------------------
?- a = a.
?- a = b.
?- location(apple, kitchen) = location(apple, kitchen).
?- location(apple, kitchen) = location(pear, kitchen).
?- a(b,c(d,e(f,g))) = a(b,c(d,e(f,g))).
?- a(b,c(d,e(f,g))) = a(b,c(d,e(g,f))).
?- X = a.
?- 4 = Y.
?- location(apple, kitchen) = location(apple, X).
?- location(X,Y) = location(apple, kitchen).
?- location(apple, X) = location(Y, kitchen).
------------------------------------------------------------
command_loop:-
repeat,
write('Enter end to exit): '),
read(X),
write(X), nl,
X = end.
-------------------------------------------------------------
member(X, [X, _ ]).
member(X,[Y | _ ]) :- X = Y.
member(X,[_|Y]) :- member(X,Y).
oo_class( rectangle(H, W),
methods([ ( area(A) :- A is H * W ),
( perimeter(P) :- P is 2 * (H + W) )
]) ).
oo_class( circle(R),
methods([ ( area(A) :- A is R * R ),
( perimeter(P) :- 2 * R )
]) ).
oo_send(Obj, Mess) :-
oo_class(Obj, methods(Ms)),
member((Mess :- Meth), Ms),
call(Meth).
shapes([rectangle(2, 3), circle(10), rectangle(4, 5), circle(4)]).
go :-
shapes(L),
member(S, L),
oo_send(S, area(A)),
write('el area de'),tab(2),write(S),write('='),write(A), nl,
fail.
Para ejecutarlo:
?-go.
1.- Crear las reglas necesarias para establecer si Y es descendiente de X. 2.- Crear las primitivas necesarias para que dada una lista de enteros apliquemos la funcion incremento 3.- Utilizando las primitivas de manejo de datos (assert y retract) crear las primitivas 4. Problema de comprensión de los operadores. 5. Estructuras de datos:Crear el tipo de datos coche, cuyos tipos de datos serán la marca |