real language

Bookmarking Digg Diigo DZone Earthlink Google
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language CHR


Author:Jon Sneyers
Score: (3.00 in 15 votes)
% 99 bottles of beer, CHR(Prolog) version.
% tested in the K.U.Leuven CHR implementation in SWI-Prolog
% Mon Jun 19 14:22:05 2006 Jon Sneyers
% top-level goal: beer

:- use_module(library(chr)).
:- chr_constraint beer, beer(+int), bottle, sing,
                capital, bottles, ofbeer, onwall, comma,
                take_one, dotnl,
                bottle_count, bottles(+int), writeresult.

:- chr_option(toplevel_show_store,off).
:- chr_option(optimize,full).
:- chr_option(debug,off).

% make 99 bottles and start singing
beer <=> beer(99), sing.

% make N bottles
makebottle @ beer(N) <=> N > 0 | bottle, beer(N-1).
endloop    @ beer(_) <=> true.

% stop singing if we're already singing
% (change to simpagation rule to make the song infinite)
sing, sing <=> true.

% sing two lines
sing ==> capital, bottles, ofbeer, onwall, comma, bottles, ofbeer, dotnl,
         take_one, comma, bottles, ofbeer, onwall, dotnl, nl.

% if we're still singing: sing again
sing <=> sing.

% take one bottle down
take_one, bottle <=> write('Take one down and pass it around').

% no more bottles: go to the store and start again
take_one <=> write('Go to the store and buy some more'), beer.

% print how many bottles there are in the store
bottle, bottle \ bottles <=> bottle_count, write(' bottles').
bottle \ bottles  <=> write('1 bottle').
capital, bottles <=> write('No more bottles').
         bottles <=> write('no more bottles').

% count the number of bottles in the store
bottle_count, bottle ==> bottles(1).
bottle_count <=> writeresult.
bottles(X), bottles(Y) <=> Z is X+Y, bottles(Z).
writeresult, bottles(N) <=> write(N).

% write stuff
onwall <=> write(' on the wall').
ofbeer <=> write(' of beer').
dotnl <=> write('.'), nl.
comma \ capital <=> true.
comma <=> write(', ').

Download Source | Write Comment

Alternative Versions

a CHR(Java) versionPeter Van Weert10/16/060


>>  yyyyyyy said on 11/19/09 17:10:12

yyyyyyy SWI prolog.
consider changing line 20
makebottle @ beer(N) <=> N > 0 | bottle, beer(N-1).
makebottle @ beer(N) <=> N > 0 | bottle, N1 is N-1, beer(N1).
because of
CHR compiler TYPE ERROR:
`--> Invalid functor in body goal compound(beer, 1, [compound(-, 2, [var(1, _G32266), atomic(1)], _G32266-1)], beer(_G32266-1)) of rule makebottle (line 20):
found `compound(-, 2, [var(1, _G32266), atomic(1)], _G32266-1)',
expected type `int'!
% bottle compiled 0.38 sec, 2,124,380 bytes

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!




Security Code: