[mercury-users] mode decls are promises? What are promise decls?

Mark Brown mark at csse.unimelb.edu.au
Mon Feb 12 14:32:18 AEDT 2007


On 11-Feb-2007, doug.auclair at logicaltypes.com <doug.auclair at logicaltypes.com> wrote:
> Dear all,
> 
> I have this bit of code to initialize a random switch:
> 
> -----
> :- pred init_each_weight_n_threshold(peano, int, supply, supply,
> 	                             list(float), list(float)).
> :- mode init_each_weight_n_threshold(in, in, in, out, in, out(non_empty_list))
>         is det.
> 
> init_each_weight_n_threshold(zero, _, !RNG) --> [].
> init_each_weight_n_threshold(s(Peano), Maxie, !RNG) -->
> 	{ random(Rand, !RNG) },
> 	cons(fdiv(Rand, Maxie)),
> 	init_each_weight_n_threshold(Peano, Maxie, !RNG).
> -----
> 
> called with:
> 
> 	init_each_weight_n_threshold(s(peano_from_int(Size)), Maxie, R1, _RNG,
> 	                             [], [Threshold|Weights]).
> 
> Now, since the initial call has Peano always some successor to zero, then
> the out(non_empty_list) is the correct mode argument, and the program
> compiles and runs as expected.

No, it shouldn't compile.  The compiler looks at the initial insts to
determine all of the possible calling patterns, rather than just the
calling patterns that appear in the program.  In this case the first
clause of init_each_weight_n_threshold can be run with the second last
argument being empty (since the initial inst is `ground'), so the
compiler shouldn't be able to infer that the last argument will always
be non-empty.

> But how does the compiler know or
> guarantee this?  For, when I change the call to:
> 
> 	init_each_weight_n_threshold(peano_from_int(Size), Maxie, R1, _RNG,
> 	                             [], [Threshold|Weights]).
> 
> (n.b.: no more successor on the peano_from_int/1 result), it still compiles
> but dies at runtime with a "caught bus error" when Size is set to 0.

That's the problem: the compiler either *thinks* it knows that the last
argument will be non-empty, or it doesn't check the final inst properly.
In either case it is a compiler bug.

Thanks for spotting this.

Cheers,
Mark.

--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list