Changeset 4094 for trunk/library

Show
Ignore:
Timestamp:
02/16/08 03:54:30 (11 months ago)
Author:
pmoura
Message:

Added forto/5 and fordownto/5 meta-predicates to the library object "loop".

Location:
trunk/library
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/library/loop.lgt

    r4090 r4094  
    66        version is 1.2, 
    77        author is 'Paulo Moura', 
    8         date is 2008/2/15, 
     8        date is 2008/2/16, 
    99        comment is 'Loop control structures predicates.']). 
    1010 
     
    3131        foreach_inv(List, Count, Goal). 
    3232 
     33    :- meta_predicate(forto_aux(*, *, *, *, ::)). 
     34    forto_aux(Count, First, Last, Increment, Goal) :- 
     35        (   First =< Last -> 
     36            \+ \+ (Count = First, call(Goal)), 
     37            Next is First + Increment, 
     38            forto_aux(Count, Next, Last, Increment, Goal) 
     39        ;   true 
     40        ). 
     41 
    3342    :- meta_predicate(forto(*, *, ::)). 
    3443    forto(FirstExp, LastExp, Goal) :- 
    3544        First is FirstExp, 
    3645        Last is LastExp, 
    37         (   First =< Last -> 
    38             \+ \+ call(Goal), 
    39             Next is First + 1, 
    40             forto(Next, Last, Goal) 
    41         ;   true 
    42         ). 
     46        forto_aux(_, First, Last, 1, Goal). 
    4347 
    4448    :- meta_predicate(forto(*, *, *, ::)). 
     
    4650        First is FirstExp, 
    4751        Last is LastExp, 
    48         (   First =< Last -> 
     52        forto_aux(Count, First, Last, 1, Goal). 
     53 
     54    :- meta_predicate(forto(*, *, *, *, ::)). 
     55    forto(Count, FirstExp, LastExp, IncrementExp, Goal) :- 
     56        First is FirstExp, 
     57        Last is LastExp, 
     58        Increment is abs(IncrementExp), 
     59        forto_aux(Count, First, Last, Increment, Goal). 
     60 
     61    :- meta_predicate(fordownto_aux(*, *, *, *, ::)). 
     62    fordownto_aux(Count, First, Last, Decrement, Goal) :- 
     63        (   First >= Last -> 
    4964            \+ \+ (Count = First, call(Goal)), 
    50             Next is First + 1, 
    51             forto(Count, Next, Last, Goal) 
     65            Next is First - Decrement, 
     66            fordownto_aux(Count, Next, Last, Decrement, Goal) 
    5267        ;   true 
    5368        ). 
     
    5772        First is FirstExp, 
    5873        Last is LastExp, 
    59         (   First >= Last -> 
    60             \+ \+ call(Goal), 
    61             Next is First - 1, 
    62             fordownto(Next, Last, Goal) 
    63         ;   true 
    64         ). 
     74        fordownto_aux(_, First, Last, 1, Goal). 
    6575 
    6676    :- meta_predicate(fordownto(*, *, *, ::)). 
     
    6878        First is FirstExp, 
    6979        Last is LastExp, 
    70         (   First >= Last -> 
    71             \+ \+ (Count = First, call(Goal)), 
    72             Next is First - 1, 
    73             fordownto(Count, Next, Last, Goal) 
    74         ;   true 
    75         ). 
     80        fordownto_aux(Count, First, Last, 1, Goal). 
     81 
     82    :- meta_predicate(fordownto(*, *, *, *, ::)). 
     83    fordownto(Count, FirstExp, LastExp, DecrementExp, Goal) :- 
     84        First is FirstExp, 
     85        Last is LastExp, 
     86        Decrement is abs(DecrementExp), 
     87        fordownto_aux(Count, First, Last, Decrement, Goal). 
    7688 
    7789:- end_object. 
  • trunk/library/loopp.lgt

    r4091 r4094  
    55        version is 1.2, 
    66        author is 'Paulo Moura', 
    7         date is 2008/2/15, 
     7        date is 2008/2/16, 
    88        comment is 'Loop control constructs protocol.']). 
     9 
     10 
     11    :- public(whiledo/2). 
     12    :- meta_predicate(whiledo(::, ::)). 
     13    :- mode(whiledo(+callable, @callable), zero_or_one). 
     14    :- info(whiledo/2, [ 
     15        comment is 'While Condition is true do Action.', 
     16        argnames is ['Condition', 'Action']]). 
    917 
    1018    :- public(dowhile/2). 
     
    1725    :- public(foreach/3). 
    1826    :- meta_predicate(foreach(*, *, ::)). 
    19     :- mode(foreach(-term, +list(term), @callable), zero_or_one). 
     27    :- mode(foreach(@var, +list(term), @callable), zero_or_one). 
    2028    :- info(foreach/3, [ 
    21         comment is 'For each element Count in List call Goal.', 
    22         argnames is ['Count', 'List', 'Goal']]). 
     29        comment is 'For each element Element in List call Goal.', 
     30        argnames is ['Element', 'List', 'Goal']]). 
    2331 
    2432    :- public(forto/3). 
    2533    :- meta_predicate(forto(*, *, ::)). 
    26     :- mode(forto(+integer, +integer, @callable), zero_or_one). 
     34    :- mode(forto(+number, +number, @callable), zero_or_one). 
    2735    :- info(forto/3, [ 
    28         comment is 'Counting from First to Last call Goal. For convenience and clarity, First and Last can be integer expressions.', 
     36        comment is 'Call Goal counting up from First to Last. Increment is 1. For convenience and clarity, First and Last can be arithmetic expressions.', 
    2937        argnames is ['First', 'Last', 'Goal']]). 
    3038 
    3139    :- public(forto/4). 
    3240    :- meta_predicate(forto(*, *, *, ::)). 
    33     :- mode(forto(-integer, +integer, +integer, @callable), zero_or_one). 
     41    :- mode(forto(@var, +number, +number, @callable), zero_or_one). 
    3442    :- info(forto/4, [ 
    35         comment is 'Call Goal counting from First to Last and instantiating Count to each successive value. For convenience and clarity, First and Last can be integer expressions.', 
     43        comment is 'Call Goal counting up from First to Last and instantiating Count to each successive value. Increment is 1. For convenience and clarity, First and Last can be arithmetic expressions.', 
    3644        argnames is ['Count', 'First', 'Last', 'Goal']]). 
     45 
     46    :- public(forto/5). 
     47    :- meta_predicate(forto(*, *, *, *, ::)). 
     48    :- mode(forto(@var, +number, +number, +number, @callable), zero_or_one). 
     49    :- info(forto/5, [ 
     50        comment is 'Call Goal counting up from First to Last and instantiating Count to each successive value. For convenience and clarity, First, Last, and Increment can be arithmetic expressions. The absolute value of Increment is used.', 
     51        argnames is ['Count', 'First', 'Last', 'Increment', 'Goal']]). 
    3752 
    3853    :- public(fordownto/3). 
    3954    :- meta_predicate(fordownto(*, *, ::)). 
    40     :- mode(fordownto(+integer, +integer, @callable), zero_or_one). 
     55    :- mode(fordownto(+number, +number, @callable), zero_or_one). 
    4156    :- info(fordownto/3, [ 
    42         comment is 'Counting from First to Last call Goal. For convenience and clarity, First and Last can be integer expressions.', 
     57        comment is 'Call Goal counting down from First to Last. Decrement is 1. For convenience and clarity, First and Last can be arithmetic expressions.', 
    4358        argnames is ['First', 'Last', 'Goal']]). 
    4459 
    4560    :- public(fordownto/4). 
    4661    :- meta_predicate(fordownto(*, *, *, ::)). 
    47     :- mode(fordownto(-integer, +integer, +integer, @callable), zero_or_one). 
     62    :- mode(fordownto(@var, +number, +number, @callable), zero_or_one). 
    4863    :- info(fordownto/4, [ 
    49         comment is 'Call Goal counting from First to Last and instantiating Count to each successive value. For convenience and clarity, First and Last can be integer expressions.', 
     64        comment is 'Call Goal counting down from First to Last and instantiating Count to each successive value. Decrement is 1. For convenience and clarity, First and Last can be arithmetic expressions.', 
    5065        argnames is ['Count', 'First', 'Last', 'Goal']]). 
    5166 
    52     :- public(whiledo/2). 
    53     :- meta_predicate(whiledo(::, ::)). 
    54     :- mode(whiledo(+callable, @callable), zero_or_one). 
    55     :- info(whiledo/2, [ 
    56         comment is 'While Condition is true do Action.', 
    57         argnames is ['Condition', 'Action']]). 
     67    :- public(fordownto/5). 
     68    :- meta_predicate(fordownto(*, *, *, *, ::)). 
     69    :- mode(fordownto(@var, +number, +number, +number, @callable), zero_or_one). 
     70    :- info(fordownto/5, [ 
     71        comment is 'Call Goal counting down from First to Last and instantiating Count to each successive value. For convenience and clarity, First, Last, and Decrement can be arithmetic expressions. The absolute value of Decrement is used.', 
     72        argnames is ['Count', 'First', 'Last', 'Decrement', 'Goal']]). 
    5873 
    5974:- end_protocol.