Answers to FAQ

  • Photon machines have GNU Prolog installed on them. To run Prolog type 'gprolog' on the prompt.
  • The facts and the rules which form the part of your Prolog program have to be written in a '.pl' file. An example is taken up later on to illustrate a typical '.pl' file.
  • To work with a given '.pl' file, you have to type '['file_name'].' on the prompt that you get after running 'gprolog'. This will complile that '.pl' file.
  • Now you can query the prolog program by typing in commands. NOTE that each command must be terminated by a full-stop.
  • A query may have multiple answers. To view the next answer, type ';', else press 'enter'.
  • NOTE: Be careful while using variable names!! Remember that variablr names begin with capital letters.

    Examples

  • Consider the example regarding the Ragas and Songs taken up in class. It is in the lecture notes. Here is the '.pl' file containing the corresponding prolog program.

    ---------- raga.pl ----------

    mela_raga(kharaharapriya).
    mela_raga(mayamalavagoula).


    same_family(kharaharapriya, sahana).
    same_family(kharaharapriya, khamas).
    same_family(mayamalavagoula, saveri).

    same_family(R, R).
    same_family(R1, R2) :- same_family(R2, R1).


    song(igiripai, sahana).
    song(chakkani, kharaharapriya).
    song(brocheva, khamas).
    song(karikalaba, saveri).
    song(merusamana, mayamalavagoula).
    song(devaddeva, mayamalavagoula).


    likes(siva, saveri).


    likes_song(Person, Song) :- song(Song, Raga),
           likes(Person, Raga).
    likes_song(Person, Song) :- likes(Person, Raga1),
           song(Song, Raga2),
           same_family(Raga1, Raga2).


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

    Goals:-



  • The addition operation


    The 'add.pl' file is given below:-

    add(0, Y, Y).
    add(s(X), Y, s(Z)) :- add(X, Y, Z).

    Try the following Goals :- and so on.

    A trace of execution is given below. Take a look at it to know what to expect when you give commands.


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

    [toley@photon-40 prolog]$ gprolog
    GNU Prolog 1.2.1
    6 By Daniel Diaz
    Copyright (C) 1999-2002 Daniel Diaz
    | ?- ['add.pl'].
    compiling /users/pg02/toley/prolog/add.pl for byte code...
    /users/pg02/toley/prolog/add.pl compiled, 2 lines read - 474 bytes written, 15 ms

    yes
    | ?-

    // now we are ready to give commands.

    | ?- listing.

    add(0, A, A).
    add(s(A), B, s(C)) :-
    add(A, B, C).

    yes
    | ?- add(0, s(0), s(0)).

    Yes
    | ?- add(0, s(s(0)), Ans).

    Ans = s(s(0))

    yes
    | ?- add(A,B,s(0)).

    // Note that this query has multiple answers. Afetr each answer, you have to press ';' to view the next answer.

    A = 0
    B = s(0) ? ;

    A = s(0)
    B = 0 ? ;

    no
    | ?-

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