## Who uses Prolog?

• IBM -> For Natural Processing Language on Watson.
• Genexus -> Cross-Platform, knowledge representation-based, development tool.
• Microsoft -> For Windows NT.
• Ericsson -> The first Erlang compiler was written in Prolog.

## Facts, Rules and Queries

### Prolog programs are knowledge bases, collections of facts and rules which describe some collection or relationships.

```				```

functional(erlang).

logical(mercury).

logical(prolog).
``````

### How do we use it? We ask queries to it...we'll see how...

```				```
functional(prolog).

false.

true.

logical(X).

x = prolog ;

x = mercury.
``````

### Let's see a more complex example...

```				```
likes(blag,chocolate).

likes(aaron,chocolate).

likes(julia,vanilla).

eats(X,Y) :- likes(X,Y).
``````

### These are the queries that we can use...

```				```
likes(blag, chocolate).

true.

eats(julia,X).

x = vanilla.
``````

## Unification

### And this basically means...two objects unify if they have the same value...

```				```
blag = blag.

true.

blag = julia.

false.

X = blag.

X = blag. true.

X = blag, X = julia.

false.
``````

## Recursion

### Recursion can be use in definitions, which makes it easier to understand.

```				```
likes(julia,vanilla).

icecream(vanilla,'banana split').

eats(X,Y) :- likes(X,Y), !.

eats(X,Y) :- icecream(Z,Y),

likes(X,Z).
``````

### The ! means "stop".

```				```
eats(julia,'banana split').

true.

eats(julia,vanilla).

true.
``````

## Lists

### List can and almost everytime are, divided into Head and Tail...being head the first element and tail the rest...

```				```
list([H|T]) :- write(H), nl,

write(T).

erlang

true.
``````

## Fibonacci List

### And yes...you're right...Prolog uses the same extension as Perl...funny coincidence I guess...

```				```
fibo(NUM,A,B,[H|T]) :- (NUM > 1 ->

H is A + B,

X is NUM - 1,

(A =:= 0 ->

fibo(X,H,B,T);

fibo(X,H,A,T))).

fibo(_,_,_,[]).

fibonacci(NUM,R) :- fibo(NUM,0,1,X),

!,

append([0,1], X, R).
``````

## Making an LED Number App

### Name your file "ledNumbers.pl" (all in lowercase, only N in uppercase).

```				```
number(0,[[' _  '],['| | '],['|_| ']]).

number(1,[['  '],['| '],['| ']]).

number(2,[[' _  '],[' _| '],['|_  ']]).

number(3,[['_  '],['_| '],['_| ']]).

number(4,[['    '],['|_| '],['  | ']]).

number(5,[[' _  '],['|_  '],[' _| ']]).

number(6,[[' _  '],['|_  '],['|_| ']]).

number(7,[['_   '],[' |  '],[' |  ']]).

number(8,[[' _  '],['|_| '],['|_| ']]).

number(9,[[' _  '],['|_| '],[' _| ']]).
``````
```				```
digits(0,[]).

digits(X,[H|T]) :- (X/10 > 0 ->

H1 is floor(X/10),

H is X mod 10,

digits(H1,T)), !.

accRev([],A,A).

accRev([H|T],A,R) :- accRev(T,[H|A],R).

getDigits(L,R) :- digits(L,Y), accRev(Y, [], R).
``````
```				```
show_records([]).

show_records([A|B]) :-

print_records(A), nl,

show_records(B).

print_records([]).

print_records([A|B]) :-

format('~w',A),

print_records(B).

merge([L], L).

merge([H1,H2|T], R) :- maplist(append, H1, H2, H),

merge([H|T], R),

!.
``````
```				```
listnum([],[]).

listnum([H1|T1],[R|Y]) :- number(H1,R),

listnum(T1,Y).

led(X) :- getDigits(X,Y),

listnum(Y,Z),

merge(Z,R),

show_records(R).
``````

## Random Names

### Name your file "randomnames.pl" (all in lowercase).

```				```
name(0,'Anne'). name(1,'Gigi'). name(2,'Blag').

name(3,'Juergen'). name(4,'Marek'). name(5,'Ingo').

name(6,'Lars'). name(7,'Julia'). name(8,'Danielle').

name(9,'Rocky'). name(10,'Julien'). name(11,'Uwe').

name(12,'Myles'). name(13,'Mike'). name(14,'Steven').

name(15,'Fanny').

last_name(4,"Kowalkiewicz"). last_name(5,"Swauerzapf").

last_name(6,"Karg"). last_name(7,"Satsuta").

last_name(8,"Keene"). last_name(9,"Ongkowidjojo").

last_name(10,"Vayssiere"). last_name(11,"Kylau").

last_name(12,"Fenlon"). last_name(13,"Flynn").

last_name(14,"Flynn"). last_name(15,"Tan").
``````
```				```
full_names(100000, 100000,[]).

full_names(TOP,NUM,[H|T]) :- (NUM < TOP ->

X is NUM + 1,

random_between(0,15,L),

random_between(0,15,R),

name(L,L1),

last_name(R,R1),

H = [L1,R1],

full_names(TOP,X,T),!).
``````
```				```
random_names(_) :- statistics(walltime, [_ | [_]]),

full_names(100000,0,_),

statistics(walltime,

[_ | [ExecutionTime]]),

write('Execution took '),

write(ExecutionTime),

write(' ms.'),

nl.
``````

## Decimal to Romans

### Name your file "dectoromans.pl" (all in lowercase).

```				```
number(1000,'M'). number(900,'CM'). number(500,'D').

number(400,'CD'). number(100,'C'). number(90,'XC').

number(50,'L'). number(40,'XL'). number(10,'X').

number(9,'IX'). number(5,'V'). number(4,'IV').

number(1,'I').

numbers(0,[1000,900,500,400,100,90,50,40,10,9,5,4,1]).
``````
```				```
toRomans(_,[],[]).

toRomans(NUM, [H|T]) :- (NUM >= H ->

number(H,R),

write(R),

X is NUM - H,

(X > 0 ->

numbers(0,T1),

toRomans(X,T1),

!;

!);

toRomans(NUM,T)).

romans(X) :- numbers(0,L), toRomans(X,L).
``````

## Count Letters

### "'This is a text file that we are going to read it using Prolog.'"

```				```

X = Line,

close(Str).

count_occur(List, Occ):- findall([X,L],

(bagof(true,member(X,List),Xs),

length(Xs,L)), Occ).

count_letters(Y) :- main(X),

atom_chars(X,L),

count_occur(L,Y).
``````