Advertisement
logicmoo

pSET.pro

Aug 16th, 2017
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 6.19 KB | None | 0 0
  1. set2nat(Xs,N):-set2nat(Xs,0,N).
  2.  
  3. set2nat([],R,R).
  4. set2nat([X|Xs],R1,Rn):-R2 is R1+(1<<X),set2nat(Xs,R2,Rn).
  5.  
  6. hfs2nat(N,R):-default_ulimit(D),hfs2nat_(D,N,R).  
  7.  
  8. hfs2nat_(_,[],R):-!,R=0.
  9. hfs2nat_(Ulimit,N,R):-integer(N),N>0,N<Ulimit,!,R=N.
  10. hfs2nat_(Ulimit,Ts,R):-maplist(hfs2nat_(Ulimit),Ts,T),set2nat(T,R).
  11.  
  12. default_ulimit(1).
  13.  
  14. nat2set(N,Xs):-findall(X,nat2element(N,X),Xs).
  15.  
  16. nat2element(N,K):-nat2el(N,0,K).
  17.  
  18. nat2el(N,K1,Kn):-
  19.   N>0, B is /\(N,1), N1 is N>>1,
  20.   nat2more(B,N1,K1,Kn).
  21.  
  22. nat2more(1,_,K,K).
  23. nat2more(_,N,K1,Kn):-K2 is K1+1,nat2el(N,K2,Kn).
  24.  
  25. nat2hfs_(_,0,R):-!,R=[].
  26. nat2hfs_(Ulimit,N,R):-N<Ulimit,!,R=N.
  27. nat2hfs_(Ulimit,N,R):-nat2set(N,Ns),maplist(nat2hfs_(Ulimit),Ns,R).
  28.  
  29. nat2hfs(N,R):-default_ulimit(D),nat2hfs_(D,N,R).
  30.  
  31. nat(0).
  32. nat(N):-nat(N1),N is N1+1.
  33.  
  34. iterative_hfs_generator(HFS):-default_ulimit(D),hfs_with_urelements(D,HFS).
  35.  
  36. hfs_with_urelements(Ulimit,HFS):-nat(N),nat2hfs_(Ulimit,N,HFS).
  37.  
  38. all_subsets([],[[]]).
  39. all_subsets([X|Xs],Zss):-all_subsets(Xs,Yss),extend_subsets(Yss,X,Zss).
  40.  
  41. extend_subsets([],_,[]).
  42. extend_subsets([Ys|Yss],X,[Ys,[X|Ys]|Zss]):-extend_subsets(Yss,X,Zss).
  43.  
  44. hfs_generator(NewSet):-nat(N),hfs_level(N,NewSet).
  45.  
  46. hfs_level(N,NewSet):-N1 is N+1,
  47.   subsets_at_stage(N1,[],Hss1),subsets_at_stage(N,[],Hss),
  48.   member(NewSet,Hss1),not(member(NewSet,Hss)).
  49.  
  50. subsets_at_stage(0,X,X).
  51. subsets_at_stage(N,X,Xss):-N>0,N1 is N-1,
  52.   all_subsets(X,Xs),
  53.   subsets_at_stage(N1,Xs,Xss).
  54.  
  55. nat2hypergraph(N,Nss):-nat2set(N,Ns),maplist(nat2set,Ns,Nss).
  56.  
  57. hypergraph2nat(Nss,N):-maplist(set2nat,Nss,Ns),set2nat(Ns,N).
  58.  
  59. hfold(_,G,N,R):- integer(N),!,call(G,N,R).
  60. hfold(F,G,Xs,R):-maplist(hfold(F,G),Xs,Rs),call(F,Rs,R).
  61.  
  62. hsize(HFS,Size):-hfold(hsize_f,hsize_g,HFS,Size).
  63.  
  64. hsize_f(Xs,S):-sumlist(Xs,S1),S is S1+1.
  65. hsize_g(_,1).
  66.  
  67. gfold(_,G,Ulimit,_,N,R):- integer(N),N<Ulimit,!,call(G,N,R).
  68. gfold(F,G,Ulimit,T,N,R):-
  69.   call(T,N,TransformedN),
  70.   maplist(gfold(F,G,Ulimit,T),TransformedN,Rs),
  71.   call(F,Rs,R).
  72.  
  73. nfold(F,G,Ulimit,N,R):-gfold(F,G,Ulimit,nat2set,N,R).
  74. nfold1(F,G,N,R):-default_ulimit(D),nfold(F,G,D,N,R).
  75.  
  76. nsize(N,R):-default_ulimit(Ulimit),nsize(Ulimit,N,R).
  77. nsize(Ulimit,N,R):-nfold(hsize_f,hsize_g,Ulimit,N,R).
  78.  
  79. toNat(F,Hs,R):-maplist(hfs2nat,Hs,Ns),call(F,Ns,N),nat2hfs(N,R).
  80.  
  81. toNat1(F,X,R):-hfs2nat(X,N),call(F,N,NR),nat2hfs(NR,R).
  82.  
  83. toNat2(F,X,Y,R):-
  84.   hfs2nat(X,NX),hfs2nat(Y,NY),
  85.     call(F,NX,NY,NR),
  86.   nat2hfs(NR,R).
  87.  
  88. toHFS(F,Ns,N):-maplist(nat2hfs,Ns,Hs),call(F,Hs,H),hfs2nat(H,N).
  89.  
  90. toHFS1(F,X,R):-nat2hfs(X,N),call(F,N,NR),hfs2nat(NR,R).
  91.  
  92. toHFS2(F,X,Y,R):-
  93.   nat2hfs(X,NX),nat2hfs(Y,NY),
  94.   call(F,NX,NY,NR),hfs2nat(NR,R).
  95.  
  96. cantor_pair(K1,K2,P):-P is (((K1+K2)*(K1+K2+1))//2)+K2.
  97.  
  98. cantor_unpair(Z,K1,K2):-I is floor((sqrt(8*Z+1)-1)/2),
  99.   K1 is ((I*(3+I))//2)-Z,
  100.   K2 is Z-((I*(I+1))//2).
  101.  
  102. bitmerge_pair(A,B,P):-up0(A,X),up1(B,Y),P is X+Y.
  103.  
  104. bitmerge_unpair(P,A,B):-down0(P,A),down1(P,B).
  105.  
  106. even_up(A,R):-nat2element(A,X),E is X<<1,R is 1<<E.
  107. odd_up(A,R):-nat2element(A,X),E is 1+(X<<1),R is 1<<E.
  108. even_down(A,R):-nat2element(A,X),even(X),E is X>>1,R is 1<<E.
  109. odd_down(A,R):-nat2element(A,X),odd(X),E is (X>>1), R is 1<<E.
  110.  
  111. even(X):- 0 =:= /\(1,X).
  112. odd(X):- 1 =:= /\(1,X).
  113.  
  114. up0(A,P):-findall(R,even_up(A,R),Rs),sumlist(Rs,P).
  115. up1(A,P):-findall(R,odd_up(A,R),Rs),sumlist(Rs,P).
  116. down0(A,X):-findall(R,even_down(A,R),Rs),sumlist(Rs,X).
  117. down1(A,X):-findall(R,odd_down(A,R),Rs),sumlist(Rs,X).
  118.  
  119. bitmerge_pair(X-Y,Z):-bitmerge_pair(X,Y,Z).
  120.  
  121. bitmerge_unpair(Z,X-Y):-bitmerge_unpair(Z,X,Y).
  122.  
  123. nat_powset(N,PN):-toHFS1(all_subsets,N,PN).
  124.  
  125. %nat_powset_alt i = product (map (\k->1+(exp2 . exp2) k) (nat2set i))
  126.  
  127. hfs_ordinal(0,[]).
  128. hfs_ordinal(N,Os):-N>0,N1 is N-1,findall(I,between(0,N1,I),Is),
  129.   maplist(hfs_ordinal,Is,Os).
  130.  
  131. nat_ordinal(N,OrdN):-hfs_ordinal(N,H),hfs2nat(H,OrdN).
  132.  
  133. nat_choice_fun(N,CFN):-nat2set(N,Es),
  134.   maplist(nat2set,Es,Ess),maplist(choice_of_one,Ess,Hs),
  135.   maplist(bitmerge_pair,Es,Hs,Ps),set2nat(Ps,CFN).
  136.  
  137. choice_of_one([X|_],X).
  138.  
  139. nat2memb(N,XY):-default_ulimit(D),nat2memb(D,N,XY).
  140. nat2memb(Ulimit,N,X-Y):-nat2contains(Ulimit,N,Y-X).
  141.  
  142. nat2contains(N,XY):-default_ulimit(D),nat2contains(D,N,XY).
  143. nat2contains(Ulimit,N,E):-nat2element(N,X),
  144.   ( E=N-X
  145.   ; X>=Ulimit,nat2contains(Ulimit,X,E)
  146.   ).
  147.  
  148. nat2cdag(L,N,G):-
  149.   findall(E,nat2contains(L,N,E),Es),
  150.   vertices_edges_to_ugraph([],Es,G).
  151.  
  152. nat2mdag(L,N,G):-
  153.   findall(E,nat2memb(L,N,E),Es),
  154.   vertices_edges_to_ugraph([],Es,G).  
  155.  
  156. to_dag(N,NewG):-default_ulimit(Ulimit),to_dag(Ulimit,N,NewG).
  157.  
  158. to_dag(Ulimit,N,NewG):-
  159.   findall(E,nat2contains(Ulimit,N,E),Es),
  160.   vertices_edges_to_ugraph([],Es,G),
  161.   vertices(G,Rs),reverse(Rs,Vs),
  162.   empty_assoc(D),remap(Vs,0-D,_RVs,KD),remap(Es,KD,REs,_NewKD),
  163.   vertices_edges_to_ugraph([],REs,NewG).
  164.  
  165. remap(Xs,Rs):-empty_assoc(D),remap(Xs,0-D,Rs,_KD).
  166.  
  167. remap([],KD,[],KD).
  168. remap([X|Xs],KD1,[A|Rs],KD3):-integer(X),!,
  169.   assoc(X,A,KD1,KD2),
  170.   remap(Xs,KD2,Rs,KD3).
  171. remap([X-Y|Xs],KD1,[A-B|Rs],KD4):-
  172.   assoc(X,A,KD1,KD2),assoc(Y,B,KD2,KD3),
  173.   remap(Xs,KD3,Rs,KD4).
  174.  
  175. assoc(X,R,K-D,KD):-get_assoc(X,D,A),!,R=A,KD=K-D.
  176. assoc(X,K,K-D,NewK-NewD):-NewK is K+1,put_assoc(X,D,K,NewD).
  177.  
  178. from_dag(G,N):-vertices(G,[Root|_]),compute_decoration(G,Root,N).
  179.  
  180. compute_decoration(G,V,Ds):-neighbors(V,G,Es),compute_decorations(G,Es,Ds).
  181.  
  182. compute_decorations(_,[],0).
  183. compute_decorations(G,[E|Es],N):-
  184.   maplist(compute_decoration(G),[E|Es],Ds),
  185.   set2nat(Ds,N).
  186.  
  187. nat2digraph(N,G):-nat2set(N,Ns),
  188.   maplist(bitmerge_unpair,Ns,Ps),
  189.   vertices_edges_to_ugraph([],Ps,G).
  190.  
  191. digraph2nat(G,N):-edges(G,Ps),
  192.   maplist(bitmerge_pair,Ps,Ns),
  193.   set2nat(Ns,N).
  194.  
  195. transpose_nat(N,TN):-nat2digraph(N,G),transpose(G,T),digraph2nat(T,TN).
  196.  
  197. setShow(S):-gshow(S,"{,}"),nl.
  198.  
  199. gshow(0,[L,_C,R]):-put(L),put(R).
  200. gshow(N,_):-integer(N),N>0,!,write(N).
  201. gshow(Hs,[L,C,R]):-put(L),gshow_all(Hs,[L,C,R]),put(R).
  202.  
  203. gshow_all([],_).
  204. gshow_all([H],LCR):-gshow(H,LCR).
  205. gshow_all([H,G|Hs],[L,C,R]):-
  206.   gshow(H,[L,C,R]),
  207.   ([C]\=="~"->put(C);true),
  208.   gshow_all([G|Hs],[L,C,R]).
  209.  
  210. test:-
  211.   G=[0-[1, 2, 5, 6, 7], 1-[7, 9], 2-[7, 10], 3-[7],
  212.      4-[8, 10],5-[8, 9], 6- [8], 7-[9], 8-[9], 9-[10], 10-[]],
  213.   from_dag(G,N),
  214.   to_dag(N,G1),
  215.   from_dag(G1,N2),
  216.   write(N+G),nl,nl,
  217.   write(N2+G1),nl,nl.
  218.  
  219. c:-['pSET.pro'].
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement