prologmaplist

Is there a better method than making a new rule to change variable order for maplist?


I have written a program that defines a relation between a 'full' data table a set of sets of column headers (features), an aggregate function and the aggregate table.

An example query :

 ?- data(D), fulltable_aggfunction_sets_aggtable(D,mean,[[a,b,c],[d,e,f]],AggTable), 
print_data(D),print_data(AggTable).

[a,b,c,d,e,f,g,class] 
[1,1,1,1,1,1,1,0] 
[2,3,4,5,4,2,1,0] 
[3,1,3,4,6,7,8,1] 
[1,2,3,6,1,1,2,1] 

[feature(1,mean,[a,b,c]),feature(2,mean,[d,e,f])] 
[1,1] 
[3,3.6666666666666665] 
[2.3333333333333335,5.666666666666667] 
[2,2.6666666666666665] 

D = [[a, b, c, d, e, f, g, class], [1, 1, 1, 1, 1, 1, 1|...], [2, 3, 4, 5, 4, 2|...], [3, 1, 3, 4, 6|...], [1, 2, 3, 6|...]],
AggTable = [[feature(1, mean, [a, b, c]), feature(2, mean, [d, e, f])], [1, 1], [3, 3.6666666666666665], [2.3333333333333335, 5.666666666666667], [2, 2.6666666666666665]] 

The following is my code:

fulltable_aggfunction_sets_aggtable(Full,Func,Sets,Aggtable):-
 Full =[Features|Data],
 flist_sets_indexs(Features,Sets,Indexs),
 maplist(indexs_flist_sets(Indexs),Data,Datasplits),
 maplist(aggfun_listoflists_values(Func),Datasplits,AggData),
 list_indexes(Sets,SetIndex),
 maplist(name_set_id_feature(Func),Sets,SetIndex,FeatureNames),
 append([FeatureNames],AggData,Aggtable).

name_set_id_feature(Func,Set,Id,feature(Id,Func,Set)).

list_indexes(List,Indexes):-
 findall(I,nth1(I,List,_),Indexes).

aggfunc_list_value(sum,List,Value):-
 sumlist(List,Value).

aggfunc_list_value(mean,List,Value):-
 sumlist(List,Sum),
 length(List,L),
 Value is Sum/L.

aggfun_listoflists_values(Fun,ListsofLists,Values):-
 maplist(aggfunc_list_value(Fun),ListsofLists,Values).

my_nth0(List,Elem,I):- nth0(I,List,Elem).

indexs_flist_sets(I,F,S):-flist_sets_indexs(F,S,I).

flist_sets_indexs(Features,Sets,Indexs):-
 maplist(flist_set_indexes(Features),Sets,Indexs).

flist_set_indexes(Features,Set,Indexs):-
 maplist(my_nth0(Features),Set,Indexs).

%aux
print_data(Data_set):-
 maplist(print_line,Data_set).

print_line(Data_line):-
 format("~w ~n",[Data_line]).

data(Data):-
 Data =[[a,b,c,d,e,f,g,class],
       [1,1,1,1,1,1,1,0],
       [2,3,4,5,4,2,1,0],
       [3,1,3,4,6,7,8,1],
       [1,2,3,6,1,1,2,1]].

In my code I have these two lines which just reorder arguments so that I can pass these rules into a maplist.

my_nth0(List,Elem,I):- nth0(I,List,Elem).

indexs_flist_sets(I,F,S):-flist_sets_indexs(F,S,I).

Is there a better way to do this? How can I use maplist in this case so as not to have to define these rules?


Solution

  • in SWI-Prolog there are library(yall) and library(lambda).

    Using library(yall), for instance

    flist_set_indexes(Features,Set,Indexs):-
     maplist({Features}/[Elem,I]>>nth0(I,Features,Elem),Set,Indexs).
    
    print_data(Data_set):-
     maplist([Data_line]>>format("~w ~n",[Data_line]),Data_set).
    

    while using the second

    :- use_module(library(lambda)).
    
    flist_set_indexes(Features,Set,Indexs):-
     maplist(\Elem^I^nth0(I,Features,Elem),Set,Indexs).
    
    %aux
    print_data(Data_set):-
     maplist(\Data_line^format("~w ~n",[Data_line]),Data_set).
    

    Libraries have similar functionalities. but note that yall requires a 'capture' of Features. library(lambda) is available after

    ?- pack_install(lambda).
    

    while library(yall) is autoloaded