Skip to content

Instantly share code, notes, and snippets.

@jki127
Last active November 26, 2019 21:34
Show Gist options
  • Save jki127/eb51d034077bec6a3bd4eaf48d774cd2 to your computer and use it in GitHub Desktop.
Save jki127/eb51d034077bec6a3bd4eaf48d774cd2 to your computer and use it in GitHub Desktop.

Prolog: Cut Operator & CLP(FD) - Programming Languages - Nov 26th, 2019

Cut Operator !

p :- a, b.
p :- c.

The eqivalent of this code in boolean algebra is:

(a and b) or c

Now let's add a cut:

p :- a,!,b.
p :- c.

The eqivalent of this code in boolean algebra is:

(a and b) or (not-a and c)

Maze

The following code represents a maze in Prolog:

% Walls are defined like so
mazeWall(0,0).
mazeWall(0,1).
% ...
% ...
mazeWall(10,12).

% Start and end points of the maze
mazeStartPos(0,1).
mazeEndPos(0,2).

% Dimensions of the maze 
mazeDimensions(10,12).

% Given a row and column, return a char
% 'S' for start
% 'E' for end
% '*' for wall
mazeElement(R,C,'S') :- mazeStartPos(R,C), !.
mazeElement(R,C,'E') :- mazeEndPos(R,C), !.
mazeElement(R,C,'*') :- mazeWall(R,C), !.
mazeElement(_,_,' ').

% Helper function to detect when we should print a newline
mazeNewLine(C) :- mazeDimensions(_,C), nl.

% Print the entire maze
printMaze :-
	mazeDimensions(Rows, Cols),
	between(0, Rows, Row),
	between(0, Cols, Col),
	mazeElement(Row, Col, Appearance),
	write(Appearance),
	mazeNewLine(Col),
	fail.

CLP(FD)

Constraint Logic Programming over Finite Domains

The following query returns an error:

?- 7 is X+4.

is does not support equations with variables.

There is a library for Prolog called CLP(FD) that adds an #= operator. It allows us to use variables in equations. It does not work with floating-point numbers.

?- X #= 3+4.
X = 7.

?- X #< 3.
X in inf..2.

?- X #< 3, X #> -5.
X in -4..2.

If we want values returned one at a time, we can use label.

label takes in a list of CLPFD variables.

?- X #< 3, X #> -5, label([X]).
X = -4.
X = -3.
X = -2.
...

\/ is an OR symbol

?- X in 3 \/ 50, label([X]).
X = 3.
X = 50.

// is for integer division

?- X // 2 #= 5.
X in 10..11.

abs for absolute value

?- 3 #= abs(X)
X in 3\/-3.

midpoint

midpoint(X,Y) :- midpoint #= (X+Y)//2.

Farm problem

A farmer has some chickens and some cows, for a total of 30 animals. The animals have total of 74 legs. How many chickens and how many cows does the farmer have?

farm(Cows,Chickens) :-
	(4*Cows)+(2*Chickens) #= 74, 
	Cows + Chickens #= 30,
	Cows #> 0, Chickens #> 0,
	label([Cows, Chickens]).

Fibonacci

fib(0,0).
fib(1,1).
fib(X,Y) :-
	X#>1, A1#=X-1,
	A2#=X-2, fib(A1,P1), fib(A2,P2),
	Y #= P1 + P2.

ranges

[X,Y,Z] ins 1..3, label([X,Y,Z]).

The above returns all permutations of three numbers within the range 1 to 3.

[X,Y,Z] ins 1..3, all_different([X,Y,Z]), label([X,Y,Z]).

The above returns all combinations of three unique number within the range 1 to 3.

Map Coloring

Regions that share a border cannot be the same color.

Let's represent each color with an integer

% 0 = orange, 1 = red, 2 = blue, 3 = purple
region(Rs) :-
	Rs = [A,B,C,D,E,F],
	Rs ins 0..3,
	A #\= B, A #\= C, A #\= D, A #\= F,
	B #\= C, B #= D,
	C #\= E, C #\= D,
	D #\= E, D #\= F,
	E #\= F,
	label(Rs).

#school/f19/proglang-f19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment