set2nat(Xs,N):-set2nat(Xs,0,N). set2nat([],R,R). set2nat([X|Xs],R1,Rn):-R2 is R1+(1<0,N0, B is /\(N,1), N1 is N>>1, nat2more(B,N1,K1,Kn). nat2more(1,_,K,K). nat2more(_,N,K1,Kn):-K2 is K1+1,nat2el(N,K2,Kn). nat2hfs_(_,0,R):-!,R=[]. nat2hfs_(Ulimit,N,R):-N0,N1 is N-1, all_subsets(X,Xs), subsets_at_stage(N1,Xs,Xss). nat2hypergraph(N,Nss):-nat2set(N,Ns),maplist(nat2set,Ns,Nss). hypergraph2nat(Nss,N):-maplist(set2nat,Nss,Ns),set2nat(Ns,N). hfold(_,G,N,R):- integer(N),!,call(G,N,R). hfold(F,G,Xs,R):-maplist(hfold(F,G),Xs,Rs),call(F,Rs,R). hsize(HFS,Size):-hfold(hsize_f,hsize_g,HFS,Size). hsize_f(Xs,S):-sumlist(Xs,S1),S is S1+1. hsize_g(_,1). gfold(_,G,Ulimit,_,N,R):- integer(N),N>1,R is 1<>1), R is 1<1+(exp2 . exp2) k) (nat2set i)) hfs_ordinal(0,[]). hfs_ordinal(N,Os):-N>0,N1 is N-1,findall(I,between(0,N1,I),Is), maplist(hfs_ordinal,Is,Os). nat_ordinal(N,OrdN):-hfs_ordinal(N,H),hfs2nat(H,OrdN). nat_choice_fun(N,CFN):-nat2set(N,Es), maplist(nat2set,Es,Ess),maplist(choice_of_one,Ess,Hs), maplist(bitmerge_pair,Es,Hs,Ps),set2nat(Ps,CFN). choice_of_one([X|_],X). nat2memb(N,XY):-default_ulimit(D),nat2memb(D,N,XY). nat2memb(Ulimit,N,X-Y):-nat2contains(Ulimit,N,Y-X). nat2contains(N,XY):-default_ulimit(D),nat2contains(D,N,XY). nat2contains(Ulimit,N,E):-nat2element(N,X), ( E=N-X ; X>=Ulimit,nat2contains(Ulimit,X,E) ). nat2cdag(L,N,G):- findall(E,nat2contains(L,N,E),Es), vertices_edges_to_ugraph([],Es,G). nat2mdag(L,N,G):- findall(E,nat2memb(L,N,E),Es), vertices_edges_to_ugraph([],Es,G). to_dag(N,NewG):-default_ulimit(Ulimit),to_dag(Ulimit,N,NewG). to_dag(Ulimit,N,NewG):- findall(E,nat2contains(Ulimit,N,E),Es), vertices_edges_to_ugraph([],Es,G), vertices(G,Rs),reverse(Rs,Vs), empty_assoc(D),remap(Vs,0-D,_RVs,KD),remap(Es,KD,REs,_NewKD), vertices_edges_to_ugraph([],REs,NewG). remap(Xs,Rs):-empty_assoc(D),remap(Xs,0-D,Rs,_KD). remap([],KD,[],KD). remap([X|Xs],KD1,[A|Rs],KD3):-integer(X),!, assoc(X,A,KD1,KD2), remap(Xs,KD2,Rs,KD3). remap([X-Y|Xs],KD1,[A-B|Rs],KD4):- assoc(X,A,KD1,KD2),assoc(Y,B,KD2,KD3), remap(Xs,KD3,Rs,KD4). assoc(X,R,K-D,KD):-get_assoc(X,D,A),!,R=A,KD=K-D. assoc(X,K,K-D,NewK-NewD):-NewK is K+1,put_assoc(X,D,K,NewD). from_dag(G,N):-vertices(G,[Root|_]),compute_decoration(G,Root,N). compute_decoration(G,V,Ds):-neighbors(V,G,Es),compute_decorations(G,Es,Ds). compute_decorations(_,[],0). compute_decorations(G,[E|Es],N):- maplist(compute_decoration(G),[E|Es],Ds), set2nat(Ds,N). nat2digraph(N,G):-nat2set(N,Ns), maplist(bitmerge_unpair,Ns,Ps), vertices_edges_to_ugraph([],Ps,G). digraph2nat(G,N):-edges(G,Ps), maplist(bitmerge_pair,Ps,Ns), set2nat(Ns,N). transpose_nat(N,TN):-nat2digraph(N,G),transpose(G,T),digraph2nat(T,TN). setShow(S):-gshow(S,"{,}"),nl. gshow(0,[L,_C,R]):-put(L),put(R). gshow(N,_):-integer(N),N>0,!,write(N). gshow(Hs,[L,C,R]):-put(L),gshow_all(Hs,[L,C,R]),put(R). gshow_all([],_). gshow_all([H],LCR):-gshow(H,LCR). gshow_all([H,G|Hs],[L,C,R]):- gshow(H,[L,C,R]), ([C]\=="~"->put(C);true), gshow_all([G|Hs],[L,C,R]). test:- G=[0-[1, 2, 5, 6, 7], 1-[7, 9], 2-[7, 10], 3-[7], 4-[8, 10],5-[8, 9], 6- [8], 7-[9], 8-[9], 9-[10], 10-[]], from_dag(G,N), to_dag(N,G1), from_dag(G1,N2), write(N+G),nl,nl, write(N2+G1),nl,nl. c:-['pSET.pro'].