general PROLOG questions 
Author Message
 general PROLOG questions

Hello.
  I am currently learning how to use PROLOG for my logic class. It
is an assignment showing the relationship between logic and PROLOG
language. Please help or give me any relevant information. Few
questions:
        1)   I need to know how to write a set of PROLOG rules
             that represent the rules of some organizational  
             system such as: the conditions for passing a
             course in college or for determining eligibility
             for a post graduate grant.

       2)    Using a definitions of the listsize predicate for  
             guidence, a definition for a rule that will return
             the nth element of a given list. For example:    
             nth([a,short,list],2,Nth) should return Nth = short.

       3)    To write a set of PROLOG rules to determine if a given
             list of integers is in ascending order.

       4)    In the following  program:

           /* data */
          line(carlisle, newcastle, 62).
          line(newcastle, middlesbrough, 48).
          line(newcastle, darlington, 36).
          line(darlingtone, middlesbrough, 15).
          line(darlington, northallerton, 14).
          line(northallerton, york, 30).
          line(york, leeds, 25).
          line(leeds, manchester, 43).
          line(manchester, liverpool, 31).
          line(liverpool, preston, 28).
          line(manchester, preston, 31).
          line(preston, lancaster, 21).
          line(leeds, lancaster, 70).
          line(leeds, carlisle, 113).
          line(lancaster, carlisle, 69).

         with two rules:
         link (A,B,D) :-      link(A,B,D) :-
            line(A,B,D).         line(B,A,D).

        and with the of what is the route from A to B and the total distance?
        for example:

           | ?- route(manchester,newcastle,Route,Distance).

        is given below:

           route (S,F,R,D) :-
             route 1(S,F,[S],R,D).

           route (S,F,,[],D) :-
             link (S,F,D).

           route 1(S,F,S2,[S1|R],D) :-
            link(S,S1,D1),
            not (member(S1,S2))
            route 1(S1,F,[S1|S2],R,D2)
            D is (D1 + D2).

        then the distances are accumalated in D.

        the question is how to write an enchanced route finding program
        that finds alternative routes and establishes which is the
        shortest.

       Please send any comments and suggestions to the questions quickly
       as I am currently working on a thesis on the relationship between
       PROLOG and Predicate logic and do not have enough time to become
       an expert in the language. Please, any help is GREATLY appreciated.
       Thank you very much.
                                     Thank you,
                                     Anthony Bush.
       Email me!

       thanks.



Wed, 19 May 1993 07:28:00 GMT  
 general PROLOG questions

Quote:
}Hello.
}  I am currently learning how to use PROLOG for my logic class. It

After you finish his homework, could you take a look at mine?

Chris
--

Copyright 1989 Chris Bryden permission granted for non-profit reproduction only
-------------------------------------------------------------------------------
                 expectation is the mother of disappointment



Wed, 19 May 1993 16:53:00 GMT  
 general PROLOG questions

to do his PROLOG homework for him.

This offends me.  Am I alone in this?  It seems to me that if you "don't
have the time to become an expert" in the topic of your graduate thesis,
then you darn well better pick another topic for your thesis.  This posting
seems to be a bit like purchasing one of those canned reports instead of
doing research - legal, yes, but ethical?

I don't know.  Maybe I'm overreacting, but I think you're going to need
a pretty fundamental understanding of PROLOG in order to do a worthwhile
comparison of PROLOG to Predicate Logic.


...!sun!portal!cup.portal.com!tnr |  Great Quake of '89.



Wed, 19 May 1993 18:01:00 GMT  
 general PROLOG questions
Quote:

>        1)   I need to know how to write a set of PROLOG rules
>             that represent the rules of some organizational  
>             system such as: the conditions for passing a
>             course in college

pass_condition([68,111,32,116,104,101,32,112,114,111,106,
                101,99,116,32,121,111,117,114,115,101,108,
                102,33]).

Quote:

>       2)    Using a definitions of the listsize predicate for  
>             guidence, a definition for a rule that will return
>             the nth element of a given list. For example:    
>             nth([a,short,list],2,Nth) should return Nth = short.

listsize([_, _, _, _ | _], 'BIG') :- !.
listsize(_, small).

Hence it is easy to extend this to nth, at least for small lists.

Quote:
>       3)    To write a set of PROLOG rules to determine if a given
>             list of integers is in ascending order.

ascending(L) :- sort(L, S),
        ( L = S ->
                write(yes)
        ; listsize(L, S1),listsize(S, S1) ->
                write(no)
        ;
                write(maybe)
        ).

Quote:
>       4)    In the following  program:

Unfortunately there were several syntax errors on the system I ran it
on, so I am unable to help you in detail.  However, try reading some of
Dijkstra's work.  I think he develops a shortest path algorithm quite
nicely.

Quote:
>         link (A,B,D) :-
               ^
>        and with the of what is the route from A to B and the total distance?

                      ^
Quote:
>             route 1(S,F,[S],R,D).

                     ^


Wed, 19 May 1993 07:51:00 GMT  
 general PROLOG questions

Quote:

>Works for any length list.

Since when has the fifth element of the empty list been []?????

Please, don't encourage cs211 students to post their assignments all
over the world, on four different newsgroups!  Mr. s65 should be flamed,
not helped (luckily your code was almost as bad as mine:-).  See my
answer to Question 1.

        lee



Wed, 19 May 1993 04:47:00 GMT  
 general PROLOG questions

Quote:

>What about:
>nth([],_,[]) :- !.
>nth([Item|List],1,Item) :- !.
>nth([H|T],N,Item) :- NN is N-1,nth(T,NN,Item).

>Works for any length list. We don't even have to know anything about listsize!

Richard O'Keefe's temporary absence from the net is sorely felt in cases
like this one. I'll try my inadequate best to replace him here (-:). Anyway,
the short example above suffers from several problems:

1. Unnecessary cuts: the cut in the first clause is not needed, since its head
is incompatible with those of the other to clauses, and in the intended use of
the predicate the first two arguments are instantiated.

2. Unclear specification, datatype incoherence: the commonsensical definition
of nth has no solutions for the empty list, so the first clause should not
be there. Furthermore, using [] as some kind of 'null' value is a bad idea.
[] should be used only for the empty list (I'm becoming more and more taken
by strong typing...). In the example above, confusion would follow it the
Item returned from one of the lower clauses were [] (it can be a list element
too!)

3. The program will loop if called with N < 1 (a standard programming
error not restricted to Prolog)

4. In predicates where one argument is used to select a component of another
argument, it is better style (as well as more efficient in most Prolog systems)
to have the selector as the first argument.

Here's a cleaner version

nth(1, [Elem|_], Elem).
nth(I, [_|Rest], Elem) :-
   I > 0,
   J is I - 1,
   nth(J, Rest, Elem).

If your Prolog leaves an extraneous choice point when succeeding at the
base case (as unfortunately most will), a cut may be used for efficiency

nth(1, [Elem|_], Elem) :- !.
nth(I, [_|Rest], Elem) :-
   I > 0,
   J is I - 1,
   nth(J, Rest, Elem).

or a conditional used instead.

And now a plea: please apply simple style checks like the above to your
Prolog code. It pays off in efficency, robustness and clarity. Programming
(and logic programming in particular) will not be respected as an
engineering discipline (as opposed to a black art) if simple principles
of quality keep being ignored.

Fernando Pereira
AT&T Bell Laboratories
Murray Hill, NJ



Wed, 19 May 1993 06:43:00 GMT  
 general PROLOG questions

Quote:
>nth(1, [Elem|_], Elem).
>nth(I, [_|Rest], Elem) :-
>   I > 0,
>   J is I - 1,
>   nth(J, Rest, Elem).

>If your Prolog leaves an extraneous choice point when succeeding at the
>base case (as unfortunately most will), a cut may be used for efficiency

If the I > 0 is changed to I > 1 (as was probably meant) its easy to detect
that no choicepoint is needed.

Can anybody's prolog compiler/analyser detect this without the change?

Andrew Taylor



Wed, 19 May 1993 22:54:00 GMT  
 general PROLOG questions

Quote:

>Here's a cleaner version

>nth(1, [Elem|_], Elem).
>nth(I, [_|Rest], Elem) :-
>   I > 0,
>   J is I - 1,
>   nth(J, Rest, Elem).

But then, like Mr. Naish says, what would Mr. Bush do if the code is perfect?
I'm sure Mr. Bush is intelligent enough to question the validity of the given
codes, discovers that it does not work, try to figure out what's going on, and
may be learn a concept or two about Prolog programming, which is, I trust the
purpose of the exercies, and not how to write the nth function in Prolog. But
then, what am I to say, I could be wrong (about the purpose of the exercies or
Mr. Bush's intelligence, or both)! We'll leave the purpose of assignments
debate to comp.edu and Mr. Bush's intelligence to ... ?
--------------------------------------------------




Wed, 19 May 1993 04:50:00 GMT  
 general PROLOG questions
This all looks like fairly standard Prolog stuff which could be
found in any introductory text book to the language. You
shouldn't need to post worldwide to get the answers. If you are
too lazy to go to the library and look it up, try asking a few
people around your department for help.

It may be that you are in a place where there are no Prolog
experts and not much guidance to the language. If so, then I
think it would be legitimate to ask for help over the net, but
your posting didn't state that to be the case.



Wed, 19 May 1993 17:20:00 GMT  
 general PROLOG questions
Hey, what's wrong with this as a solution?

nth0(Num,List,Item):-
    append(X,[Item|_],List),
    length(X,Num).

Of course the obvious answer is why bother, since it's in the library.

--
------------------------------------------------------------------------
Another Gruntpig production, in association with the Rat Lab Steamworks.

Bigamy is defined as having one wife too many.
Monogamy is defined the same way.
-Oscar Wilde



Wed, 19 May 1993 22:35:00 GMT  
 general PROLOG questions

Quote:
Huntbach) writes:
> ...
> It may be that you are in a place where there are no Prolog
> experts and not much guidance to the language. If so, then I
> think it would be legitimate to ask for help over the net, but
> your posting didn't state that to be the case.

One assumes there must be *some* Prolog expertise in the place --
otherwise, who's teaching the course this guy is taking?  It seems to me
that either:
  (i) the course instructor has not told them much about Prolog nor
provided            sources from which they can fiund information, or
 (ii) this student is trying take the easy way out and get someone in
some other        part of the world to do the work for him -- in which
case Lee's help is           just the sort he deserves!

Then again, maybe he's really overawed by having the ability to talk to
the rest of the world and didn't have anything else to say to us.



Wed, 19 May 1993 22:08:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. General prolog question

2. General Questions about Prolog and Logic Problems (truth Tables)

3. General PROLOG programming

4. general syntax description of prolog

5. Looking for source code for Prolog (general)

6. Comments wanted on SICStus (and prolog in general) under HP-UX

7. Prolog/V Specific and General

8. newbie prolog question (sorting in prolog)

9. Prolog/Lisp vs other languages (was Simple Prolog Question)

10. Questions: Bushy trees in Prolog and C functions from/to Prolog

11. stupid prolog question: cat(1) in prolog?

12. another General publicly calling to drop Ada (drop the General instead :-)

 

 
Powered by phpBB® Forum Software