Libertà vo cercando, ch'è sì cara, come sa chi per lei vita rifiuta

Categoria: prolog

Swi-Prolog : ricorsione

Learn Prolog now : Recursion
Lista dei predicati in Prolog

% -- hungry bear
isDigering(X,Y):- justAte(X,Y).
isDigering(X,Y):- justAte(X,Z),
		  isDigering(Z,Y).
					
justAte(bear, john).
justAte(bear, honey).
justAte(john, chicken).
justAte(john, potatoes).
justAte(chicken, worm).

/*
	?- isDigering(bear, Gnam).
		Gnam = john ;
		Gnam = honey ;
		Gnam = chicken ;
		Gnam = potatoes ;
		Gnam = worm ;
*/

% -- The history of music 

descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z),
	       descend(Z,Y).
				
child(worksong,blues).
child(blues,jazz).
child(jazz, prog).
child(blues,rockabilly).
child(rockabilly, hardRock).
child(hardRock, metal).

% -- Counting one by one

numeral(0).
numeral( succ(X) ):- numeral(X).

% -----------------------

% -- King Fibonacci 

fib(1, 1).
fib(X, Y):- X>0, Z is X-1 ,
	    fib(Z, Y2),
	    Y is X*Y2 .

% somma da una lista			
sumList([], 0).
sumList(List, Sum):- [Head| Tail] = List,
		     sumList( Tail, TempSum),
		     Sum is  TempSum + Head.


% riconosce liste con lo stesso numero di caratteri
a2b([],[]).
a2b([_|A], [_|B]) :- a2b(A, B).


% calcolo lunghezza delle stringhe 
len([],0).
len([_|T], N):- len(T,X), 
		N is X+1.

% mischia due stringhe un carattere alla volta 
combine([],[],[]).
combine(L1, L2, Result):- 	L1 = [A|T1], L2 = [B|T2],
				combine(T1, T2, R),
				append([A,B],R,Result).
				%Result = [A,B,R]. crea una lista di liste	
	/* 
		 combine([c,n],[a,e],X).
		 X = [c, a, n, e] ;
	*/

[2] SWI-Prolog + Eclipse : Parole crociate

Prima di svolgere il prossimo esercizio consiglio di installare il plugin PDT su Eclipse per avere una postazione di lavoro più comoda e potente :

Eclipse : versione successiva alla 4
SWI-Prolog : versione successiva alla 6
PDT

Attenzione a non installare la versione nel repository di ubuntu che è più vecchia

$ apt-cache showpkg swi-prolog
Package: swi-prolog
Versions:
6.4.1-1-g09194b7-raringppa2 ... <- da installare
5.10.4-5ubuntu1 (/var/lib/apt/lists/i

$ sudo apt-get install swi-prolog=6.4.1-1-g09194b7-raringppa2

Exercise 2.4 Here are six Italian words:
astante , astoria , baratto , cobalto , pistola , statale .
They are to be arranged, crossword puzzle fashion, in the following grid:
The following knowledge base represents a lexicon containing these words:

word(astante, a,s,t,a,n,t,e).
word(astoria, a,s,t,o,r,i,a).
word(baratto, b,a,r,a,t,t,o).
word(cobalto, c,o,b,a,l,t,o).
word(pistola, p,i,s,t,o,l,a).
word(statale, s,t,a,t,a,l,e).
Write a predicate crossword/6 that tells us how to fill in the grid. The first three arguments should be the vertical words from left to right, and the last three arguments the horizontal words from top to bottom.

% 3 verticali V1,V2,V3 e 3 orizzonatali H1,H2,H3
word(astante, a,s,t,a,n,t,e).
word(astoria, a,s,t,o,r,i,a).
word(baratto, b,a,r,a,t,t,o).
word(cobalto, c,o,b,a,l,t,o).
word(pistola, p,i,s,t,o,l,a).
word(statale, s,t,a,t,a,l,e).

crossword(V1,V2,V3, H1,H2,H3) :- 
		word(V1, _,V1H1,_,V1H2,_,V1H3,_),
		word(V2, _,V2H1,_,V2H2,_,V2H3,_),
		word(V3, _,V3H1,_,V3H2,_,V3H3,_),
		word(H1, _,V1H1,_,V2H1,_,V3H1,_),
		word(H2, _,V1H2,_,V2H2,_,V3H2,_),
		word(H3, _,V1H3,_,V2H3,_,V3H3,_),
             % non incrociare le parole con sé stesse
		H1\=V1, H2\=V2, H3\=V3.	

% chi più ne ha più ne metta :)
/*
word(scatole, s,c,a,t,o,l,e).
word(estonia, e,s,t,o,n,i,a).
word(baretto, b,a,r,e,t,t,o).
word(cavallo, c,a,v,a,l,l,o).
word(ceretta, c,e,r,e,t,t,a).
word(cintola, c,i,n,t,o,l,a).
word(pittalo, p,i,t,t,a,l,o).
word(lisbona, l,i,s,b,o,n,a).
word(funesto, f,u,n,e,s,t,o).
word(colante, c,o,l,a,n,t,e).
*/

