|
From: Vitor S. C. <vs...@us...> - 2008-07-16 10:45:41
|
Update of /cvsroot/yap/library In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv19009/library Modified Files: apply_macros.yap swi.yap Log Message: add extra versions to maplist and fix apply macros to handle predicates with the same aargument. Index: apply_macros.yap =================================================================== RCS file: /cvsroot/yap/library/apply_macros.yap,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- apply_macros.yap 11 Jul 2008 17:02:07 -0000 1.5 +++ apply_macros.yap 16 Jul 2008 10:45:47 -0000 1.6 @@ -14,7 +14,10 @@ :- module(apply_macros, [selectlist/3, checklist/2, + maplist/2, maplist/3, + maplist/4, + maplist/5, convlist/3, mapargs/3, sumargs/4, @@ -31,7 +34,10 @@ :- meta_predicate selectlist(:,+,-), checklist(:,+), + maplist(:,+), maplist(:,+,-), + maplist(:,+,+,-), + maplist(:,+,+,+,-), convlist(:,+,-), mapargs(:,+,-), mapargs_args(:,+,-,+), @@ -121,6 +127,15 @@ call(Pred, In), checklist(Pred, ListIn). +% maplist(Pred, OldList) +% succeeds when Pred(Old,New) succeeds for each corresponding +% Old in OldList, New in NewList. In InterLisp, this is MAPCAR. +% It is also MAP2C. Isn't bidirectionality wonderful? +maplist(_, []). +maplist(Pred, [In|ListIn]) :- + call(Pred, In), + maplist(Pred, ListIn). + % maplist(Pred, OldList, NewList) % succeeds when Pred(Old,New) succeeds for each corresponding % Old in OldList, New in NewList. In InterLisp, this is MAPCAR. @@ -130,6 +145,24 @@ call(Pred, In, Out), maplist(Pred, ListIn, ListOut). +% maplist(Pred, List1, List2, List3) +% succeeds when Pred(Old,New) succeeds for each corresponding +% Gi in Listi, New in NewList. In InterLisp, this is MAPCAR. +% It is also MAP2C. Isn't bidirectionality wonderful? +maplist(_, [], [], []). +maplist(Pred, [A1|L1], [A2|L2], [A3|L3]) :- + call(Pred, A1, A2, A3), + maplist(Pred, L1, L2, L3). + +% maplist(Pred, List1, List2, List3, List4) +% succeeds when Pred(Old,New) succeeds for each corresponding +% Gi in Listi, New in NewList. In InterLisp, this is MAPCAR. +% It is also MAP2C. Isn't bidirectionality wonderful? +maplist(_, [], [], [], []). +maplist(Pred, [A1|L1], [A2|L2], [A3|L3], [A4|L4]) :- + call(Pred, A1, A2, A3, A4), + maplist(Pred, L1, L2, L3, L4). + % convlist(Rewrite, OldList, NewList) % is a sort of hybrid of maplist/3 and sublist/3. % Each element of NewList is the image under Rewrite of some @@ -234,7 +267,7 @@ !, aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), % the new goal - pred_name(maplist, Proto, GoalName), + pred_name(maplist, 3, Proto, GoalName), append(MetaVars, [ListIn, ListOut], GoalArgs), Goal =.. [GoalName|GoalArgs], % the new predicate declaration @@ -253,7 +286,26 @@ !, aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), % the new goal - pred_name(checklist, Proto, GoalName), + pred_name(checklist, 2, Proto, GoalName), + append(MetaVars, [List], GoalArgs), + Goal =.. [GoalName|GoalArgs], + % the new predicate declaration + HeadPrefix =.. [GoalName|PredVars], + append_args(HeadPrefix, [[]], Base), + append_args(HeadPrefix, [[In|Ins]], RecursionHead), + append_args(Pred, [In], Apply), + append_args(HeadPrefix, [Ins], RecursiveCall), + compile_aux([ + Base, + (RecursionHead :- Apply, RecursiveCall) + ], Module). + +user:goal_expansion(maplist(Meta, List), Mod, Goal) :- + callable(Meta), + !, + aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), + % the new goal + pred_name(maplist, 2, Proto, GoalName), append(MetaVars, [List], GoalArgs), Goal =.. [GoalName|GoalArgs], % the new predicate declaration @@ -267,12 +319,50 @@ (RecursionHead :- Apply, RecursiveCall) ], Module). +user:goal_expansion(maplist(Meta, L1, L2, L3), Mod, Goal) :- + callable(Meta), + !, + aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), + % the new goal + pred_name(maplist, 4, Proto, GoalName), + append(MetaVars, [L1, L2, L3], GoalArgs), + Goal =.. [GoalName|GoalArgs], + % the new predicate declaration + HeadPrefix =.. [GoalName|PredVars], + append_args(HeadPrefix, [[], [], []], Base), + append_args(HeadPrefix, [[A1|A1s], [A2|A2s], [A3|A3s]], RecursionHead), + append_args(Pred, [A1, A2, A3], Apply), + append_args(HeadPrefix, [A1s, A2s, A3s], RecursiveCall), + compile_aux([ + Base, + (RecursionHead :- Apply, RecursiveCall) + ], Module). + +user:goal_expansion(maplist(Meta, L1, L2, L3, L4), Mod, Goal) :- + callable(Meta), + !, + aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), + % the new goal + pred_name(maplist, 5, Proto, GoalName), + append(MetaVars, [L1, L2, L3, L4], GoalArgs), + Goal =.. [GoalName|GoalArgs], + % the new predicate declaration + HeadPrefix =.. [GoalName|PredVars], + append_args(HeadPrefix, [[], [], [], []], Base), + append_args(HeadPrefix, [[A1|A1s], [A2|A2s], [A3|A3s], [A4|A4s]], RecursionHead), + append_args(Pred, [A1, A2, A3, A4], Apply), + append_args(HeadPrefix, [A1s, A2s, A3s, A4s], RecursiveCall), + compile_aux([ + Base, + (RecursionHead :- Apply, RecursiveCall) + ], Module). + user:goal_expansion(selectlist(Meta, ListIn, ListOut), Mod, Goal) :- callable(Meta), !, aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), % the new goal - pred_name(selectlist, Proto, GoalName), + pred_name(selectlist, 3, Proto, GoalName), append(MetaVars, [ListIn, ListOut], GoalArgs), Goal =.. [GoalName|GoalArgs], % the new predicate declaration @@ -294,7 +384,7 @@ !, aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), % the new goal - pred_name(include, Proto, GoalName), + pred_name(include, 3, Proto, GoalName), append(MetaVars, [ListIn, ListOut], GoalArgs), Goal =.. [GoalName|GoalArgs], % the new predicate declaration @@ -315,7 +405,7 @@ !, aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), % the new goal - pred_name(exclude, Proto, GoalName), + pred_name(exclude, 3, Proto, GoalName), append(MetaVars, [ListIn, ListOut], GoalArgs), Goal =.. [GoalName|GoalArgs], % the new predicate declaration @@ -336,7 +426,7 @@ !, aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), % the new goal - pred_name(partition, Proto, GoalName), + pred_name(partition, 4, Proto, GoalName), append(MetaVars, [ListIn, List1, List2], GoalArgs), Goal =.. [GoalName|GoalArgs], % the new predicate declaration @@ -357,7 +447,7 @@ !, aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), % the new goal - pred_name(partition2, Proto, GoalName), + pred_name(partition2, 5, Proto, GoalName), append(MetaVars, [ListIn, List1, List2, List3], GoalArgs), Goal =.. [GoalName|GoalArgs], % the new predicate declaration @@ -395,7 +485,7 @@ !, aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), % the new goal - pred_name(convlist, Proto, GoalName), + pred_name(convlist, 3, Proto, GoalName), append(MetaVars, [ListIn, ListOut], GoalArgs), Goal =.. [GoalName|GoalArgs], % the new predicate declaration @@ -416,7 +506,7 @@ !, aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), % the new goal - pred_name(sumlist, Proto, GoalName), + pred_name(sumlist, 4, Proto, GoalName), append(MetaVars, [List, AccIn, AccOut], GoalArgs), Goal =.. [GoalName|GoalArgs], % the new predicate declaration @@ -457,7 +547,7 @@ !, aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), % the new goal - pred_name(mapnodes, Proto, GoalName), + pred_name(mapnodes, 3, Proto, GoalName), append(MetaVars, [[InTerm], [OutTerm]], GoalArgs), Goal =.. [GoalName|GoalArgs], % the new predicate declaration @@ -487,7 +577,7 @@ !, aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), % the new goal - pred_name(checknodes, Proto, GoalName), + pred_name(checknodes, 2, Proto, GoalName), append(MetaVars, [[Term]], GoalArgs), Goal =.. [GoalName|GoalArgs], % the new predicate declaration @@ -515,7 +605,7 @@ !, aux_preds(Meta, MetaVars, Pred, PredVars, Proto, Mod, Module), % the new goal - pred_name(sumnodes, Proto, GoalName), + pred_name(sumnodes, 4, Proto, GoalName), append(MetaVars, [[Term], AccIn, AccOut], GoalArgs), Goal =.. [GoalName|GoalArgs], % the new predicate declaration @@ -583,7 +673,7 @@ aux_args([Arg|Args], MVars, [Arg|PArgs], PVars, [Arg|ProtoArgs]) :- aux_args(Args, MVars, PArgs, PVars, ProtoArgs). -pred_name(Macro, Proto, Name) :- - format_to_chars('\'~a(~w)\'.',[Macro, Proto], Chars), +pred_name(Macro, Arity, Proto, Name) :- + format_to_chars('\'~a(~d,~w)\'.',[Macro, Arity, Proto], Chars), read_from_chars(Chars, Name). Index: swi.yap =================================================================== RCS file: /cvsroot/yap/library/swi.yap,v retrieving revision 1.27 retrieving revision 1.28 diff -u -r1.27 -r1.28 --- swi.yap 16 Jul 2008 10:34:00 -0000 1.27 +++ swi.yap 16 Jul 2008 10:45:47 -0000 1.28 @@ -58,6 +58,10 @@ swi_predicate_table(_,maplist(X,Y),maplist,maplist(X,Y)). swi_predicate_table(_,maplist(X,Y,Z),maplist,maplist(X,Y,Z)). swi_predicate_table(_,maplist(X,Y,Z,W),maplist,maplist(X,Y,Z,W)). +swi_predicate_table(_,append(X,Y),lists,append(X,Y)). +swi_predicate_table(_,append(X,Y,Z),lists,append(X,Y,Z)). +swi_predicate_table(_,member(X,Y),lists,member(X,Y)). +swi_predicate_table(_,nextto(X,Y,Z),lists,nextto(X,Y,Z)). swi_predicate_table(_,is_list(X),lists,is_list(X)). swi_predicate_table(_,min_list(X,Y),lists,min_list(X,Y)). swi_predicate_table(_,nth(X,Y,Z),lists,nth(X,Y,Z)). @@ -65,9 +69,6 @@ swi_predicate_table(_,nth1(X,Y,Z),lists,nth(X,Y,Z)). swi_predicate_table(_,memberchk(X,Y),lists,memberchk(X,Y)). swi_predicate_table(_,flatten(X,Y),lists,flatten(X,Y)). -swi_predicate_table(_,member(X,Y),lists,member(X,Y)). -swi_predicate_table(_,append(X,Y),lists,append(X,Y)). -swi_predicate_table(_,append(X,Y,Z),lists,append(X,Y,Z)). swi_predicate_table(_,select(X,Y,Z),lists,select(X,Y,Z)). swi_predicate_table(_,hash_term(X,Y),terms,term_hash(X,Y)). swi_predicate_table(_,term_hash(X,Y),terms,term_hash(X,Y)). |