Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Code Reviews

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

83%
+8 −0
Code Reviews Solving logical puzzle with negation and undefined aspects in Prolog

Assume this trivial logic puzzle which I have made up: There are three boys, Fred, John and Max. No two of the boys have the same age. Max is older than John. Fred is not the oldest one. Quest...

0 answers  ·  posted 2y ago by Dirk Herrmann‭  ·  edited 2y ago by Dirk Herrmann‭

Question prolog
#2: Post edited by user avatar Dirk Herrmann‭ · 2022-05-11T23:05:32Z (over 2 years ago)
  • Assume this trivial logic puzzle which I have made up:
  • There are three boys, Fred, John and Max. No two of the boys have the same age. Max is older than John. Fred is not the oldest one. Question 1: Who is the oldest one? Question 2: What could be possible orderings by age?
  • Obviously, Max is the oldest one. And, from the given information the following two orderings are possible: Max,Fred,John and Max,John,Fred.
  • But, formulating this trivial logic puzzle in Prolog appeared to be surprisingly difficult (at least for me, as I am still learning Prolog). Particularly the statement "Fred is not the oldest one" caused me a lot of headache. While this restricts the solution space, it still leaves possibilities open with respect to whether Fred is older than John or not.
  • In the end, I came up with the following solution, which works with SWI Prolog:
  • ``` prolog
  • boys(Boys) :- Boys = [fred,john,max].
  • older(A,B,List) :-
  • nth0(A_idx,List,A),
  • nth0(B_idx,List,B),
  • A_idx < B_idx.
  • fromoldest(A,B,C) :-
  • boys(Boys), % there are three boys
  • permutation(Boys,[A,B,C]), % all of different age
  • older(max,john,[A,B,C]), % max is older than john
  • dif(A,fred). % fred is not the oldest
  • ```
  • I would be glad to get feedback on this approach:
  • * Is this idiomatic Prolog or what constructs should be preferred?
  • * Are there more straightforward ways of solving this?
  • * What about performance (like, use of `permutation`)?
  • Assume this trivial logic puzzle which I have made up:
  • There are three boys, Fred, John and Max. No two of the boys have the same age. Max is older than John. Fred is not the oldest one. Question 1: Who is the oldest one? Question 2: What could be possible orderings by age?
  • Obviously, Max is the oldest one. And, from the given information the following two orderings are possible: Max,Fred,John and Max,John,Fred.
  • But, formulating this trivial logic puzzle in Prolog appeared to be surprisingly difficult (at least for me, as I am still learning Prolog). Particularly the statement "Fred is not the oldest one" caused me a lot of headache. While this restricts the solution space, it still leaves possibilities open with respect to whether Fred is older than John or not.
  • In the end, I came up with the following solution, which works with SWI Prolog if you issue the query `?- fromoldest(A,B,C).`:
  • ``` prolog
  • boys(Boys) :- Boys = [fred,john,max].
  • older(A,B,List) :-
  • nth0(A_idx,List,A),
  • nth0(B_idx,List,B),
  • A_idx < B_idx.
  • fromoldest(A,B,C) :-
  • boys(Boys), % there are three boys
  • permutation(Boys,[A,B,C]), % all of different age
  • older(max,john,[A,B,C]), % max is older than john
  • dif(A,fred). % fred is not the oldest
  • ```
  • I would be glad to get feedback on this approach:
  • * Is this idiomatic Prolog or what constructs should be preferred?
  • * Are there more straightforward ways of solving this?
  • * What about performance (like, use of `permutation`)?
#1: Initial revision by user avatar Dirk Herrmann‭ · 2022-05-11T23:00:21Z (over 2 years ago)
Solving logical puzzle with negation and undefined aspects in Prolog
Assume this trivial logic puzzle which I have made up:

There are three boys, Fred, John and Max.  No two of the boys have the same age.  Max is older than John.  Fred is not the oldest one.  Question 1: Who is the oldest one?  Question 2: What could be possible orderings by age?

Obviously, Max is the oldest one.  And, from the given information the following two orderings are possible: Max,Fred,John and Max,John,Fred.

But, formulating this trivial logic puzzle in Prolog appeared to be surprisingly difficult (at least for me, as I am still learning Prolog).  Particularly the statement "Fred is not the oldest one" caused me a lot of headache.  While this restricts the solution space, it still leaves possibilities open with respect to whether Fred is older than John or not.

In the end, I came up with the following solution, which works with SWI Prolog:

``` prolog
boys(Boys) :- Boys = [fred,john,max].

older(A,B,List) :-
    nth0(A_idx,List,A),
    nth0(B_idx,List,B),
    A_idx < B_idx.

fromoldest(A,B,C) :-
    boys(Boys),                 % there are three boys
    permutation(Boys,[A,B,C]),  % all of different age
    older(max,john,[A,B,C]),    % max is older than john
    dif(A,fred).                % fred is not the oldest
```

I would be glad to get feedback on this approach:
* Is this idiomatic Prolog or what constructs should be preferred?
* Are there more straightforward ways of solving this?
* What about performance (like, use of `permutation`)?