quick introduction to prolog prolog)

Upload: mc-rene-solis-r

Post on 30-May-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Quick Introduction to Prolog Prolog)

    1/6

    //www.cs.ccsu.edu/~markov/ccsu_courses/prolog.txt[3/1/2010 7:17:20 AM]

    ================================================================Quick Introduction to Prolog (SWI-Prolog)

    ================================================================Contents:

    ================================================================Getting started

    I. Examples of problem solving in Prolog1. Finding a path in a directed graph

    2. Solving systems of equations with Peano numbers================================================================Getting started

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

    nstall SWI-Prolog using the infomation at http://www.swi-prolog.org/

    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 typeueries (goals, questions). Try this one:

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

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

    Now you get

    X=1

    ress Enter to end the query or if you want alternative solutionsype semicolon (;). Then you get:

    X = 1 ;

    X = 2 ;

    X = 3 ;

    No?-

    The number 4 before the prompt indicates how many times the promptas been printed.)

    hese are all the solutions of this query, i.e. all values ofhe variable X that satisfy the query (all members of the list).

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

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

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

    X = _G354

  • 8/14/2019 Quick Introduction to Prolog Prolog)

    2/6

    //www.cs.ccsu.edu/~markov/ccsu_courses/prolog.txt[3/1/2010 7:17:20 AM]

    = [1, 2, 3]

    Or you may try a conjunction of two goals to get the intersectionf two sets (lists). For example:

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

    X = 2 ;

    X = 3 ;

    No?-

    Or again, using findall you get all solutions in a list:

    RROR: Stream user_input:71: Syntax error: Unexpected end of clause0 ?- findall(X,(member(X,[1,2,3]),member(X,[a,b,3,c,2])),L).

    X = _G504

    = [2, 3]

    ** YOU MAY TYPE THE QUERIES IN MULTIPLE LINES AND PUT A DOT (.) AT THE ENDOF THE LAST LINE. FOR EXAMPLE:

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

    X = 1

    rolog prints a bar (|) in the beginning of the line, indicating thathis is a continuation.

    You may also get ERROR MESSAGES. For example:

    4 ?- member (X,[1,2,3]).RROR: Syntax error: Operator expectedRROR: member (X,[1,2,3]

    RROR: ** here **RROR: ) .4 ?-

    ** NO SPACE IS ALLOWED AFTER A PREDICATE OR GOAL NAME (e.g. member).

    4 ?- member(X,[1,2,3].RROR: Syntax error: Unexpected end of clauseRROR: member(X,[1,2,3]RROR: ** here **RROR: .

  • 8/14/2019 Quick Introduction to Prolog Prolog)

    3/6

    //www.cs.ccsu.edu/~markov/ccsu_courses/prolog.txt[3/1/2010 7:17:20 AM]

    4 ?-

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

    ** MAKE SURE THAT ALL PARENTHESES AND SRUARE BRACKETS AREPROPERLY PAIRED !!!

    4 ?- MEMBER(X,[1,2,3]).RROR: Syntax error: Operator expected

    RROR: MEMBER(X,[1,2,3]RROR: ** here **RROR: ) .4 ?-

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

    ================================================================I. Examples of problem solving in Prolog. Using files.================================================================

    . Finding a path in a directed graph------------------------------------

    Consider the following graph

    1 --> 2 --> 3 --> 4| ||--------> 5| |\-------------> 6

    n Prolog this is represented as a set of facts. i.e.

    rc(1,2).rc(2,3).rc(3,4).rc(3,5).rc(2,5).rc(5,6).rc(2,6).

    Now we want to define a procedure to find a path between two nodes X and Yn this graph. That is, we want to define a predicate path(X,Y,Path).

    We just follow our intuition:

    . If there is an arc between X and Y, then Path=arc(X,Y). That is,if arc(X,Y) then path(X,Y)). Or, in Prolog this is:

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

    . Otherwise, if there is an arc between X and some other node Z, and there isa path from Z to Y, then there is a path from X to Y too. That is,

  • 8/14/2019 Quick Introduction to Prolog Prolog)

    4/6

    //www.cs.ccsu.edu/~markov/ccsu_courses/prolog.txt[3/1/2010 7:17:20 AM]

    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).

    . The only thing that is not so intuitive is how we get the third argumentof path. This is a technicality that allows us to create lists and is abuilt-in feature of Prolog.

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

    escribing the graph to the Prolog database. To do this we need to createtext 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" inhe folder "c:/prolog".

    We can load this file into the Prolog database by using the followinguery:

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

    After a successfull load (compilation) we get the prompt again.hen, we may ask the following questions (type ; after the Prolog answer

    o see alternative solutions, or just to continue with theext question):

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

    ind paths from node 1 to other nodes in the graph.- path(1,X,P).

    ind paths from some/all nodes to node 5.- path(X,5,P).

    ind paths with length 3 from node 1 to node 6.- path(1,6,[A,B,C]).

    ind paths with length 3 from node 1 to node 6.- path(1,6,P), length(P,3).

    ind paths in the graph that are longer than 2 arcs.- path(X,Y,P), length(P,L), L>2.

    ind paths with length 2 that go through node 3.- path(X,Y,[arc(X,3),arc(3,Y)]).

    ind paths in the graph that include arc(3,4).- path(X,Y,P),member(arc(3,4),P).

    . Solving systems of equations with Peano numbers-------------------------------------------------

    he original Peano numbers are defined functionally as 0 and s(s(...s(0)...)).

  • 8/14/2019 Quick Introduction to Prolog Prolog)

    5/6

    //www.cs.ccsu.edu/~markov/ccsu_courses/prolog.txt[3/1/2010 7:17:20 AM]

    Here we use a similar definition based on lists.

    is represented as an empty list [].An integer N>0 is represented as a list of N 1's

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

    rolog has a built-in predicate that can be used for adding such numbers.his is the same prdicate that appends lists.

    et's now solve the following system of equations:

    X+Y=5X-Y=3

    irst, we make the transformation X-Y=3 ==> Y+3=X (why?).hen we put the two equatons as Prolog predicates.

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

    Obviously, both equations must be true, i.e. we need a conjunction between thewo 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]

    e. X=4, Y=1

    Here is another, simpler example:

    1 ?- 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 = [] ;

    No2 ?-

  • 8/14/2019 Quick Introduction to Prolog Prolog)

    6/6

    What we get here is actually all possible solutions of the equation X+Y=5. That is:X=0, Y=5X=1, Y=4X=2, Y=3X=3, Y=2X=4, Y=1X=5, Y=0

    Happy Prolog-ing...