enforce()?

Steven Schveighoffer schveiguy at yahoo.com
Mon Jul 19 04:36:01 PDT 2010


On Sat, 17 Jul 2010 16:25:16 -0400, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> On 06/16/2010 05:47 AM, Steven Schveighoffer wrote:
>> On Tue, 15 Jun 2010 22:23:15 -0400, Andrei Alexandrescu
>> <SeeWebsiteForEmail at erdani.org> wrote:
>>
>>> bearophile wrote:
>>>> I have counted about 200 usages of std.contracts.enforce() inside
>>>> Phobos. Can you tell me what's the purpose of enforce() in a language
>>>> that has built-in Contract Programming?
>>>
>>> You need to read TDPL for that :o).
>>>
>>>> And what are the purposes of std.contracts.AssumeSorted()? Is it
>>>> useful for something?
>>>
>>> AssumeSorted was an experiment. I think it has drawbacks that I don't
>>> know how to address, so I'll retire it.
>>
>> Hm... what are the drawbacks (besides it not being enforced)? I thought
>> it was a good solution.
>
> Sorry I took so long (over one month!) to reply to this. I've delayed  
> the reply to the point when it could be integrated within the upcoming  
> thread about improving std.algorithm.find.
>
> The problem with AssumeSorted is that generally predicates in D (and  
> also in most other languages) are not easy to compare. Let's first  
> recall AssumSorted's definition. It's just a wrapper:
>
> /**
> Passes the type system the information that $(D range) is already
> sorted by predicate $(D pred). No checking is performed; debug builds
> may insert checks randomly. To insert a check, see $(XREF algorithm,
> isSorted).
>   */
> struct AssumeSorted(Range, alias pred = "a < b")
> {
>      /// Alias for $(D Range).
>      alias Range AssumeSorted;
>      /// The passed-in range.
>      Range assumeSorted;
>      /// The sorting predicate.
>      alias pred assumeSortedBy;
> }
>
> /// Ditto
> AssumeSorted!(Range, pred) assumeSorted(alias pred = "a < b", Range)
> (Range r)
> {
>      AssumeSorted!(Range, pred) result;
>      result.assumeSorted = r;
>      return result;
> }
>
> The recommended way to use the facility is:
>
> int[] a = [ -1, 0, 1, 2, 3, 4, 5 ];
> assert(find(assumeSorted(a), 3) == [ 3, 4, 5 ]);
>
> find() uses simple means to detect that its first argument has type  
> AssumeSorted and takes advantage of that when searching (specifically by  
> doing binary search).
>
> So far so good. The problem ensues when we want to make sure that the  
> sorting predicate is in sync with the search predicate. For example, if  
> the search predicate is "==" then it's okay to use "<" or ">" as a  
> sorting predicate. But searching for "a.zip == b.zip" in a range sorted  
> by "a.name < b.name" is not okay.
>
> If predicates were all expressed as strings, probably some string  
> manipulation could be done to see whether they are compatible. But as  
> things stand, assertSorted has quite limited power.

Just thinking out loud here, couldn't you use the predicate already in  
AssumeSorted?  I mean, if you're going to pass AssumeSorted into find, you  
don't want to also specify the predicate as then the range just becomes a  
standard range.

There must be some kind of way to use template constraints to kill the  
predicate arg to find when the range is an AssumeSorted struct.  If not,  
there should be.

-Steve


More information about the Digitalmars-d mailing list