2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,28 +1,32 @@
|
|||
% populate domain by selecting from it
|
||||
attrs(H,[N-V|R]):- memberchk( N-X, H), X=V, % unique attribute names
|
||||
(R=[] -> true ; attrs(H,R)).
|
||||
one_of(HS,AS) :- member(H,HS), attrs(H,AS).
|
||||
two_of(HS,G,AS):- call(G,H1,H2,HS), maplist(attrs,[H1,H2],AS).
|
||||
left_of(A,B,HS):- append(_,[A,B|_],HS).
|
||||
next_to(A,B,HS):- left_of(A,B,HS) ; left_of(B,A,HS).
|
||||
zebra( Z, HS):-
|
||||
length( HS, 5),
|
||||
member( H1, HS), nation(H1, eng), color(H1, red),
|
||||
member( H2, HS), nation(H2, swe), owns( H2, dog),
|
||||
member( H3, HS), nation(H3, dane), drink(H3, tea),
|
||||
left_of(A,B, HS), color( A, green), color( B, white),
|
||||
member( H4, HS), drink( H4, coffee), color(H4, green),
|
||||
member( H5, HS), smoke( H5, palmal), owns( H5, birds),
|
||||
member( H6, HS), color( H6, yellow), smoke(H6, dunhill),
|
||||
middle( C, HS), drink( C, milk),
|
||||
first( D, HS), nation( D, norweg),
|
||||
next_to(E,F, HS), smoke( E, blend), owns( F, cats),
|
||||
next_to(G,H, HS), owns( G, horse), smoke( H, dunhill),
|
||||
member( H7, HS), smoke( H7, bluemas), drink(H7, beer),
|
||||
member( H8, HS), nation(H8, german), smoke(H8, prince),
|
||||
next_to(I,J, HS), nation( I, norweg), color( J, blue),
|
||||
next_to(V,W, HS), drink( W, water), smoke( V, blend),
|
||||
member( X, HS), owns( X, zebra), nation(X, Z).
|
||||
|
||||
zebra(Zebra,Houses):-
|
||||
Houses = [A,_,C,_,_], % 1
|
||||
maplist( one_of(Houses), [ [ nation-englishman, color-red ] % 2
|
||||
, [ nation-swede, owns -dog ] % 3
|
||||
, [ nation-dane, drink-tea ] % 4
|
||||
, [ drink -coffee, color-green ] % 6
|
||||
, [ smoke -'Pall Mall', owns -birds ] % 7
|
||||
, [ color -yellow, smoke-'Dunhill' ] % 8
|
||||
, [ drink -beer, smoke-'Blue Master'] % 13
|
||||
, [ nation-german, smoke-'Prince' ] % 14
|
||||
] ),
|
||||
two_of(Houses, left_of, [[color -green ], [color -white ]]), % 5
|
||||
maplist(attrs, [C,A], [[drink -milk ], [nation-norwegian]]), % 9, 10
|
||||
maplist(two_of(Houses,next_to),
|
||||
[ [[smoke -'Blend' ], [owns -cats ]] % 11
|
||||
, [[owns -horse ], [smoke-'Dunhill' ]] % 12
|
||||
, [[nation-norwegian], [color-blue ]] % 15
|
||||
, [[drink -water ], [smoke-'Blend' ]] % 16
|
||||
] ),
|
||||
one_of(Houses, [ owns-zebra, nation-Zebra]).
|
||||
left_of( A, B, HS):- append( _, [A,B|_], HS).
|
||||
next_to( A, B, HS):- left_of( A, B, HS) ; left_of( B, A, HS).
|
||||
middle( A, [_,_,A,_,_]).
|
||||
first( A, [A|_]).
|
||||
|
||||
attr( House, Name-Value):-
|
||||
memberchk( Name-X, House), % unique attribute names
|
||||
X = Value. % set, validate, or reject
|
||||
nation(H, V):- attr( H, nation-V).
|
||||
owns( H, V):- attr( H, owns-V). % select an attribute
|
||||
smoke( H, V):- attr( H, smoke-V). % from an extensible record
|
||||
color( H, V):- attr( H, color-V). % of house attributes
|
||||
drink( H, V):- attr( H, drink-V). % which *is* a house
|
||||
|
|
|
|||
|
|
@ -1,12 +1,32 @@
|
|||
?- time(( zebra(Z,HS), ( maplist(length,HS,_) -> maplist(sort,HS,S),
|
||||
maplist(writeln,S),nl,writeln(Z) ), false ; true)).
|
||||
attrs( R, [N-V|T]):- % extensible record, attribute specs
|
||||
memberchk( N-X, R), % unique attribute names
|
||||
X = V, % set, validate, or reject
|
||||
( T = [] -> true ; attrs( R, T) ).
|
||||
|
||||
[color-yellow,drink-water, nation-norwegian, owns-cats, smoke-Dunhill ]
|
||||
[color-blue, drink-tea, nation-dane, owns-horse, smoke-Blend ]
|
||||
[color-red, drink-milk, nation-englishman,owns-birds, smoke-Pall Mall ]
|
||||
[color-green, drink-coffee,nation-german, owns-zebra, smoke-Prince ]
|
||||
[color-white, drink-beer, nation-swede, owns-dog, smoke-Blue Master]
|
||||
specs( P, Hs, [Spec1]) :- call(P, A, Hs), attrs(A, Spec1).
|
||||
specs( P, Hs, [Spec1, Spec2]) :- call(P, A, B, Hs), attrs(A, Spec1), attrs(B, Spec2).
|
||||
|
||||
german
|
||||
% 234,852 inferences, 0.100 CPU in 0.170 seconds (59% CPU, 2345143 Lips)
|
||||
true.
|
||||
left_of( A, B, HS):- append( _, [A,B|_], HS).
|
||||
next_to( A, B, HS):- left_of( A, B, HS) ; left_of( B, A, HS).
|
||||
|
||||
zebra( Zebra, Houses):- % a house *is* a collection of attributes
|
||||
Houses = [A,_,C,_,_], % 1
|
||||
maplist( specs( member, Houses),
|
||||
[ [[ nation-englishman, color-red ]] % 2
|
||||
, [[ nation-swede, owns -dog ]] % 3
|
||||
, [[ nation-dane, drink-tea ]] % 4
|
||||
, [[ drink -coffee, color-green ]] % 6
|
||||
, [[ smoke -'Pall Mall', owns -birds ]] % 7
|
||||
, [[ color -yellow, smoke-'Dunhill' ]] % 8
|
||||
, [[ drink -beer, smoke-'Blue Master']] % 13
|
||||
, [[ nation-german, smoke-'Prince' ]] % 14
|
||||
] ),
|
||||
specs( left_of, Houses, [[color -green ], [color-white ]]), % 5
|
||||
maplist( attrs, [A, C], [[nation-norwegian], [drink-milk ]]), % 10, 9
|
||||
maplist( specs( next_to, Houses),
|
||||
[ [[smoke -'Blend' ], [owns -cats ]] % 11
|
||||
, [[owns -horse ], [smoke-'Dunhill' ]] % 12
|
||||
, [[nation-norwegian], [color-blue ]] % 15
|
||||
, [[drink -water ], [smoke-'Blend' ]] % 16
|
||||
] ),
|
||||
specs( member, Houses, [[ owns-zebra, nation-Zebra]]).
|
||||
|
|
|
|||
|
|
@ -1,45 +1,12 @@
|
|||
:- initialization(main).
|
||||
?- time(( zebra(Z,HS), ( maplist(length,HS,_) -> maplist(sort,HS,S),
|
||||
maplist(writeln,S), nl, writeln(Z) ), false ; true)).
|
||||
|
||||
[color-yellow,drink-water, nation-norwegian, owns-cats, smoke-Dunhill ]
|
||||
[color-blue, drink-tea, nation-dane, owns-horse, smoke-Blend ]
|
||||
[color-red, drink-milk, nation-englishman,owns-birds, smoke-Pall Mall ]
|
||||
[color-green, drink-coffee,nation-german, owns-zebra, smoke-Prince ]
|
||||
[color-white, drink-beer, nation-swede, owns-dog, smoke-Blue Master]
|
||||
|
||||
zebra(X) :-
|
||||
houses(Hs), member(h(_,X,zebra,_,_), Hs)
|
||||
, findall(_, (member(H,Hs), write(H), nl), _), nl
|
||||
, write('the one who keeps zebra: '), write(X), nl
|
||||
.
|
||||
|
||||
|
||||
houses(Hs) :-
|
||||
Hs = [_,_,_,_,_] % 1
|
||||
, H3 = h(_,_,_,milk,_), Hs = [_,_,H3,_,_] % 9
|
||||
, H1 = h(_,nvg,_,_,_ ), Hs = [H1|_] % 10
|
||||
|
||||
, maplist( flip(member,Hs),
|
||||
[ h(red,eng,_,_,_) % 2
|
||||
, h(_,swe,dog,_,_) % 3
|
||||
, h(_,dan,_,tea,_) % 4
|
||||
, h(green,_,_,coffe,_) % 6
|
||||
, h(_,_,birds,_,pm) % 7
|
||||
, h(yellow,_,_,_,dh) % 8
|
||||
, h(_,_,_,beer,bm) % 13
|
||||
, h(_,ger,_,_,pri) % 14
|
||||
])
|
||||
|
||||
, infix([ h(green,_,_,_,_)
|
||||
, h(white,_,_,_,_) ], Hs) % 5
|
||||
|
||||
, maplist( flip(nextto,Hs),
|
||||
[ [h(_,_,_,_,bl ), h(_,_,cats,_,_)] % 11
|
||||
, [h(_,_,horse,_,_), h(_,_,_,_,dh )] % 12
|
||||
, [h(_,nvg,_,_,_ ), h(blue,_,_,_,_)] % 15
|
||||
, [h(_,_,_,water,_), h(_,_,_,_,bl )] % 16
|
||||
])
|
||||
.
|
||||
|
||||
|
||||
flip(F,X,Y) :- call(F,Y,X).
|
||||
|
||||
infix(Xs,Ys) :- append(Xs,_,Zs) , append(_,Zs,Ys).
|
||||
nextto(P,Xs) :- permutation(P,R), infix(R,Xs).
|
||||
|
||||
|
||||
main :- findall(_, (zebra(_), nl), _), halt.
|
||||
german
|
||||
% 202,730 inferences, 0.031 CPU in 0.031 seconds (100% CPU, 6497715 Lips)
|
||||
true.
|
||||
|
|
|
|||
45
Task/Zebra-puzzle/Prolog/zebra-puzzle-5.pro
Normal file
45
Task/Zebra-puzzle/Prolog/zebra-puzzle-5.pro
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
:- initialization(main).
|
||||
|
||||
|
||||
zebra(X) :-
|
||||
houses(Hs), member(h(_,X,zebra,_,_), Hs)
|
||||
, findall(_, (member(H,Hs), write(H), nl), _), nl
|
||||
, write('the one who keeps zebra: '), write(X), nl
|
||||
.
|
||||
|
||||
|
||||
houses(Hs) :-
|
||||
Hs = [_,_,_,_,_] % 1
|
||||
, H3 = h(_,_,_,milk,_), Hs = [_,_,H3,_,_] % 9
|
||||
, H1 = h(_,nvg,_,_,_ ), Hs = [H1|_] % 10
|
||||
|
||||
, maplist( flip(member,Hs),
|
||||
[ h(red,eng,_,_,_) % 2
|
||||
, h(_,swe,dog,_,_) % 3
|
||||
, h(_,dan,_,tea,_) % 4
|
||||
, h(green,_,_,coffe,_) % 6
|
||||
, h(_,_,birds,_,pm) % 7
|
||||
, h(yellow,_,_,_,dh) % 8
|
||||
, h(_,_,_,beer,bm) % 13
|
||||
, h(_,ger,_,_,pri) % 14
|
||||
])
|
||||
|
||||
, infix([ h(green,_,_,_,_)
|
||||
, h(white,_,_,_,_) ], Hs) % 5
|
||||
|
||||
, maplist( flip(nextto,Hs),
|
||||
[ [h(_,_,_,_,bl ), h(_,_,cats,_,_)] % 11
|
||||
, [h(_,_,horse,_,_), h(_,_,_,_,dh )] % 12
|
||||
, [h(_,nvg,_,_,_ ), h(blue,_,_,_,_)] % 15
|
||||
, [h(_,_,_,water,_), h(_,_,_,_,bl )] % 16
|
||||
])
|
||||
.
|
||||
|
||||
|
||||
flip(F,X,Y) :- call(F,Y,X).
|
||||
|
||||
infix(Xs,Ys) :- append(Xs,_,Zs) , append(_,Zs,Ys).
|
||||
nextto(P,Xs) :- permutation(P,R), infix(R,Xs).
|
||||
|
||||
|
||||
main :- findall(_, (zebra(_), nl), _), halt.
|
||||
Loading…
Add table
Add a link
Reference in a new issue