[mercury-users] Another indexing proposal (was mercury website (was Suggestion: new operator))

schachte at cs.mu.OZ.AU schachte at cs.mu.OZ.AU
Fri Oct 20 20:06:53 AEDT 2000


On 20 Oct, Mattias Waldau wrote:
> why invent a new operator not found in any other language? is this to try to
> make the language as obscure as possible?
> 
> the only standard is [ ],
> 
>> Map @ Key     <=> map__lookup(Map, Key)
>> String @ Idx  <=> string__unsafe_index(String, Idx)
>> Array @ Idx   <=> array__lookup(Array, Idx)
> 
> should be
> 
> Map[Key]
> String[Idx]
> Array[Idx]

I think brackets would be difficult syntactically.

An alternative which is in some ways preferable is parentheses:

    1.  It makes good sense to think of at least maps and arrays, and
        maybe strings as well, as functions which can be applied to an
        index.

    2.  Mercury already understands Variable(Arguments) syntax.

    3.  FORTRAN uses parentheses for array indexing.  We do want to be
        compatible with FORTRAN, don't we? :-)

The only catch is that, of course, you can't apply a data value to some
arguments.  It currently doesn't make sense, but that could be changed.
The syntax for declaring a type could be extended to allow the user to
somehow define what expression should be evaluated when a variable of
that type is applied to arguments.

For concreteness, here's a simple proposal.  Like the "where equality
is ..." suffix on type declarations, Mercury would allow "where
application is...".  For example:

	:- type map(K,V) ---> ...
		where application is map__lookup.

	:- type string ...
		where application is string__application.


	% select a single character of a string
	:- func string__application(string::in, int::in) = char is det.

	% take a substring specified by start and end character positions
	:- func string__application(string::in, int::in, int::in) = string
	        is det.

The arities that map variables could be called with would be determined
by the arities supported by 'lookup'.  So if Map were a variable of
type map/2 and String were of type string, then Map("foo") would be
transformed to map__lookup(Map,"foo"), String(3) would turn into
string__application(String, 3), and String(3,5) would become
string__application(String, 3, 5).

By having the "where application is" part just specify a name, we
allow arity based overloading.  If the specified name doesn't include
a module qualifier, it could even allow type-based overloading.

-- 
Peter Schachte                     The use of COBOL cripples the mind; its
mailto:schachte at cs.mu.OZ.AU        teaching should, therefore, be regarded
http://www.cs.mu.oz.au/~schachte/  as a criminal offense.
PGP: finger schachte at 128.250.37.3      -- E. W. Dijkstra 


--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list