/*
one solution :
     a   c   p
   a s t o r i a  
     t   b   s 
   b a r a t t o  
     n   l   o   
   s t a t a l e  	
     e   o   a
*/

Primo script in Prolog

SWI-Prolog è la versione qui utilizzata.
Sistema: Ubuntu 13.04.
Strumenti: Gedit, Terminale.
Per installarlo : sudo apt-get install swi-prolog

Prolog is a declarative programming language. This means that in prolog, you do not write out what the computer should do line by line, as in procedural languages such as C and Java . The general idea behind declarative languages is that you describe a situation. Based on this code, the interpreter or compiler will tell you a solution. In the case of prolog, it will tell you whether a prolog sentence is true or not and, if it contains variables, what the values of the variables need to be.

Piccolo script per iniziare a conoscere il Prolog

largeFamily.pl

% database familiare
% iniziamo da lontano definendo un insieme di esseri umani..

human(john).
human(david).
human(gino).
human(bobbysolo).
human(karen).
human(gloria).
% ora separiamoli in base al sesso :
sex(john, man). % maschietti
sex(david, man).
sex(gino, man).
sex(bobbysolo, man).
sex(karen, woman). % femminucce
sex(sasha, woman).
sex(gloria, woman).
% iniziamo a definire le coppie
partner(john, gloria).
partner(david, britney).
partner(gino, topino).
partner(john, karen).
haveChild(john, sasha). % genitori
%haveChild(john, david).
% john,karen -> sasha,bobbysolo -> rocco,brigitte -> moana
haveChild(karen, sasha).
haveChild(gloria, david).
haveChild(sasha, rocco).
haveChild(bobbysolo, rocco).
haveChild(rocco, moana).
haveChild(brigitte, moana).
% definiamo ora delle funzioni che controllino se
% - A sia il padre di B
isFather(A, B) :- sex( A, man), haveChild(A,B).
% - A sia la madre di B
isMother(A, B) :- sex( A, woman), haveChild(A,B).
% - F e M siano padre a madre di A
parents(A, F, M) :- isFather(F, A), isMother(M,A).
% - A sia un avo di B
ancestor(A,B) :- haveChild(A,B).
ancestor(A,B) :- haveChild(A,X), ancestor(X,B). % non senti puzza di ricorsione ?
% - uno dei due è il padre/madre, il figliastro, il fratello/astro o un progenitore
areRelatives(A,B) :- isFather(A,B) | isFather(B,A) | isMother(A,B) | isMother(B,A);
partner(A,C), haveChild(C,B);
partner(C,A), haveChild(C,B);
haveChild(C,A), haveChild(D,B), ( partner(C,D); partner(D,C) );
ancestor(A,B); ancestor(B,A).

Mentre queste sono delle possibili interrogazioni da terminale

?- isFather(john,_).
true.
?- isFather(john,B).
B = david.
?- parents(david,Father,Mother).
Father = john,
Mother = gloria ;
false.

?- ancestor(john, Discendente).
Discendente = sasha ;
Discendente = rocco ;
Discendente = moana ;
false.

?- areRelatives(gloria, sasha).
true .
?- areRelatives(john,david).
true .
?- areRelatives(karen, moana).
true ;

Powered by WordPress & Theme by Anders Norén