###### tags: `prolog`
# TP Prolog - s10 - ISC IL 3a
## Baudin Valentin et Braillard Simon
## Exercice 1
```prolog=
:- op(650, xfx, '~~>').
%---------------------------------------------------------
%--- bb_run(+InitialBlackboard, -FinalBlackboard)
%---------------------------------------------------------
bb_run(BB, BBFinal) :-
bb_filter(BB, Rs),
bb_applySome(BB, Rs, BBFinal).
%--- bb_applySome(+BB, +PossibleRules, -BBFinal) :
% BBFinal is the result when applying a rule randomly taken
% from the list, then running the rest of the program
bb_applySome(BB, [], BB). % no more possible rules, exit.
bb_applySome(BB, Rules, BBFinal) :-
random_member(Rule, Rules), % pick a rule at random
bb_applyRule(BB, Rule, BB1), % apply it
bb_db(BB),
!, % cut to avoid backtracking
bb_run(BB1, BBFinal). % continue with the rest of the program
%--- bb_oneRule(+BB, -R) : R is a rule with IN data in blackboard,
% and valid Conditions
bb_oneRule(BB, In/Cond~~>Body) :-
In/Cond ~~> Body,
bb_extract(BB, In, _),
bb_solve(Cond).
%--- bb_filter(+BB, -Rs) : Rs contains every matching rule with respect to BB
bb_filter(BB, Rs) :-
findall(Rule, bb_oneRule(BB, Rule), Rs).
%--- bb_solve(+GoalList) : solves each goal in the list
bb_solve([]).
bb_solve([Goal | GoalList]) :-
Goal,
bb_solve(GoalList).
%---bb_applyRule(+BB, +Rule, -NewBB):
% NewBB is the blackboard after removing IN data from BB,
% playing the actions, and adding OUT data into BB
bb_applyRule(BB, In/_Cond~~>Action/Out, NewBB1) :-
bb_extract(BB, In, NewBB),
bb_solve(Action),
append(NewBB, Out, NewBB1).
%--- bb_extract(+BB, +InList, -NewBB) : NewBB is BB without In elements
%--- Example: bb_extract([a,b,c,d,e,f], [b,e], R) gives R = [a,c,d,f]
bb_extract(BB, [], BB).
bb_extract(BB, [In | InList], NewBB) :-
select(In, BB, NewBB1),
bb_extract(NewBB1, InList, NewBB).
% random_member(-X, +List:list)
% X is a random member of List. Equivalent to random_between(1, |List|),
% followed by nth1/3. Fails of List is the empty list.
random_member(X, Xs) :-
length(Xs, Length),
L is Length + 1,
random(1, L, Random),
nth(Random, Xs, X).
%---bb_db(+BB) : current blackboard tracing
bb_db(BB) :- write(BB), nl.
% bb_db(BB) :- write(BB), nl, get_char(_).
```
### Output
```
| ?- > test_bb_pgdc.
[v(18),v(42),v(72),v(30),m(4)]
[v(72),v(30),m(4),v(18),v(24)]
[m(4),v(18),v(24),v(30),v(42)]
[m(4),v(18),v(30),v(24),v(18)]
[m(4),v(30),v(18),v(18),v(6)]
[m(4),v(30),v(18),v(6),v(12)]
[m(4),v(18),v(12),v(6),v(24)]
[m(4),v(6),v(24),v(12),v(6)]
[m(4),v(24),v(6),v(6),v(6)]
[v(24),v(6),v(6),m(3)]
[v(24),v(6),m(2)]
[m(2),v(6),v(18)]
[m(2),v(6),v(12)]
[m(2),v(6),v(6)]
[v(6),m(1)]
Expected: [pgdc(6)][pgdc(6)]
true ? >
(1 ms) yes
| ?-
```
## Exercice 2
```prolog=
[e(Index, V), e(Index2, V2)]/[Index < Index2, V > V2]
~~> []/[e(Index2, V), e(Index, V2)] .
[V, n(N)]/[number(V)] ~~> [N1 is N+1]/[e(N, V), n(N1)] .
```
### Output
```
| ?- > test_bb_sorting.
[8,2,5,9,n(0)]
[2,5,9,e(0,8),n(1)]
[5,9,e(0,8),e(1,2),n(2)]
[5,e(0,8),e(1,2),e(2,9),n(3)]
[5,e(2,9),n(3),e(1,8),e(0,2)]
[e(2,9),e(1,8),e(0,2),e(3,5),n(4)]
[e(1,8),e(0,2),n(4),e(3,9),e(2,5)]
[e(0,2),n(4),e(3,9),e(2,8),e(1,5)]
true ? >
yes
| ?-
```