Re: Smart Matching clarification

2006-11-18 Thread Jonathan Lang

Jonathan Lang wrote:

Larry Wall wrote:
> Jonathan Lang wrote:
> : Looking through the table provided, I ran across the following:
> :
> :$_  $xType of Match ImpliedMatching Code
> :==  = ==
> :HashHash  hash keys identical  match if $_.keys.sort
> : »eq« $x.keys.sort
> :
> : My understanding is that at the time this was written, the working
> : theory was that Hash keys would always be strings.  I'm wondering:
> : should this entry replace 'eq' with '===' or 'eqv', so that non-string
> : keys can also be compared for equivalent values?  If so, which
> : operator should replace 'eq'?  (I'm leaning toward '===', since S03
> : defines '$a eq $b' as '~$a === ~$b'.)
>
> Yes, it should be ===.  But in revising the smartmatching tables for this
> and other ===nesses, and thinking about how hashes may or may not be
> implemented as ordered underneath, it seems to me that .keys.sort is
> suboptimal if sort has to second-guess the ordering provided by the
> underlying hash.

Not only is it suboptimal, it might not be possible.  Sorting depends
on cmp returning Order::Increase, Order::Same, or Order::Decrease in
every case; but what if you're comparing Color::Blue to Bool::True?
And what about classes that involve partial ordering, such as sets?
Heck, how do you '[cmp] Color::Blue, Color::Gray', and what does
"sqrt(-1) cmp 0" (or even "sqrt(-1) <=> 0") return?


OK: IIRC, this original definition also preceded Sets.  So instead of
"$_.keys.sort »===« $x.keys.sort", perhaps this should simply be
"Set($_.keys) === Set($x.keys)", corrected for proper syntax.  Heck,
perhaps "$_.keys" and "$x.keys" should _be_ Sets.

--
Jonathan "Dataweaver" Lang


Set-returning .keys (was Re: Smart Matching clarification)

2006-11-18 Thread Darren Duncan

At 3:24 AM -0800 11/18/06, Jonathan Lang wrote:

Jonathan Lang wrote:

Larry Wall wrote:
 > it seems to me that .keys.sort is
 > suboptimal if sort has to second-guess the ordering provided by the

 underlying hash.


Not only is it suboptimal, it might not be possible.  Sorting depends
on cmp returning Order::Increase, Order::Same, or Order::Decrease in
every case; but what if you're comparing Color::Blue to Bool::True?
And what about classes that involve partial ordering, such as sets?
Heck, how do you '[cmp] Color::Blue, Color::Gray', and what does
"sqrt(-1) cmp 0" (or even "sqrt(-1) <=> 0") return?


OK: IIRC, this original definition also preceded Sets.  So instead of
"$_.keys.sort »===« $x.keys.sort", perhaps this should simply be
"Set($_.keys) === Set($x.keys)", corrected for proper syntax.  Heck,
perhaps "$_.keys" and "$x.keys" should _be_ Sets.


I seem to remember having this discussion months 
ago when trying to implement Set::Relation.


Absolutely .keys should return a Set.

We *know* already that all keys in a keyed 
collection are unique, so why not explicitly 
stamp them as such from the start by returning 
them in a Set container; then users of those keys 
don't have to recheck them for uniqueness, such 
as in a Set's constructor, if they want to use 
them as a set.


Then we can reliably and tersely say "$_.keys === 
$x.keys" and it will do the right thing.


Similarly, we can do set operations with the keys 
more tersely, such as "$_.keys subset $x.keys" or 
"$_.keys superset $x.keys" or "$_.keys union 
$x.keys" or "$_.keys intersection $x.keys" or 
"$_.keys minus $x.keys" etc.


And you can compare the keysets of 2 collections 
reliably even when keys are objects of a data 
type that is *not* ordinal, which makes it more 
general.


Finally, common operations like .keys.sort (which 
returns a Seq or List) can be written just as 
tersely or the same as before, so that syntax can 
be kept.


On a tangential matter, if you follow my 
suggestion of another email and actually add a 
(immutable) Bag type to the language, which your 
documentation already references as a common 
thing people may use, then .values can return 
that, since it is an unordered collection that 
may have duplicates.  Once again, you can then 
compare the value lists of 2 Hashes set without 
sorting them.  Still, regardless of what you do 
here, making .keys return a Set should be done.


-- Darren Duncan