fork(1) download
  1. :- set_prolog_flag(verbose,silent).
  2. :- prompt(_, '').
  3. :- use_module(library(readutil)).
  4.  
  5. % set membership with non-pure builtin \==
  6. % to cut down duplicate answers as sets
  7. memb(X,[X|_]).
  8. memb(X,[Y|L]) :- X \== Y, memb(X,L).
  9.  
  10. % unify finite sets (i.e. set with [] at the end)
  11. unify_set(A,B) :- subset_of(A,B), subset_of(B,A).
  12.  
  13. subset_of([], _).
  14. subset_of([X|R],L) :- memb(X,L), subset_of(R,L).
  15.  
  16. % unify open ended sets with possibly uninstantiated variable tail at the end
  17. unify_oeset(A,B) :- ( var(A); var(B) ), !, A=B.
  18. unify_oeset(A,B) :-
  19. split_heads(A,Xs-T1), make_set(Xs,S1),
  20. split_heads(B,Ys-T2), make_set(Ys,S2),
  21. unify_oe_set(S1-T1, S2-T2).
  22.  
  23. make_set(L,S) :- setof(X,memb(X,L),S).
  24.  
  25. split_heads([],[]-[]).
  26. split_heads([X|T],[X]-T) :- var(T), !, true.
  27. split_heads([X|Xs],[X|Hs]-T) :- split_heads(Xs,Hs-T).
  28.  
  29. % helper function for unify_oeset (naive solution, possibly many duplication)
  30. unify_oe_set(Xs-T1,Ys-T2) :- T1==[], T2==[], unify_set(Xs,Ys).
  31. unify_oe_set(Xs-T1,Ys-T2) :- T1==[], subset_of(Ys,Xs), T2=Xs.
  32. unify_oe_set(Xs-T1,Ys-T2) :- T2==[], subset_of(Xs,Ys), T1=Ys.
  33. unify_oe_set(Xs-T1,Ys-T2) :- append(Xs,Ys,Zs), append(Zs,T,T1), append(Zs,T,T2).
  34.  
  35.  
  36. main:-
  37. process,
  38.  
  39. process:-
  40. /* your code goes here */
  41. unify_oeset([1,3,5,9|T1],[2,3,5,6|T2]), print(T1), nl, print(T2), nl,
  42.  
  43. :- main.
Success #stdin #stdout 0.06s 7736KB
stdin
Standard input is empty
stdout
[1,3,5,9,2,3,5,6|_G1687]
[1,3,5,9,2,3,5,6|_G1687]