Help a beginner in VIP!

Discussions related to Visual Prolog
Jacques.Lauwerys
Posts: 3
Joined: 13 Jan 2016 14:14

Help a beginner in VIP!

Unread post by Jacques.Lauwerys » 13 Jan 2016 14:24

Hi,

I am learning VIP (Visual Prolog Environment 7.5 Commercial Edition) with the help of the book VisualPrologBeginners_7.2.pdf.
Reproducing the folowing example (Chapter 10 Recursion, list and sorting) :

Code: Select all

implement main     open core clauses     run() :-         console::init,        counter:: count(1),        _X=stdIO::readChar(). end implement main   goal     console::runUtf8(main::run).

Code: Select all

class counter     open core   predicates count : (integer Number) procedure.   end class counter

Code: Select all

implement counter     open core clauses count(Number) :-     Number<11,     stdIO::write(Number),     stdIO::nl,     NextNumber = Number+1,     count(NextNumber).   count(_) :-     stdIO :: write("C'est la fin !\n"), !. end implement counter
I have the following errors

Error message :
Type Action Description Filename Path
e631 The predicate 'counter::count/1 (i)', which is declared as 'procedure', is actually 'multi' Counter.pro

replacing "count(_)" with "count(11) in the second clause :
Type Action Description Filename Path
e631 The predicate 'counter::count/1 (i)', which is declared as 'procedure', is actually 'nondeterm' Counter.pro

I don't understand the reason why.

Thank you in advance for help.

Jacques
Kind regards.
Jacques (Individual)

B.Hooijenga
VIP Member
Posts: 69
Joined: 11 Jul 2002 23:01

Unread post by B.Hooijenga » 13 Jan 2016 19:57

Hello Jacques,

This should be the correct code I think.

Code: Select all

implement main open core   clauses     run() :-         count(1),         _ = stdio::readChar().   class predicates     count : (integer Number) procedure.   clauses     count(Number) :-         Number < 11,         stdio::write(Number),         stdio::write("\n"),         NextNumber = Number+1,         count(NextNumber),         !.     count(_) :-         stdio::write("C'est la fin !\n"), !.   end implement main   goal     console::runUtf8(main::run).
Kind regards

Ben

Jacques.Lauwerys
Posts: 3
Joined: 13 Jan 2016 14:14

Unread post by Jacques.Lauwerys » 14 Jan 2016 8:16

Hello Ben,

Sorry, I tried it, but it does not solve the problem. I received the same error messages.
On the other hand, ( I am not sure because I can't test) the cut you introduce in te count(Number) clause, should it not have the effect of not running the count clause (11), which is not the intention.

Jacques
Kind regards.
Jacques (Individual)

B.Hooijenga
VIP Member
Posts: 69
Joined: 11 Jul 2002 23:01

Unread post by B.Hooijenga » 14 Jan 2016 11:06

Hello Jacques,


The cut has to be there for the following reason.

Without a cut predicate count()/1 has two possibilities:

Code: Select all

count(Number)
and

Code: Select all

count(_)
and is therefore multi.

With a cut the predicate count()/1 is a procedure which means there is only one solution.

If I may be so bold to make a remark:
In a recursive predicate the stop-condition is mostly written first. (with a cut!)

Your program looks then like this

Code: Select all

clauses      count(11) :-         stdIO :: write("C'est la fin !\n"),         !.      count(Number) :-         stdIO::write(Number, "\n"),         count( Number+1).        
Kind regards

Ben.

Jacques.Lauwerys
Posts: 3
Joined: 13 Jan 2016 14:14

Unread post by Jacques.Lauwerys » 14 Jan 2016 16:52

Hello Ben,

Thank You so much for the suggestions.
For sure You may make remarks ! I am only learning to use the software.
In fact, both suggested solutions work !
I had made a mistake in trying to implement the first suggestion.

Again thank you for your advice.
Kind regards.
Jacques (Individual)

Post Reply