366 views

Uploaded by MC. Rene Solis R.

Quick Prolog intro

- A Prolog Introduction for Hackers
- prolog
- Prolog+Example+Programs
- 0.day1
- Building Expert Systems in Prolog
- lect_in_MNS
- Learn Prolog Now! - Patrick Blackburn, Johan Bos, Kristina Striegnitz
- 5 Simple Prolog Programs
- The World Trade Network_working Paper
- Learn Prolog Now 03 5062
- 212 Paper
- Prolog Intro
- The Art of Prolog (2nd Ed) - Leon Sterling, Ehud Shapiro
- Frequently Asked Questions - comp.lang.prolog
- 46873101-Prolog
- Parts List Wave125i 2005
- 2.5 Sub Chapter Notes
- Graph Structure in Web (Broder)
- Midterm 11su
- Equation Editor Shortcut Commands

You are on page 1of 6

=================================================================

Contents:

=================================================================

I. Getting started

II. Examples of problem solving in Prolog

1. Finding a path in a directed graph

2. Solving systems of equations with Peano numbers

=================================================================

I. Getting started

=================================================================

Run it and wait until you get the SWI-Prolog window with the prompt "?-".

When Prolog is ready it prints a prompt (?-) and you can type

queries (goals, questions). Try this one:

?- member(X,[1,2,3]).

*** NOTE THAT ALL PROLOG QUERIES MUST END WITH A FULL STOP (.) !!!

X=1

type semicolon (;). Then you get:

X=1;

X=2;

X=3;

No

4 ?-

(The number 4 before the prompt indicates how many times the prompt

has been printed.)

These are all the solutions of this query, i.e. all values of

the variable X that satisfy the query (all members of the list).

*** NOTE THAT THE VARIABLES MUST BEGIN WITH A CAPITAL LETTER !!!

If you whant to get all solutions in a list you can use this:

4 ?- findall(X,member(X,[1,2,3]),L).

X = _G354

L = [1, 2, 3]

of two sets (lists). For example:

9 ?- member(X,[1,2,3]),member(X,[a,b,3,c,2]).

X=2;

X=3;

No

5 ?-

10 ?- findall(X,(member(X,[1,2,3]),member(X,[a,b,3,c,2])),L).

X = _G504

L = [2, 3]

*** YOU MAY TYPE THE QUERIES IN MULTIPLE LINES AND PUT A DOT (.) AT THE END

OF THE LAST LINE. FOR EXAMPLE:

13 ?- member(X,

| [1,2,

| 3

| ]

| ).

X=1

Prolog prints a bar (|) in the beginning of the line, indicating that

this is a continuation.

14 ?- member (X,[1,2,3]).

ERROR: Syntax error: Operator expected

ERROR: member (X,[1,2,3]

ERROR: ** here **

ERROR: ) .

14 ?-

