Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -5,17 +5,17 @@ next_to(A,B,C):- left_of(A,B,C) ; left_of(B,A,C).
left_of(A,B,C):- append(_,[A,B|_],C).
zebra(Owns, HS):- % color,nation,pet,drink,smokes
HS = [h(_,norwegian,_,_,_),_, h(_,_,_,milk,_),_,_],
select( [h(red,englishman,_,_,_),h(_,swede,dog,_,_),
h(_,dane,_,tea,_), h(_,german,_,_,prince)], HS),
select( [h(_,_,birds,_,pallmall),h(yellow,_,_,_,dunhill),
h(_,_,_,beer,bluemaster)], HS),
left_of( h(green,_,_,coffee,_), h(white,_,_,_,_), HS),
next_to( h(_,_,_,_,dunhill), h(_,_,horse,_,_), HS),
next_to( h(_,_,_,_,blend), h(_,_,cats, _,_), HS),
next_to( h(_,_,_,_,blend), h(_,_,_,water,_), HS),
next_to( h(_,norwegian,_,_,_), h(blue,_,_,_,_), HS),
member( h(_,Owns,zebra,_,_), HS).
HS = [ h(_,norwegian,_,_,_), _, h(_,_,_,milk,_), _, _],
select( [ h(red,englishman,_,_,_), h(_,swede,dog,_,_),
h(_,dane,_,tea,_), h(_,german,_,_,prince) ], HS),
select( [ h(_,_,birds,_,pallmall), h(yellow,_,_,_,dunhill),
h(_,_,_,beer,bluemaster) ], HS),
left_of( h(green,_,_,coffee,_), h(white,_,_,_,_), HS),
next_to( h(_,_,_,_,dunhill), h(_,_,horse,_,_), HS),
next_to( h(_,_,_,_,blend), h(_,_,cats, _,_), HS),
next_to( h(_,_,_,_,blend), h(_,_,_,water,_), HS),
next_to( h(_,norwegian,_,_,_), h(blue,_,_,_,_), HS),
member( h(_,Owns,zebra,_,_), HS).
:- ?- time(( zebra(Who, HS), maplist(writeln,HS), nl, write(Who), nl, nl, fail
; write('No more solutions.') )).

View file

@ -1,30 +1,28 @@
% populate domain by selecting from it
nation(H,V):- memberchk( nation(X), H), X=V. % select the "nation" attribute
owns( H,V):- memberchk( owns( X), H), X=V. % ...
smoke( H,V):- memberchk( smoke( X), H), X=V.
color( H,V):- memberchk( color( X), H), X=V.
drink( H,V):- memberchk( drink( X), H), X=V.
to_the_left(A,B,HS):- append(_,[A,B|_],HS).
next_to(A,B,HS):- to_the_left(A,B,HS) ; to_the_left(B,A,HS).
middle(A, [_,_,A,_,_]).
first(A, [A|_]).
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(Zebra,Houses):-
length(Houses,5),
member(H2, Houses), nation(H2, englishman), color( H2, red),
member(H3, Houses), nation(H3, swede), owns( H3, dog),
member(H4, Houses), nation(H4, dane), drink( H4, tea),
to_the_left(H5,H5b,Houses), color(H5, green), color(H5b, white),
member(H6, Houses), drink( H6, coffee), color( H6, green),
member(H7, Houses), smoke( H7, 'Pall Mall'), owns( H7, birds),
member(H8, Houses), color( H8, yellow), smoke( H8, 'Dunhill'),
middle(H9, Houses), drink( H9, milk),
first(H10, Houses), nation(H10, norwegian),
next_to(H11,H11b,Houses), smoke( H11, 'Blend'), owns( H11b, cats),
next_to(H12,H12b,Houses), owns( H12, horse), smoke(H12b, 'Dunhill'),
member(H13, Houses), drink( H13, beer), smoke( H13, 'Blue Master'),
member(H14, Houses), nation(H14, german), smoke( H14, 'Prince'),
next_to(H15,H15b,Houses), nation(H15, norwegian), color(H15b, blue),
next_to(H16,H16b,Houses), drink( H16, water), smoke(H16b, 'Blend'),
member(Zebra,Houses), owns(Zebra, zebra).
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]).

View file

@ -1,12 +1,12 @@
?- time(( zebra(Z,HS), ( maplist(length,HS,_) -> maplist(sort,HS,S),
maplist(writeln,S),nation(Z,R),nl,writeln(R) ), false ; true)).
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)]
[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]
german
% 138,899 inferences, 0.060 CPU in 0.110 seconds (55% CPU, 2311655 Lips)
% 234,852 inferences, 0.100 CPU in 0.170 seconds (59% CPU, 2345143 Lips)
true.

View 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.