<div dir="ltr">The implementation of set-union for hash sets would be unnecessarily slow if that's how I wrote it. It's faster to extract the hash tables and operate directly on those. It's even faster still to find the largest immutable hash table in the argument list and start from that. On top of that, the operation is specified, since before my changes, to go through the arguments and reject any hash sets that use a different notion of equality, so that the output of set-union has a meaningful semantics. For an arbitrary other set representation, I don't necessarily know what the notion of equality is. So currently, I just reject all of them.<br>
</div><div class="gmail_extra"><br clear="all"><div>Carl Eastlund</div>
<br><br><div class="gmail_quote">On Thu, Aug 22, 2013 at 1:52 PM, Jay McCarthy <span dir="ltr"><<a href="mailto:jay.mccarthy@gmail.com" target="_blank">jay.mccarthy@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I find this an unsatisfying consequence of the implementation and not<br>
necessary. For instance, you could implement set-union purely<br>
generically as:<br>
<br>
(define (set-union a b)<br>
(for/fold ([a a]) ([e (in-set b)]) (set-add a e)))<br>
<br>
and then it would work for everything. Right?<br>
<br>
Jay<br>
<div class="HOEnZb"><div class="h5"><br>
On Thu, Aug 22, 2013 at 10:14 AM, Carl Eastlund <<a href="mailto:cce@ccs.neu.edu">cce@ccs.neu.edu</a>> wrote:<br>
> Set-union never worked for even different hash set representations. Even<br>
> before I touched the code, the union of an eq set and an eqv set, for<br>
> instance, caused a runtime error.<br>
><br>
> Generics do not have multiple dispatch. That's not a mechanism we have<br>
> right now. And "fallbacks" are for when there's no method implemented for a<br>
> given receiver value, they're not particularly related to a question of "are<br>
> these things the same type".<br>
><br>
> I chose to keep the semantics that union and operations like it only work<br>
> with the same representation. Partly because that's how things already<br>
> were, and partly to set the precedent that generics authors don't have to<br>
> write two versions of every method. Perhaps this wasn't the best idiom, but<br>
> it's what I wrote. Perhaps there's a better idiom for fallbacks that makes<br>
> this work more cleanly. Anyway, right now, generic sets are designed so you<br>
> can use any one representation, but they don't combine representations.<br>
><br>
> Carl Eastlund<br>
><br>
><br>
> On Thu, Aug 22, 2013 at 9:03 AM, J. Ian Johnson <<a href="mailto:ianj@ccs.neu.edu">ianj@ccs.neu.edu</a>> wrote:<br>
>><br>
>> No, it doesn't seem to be using the fallback in this case.<br>
>><br>
>> ianj@sampson:~/racket/racket/bin$ ./racket -il xrepl<br>
>> Welcome to Racket v5.90.0.8.<br>
>> -> (set-union '() (set))<br>
>> ; in-list: contract violation<br>
>> ; expected: list?<br>
>> ; given: (set)<br>
>> ; [,bt for context]<br>
>> -><br>
>><br>
>> -Ian<br>
>> ----- Original Message -----<br>
>> From: "Sam Tobin-Hochstadt" <<a href="mailto:samth@cs.indiana.edu">samth@cs.indiana.edu</a>><br>
>> To: "J. Ian Johnson" <<a href="mailto:ianj@ccs.neu.edu">ianj@ccs.neu.edu</a>>, "Carl Eastlund" <<a href="mailto:cce@ccs.neu.edu">cce@ccs.neu.edu</a>><br>
>> Cc: <a href="mailto:dev@racket-lang.org">dev@racket-lang.org</a>, "Matthew Flatt" <<a href="mailto:mflatt@cs.utah.edu">mflatt@cs.utah.edu</a>><br>
>> Sent: Thursday, August 22, 2013 8:51:30 AM GMT -05:00 US/Canada Eastern<br>
>> Subject: Re: [racket-dev] Lists aren't sets, but have set-like operations<br>
>><br>
>> Wait, `set-union` of two different set representations doesn't work?<br>
>><br>
>> Sam<br>
>><br>
>> On Thu, Aug 22, 2013 at 8:07 AM, J. Ian Johnson <<a href="mailto:ianj@ccs.neu.edu">ianj@ccs.neu.edu</a>> wrote:<br>
>> > You misunderstand. I used set-union, but single dispatch saw '() and<br>
>> > used list-union for the '() (set) combination.<br>
>> > -Ian<br>
>> > ----- Original Message -----<br>
>> > From: "Sam Tobin-Hochstadt" <<a href="mailto:samth@cs.indiana.edu">samth@cs.indiana.edu</a>><br>
>> > To: "J. Ian Johnson" <<a href="mailto:ianj@ccs.neu.edu">ianj@ccs.neu.edu</a>><br>
>> > Cc: <a href="mailto:dev@racket-lang.org">dev@racket-lang.org</a>, "Matthew Flatt" <<a href="mailto:mflatt@cs.utah.edu">mflatt@cs.utah.edu</a>><br>
>> > Sent: Thursday, August 22, 2013 8:02:50 AM GMT -05:00 US/Canada Eastern<br>
>> > Subject: Re: [racket-dev] Lists aren't sets, but have set-like<br>
>> > operations<br>
>> ><br>
>> ><br>
>> ><br>
>> > But 'list-union' is not a generic operation so it isn't surprising that<br>
>> > this didn't work. To do this generically, you'd need to use 'set-union'.<br>
>> ><br>
>> > Sam<br>
>> > On Aug 22, 2013 7:59 AM, "J. Ian Johnson" < <a href="mailto:ianj@ccs.neu.edu">ianj@ccs.neu.edu</a> > wrote:<br>
>> ><br>
>> ><br>
>> > The problem manifested itself when I got an exception that in-list can't<br>
>> > be called on (set), which really confused me. (set? '()) answered true, so<br>
>> > it tried to do (list-union '() (set)), which failed.<br>
>> > Generic sets as they are don't work generically. Some action should be<br>
>> > taken. Either set? means what it once did, or we do some awfully slow<br>
>> > multiple dispatch for set operations. My bias shows.<br>
>> > -Ian<br>
>> > ----- Original Message -----<br>
>> > From: "Matthew Flatt" < <a href="mailto:mflatt@cs.utah.edu">mflatt@cs.utah.edu</a> ><br>
>> > To: "Carl Eastlund" < <a href="mailto:cce@ccs.neu.edu">cce@ccs.neu.edu</a> ><br>
>> > Cc: "J. Ian Johnson" < <a href="mailto:ianj@ccs.neu.edu">ianj@ccs.neu.edu</a> >, "dev" < <a href="mailto:dev@racket-lang.org">dev@racket-lang.org</a> ><br>
>> > Sent: Thursday, August 22, 2013 7:22:25 AM GMT -05:00 US/Canada Eastern<br>
>> > Subject: Re: [racket-dev] Lists aren't sets, but have set-like<br>
>> > operations<br>
>> ><br>
>> > How much should we prioritize backward compatibility in this case?<br>
>> ><br>
>> > One possibility is to make `set?' mean `hash-set?', and add<br>
>> > `generic-set?' in place of the current `set?'. That's uglier,<br>
>> > obviously, but it would be better if we want to prioritize backward<br>
>> > compatibility.<br>
>> ><br>
>> > At Wed, 21 Aug 2013 19:14:06 -0400, Carl Eastlund wrote:<br>
>> >> Ah, yes. The set? predicate no longer distinguishes a representation.<br>
>> >> There are several predicates for the original set type, now called<br>
>> >> "hash<br>
>> >> sets": set-eq?, set-eqv?, set-equal?, set-mutable?, set-immtuable?, and<br>
>> >> set-weak?. I didn't add the basic "hash-set?", but perhaps I should.<br>
>> >> It's<br>
>> >> a weird name, since "hash-set" and "hash-set!" are already existing,<br>
>> >> unrelated functions.<br>
>> >><br>
>> >> Carl Eastlund<br>
>> >><br>
>> >><br>
>> >> On Wed, Aug 21, 2013 at 7:08 PM, J. Ian Johnson < <a href="mailto:ianj@ccs.neu.edu">ianj@ccs.neu.edu</a> ><br>
>> >> wrote:<br>
>> >><br>
>> >> > Okay, I can abide. However, that doesn't really get at my<br>
>> >> > frustration. I'm<br>
>> >> > using the set constructor, that appears to now be an<br>
>> >> > immutable-custom-set<br>
>> >> > with make-immutable-hash as its make-table. So what I'm looking for<br>
>> >> > is not<br>
>> >> > set?, but set-immutable?, as it's a distinct (family of) struct types<br>
>> >> > that<br>
>> >> > won't clash with the primitive data that I'm otherwise using.<br>
>> >> > -Ian<br>
>> >> > ----- Original Message -----<br>
>> >> > From: "Carl Eastlund" < <a href="mailto:cce@ccs.neu.edu">cce@ccs.neu.edu</a> ><br>
>> >> > To: "J. Ian Johnson" < <a href="mailto:ianj@ccs.neu.edu">ianj@ccs.neu.edu</a> ><br>
>> >> > Cc: "dev" < <a href="mailto:dev@racket-lang.org">dev@racket-lang.org</a> ><br>
>> >> > Sent: Wednesday, August 21, 2013 6:58:56 PM GMT -05:00 US/Canada<br>
>> >> > Eastern<br>
>> >> > Subject: Re: [racket-dev] Lists aren't sets, but have set-like<br>
>> >> > operations<br>
>> >> ><br>
>> >> ><br>
>> >> > Ian, sets are now a generic datatype, like dictionaries. Association<br>
>> >> > lists<br>
>> >> > are dictionaries, and lists are now sets. They're also streams and<br>
>> >> > sequences. They're not just "set-like".<br>
>> >> ><br>
>> >> ><br>
>> >> ><br>
>> >> > Carl Eastlund<br>
>> >> ><br>
>> >> ><br>
>> >> > On Wed, Aug 21, 2013 at 6:56 PM, J. Ian Johnson < <a href="mailto:ianj@ccs.neu.edu">ianj@ccs.neu.edu</a> ><br>
>> >> > wrote:<br>
>> >> ><br>
>> >> ><br>
>> >> > I just wasted about 2 hours tracking down a bug that ended up being<br>
>> >> > due to<br>
>> >> > (set? '()) now evaluating to #t. I have no problems with set-union,<br>
>> >> > intersection, etc. being defined for lists, but to treat lists as<br>
>> >> > sets<br>
>> >> > always is perverse to me. The contracts for set operations should use<br>
>> >> > set-like? for (or/c set? list?) and keep the two constructions<br>
>> >> > separate.<br>
>> >> ><br>
>> >> > This conflation is almost as bad as treating empty list as false.<br>
>> >> ><br>
>> >> > -Ian<br>
>> >> > _________________________<br>
>> >> > Racket Developers list:<br>
>> >> > <a href="http://lists.racket-lang.org/dev" target="_blank">http://lists.racket-lang.org/dev</a><br>
>> >> ><br>
>> >> ><br>
>> >> ><br>
>> >> ><br>
>> >> _________________________<br>
>> >> Racket Developers list:<br>
>> >> <a href="http://lists.racket-lang.org/dev" target="_blank">http://lists.racket-lang.org/dev</a><br>
>> > _________________________<br>
>> > Racket Developers list:<br>
>> > <a href="http://lists.racket-lang.org/dev" target="_blank">http://lists.racket-lang.org/dev</a><br>
>><br>
><br>
><br>
> _________________________<br>
> Racket Developers list:<br>
> <a href="http://lists.racket-lang.org/dev" target="_blank">http://lists.racket-lang.org/dev</a><br>
><br>
<br>
<br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">--<br>
Jay McCarthy <<a href="mailto:jay@cs.byu.edu">jay@cs.byu.edu</a>><br>
Assistant Professor / Brigham Young University<br>
<a href="http://faculty.cs.byu.edu/~jay" target="_blank">http://faculty.cs.byu.edu/~jay</a><br>
<br>
"The glory of God is Intelligence" - D&C 93<br>
<br>
</font></span></blockquote></div><br></div>