14 ?- member(X,[1,2,3].

ERROR: Syntax error: Unexpected end of clause

ERROR: member(X,[1,2,3]

ERROR: ** here **

ERROR: .

14 ?-

*** ANY NUMBER OF BLANKS OR NEW LINES CAN BE USED AFTER A COMMA.

*** MAKE SURE THAT ALL PARENTHESES AND SRUARE BRACKETS ARE

PROPERLY PAIRED !!!

14 ?- MEMBER(X,[1,2,3]).

ERROR: Syntax error: Operator expected

ERROR: MEMBER(X,[1,2,3]

ERROR: ** here **

ERROR: ) .

14 ?-

*** TYPE ALL PREDICATES OR GOALS (as member) with lower case letters !!!

=================================================================

II. Examples of problem solving in Prolog. Using files.

=================================================================

-------------------------------------

| |

|--------> 5

| |

\-------------> 6

arc(1,2).

arc(2,3).

arc(3,4).

arc(3,5).

arc(2,5).

arc(5,6).

arc(2,6).

Now we want to define a procedure to find a path between two nodes X and Y

in this graph. That is, we want to define a predicate path(X,Y,Path).

We just follow our intuition:

if arc(X,Y) then path(X,Y)). Or, in Prolog this is:

path(X,Y,[arc(X,Y)]) :- arc(X,Y).

2. Otherwise, if there is an arc between X and some other node Z, and there is

a path from Z to Y, then there is a path from X to Y too. That is,

if arc(X,Z) and path(Z,Y) then path(X,Y). In Prolog this is:

path(X,Y,[arc(X,Z)|P]) :- arc(X,Z),path(Z,Y,P).

3. The only thing that is not so intuitive is how we get the third argument

of path. This is a technicality that allows us to create lists and is a

built-in feature of Prolog.

Now, we have to add these two rules (clauses) along with the set of facts

describing the graph to the Prolog database. To do this we need to create

a text file and then copy/paste the above two rules and 7 facts into it.

Assume we use, for example, NOTEPAD and create a file named "path.pl" in

the folder "c:/prolog".

We can load this file into the Prolog database by using the following

query:

?- ['c:/prolog/path.pl'].

Then, we may ask the following questions (type ; after the Prolog answer

to see alternative solutions, or just <Enter> to continue with the

next question):

Find the path (or all possible paths, if you use ;) from node 1 to node 6.

?- path(1,6,P).

?- path(1,X,P).

?- path(X,5,P).

?- path(1,6,[A,B,C]).

?- path(1,6,P), length(P,3).

?- path(X,Y,P), length(P,L), L>2.

?- path(X,Y,[arc(X,3),arc(3,Y)]).

?- path(X,Y,P),member(arc(3,4),P).

--------------------------------------------------

Here we use a similar definition based on lists.

An integer N>0 is represented as a list of N 1's

For example: 5 is [1,1,1,1,1].

Prolog has a built-in predicate that can be used for adding such numbers.

This is the same prdicate that appends lists.

X+Y=5

X-Y=3

Then we put the two equatons as Prolog predicates.

X+Y=5 ==> append(X,Y,[1,1,1,1,1])

Obviously, both equations must be true, i.e. we need a conjunction between the

two append's. So, with the following query get the answers:

?- append(X,Y,[1,1,1,1,1]),append(Y,[1,1,1],X).

X=[1,1,1,1]

Y=[1]

21 ?- append(X,Y,[1,1,1,1,1]).

X = []

Y = [1, 1, 1, 1, 1] ;

X = [1]

Y = [1, 1, 1, 1] ;

X = [1, 1]

Y = [1, 1, 1] ;

X = [1, 1, 1]

Y = [1, 1] ;

X = [1, 1, 1, 1]

Y = [1] ;

X = [1, 1, 1, 1, 1]

Y = [] ;

No

22 ?-

What we get here is actually all possible solutions of the equation X+Y=5. That is:

X=0, Y=5

X=1, Y=4

X=2, Y=3

X=3, Y=2

X=4, Y=1

X=5, Y=0

Happy Prolog-ing...

- A Prolog Introduction for HackersUploaded byMC. Rene Solis R.
- prologUploaded byabhishekgaur866
- Prolog+Example+ProgramsUploaded bycasanovakk
- 0.day1Uploaded bydavyjones00
- Building Expert Systems in PrologUploaded byGurpreet Singh
- lect_in_MNSUploaded bypra311288
- Learn Prolog Now! - Patrick Blackburn, Johan Bos, Kristina StriegnitzUploaded byapi-19484311
- 5 Simple Prolog ProgramsUploaded byMC. Rene Solis R.
- The World Trade Network_working PaperUploaded bymerposa
- Learn Prolog Now 03 5062Uploaded byVan Tuan Nguyen
- 212 PaperUploaded byacouillault
- Prolog IntroUploaded bykrank197
- The Art of Prolog (2nd Ed) - Leon Sterling, Ehud ShapiroUploaded byapi-19484311
- Frequently Asked Questions - comp.lang.prologUploaded byMC. Rene Solis R.
- 46873101-PrologUploaded bytrevor13
- Parts List Wave125i 2005Uploaded byj
- 2.5 Sub Chapter NotesUploaded byMe, Myself and I
- Graph Structure in Web (Broder)Uploaded byStanislav Vorobyev
- Midterm 11suUploaded byvivek patel
- Equation Editor Shortcut CommandsUploaded bycubanninja
- 2009-084-004-03Uploaded bydyaataha7902
- 1-s2.0-S0010448598000335-main.pdfUploaded bySyafiq Mazlan
- NotesJoe.nm Heather.cs.Ucdavis.edu -Matloff JoeUploaded byapmapm
- MathsUploaded byRaj
- DARAUploaded bySharath Kr
- Vocab Mod 2Uploaded bySamantha Lui
- Graph Traversal PatternUploaded byRand Fitzpatrick
- JdeUploaded bywijisariputiati
- CW Prosigns, Abbr EtcUploaded bykrismir
- Maximum index given domination numberUploaded bydragance106

- Python on a Chip [Phyton-on-a-Chip]Uploaded byMC. Rene Solis R.
- Proceso Admisión Bach EXHCOBA 2010Uploaded byMC. Rene Solis R.
- Arduino Cheat SheetUploaded byMC. Rene Solis R.
- Python on a Chip - ArduinoMegaUploaded byMC. Rene Solis R.
- CONVSIGUELEUploaded byMC. Rene Solis R.
- Proyecto Final de Comercio Electronico UNID 2010Uploaded byMC. Rene Solis R.
- scop3[1]Uploaded byMC. Rene Solis R.
- Controlling an Arduino With CocoaUploaded byMC. Rene Solis R.
- Tutorial Ba%cc%81sico Arduino 2010[1]Uploaded byMC. Rene Solis R.
- Prolog+ MYSQ TareaUploaded byMC. Rene Solis R.
- Programación LogicaUploaded byMC. Rene Solis R.
- SIPCOM ATA-1000 IP fxs 1 port (8bits-100) SIP Voip ATAUploaded byMC. Rene Solis R.
- Optoma DS611 Password Reset via Serial Cable (prolific chip)Uploaded byMC. Rene Solis R.
- ManualScormUploaded byMC. Rene Solis R.
- PA168V SIP FXS 1 port Configuration Ver 1.6 EnglishUploaded byMC. Rene Solis R.
- Arduino Experimentation Kit - ARDX Cutting Sheet Exercise (check guide)Uploaded byMC. Rene Solis R.
- Logic Programming and Prolog: A First GlimpseUploaded byMC. Rene Solis R.
- Bionic Arduino ClassUploaded byMC. Rene Solis R.
- 5 Simple Prolog ProgramsUploaded byMC. Rene Solis R.
- Memoria de Residencia Remigio I. Illan V.Uploaded byMC. Rene Solis R.
- tux-españolUploaded bypercyjulian
- Arduino Led Display (8 x 8 Led Matrix)-GuideUploaded byMC. Rene Solis R.
- INTRODUCCIÓN A PROLOGUploaded byMC. Rene Solis R.
- Chaps 1 3 Ai Prolog[1]Uploaded byMC. Rene Solis R.
- Arduino TMP36 Precision Temperature Sensor-GuideUploaded byMC. Rene Solis R.
- Arduino Experimenters Guide LQUploaded byAlessandra Fonseca
- Frequently Asked Questions - comp.lang.prologUploaded byMC. Rene Solis R.
- mbed_lpc1768Uploaded byMarcos Benedetti

- Logic ProgrammingUploaded byPradeep Kumar Reddy Reddy
- Practical List (AI)Uploaded byMargil Shah
- 5 Simple Prolog ProgramsUploaded byMC. Rene Solis R.
- prolog.pdfUploaded bySri Noni
- Prolog GamesUploaded byRicardo Galvan Z
- 0 Prolog UnificationUploaded bySergiu Pop
- algorithm = logic + control.pdfUploaded byirmuhidin
- rr310505-principles-of-programming-languagesUploaded bySRINIVASA RAO GANTA
- SWI Prolog 5.6 reference manualUploaded byIván Eduardo
- Picat GuideUploaded byMatthew Prussman
- Armstrong Erlang HistoryUploaded bySooraj Subramanian
- Prolog and ExpertSystemzUploaded byalmauly2
- 46873101-PrologUploaded bytrevor13
- Analysis of Architectural Space Composition UsingUploaded byshalumk
- 101319 Programming Languages Theory & PracticeUploaded bySumaya_87
- AI Question Bank 2017 18 CSEUploaded byDivyesh Nihalani
- prolog-knowledge-representation.pdfUploaded byMihai Nan
- Automated Reasoning With PROLOGUploaded byNaga Raju N
- SWI Prolog 6.2.1Uploaded byRamon Alejandro Quinteros
- Probleme PrologUploaded bybobbycardian
- Rdb Relational Database ManagerUploaded byJason Sinnamon
- Agent Speak in JasonUploaded byJulianaFonsecaAntunes
- IMP Knowledge 3Uploaded byAshok Ashokbyadav
- CS95 Deductive DatabasesUploaded byAvinash Samuel
- AIES ALL PROGRAMS COMPLETEUploaded bypsd_prasad
- 73995939 37 Resume TemplatesUploaded byAnthony Bonafide Dakush
- CH4_Depth-. Breadth-, And Best-First SearchUploaded byਰਣਜੀਤ ਢਿੱਲੋਂ
- Artificial_Intellegence.pdfUploaded byHaque Nawaz Lashari
- r05310505 Principles of Programming Langauges(www.rajeshi4you.blogspot.com)Uploaded byRajesh Vungarala
- 16368_15840-UNIT VIUploaded byanu_123456789