Self-Test 8

Correcting min/3
 
This question includes a partial program. You are advised to copy and paste this into a file. You should develop your solution on a machine and test it before looking at the solution.
 
 

A procedure for finding the minimum of two numbers is often given as:

     % 1 
     min(Min, Max, Min) :-
          Min < Max.
     % 2
     min(Max, Min, Min).
The idea is that the third argument is unified with the minimum of the two previous arguments.

Here are a series of queries:

     | ?- min(2,1,Min).

     Min = 1 ? ;

     no
ie 1 is the minimum of 1 and 2.
     | ?- min(1,2,Min).

     Min = 1 ? ;

     Min = 2 ? ;

     no
ie 1 is the minimum of 1 and 2, and 2 is also the minimum of 1 and 2.
     | ?- min(2,1,1).

     yes
ie 1 is the minimum of 1 and 2.
     | ?- min(1,2,2).

     yes
ie 2 is the minimum of 1 and 2.

Clearly the procedure is wrongly specified. Rewrite the procedure in a good logic programming style so that it responds as follows to these queries:

     | ?- min(1,2,Min).

     Min = 1 ? ;

     no
     | ?- min(2,1,Min).

     Min = 1 ? ;

     no
     | ?- min(2,1,1).

     yes
     | ?- min(1,2,2).

     no
     | ?- min(1,1,Min).

     no