PROLOG
PROLOG a general-purpose logic programming language associated with artificial intelligence and computational linguistics was first conceived by a group around Alain Colmerauer in Marseille, France, in the early 1970s has been used for theorem proving, expert systems, term rewriting, type inference, and automated planning, natural language processing
INSTALLATION For MAC Go to brew.sh and download homebrew Then go to your terminal and type brew install gun-prolog Then go to directory that has prolog files or prolog database Then type in gprolog
INSTALLATION For Windows Go to cygwin.com/install.html and click on install cygwin Then click on setup-x86 64.exe Now, we have to install Prolog and so go to www.gprolog.org/#download and click on Windows intel 64 bits auto-install setup link And prolog console is ready
FACTS AND RULES A prolog program is a collection of facts and rules Some examples of facts:
FACTS AND RULES A rule is more complex than a fact, it relates various facts An example of a rule relating the previous facts: This rule states that a Thing is expensive (has the property expensive) if the Thing is a car (has the car property) and the Thing is red (has the red property) Notice that :- means if and , means and
FACTS AND RULES Collectively, facts and rules are known as clauses A collection of clauses constitute a program The above clauses would be written in a file and the file loaded into the prolog interpreter
FACTS AND RULES Within the prolog interpreter, type car(dino). at the ?- prompt The ?- prompt is also known as the query prompt
FACTS AND RULES The interpreter reads the query and extracts the functor (property) car and the object dino Then It goes through each line of the program, always starting from the first line to find a fact that has the same functor as the query, i.e. car Prolog replies No. Because it cannot find a match in any of the rows of the program
VARIABLES X is a variable and stands for any object that has the car property Variables are always written with an upper case initial letter Objects or constants are written with a lower case initial letter Thing is a variable because of the initial upper case T In response to the above query, prolog replies
VARIABLES A variable without a value is called unbound A variable with a value is called bound In db.pl In prolog console
VARIABLES In db.pl: In prolog console:
VARIABLES In db.pl: In prolog console:
SYNTAX The Prolog data structure is the term Terms are nested structures defined recursively as Constant: Integer, Real, Atom Variable: named (initial letter upper case), (anonymous) Structure: functor with parameters or components, each of which may be a term likes (fred, whisky(malt, 12), X) where likes and whisky are functors of arity 3 and 2 respectively An atom is a functor of arity 0 Prolog has built-in binary operators, so that we may write 3+4*5 Prolog has no global variables
PROLOG CONSOLE Facts and rules are stored in a file called either the database or knowledge base: i.e. db.pl ?- [db]. (to load your knowledge base inside prolog console) Ex: ?- ['C:/Users/username/Desktop/db.pl']. ?- consult(‘db.pl’). (to load your knowledge base inside prolog console) ?- halt. (to exit out of the G prolog) $ gprolog (to return to your terminal) ?- listing. (it’s going to show a whole bunch of information about the contents of the database)
PROLOG
PROLOG ~w If you want to put in a variable here ~s If you want to transpose a string inside of here
PROLOG
CASE STATEMENTS In db.pl: In prolog console:
PROLOG
PROLOG
COMPARING VALUES In prolog console: \+ means not equal
DEBUGGING Trace is used debugging inside of prolog
DEBUGGING Notrace is used to switch off the debugger.
PROLOG
PROLOG Prolog has built-in binary operators =:= checks equality =\= checks inequality
INPUT/OUTPUT Reading input from user nl means new line read(X) and get(X) is used for this
FILE OPERATIONS
RECURSION
LOOP
NAME FUNCTION name take a string and convert it into a series of ASCII characters name also converts ASCII characters into a string