- Scribe - Tommy
- Editor -
- Editor -
October 26
Note for next class: Be sure to look at the problem set before Friday.
Prolog
We are running a prolog query engine. We have to consult a file, and then that file can be queried. There are some built-in commands for prolog.
For example:
member(x, [x,y]). Is x a member of the list [x,y]?
-returns yes.
member(X, [x,y]). Treats X as a variable and runs through the possible true values of X.
Prolog is case-sensitive. Anything that begins with an uppercase is a variable.
Statements in a file are used as facts and not asked as questions.
[seats]. Loads the file seats.
Seats contains relationships between who is sitting next to whom in class.
Initially, asked next_to( kevin, mike ) but the file returned no, even though Kevin was next to Mike. This is because Lynn typed only one half of the relationship, and prolog has not been told to accept symmetry.
Seats contains assertions, basically adding entries to a database.
next_to( mike, Who). Will find everyone next to mike. next_to( Who, WhoElse). Shows a list of all the relationships.
[user]. Allows command line input instead of using a file. Ctrl + D exits user input mode.
same_table ( X, Y ) :- next_to ( X,Y ) Is the same as writing next_to( X, Y ) implies same_table ( x, y);
- :- is a backwards implies.
same_table( X, Y ) :- next_to ( X, Z ), same_table ( Z, Y ).
The trace command allows you to enter trace mode. To exit trace mode, use notrace.
Things to Know About Prolog:
- Basic form of ground assertion
- Basic form of a rule
Cut command ( ! ) --> prevents from getting too many answers but in this class it will not typically help to get the right answer on the problem set.
Don't Care ( _ ) e.g. next_to(Chris, _) will return a true if someone is sitting next to him, but will not return who is sitting next to him
Fail --> Always returns false
The write command --> similar to display or printf e.g. write(X), write('is next to'), write(Y), nl, fail.
- assertz and retract (and dynamic declarations) The dynamic decalaration allows the use of the assertz and retract commands. example dynamic declaration: :-dynamic(next_to/2). This will make next_to a dynamic command that takes two arguments.