[mercury-users] boolean expressions from semidet predicates
Peter Hawkins
hawkinsp at cs.stanford.edu
Wed May 17 11:11:50 AEST 2006
Jonathan Morgan wrote:
> On 5/16/06, Peter Schachte <schachte at csse.unimelb.edu.au> wrote:
> What I don't like about languages like C++ and Mercury, when compared
> to a language like Haskell, is that there is no good way (that I know
> of) to define a function to work with all types that define a given
> operator (or set of operators, like those defining equality, ordering
> and numberic operations, etc.). C++ is worse, because it uses a
> single operator (+) to mean at least two different things (addition
> and string concatenation).
>
> If in Mercury I write the following and use type-inference I get
> complaints about type ambiguity between int and float, but if I remove
> either of them then it will specialise the definition to act either
> over ints, or over floats. However, if I define the equivalent
> Haskell version (with implicit importing of the Prelude), type
> inference gives me f :: Num a => a -> a -> a. While such a system
> requires the programmer to always use the same basic operators to mean
> the same thing (unlike the practise in C++) it does give constrained
> genericity for my function.
You _can_ do the same thing in Mercury using the typeclass system
(modulo the "deriving" automatic thing haskell does for you). Nothing
prevents the creation of a Num typeclass which has the appropriate
operations and with int and float as members --- it's just not the way
things are done at the moment. I assume we don't do this because
typeclasses aren't optimized very well at the moment and this would
require lots of type class dictionaries to be passed around.
Cheers,
Peter
--------------------------------------------------------------------------
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