Changeset 4593 for trunk/library

Show
Ignore:
Timestamp:
11/18/08 03:37:21 (7 weeks ago)
Author:
pmoura
Message:

Added predicates included/3, excluded/3, fold_left/4, and fold_right/4 to the library entities "metap" and "meta" (requested by Paul Crocker). Renamed the predicates filter/3 and succeeds/2 to included/3 and map/2, respectively (older names are still available using aliases in "meta").

Location:
trunk/library
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/library/meta.lgt

    r4485 r4593  
    44 
    55    :- info([ 
    6         version is 2.2, 
    7         date is 2008/10/5, 
     6        version is 2.3, 
     7        date is 2008/11/18, 
    88        author is 'Paulo Moura', 
    99        comment is 'Some useful meta-predicates.']). 
     10 
     11    :- alias(metap, map/2, succeeds/2). 
     12    :- alias(metap, include/3, filter/3). 
    1013 
    1114    callable(Term) :- 
     
    1417        atom(Functor). 
    1518 
    16     :- meta_predicate(filter(1, *, *)). 
    17     filter(_, [], []) :- !. 
    18     filter(Closure, [Arg| Args], In) :- 
     19    :- meta_predicate(include(1, *, *)). 
     20    include(_, [], []) :- !. 
     21    include(Closure, [Arg| Args], Included) :- 
    1922        (   call(Closure, Arg) -> 
    20             In = [Arg| RIn] 
    21         ;   In = RIn 
     23            Included = [Arg| Rest] 
     24        ;   Included = Rest 
    2225        ), 
    23         filter(Closure, Args, RIn). 
     26        include(Closure, Args, Rest). 
     27 
     28    :- meta_predicate(exclude(1, *, *)). 
     29    exclude(_, [], []) :- !. 
     30    exclude(Closure, [Arg| Args], Excluded) :- 
     31        (   call(Closure, Arg) -> 
     32            Excluded = Rest 
     33        ;   Excluded = [Arg| Rest] 
     34        ), 
     35        exclude(Closure, Args, Rest). 
    2436 
    2537    :- meta_predicate(partition(1, *, *, *)). 
    2638    partition(_, [], [], []) :- !. 
    27     partition(Closure, [Arg| Args], In, Out) :- 
     39    partition(Closure, [Arg| Args], Included, Excluded) :- 
    2840        (   call(Closure, Arg) -> 
    29             In = [Arg| RIn], 
    30             Out = ROut 
    31         ;   In = RIn, 
    32             Out = [Arg| ROut] 
     41            Included = [Arg| RestIncluded], 
     42            Excluded = RestExcluded 
     43        ;   Included = RestIncluded, 
     44            Excluded = [Arg| RestExcluded] 
    3345        ), 
    34         partition(Closure, Args, RIn, ROut). 
     46        partition(Closure, Args, RestIncluded, RestExcluded). 
    3547 
    3648    :- meta_predicate(ignore(::)). 
     
    4052        ;   true 
    4153        ). 
     54 
     55    :- meta_predicate(fold_left(3, *, *, *)). 
     56    fold_left(_, Result, [], Result). 
     57    fold_left(Closure, Acc, [Arg| Args], Result) :- 
     58        call(Closure, Acc, Arg, Acc2), 
     59        fold_left(Closure, Acc2, Args, Result). 
     60 
     61    :- meta_predicate(fold_right(3, *, *, *)). 
     62    fold_right(_, Result, [], Result). 
     63    fold_right(Closure, Acc, [Arg| Args], Result) :- 
     64        fold_right(Closure, Acc, Args, Acc2), 
     65        call(Closure, Arg, Acc2, Result). 
     66 
     67    :- meta_predicate(map(1, *)). 
     68    map(_, []). 
     69    map(Closure, [Head| Tail]) :- 
     70        call(Closure, Head), 
     71        map(Closure, Tail). 
    4272 
    4373    :- meta_predicate(map(2, *, *)). 
     
    5989        map(Closure, As, Bs, Cs, Ds). 
    6090 
    61     :- meta_predicate(succeeds(1, *)). 
    62     succeeds(_, []). 
    63     succeeds(Closure, [Head| Tail]) :- 
    64         call(Closure, Head), 
    65         succeeds(Closure, Tail). 
    66  
    6791:- end_object. 
  • trunk/library/metap.lgt

    r4485 r4593  
    33 
    44    :- info([ 
    5         version is 3.1, 
    6         date is 2008/10/5, 
     5        version is 3.2, 
     6        date is 2008/11/18, 
    77        author is 'Paulo Moura', 
    88        comment is 'Useful meta-predicates protocol.']). 
     
    1414        argnames is ['Term']]). 
    1515 
    16     :- public(filter/3). 
    17     :- meta_predicate(filter(1, *, *)). 
    18     :- mode(filter(+callable, +list, -list), one). 
    19     :- info(filter/3, [ 
     16    :- public(include/3). 
     17    :- meta_predicate(include(1, *, *)). 
     18    :- mode(include(+callable, +list, -list), one). 
     19    :- info(include/3, [ 
    2020        comment is 'Returns a list of all list elements that satisfy a predicate.', 
    21         argnames is ['Closure', 'List', 'In']]). 
     21        argnames is ['Closure', 'List', 'Included']]). 
     22 
     23    :- public(exclude/3). 
     24    :- meta_predicate(exclude(1, *, *)). 
     25    :- mode(exclude(+callable, +list, -list), one). 
     26    :- info(exclude/3, [ 
     27        comment is 'Returns a list of all list elements that fail to satisfy a predicate.', 
     28        argnames is ['Closure', 'List', 'Excluded']]). 
    2229 
    2330    :- public(partition/4). 
     
    2633    :- info(partition/4, [ 
    2734        comment is 'Partition a list of elements in two lists using a predicate.', 
    28         argnames is ['Closure', 'List', 'In', 'Out']]). 
     35        argnames is ['Closure', 'List', 'Included', 'Excluded']]). 
    2936 
    3037    :- public(ignore/1). 
     
    3441        comment is 'Calls Goal once but always succeeds, even if Goal fails.', 
    3542        argnames is ['Goal']]). 
     43 
     44    :- public(fold_left/4). 
     45    :- meta_predicate(fold_left(3, *, *, *)). 
     46    :- mode(fold_left(+callable, ?term, +list, ?term), zero_or_more). 
     47    :- info(fold_left/4, [ 
     48        comment is 'List folding (left associative).', 
     49        argnames is ['Closure', 'Accumulator', 'List', 'Result']]). 
     50 
     51    :- public(fold_right/4). 
     52    :- meta_predicate(fold_right(3, *, *, *)). 
     53    :- mode(fold_right(+callable, ?term, +list, ?term), zero_or_more). 
     54    :- info(fold_right/4, [ 
     55        comment is 'List folding (right associative).', 
     56        argnames is ['Closure', 'Accumulator', 'List', 'Result']]). 
     57 
     58    :- public(map/2). 
     59    :- meta_predicate(map(1, *)). 
     60    :- mode(map(+callable, ?list), zero_or_more). 
     61    :- info(map/2, [ 
     62        comment is 'True if the predicate succeeds for each list element.', 
     63        argnames is ['Closure', 'List']]). 
    3664 
    3765    :- public(map/3). 
     
    5684        argnames is ['Closure', 'List1', 'List2', 'List3', 'List4']]). 
    5785 
    58     :- public(succeeds/2). 
    59     :- meta_predicate(succeeds(1, *)). 
    60     :- mode(succeeds(+callable, +list), zero_or_more). 
    61     :- info(succeeds/2, [ 
    62         comment is 'True if the predicate succeeds for each list element.', 
    63         argnames is ['Closure', 'List']]). 
    64  
    6586:- end_protocol.