develooper Front page | perl.perl6.language | Postings from June 2006

features of and declaring Mapping|Set|Hash values|variables

Thread Next
Darren Duncan
June 29, 2006 16:24
features of and declaring Mapping|Set|Hash values|variables
Message ID:
Lately I've been wanting to cross-apply features of the Set type to 
the Mapping type, as well as of the Hash type to both of those, as I 
see all of these as being very similar to each other conceptually and 
in some cases interchangeable.

1. Looking at the language in Synopsis 6 for data types, I see:

     Set         Unordered Seqs that allow no duplicates
     Junction    Sets with additional behaviours
     Pair        Seq of two elements that serves as an one-element Mapping
     Mapping     Pairs with no duplicate keys

I would like to know if Mapping.does(Set) and whether I could use the 
full range of Set operators on them, as I would like to do?

My impression is that it be reasonable to define a generic Mapping as 
being a Set whose elements are all constrained to be Pairs, and 
further that all keys of those pairs are distinct; eg:

   subset Mapping of Set where {
     all(.members).does(Pair) and +all({.key}) == +all(.members)

If this is true, then I suggest rewording the above line in Synopsis 
6 to better clarify the situation, like this:

     Mapping     Set of Pairs that allow no duplicate Pair keys

It would be very useful to employ Set operations like subset() or 
union() or difference() etc on Mappings, that return Mappings.  With 
such binary operators, each Mapping element has to match entirely as 
a whole Pair, having both the same key and same value.  In the case 
of union(), if any source Pairs have the same keys but different 
values, the union() would fail due to the unique key constraint, with 
similar force to dividing by zero in a non-NaN setting.

2. Similarly, I would like to know if Hash.does(Mapping) and so that 
we can therefore use all the Set and Mapping operators on Hashes, but 
that their output is Hashes.  Likewise very useful.

3. I missed the syntax for declaring anonymous Set or Mapping in the 
Synopsis.  Has it been defined yet?

Something that is distinct from:

   [1,2] # Array
   (1,2) # Seq
   {1=>2,3=>4} # Hash
   (1=>2) # Pair in positional context
   1=>2 # Pair in named context
   all(1,2,3) # Junction

Eg, do we need to use keywords:


Or can this be accomplished without such keywords?

I am assuming we don't have to invoke some new() because these are 
built-in basic types.

4. Since they are already known to be unique, is it possible to get 
an actual Set returned when invoking something akin to .keys or 
.pairs or a Mapping or Hash?  Or do we need to always construct such 
ourself by wrapping the call with a set() or all(), in order to use 
set operations on them?  Would the latter case be inefficient or 

5. I'm wondering about syntax for using Sets like Junctions.  Eg, if 
I want to see if one set is a proper subset of another, can I say 

   all($s1) === any($s2)

Or do I have to rewrap their members into explicit Junctions first like this:

   all($s1.members) === any($s2.members)

I am ignoring for the moment that Set probably declares named 
operators that let me do this:


I don't want to have to use the second syntax if I don't have to, 
since I like consistency with Junction syntax.

The same questions apply to using Mappings and Hashes as Sets.

6. From on my readings of Synopsis 6,9,12, it looks like you can 
declare a Hash whose keys are restricted to non-empty strings like 

   subset FooName of Str where { $^n ne '' };
   subset FooHash of Hash{FooName} of Bar;


   subset FooHash of Hash{Str where { $^n ne '' }} of Bar;

Then if you declare a variable or routine parameter of type FooHash, 
someone can assign/pass only a Hash value to it whose keys are all 
non-empty strings.

So is that a reasonable interpretation?

7. If so, then can I use the same syntax with a Mapping, which looks 
like a Hash but that it is immutable; eg:

   subset FooMap of Mapping{FooName} of Bar;

Then if you declare a variable or routine parameter of type FooMap, 
someone can assign/pass only a Mapping value to it whose keys are all 
non-empty strings.

8. Similar question but with a Set:

   subset FooSet of Set{FooName};

Will this work to make a Set that can only have non-empty strings as members?

9. If you declare a parameter of a type that does Foo, and you pass 
an argument that is a different type that does Foo, but neither of 
them is of type Foo, will the compiler accept the binding (though it 
may fail at runtime)?

Thanks. -- Darren Duncan

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About