-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfacts.pl
55 lines (41 loc) · 1.78 KB
/
facts.pl
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
/*
NOTE: facts
- a fact is a statement that describe object properties or relations between objects
- all forms of data are called terms and each term is either a variable, an atomic term or a compound term
- facts and rules are conventionally stored in a database, such as 'db.pl' (see source on github)
*/
/*
Head.
Head :- true.
variable : starts with uppercase or _, single _ is anonymous variable
atomic term:
- atom : starts with lowercase, e.g. a, at, atom, 'Name'
- integer : 12, 100, 2_020
- others : 3.1415
compound term : P/N
*/
% color/1
color(red).
color(green).
color(blue).
% NOTE: color holds if X is red OR green OR blue
?- color(green). % true.
?- color(black). % false.
% additional facts
male(michael). % michael is male
friend(michael, adam). % michael is friend with adam
?- male(michael). % true.
?- friend(michael, X). % X = adam.
% compound terms: atom and compound term
person(michael, nationality(sweden, swedish)).
person(adam, nationality(norway, norwegian)).
?- person(michael, nationality(X, Y)). % X = sweden, Y = swedish.
?- person(X, nationality(sweden, _)). % X = michael
% type tests
?- must_be(atom, a). % true.
?- must_be(atom, 2). % ERROR: Type error: `atom' expected, found `2' (an integer)
?- must_be(integer, 2). % true.
% term inspection
?- functor(f(a, g(X)), Functor, Arity). % Functor = f, Arity = 2.
% canonical representation
?- write_canonical(a + b = [x, y, z]). % =(+(a,b),[x,y,z]) true.