const

Russell Lewis webmaster at villagersonline.com
Fri Mar 28 12:38:42 PDT 2008


Walter Bright wrote:
> Russell Lewis wrote:
>> Walter Bright wrote:
>>> Russell Lewis wrote:
>>>> Until we have this fundamental, mathematical understanding of const, 
>>>> we are just tweaking and hacking and patching in hopes we can come 
>>>> up with something good enough.
>>>
>>> But the current const regime *is* based on a mathematical notion. The 
>>> reason the previous regimes failed is not because the notion was 
>>> wrong, but because we had tried to support some special use cases.
>>
>> Are you referring to transitive const?  If so, I agree that this is 
>> *one* forward step in the formalization of const.  A very good, very 
>> important step.  But it is not, alone, a complete analysis of all of 
>> the things I described above.
>>
>> Or am I missing something?
> 
> I guess I'm missing something you mean.

What I've been thinking of late is that const is a multidimensional 
concept.  Different types of const may have different answers to each of 
the following questions:

	* Who declares that this entity is "const"?
		- Compiler?
		- Library writer?
		- Ordinary coder?
	* Who is limited by it?
		- Compiler ("don't optimize")?
		- Caller?
		- Callee?
	* Who enforces it?
		- Compiler?
		- Programmer?
		- A combination?
	* Who is interested in it?
		- Compiler?
		- Programmer?
		- Other modules?
		- Maintainer trying to understand the code?
	* Who is entitled to make assumptions based on it?
		- Compiler optimizations?
		- Programmer assert()s and algorithms?
		- Other modules?
	* How long does it last?
		- Forever?
		- From here on out?
		- In this block?
		- So long as this pointer is valid?
		- Except when we call another function?
		- Almost always, except for certain windows?
	* What are we protecting?
		- Logical semantics?
		- Function return values?
		- Binary storage?
	* What if we want to modify this?
		- Perform COW?
		- Use thunks? (COW with shared result pointer)
		- Never modify it?

So "const" is (at least) a 8-dimensional concept.  When I talk a 
mathematical understanding of const, I mean that somebody needs to list 
out the various answers to the questions above (there probably are more 
than I listed) and formally examine each one.  Also, find more 
questions.  (When I first composed this email, I had 6 questions.  Just 
in the last 10 minutes, the list expanded to 8 questions! )

What I'm hoping for is a system that either:
A) Can express any arbitrary point in the above space,
	or
B) Makes convincing arguments why some of those points are unnecessary.



Overkill?  Perhaps.  But we've tried repeatedly to make const work, and 
what we've learned is that "const is subtle, and hard to get right." 
More to the point, as we move into uncharted territory (such as trying 
to use "pure" functions and such for efficient multithreading), I am 
worried that some of the points in the space above (which seemed 
meaningless to our imperative-programming-trained brains) are going to 
become suddenly important.  What do we do, then?



More information about the Digitalmars-d mailing list