Prolog Programs Are A Collection of Facts
Prolog Programs Are A Collection of Facts
% Query.
% or Knowledge Base
% consult('knowledge.pl').
% nl stands for new line and \'s allows you to use quotes
loves(romeo, juliet).
% albert, male, female are atom constants that must begin with a
male(albert).
male(bob).
male(bill).
male(carl).
male(charlie).
male(dan).
male(edward).
female(alice).
female(betsy).
female(diana).
% female(alice). = yes
% Rules are used when you want to say that a fact depends on a group of facts
happy(albert).
happy(alice).
happy(bob).
happy(bill).
with_albert(alice).
% :- stands for if
runs(albert) :- happy(albert).
% runs(albert). = yes
happy(alice),
with_albert(alice).
does_alice_dance :- dances(alice),
swims(bob) :-
happy(bob),
near_water(bob).
% swims(bob). = no
% We can create 2 instances and if either comes back true the result
% will be yes
swims(bill) :-
happy(bill).
swims(bill) :-
near_water(bill).
% swims(bill). = yes
parent(albert, bob).
parent(albert, betsy).
parent(albert, bill).
parent(alice, bob).
parent(alice, betsy).
parent(alice, bill).
parent(bob, carl).
parent(bob, charlie).
% When you are cycling through the results the no at the end signals
% Y = bob
% Find Alberts grandchildren
% Y = charlie
write(Y), nl.
% ~n is a newline
parent(X, charlie),
brother(bob, bill).
grand_parent(X, Y) :-
parent(Z, X),
parent(Y, Z).
% X blushes if X is human
blushes(X) :- human(X).
human(derek).
% If we say one thing is true when somehing else is true, we can also
% blushes(derek). = yes
stabs(tybalt,mercutio,sword).
what_grade(5) :-
write('Go to kindergarten').
what_grade(6) :-
what_grade(Other) :-
Grade is Other - 5,
write(FName), tab(1),
% 3 > 15. = no
% 3 >= 15. = no
% 3 =< 15. = yes
% W = alice. = yes
% This says that we can assign the value of alice to W and not that
% W is equal to alice
% This says that any variable can be assigned anything and one of
% Using trace we can see how Prolog evaluates queries one at a time
warm_blooded(penguin).
warm_blooded(human).
produce_milk(penguin).
produce_milk(human).
have_feathers(penguin).
have_hair(human).
mammal(X) :-
warm_blooded(X),
produce_milk(X),
have_hair(X).
% trace.
% mammal(human).
% 1 1 Call: mammal(human) ?
% 2 2 Call: warm_blooded(human) ?
% 2 2 Exit: warm_blooded(human) ?
% 3 2 Call: produce_milk(human) ?
% 3 2 Exit: produce_milk(human) ?
% 4 2 Call: have_hair(human) ?
% 4 2 Exit: have_hair(human) ?
% 1 1 Exit: mammal(human) ?
% yes
% mammal(penguin).
% 1 1 Call: mammal(penguin) ?
% 2 2 Call: warm_blooded(penguin) ?
% 2 2 Exit: warm_blooded(penguin) ?
% 3 2 Call: produce_milk(penguin) ?
% 3 2 Exit: produce_milk(penguin) ?
% 4 2 Call: have_hair(penguin) ?
% 4 2 Fail: have_hair(penguin) ?
% 1 1 Fail: mammal(penguin) ?
% no
/*
parent(albert, bob).
parent(albert, betsy).
parent(albert, bill).
parent(alice, bob).
parent(alice, betsy).
parent(alice, bill).
parent(bob, carl).
parent(bob, charlie).
*/
related(X, Y) :-
parent(X, Z),
related(Z, Y).
% related(albert,carl). = true
% X is 2 + 2. = X = 4
% X is 3 + (2 * 10). = X = 23
% X is mod(7,2). = X = 1 (Modulus)
double_digit(X,Y) :- Y is X*2.
% double_digit(4,Y). = Y = 8
% 2nd argument
% random(0,10,X).
% between(0,10,X).
% Add 1 and assign it to X
% succ(2,X).
% X is abs(-8).
% X is max(10,5).
% X is min(10,5).
% Round a value
% X is round(10.56).
% X is truncate(10.56).
% Round down
% X is floor(10.56).
% Round up
% X is ceiling(10.56).
% 2^3
% X is 2** 3.
% sqrt, sin, cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh,
say_hi :-
read(X),
write('Hi '),
write(X).
% say_hi.
% Hi Derek
fav_char :-
get(X),
put(X),nl.
write_to_file(File, Text) :-
close(Stream).
read_file(File) :-
get_char(Stream, Char1),
process_stream(Char1, Stream),
close(Stream).
process_stream(end_of_file, _) :- !.
process_stream(Char, Stream) :-
write(Char),
get_char(Stream, Char2),
process_stream(Char2, Stream).
% ---------- HOW TO LOOP ----------
count_to_10(X) :-
write(X),nl,
Y is X + 1,
count_to_10(Y).
count_down(Low, High) :-
Z is High - Y,
write(Z),nl,
Y = 10.
count_up(Low, High) :-
Z is Y + Low,
write(Z), nl,
Y = 10.
loop(X) :-
x \= 15,
read(Guess),
write(Guess),
loop(Guess).
% guess_num.
:- dynamic(father/2).
:- dynamic(likes/2).
:- dynamic(friend/2).
:- dynamic(stabs/3).
father(lord_montague,romeo).
father(lord_capulet,juliet).
likes(mercutio,dancing).
likes(benvolio,dancing).
likes(romeo,dancing).
likes(romeo,juliet).
likes(juliet,romeo).
likes(juliet,dancing).
friend(romeo,mercutio).
friend(romeo,benvolio).
stabs(tybalt,mercutio,sword).
stabs(romeo,tybalt,sword).
% Add new clause to the database at the end of the list for the same
% predicate
% assertz(friend(benvolio, mercutio)).
% asserta(friend(mercutio, benvolio)).
% Delete a clause
% retract(likes(mercutio,dancing)).
% likes(mercutio,dancing). = no
% Delete all clauses that match
% retractall(father(_,_)).
% father(lord_montague,romeo). = no
% retractall(likes(_,dancing)).
% likes(_,dancing). = no
% You can store atoms, complex terms, variables, numbers and other
% lists in a list
% They are used to store data that has an unknown number of elements
% length([1,2,3], X).
% [H|T] = [a,b,c].
%H=a
% T = [b,c]
% of |
% List1 = [a,b,c].
% Reverse a list
% reverse([1,2,3,4,5], X).
% Concatenate 2 lists
write_list([]).
write_list([Head|Tail]) :-
write(Head), nl,
write_list(Tail).
% name(X, [65,32,114,97,110,100,111,109,32,115,116,114,105,110,103]).
name(Str1, StrList1),
name(Str2, StrList2),
name(Str3, StrList3).
/*
name('Derek', List),
put(FChar).
*/
% Get length of the string
atom_length('Derek',X).