// Abstract ------------------------------------------------------------------------------------------------------------------------------
This sample is based on Michael Spivey's book "Introduction to logic programming":
- a motel suite has two rooms: lounge, bedroom
- each of the rooms has one door and one window
- the lounge's window needs to be opposite to the door
- the bedroom's door needs to be adjacent to the lounge's door
- the bedroom's window needs to be adjacent to the bedroom's door
- the bedroom's window needs to be on the east wall
Where should the doors and the windows be?
// Examples ------------------------------------------------------------------------------------------------------------------------------
?-
#suite(:ld,:lw,:bd,:bw)
-> ( east , west , north , east ) := 1.00 (0.004) 1
-> ( east , west , south , east ) := 1.00 (0.005) 2
-> ( west , east , north , east ) := 1.00 (0.005) 3
-> ( west , east , south , east ) := 1.00 (0.006) 4
// Code ----------------------------------------------------------------------------------------------------------------------------------
// opposite walls
opposite {
(north,south);
(south,north);
(east,west);
(west,east);
}
// adjacent walls
adjacent {
(north,east);
(north,west);
(south,east);
(south,west);
(east,north);
(east,south);
(west,north);
(west,south);
}
// describe the lounge constraints
lounge {
(:ld,:lw,:bd) :- #opposite(:ld,:lw), #adjacent(:ld,:bd);
}
// describe the bedroom constraints
bedroom {
(:bd,east) :- #adjacent(:bd,east);
}
// describe the suite constraints
suite {
(:ld,:lw,:bd,:bw) :- #lounge(:ld,:lw,:bd), #bedroom(:bd,:bw);
}
// ---------------------------------------------------------------------------------------------------------------------------------------
[Home] [Email] [Twitter] [LinkedIn]