-1

What is the difference between count/3 and for/3 in prolog ? and how to use for-loop with decrementation -1 like (for(I,9,0,-1))?

  • for example :

    Dec=-2, Min_bound=0, Max_bound=9
    Result => 9,7,5,3,1

Ans Piter
  • 573
  • 1
  • 5
  • 17
  • 2
    Neither `count/3` or `for/3` are standard (official or de facto) Prolog predicates. I suggest you tag your question with the Prolog system that you're using. But its documentation should provide the answer you're seeking. – Paulo Moura Dec 29 '15 at 14:21
  • i use sisctus-prolog – Ans Piter Dec 29 '15 at 15:36

2 Answers2

3

What is the difference between count/3 and for/3 in Prolog (ECLiPSe/SICStus)?

The difference is in the "mode", i.e. whether the Max argument is used as input (+) or output (-).

  • The template for(-I,+Min,+Max) is used for controlling the number of loop iterations. The value of Max must be known at the time you start the loop, i.e. it can be a number

    ?- ( for(I,1,3) do writeln(I) ).
    1
    2
    3
    

    or it can be an expression with instantiated variables

    ?- Next=4, ( for(I,1,Next-1) do writeln(I) ).
    1
    2
    3
    

    The termination condition is I >= Max.

  • The template count(-I,+From,-To) is used to count the number of iterations. At the time you start the loop, To would normally be an uninstantiated variable, which, at the end of the loop, is unified with the number of loop iterations. In such a setting, the number of iterations must be controlled by something else, e.g. the length of a list, as in this example:

    ?- ( foreach(X,[a,b,c]),count(I,1,N) do writeln(I-X) ).
    1 - a
    2 - b
    3 - c
    
    N = 3
    Yes (0.01s cpu)
    
  • Because Prolog of course allows you to use a value instead of an uninstantiated variable, you can use the template count(-I,+From,+To), but you should be aware that the termination condition is I=To, not I>=To.

The do-loop construct, to which the for/3 and count/3 iterators belong, is an attempt to provide compact notation for the common case of iterative recursion, and to do so in a way reminiscent of procedural loop notation. The unique properties of logical variables and the generally bi-directional nature of Prolog lead to some interesting features, e.g. using the foreach/2 iterator you can use the same loop code to either traverse an existing list or to construct a new list. In the present case of iteration over numbers, this symmetry sadly isn't achieved, hence the two different iterators that you ask about.

How to use for-loop with decrementation

Nothing special, really:

    ?- ( for(I,9,0,-2) do writeln(I) ).
    9
    7
    5
    3
    1
    Yes (0.00s cpu)

or, for collecting the results in a list:

    ?- ( for(I,9,0,-2),foreach(I,Is) do true ).
    Is = [9, 7, 5, 3, 1]
    Yes (0.00s cpu)

Edit: It has been pointed out that the above does not work in SICStus (4.3.2), because the for/4 iterator is not supported. You can still do such loops using the general-purpose fromto/4 iterator, but you have to do some arithmetic yourself, e.g.

?- ( fromto(9,I,I1,-1) do I1 is I-2, writeln(I) ).
9
7
5
3
1
Yes (0.01s cpu)

For the general case, this gets a bit tricky, so that you may actually get clearer code using a recursive solution...

jschimpf
  • 4,904
  • 11
  • 24
  • Given your interpretation of `count`, an `uninstantiation_error` seems quite helpful in that situation. – false Dec 30 '15 at 16:35
  • @jschimpf the last result `( for(I,9,0,-2) do writeln(I) ).` is not with **SICStus-prolog**, it is with **TKEClipse** . – Ans Piter Dec 30 '15 at 17:28
  • @jschimpf where i find the library that contains the predicate **for/4** in the C: \ Program Files (x86) \ ECLiPSe 6.1 \ lib? – Ans Piter Dec 30 '15 at 17:53
  • You are right, I was running ECLiPSe and neglected to check with SICStus, sorry. Unfortunately, for/4 is not a predicate in itself (it is part of the do/2 predicate), so you can't simply copy it and add it to your SICStus. I'll edit my answer and add a workaround. – jschimpf Dec 30 '15 at 18:59
  • yeh but **how to do that ?** because i find for/3 and i amend it as follows ; `for1(Int, Int, _Lower,_Dec). for1(Int, Upper, Lower ,Dec) :- Lower < Upper, Next is Upper + Dec, for1(Int, Next, Lower,Dec).` but the problem remains unsolved due to **[DO]** – Ans Piter Dec 30 '15 at 20:38
2

First, your question is about do-loops, a control construct that is found in some systems. The current implementation in SICStus has quite erratic behaviour - in partiular w.r.t. count:

| ?- for(I,5,1), foreach(I,List) do true.
List = [] ? ;
no
| ?- count(I,5,1), foreach(I,List) do true.
**LOOPS**

The documentation is not clear about this looping. But from what I guess, count will always count upwards and expects that the Max value is legal ; and if not, it loops.

In any case, there are far too many such "features" in do-loops that are still unfixed since many years, that you may want to learn Prolog without do-loops first. In particular, consider higher-order constructs like maplist/2.. instead.

Community
  • 1
  • 1
false
  • 10,264
  • 13
  • 101
  • 209
  • thnx for your clarification, I use them because loops are almost the same principle as the other language like Java, C which I know but after a rigorous search I find this doc. web: [link](http://eclipseclp.org/doc/userman/umsroot023.html) with system TKECLiPse !! plz help me because I am at the crossroads and I am confused in choosing a system in which I implement my prj (SWI, SICS, GNU Prolog ...) @false – Ans Piter Dec 29 '15 at 18:43
  • 2
    @AnsPiter Why did you choose to do your project in Prolog in the first place? –  Dec 29 '15 at 23:29
  • 1
    prolog is a one of the best and fast language in which to express the semantics of other languages and it gives us both a flexible way to encode both the operational semantics of many high-level formalisms, and this is what I want to do... @ALL – Ans Piter Dec 30 '15 at 09:18
  • 1
    @AnsPiter: You cannot start using Prolog like that. It is much too different from other languages. – false Dec 30 '15 at 12:26
  • @false i have knowledge not too bad for semantics and programing language acquired from university and i think that your tips & your expertise can help me – Ans Piter Dec 30 '15 at 12:41