[M3devel] Man or Boy test

hendrik at topoi.pooq.com hendrik at topoi.pooq.com
Thu May 14 22:13:42 CEST 2009


On Thu, May 14, 2009 at 01:25:02PM -0500, Martin Bishop wrote:
> I tried implementing Knuth's Man or Boy test in Modula-3, but I can't 
> get it to work.
> 
> I assumed the code I had wouldn't work (and I was right), but eventually 
> I fiddled enough and got it to compile, but now it just segfaults when run.
> 
> MODULE ManOrBoy EXPORTS Main;
> 
> IMPORT IO, Fmt;
> 
> PROCEDURE Zero(): INTEGER = BEGIN RETURN 0; END Zero;
> PROCEDURE One(): INTEGER = BEGIN RETURN 1; END One;
> PROCEDURE Neg(): INTEGER = BEGIN RETURN -1; END Neg;
> 
> PROCEDURE A(k:INTEGER; x1, x2, x3, x4, x5: INTEGER): INTEGER =
> 
>  PROCEDURE B(): INTEGER =
>    BEGIN
>      DEC(k);
>      RETURN A(k, B(), x1, x2, x3, x4);
>    END B;
> 
>  BEGIN
>    IF k <= 0 THEN
>      RETURN x4 + x5;
>    ELSE
>      RETURN B();
>    END;
>  END A;
> 
> BEGIN
>  IO.Put(Fmt.Int(A(10, One(), Neg(), Neg(), One(), Zero())) & "\n");
> END ManOrBoy.
> 
> All I know is that it segfaults at procedure B.

Probably because it contains an infinite recursion.
I may remember wrongly, but I believe that in the original man-or-boy
program, all parameters were passed by name, not by value.  This means 
that the call to B() inside the call to A(...B()...)
would be performed only if A itself used that parameter, and then as 
many times as A actually used it (dynamically).  This the IF in A will 
break the recursion in B.  The same does not happen with call-by-value.

-- hendrik




More information about the M3devel mailing list