Você está na página 1de 8

PALABRAS RESERVADAS

http://www.cs.oswego.edu/~odendahl/manuals/prolog/intro/index.html
Termino Definicion
statement one of fact, rule, or query

record (cf. data model) of


fact (cf. unit clause)
assumed true information

deductive formulation
whereby new facts can be
rule (cf. predicate)
inferred from existing
facts
a statement asking about
query
the existence of a fact(s)
logic program collection of rules
collection of facts that can
meaning of a logic
be deduced from any set
program
of initial facts
Loading files
predicate examples
consult('predicates.p').
consult(+Files) consult(['data1','data2']).
['more-data']. [user].
reconsult('predicates.p').
reconsult(+Files)
[-'more-data.dat'].

Control
predicate examples
abort abort
halt halt.
Terms
predicate examples
| ?- var(X). X = _578 | ?- has_fur(X),
var(?Term)
var(X). no
nonvar(?Term) | ?- nonvar(X). no

| ?- X = bunny, is_soft(X). X = bunny


?Term1 = ?Term2
| ?- has_fur(X), Y = snake, X = Y. no

| ?-
functor(?Term, ?Name, ?Arity) functor(is_soft(bunny),Name,Arity).
Name = is_soft Arity = 1

| ?- Doit =.. [is_soft,X]. Doit =


is_soft(bunny). | ?- Doit =..
?Term =.. ?List
[is_soft,X], call(Doit). X = bunny Doit
= is_soft(bunny).

| ?- length([green,eggs,and,ham],L).
length(?List, ?Length)
L=4

| ?- name(cat,AsciiList). AsciiList =
name(?Atom, ?Chars)
[99,97,116]
Input/Output
predicate examples
| ?- read(X). % user types,
e.g., has_fur(bunny). X =
read(-Term) has_fur(bunny) Note
input term must be a
valid Prolog term.
| ?- write(cat). cat yes | ?-
write(?Term) write("cat"). [99,97,116]
yes

I/O of Sentences extensions necessary for parsing


natural language constructs.
predicate notes
May have to be modified
read_sentence(-
to handle special lexical
ListOfAtoms)
constraints.
write_sentence(+ListOfAt
oms)
Clause Database
predicate examples
assert(+Clause) | ?- assert(has_fur(bunny)).

| ?- asserta((is_soft(X) :- has_fur(X))). note:


extra parentheses required for clause with a
asserta(+Clause)
body. This will be the most usual version for
our purposes.

Add assertion at bottom of search path - will


assertz(+Clause)
be found last.

clause(+Head,?Body) | ?- clause(is_soft(_),B). B = has_fur(_419)

retract(+Clause) Retract the clause matching Clause

Retract all clauses with head matching Head


retractall(+Head) E.g., retractall(stuff(_)). erases all clauses of
predicate stuff/1

Abolish all clauses with head matching any in


the list Predicates E.g., abolish(stuff/1). erases
abolish(+Predicates)
all clauses of predicate stuff/1 and makes
predicate stuff/1 unknown
Sets and Bags
predicate examples
setof(?Template,+Generator,-Set) | ?- setof(X,has_fur(X),S). S = [bunny]

member(+Term,+List) | ?- member(joe,[bill, joe, ted, sally]). yes

nonmember(+Term,+List) | ?- nonmember(joe,[bill, joe, ted, sally]). no

Remove duplicates from List1. | ?-


make_set(+List1,-List2) make_set([bill, joe, ted, joe, bill, sally],Z). Z
= [bill, joe, ted, bill, sally]

Remove item Term from List1. | ?- Pets =


[dog(rover), cat(mittens), canary(tweetie)],
delete(+Term,+List1,-List2)
delete(dog(rover),Pets,NewPets). NewPets =
[cat(mittens), canary(tweetie)]

Add item Term to List1. | ?- Pets =


[cat(mittens), canary(tweetie)],
insert(+Term,+List1,-List2) insert(hamster(smelly),Pets,NewPets).
NewPets = [cat(mittens),
canary(tweetie),hamster(smelly)]
Is List1 a subset of List2? | ?- subset([bill, joe,
subset(+List1,+List2)
ted],[joe, bill, sally, ted]). yes
Do List1 and List2 have same elements? | ?-
equal(+List1,+List2) subset([bob, carol, ted, sally],[carol, bob,
sally, ted]). yes
Remove items in List2 from List1 (set
difference). | ?- Pets = [dog(rover),
difference(+List1,+List2,-List3) cat(mittens), canary(tweetie)],
difference(Pets,[dog(rover),canary(tweety)],Ne
wPets). NewPets = [cat(mittens)]
Add items in List2 to List1 (set union). | ?-
Pets = [dog(rover), cat(mittens),
canary(tweetie)],
union(+List1,+List2,-List3)
union(Pets,[dog(rover),fish(goldie)],NewPets).
NewPets = [dog(rover), cat(mittens),
canary(tweetie), fish(goldie)]
Add items in List2 to List1 (duplicates are
presered). | ?- Pets = [dog(rover),
cat(mittens)],
append(+List1,+List2,-List3)
append(Pets,[dog(rover),fish(goldie)],NewPets)
. NewPets = [dog(rover), cat(mittens),
dog(rover), fish(goldie)]
Get nth item in a list. | ?- Pets = [dog(rover),
cat(mittens), canary(tweetie)],
nth(+List,+Number,+Term)
nth(Pets,2,FavoritePet). FavoritePet =
cat(mittens)

Você também pode gostar