Skip to content

Εργαστήριο 3 στην Prolog

Θέματα που εξετάζονται στο εργαστήριο: Prolog Unit Tests1

Εξάσκηση (εκφωνήσεις και λύσεις ασκήσεων)

Άσκηση PRO3E1 Γράψτε μια βάση γνώσης που να περιέχει την πληροφορία ότι ο john και ο george είναι άνδρες, οι mary, susan και alice γυναίκες, ο john είναι γονέας της mary, η mary είναι γονέας της susan και του george, και η susan είναι γονέας της alice. Επιπλέον ορίστε το κατηγόρημα ancestor\2 που να ορίζει πότε ένα άτομο είναι πρόγονος ενός άλλου ατόμου. Γράψτε κατάλληλο κώδικα έτσι ώστε τα ακόλουθα tests να περνάνε.

pro3e1_tests.pl
pro3e1_tests.pl
:- begin_tests(dituoi_tests).
:- use_module(pro3e1).

test(male_john) :- male(john).

test(female_mary) :- female(mary).

test(john_is_parent_of_mary) :- parent(john, mary).


test(direct_ancestor, [nondet]) :-  ancestor(john, mary).
test(indirect_ancestor, [nondet]) :-  ancestor(john, alice).
test(not_ancestor, [fail]) :-  ancestor(alice, john).

:- end_tests(dituoi_tests).

Γράψτε τις εντολές που εκτελούν τα tests.

Λύση άσκησης PRO3E1
pro3e1.pl
:- module(pro3e1, [male/1, female/1, parent/2, ancestor/2]).

male(john).
male(george).

female(mary).
female(susan).
female(alice).

parent(john, mary).
parent(mary, susan).
parent(susan, alice).
parent(mary, george).

ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).

Εκκίνηση του swipl.

$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 9.2.9)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

Φόρτωση του αρχείου pro3e1_tests.pl (που περιέχει τα tests).

?- [pro3e1_tests].
true.

Εκτέλεση των tests.

?- run_tests.
[6/6] dituoi_tests:not_ancestor .................... passed (0.000 sec)
% All 6 tests passed in 0.003 seconds (0.002 cpu)

Ή αν γίνει αλλαγή στο κώδικα και θέλουμε να ξαναδοκιμάσουμε τα tests.

?- make.
true.
?- run_tests.
[6/6] dituoi_tests:not_ancestor .................... passed (0.000 sec)
% All 6 tests passed in 0.003 seconds (0.002 cpu)