-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_module.prolog
114 lines (85 loc) · 3.12 KB
/
main_module.prolog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/** Main module (main_module.prolog)
This file is a brief example of some Prolog functionalities
and main charateristics. It was written with the purpose of
getting me started with this amazing programming language..
Sure enough, it was a success: I'm hooked in :D ..
This application "dynamically" creates some workers and some
murderers (dynamically means that the facts are not written one
by one, but added to the knowledge base or "KnB" by Prolog
itself). It then prints out some stuff, so that you know that
it did something, and least but not last that it worked nice &
good.
@author AresAndy
@license BSD
*/
% ========================
% module declaration
:- module(main_module, [main/0]).
% ========================
% dynamic database population headers
% (Tells that those facts might change dynamically)
:- dynamic tool/1.
:- dynamic weapon/1.
:- dynamic worker/2.
:- dynamic killer/2.
% ========================
% Utility predicates
appendTopMostDB(X) :- asserta(X). /** Insert fact on top of KnB */
print_list([]) :- true. /** Prints every element of a list */
print_list([X|XS]) :-
format("~w ", X),
print_list(XS).
% ========================
% Tools creation predicates
createTools([]) :- /** Create tools: no tools provided */
true.
createTools([X|XS]) :- /** Create tools: tools provided */
appendTopMostDB(tool(X)),
createTools(XS).
% ========================
% Weapons creation predicates
createWeapons([]) :- /** Create weapons: no weapons provided */
true.
createWeapons([X|XS]) :- /** Create weapons: weapons provided */
appendTopMostDB(weapon(X)),
createWeapons(XS).
% ========================
% Workers creation predicates
createWorkers([], _) :- /** Create workers: no names provided */
true.
createWorkers([Name|Names], []) :- /** Create workers, no tool */
appendTopMostDB(worker(Name, tool(_))),
createWorkers(Names, []).
createWorkers([Name|Names], [Tool|Tools]) :- /** Create workers with tools */
appendTopMostDB(worker(Name, tool(Tool))),
createWorkers(Names, Tools).
% ========================
% Killers creation predicates
createKillers([], _) :- /** Create killers, no name provided */
true.
createKillers([Name|Names], []) :- /** Create killers, no weapon */
appendTopMostDB(killer(Name, weapon(knife))),
createKillers(Names, []).
createKillers([Name|Names], [Wep|Weps]) :- /** Create killers, weaponized */
appendTopMostDB(killer(Name, weapon(Wep))),
createKillers(Names, Weps).
% ========================
% Querying predicates
killers(X) :- findall([X0, X1], killer(X0, X1), X). % give all killers
workers(X) :- findall([X0, X1], worker(X0, X1), X). % give all worksers
% ========================
% main predicate
main :-
write("Creating tools..."), nl,
createTools([saw, hammer, axe, nail, pencil]),
write("Creating weapons..."), nl,
createWeapons([axe, hammer, knife]),
write("Creating workers..."), nl,
createWorkers([john, aaron, jack, simon], []),
write("Creating killers..."), nl,
createKillers([judas, bruto, jason], []),
killers(Kill),
workers(Work), % To Kill and Work are bound the results
print_list(Kill), nl,
print_list(Work), nl,
halt.