Skip to content

Instantly share code, notes, and snippets.

@sheganinans
Last active November 8, 2018 04:58
Show Gist options
  • Save sheganinans/9605fd85e87099bd3abdcbfb53901652 to your computer and use it in GitHub Desktop.
Save sheganinans/9605fd85e87099bd3abdcbfb53901652 to your computer and use it in GitHub Desktop.
Not so Constraint Logic Programming with Mercury
:- module not_so_clp.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int.
:- import_module solutions.
% Demonstrating basic CLP(int)-like solving using non-deterministic search with Mercury.
% A chicken farmer also has some cows for a total of 30 animals, and the animals have 74 legs in all.
% How many chickens does the farmer have?
% https://www.metalevel.at/prolog/optimization
:- pred cow_chicken(int::out, int::out) is nondet.
cow_chicken(Cows, Chickens) :-
int.nondet_int_in_range(0,30,Chickens),
int.nondet_int_in_range(0,30,Cows),
Chickens + Cows = 30,
Chickens*2 + Cows*4 = 74.
main(!IO) :-
solutions(pred({A,B}::out) is nondet :- cow_chicken(A, B), CowsChickens),
io.write(CowsChickens, !IO),
io.nl(!IO).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment