[comp.lang.prolog] general PROLOG questions

cs211s65@uhccux.uhcc.hawaii.edu (Cs211s65) (12/06/89)

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!
       bush@uhccux.bitnet
       thanks.

bryden@vax1.acs.udel.EDU (Christopher F. Bryden) (12/07/89)

In article <> cs211s65@uhccux.uhcc.hawaii.edu (Cs211s65) writes:
}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
-- 
bryden@vax1.acs.udel.edu             ...{unidot,uunet}!cfg!udel!udccvax1!bryden
Copyright 1989 Chris Bryden permission granted for non-profit reproduction only
-------------------------------------------------------------------------------
                 expectation is the mother of disappointment

Tim_N_Roberts@cup.portal.com (12/08/89)

In <5631@uhccux.uhcc.hawaii.edu>,  cs211s65@uhccux.uhcc.hawaii.edu asks us
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.

TNR@cup.portal.com                |  I Survived The
...!sun!portal!cup.portal.com!tnr |  Great Quake of '89.

lee@munnari.oz.au (Lee Naish) (12/08/89)

In article <5631@uhccux.uhcc.hawaii.edu> cs211s65@uhccux.uhcc.hawaii.edu (Cs211s65) writes:
>        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]).

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

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

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

>         link (A,B,D) :-
               ^
>        and with the of what is the route from A to B and the total distance?
                      ^
>             route 1(S,F,[S],R,D).
                     ^

lee@munnari.oz.au (Lee Naish) (12/09/89)

In article <793@fs1.ee.ubc.ca> vincel@fs0.ee.ubc.ca (Vincent Li) writes:
>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

pereira@alice.UUCP (Fernando Pereira) (12/09/89)

In article <793@fs1.ee.ubc.ca> vincel@fs0.ee.ubc.ca writes:
>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
pereira@research.att.com

andrewt@cs.su.oz (Andrew Taylor) (12/11/89)

>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

vincel@fs0.ee.ubc.ca (vincent li) (12/11/89)

In article <10231@alice.UUCP> pereira@alice.UUCP () writes:
>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 ... ?
--------------------------------------------------
vincel@ee.ubc.ca                |-) WORK is a four letter word.
vinceli@triumfcl.bitnet         |-(

mmh@cs.qmc.ac.uk (Matthew Huntbach) (12/12/89)

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.

eiverson@nmsu.edu (Eric Iverson) (12/12/89)

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

lindsay@comp.vuw.ac.nz (Lindsay Groves) (12/13/89)

In article <1519@sequent.cs.qmc.ac.uk>, mmh@cs.qmc.ac.uk (Matthew
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.