[Haskell-beginners] Problems inferring instances

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Mon Jan 5 22:53:07 EST 2009


On 2009 Jan 5, at 21:40, dcmorse+haskell at gmail.com wrote:

> Learning from the example of "read" and also Real World Haskell, I
> come across the idea to overload my function's return types. Trying to
> think of an application for this, I've always wanted to write ==
> applications like in Icon, that is
>
> a === b === c means a == b && b == c.
>
> This requires === to sense what context it is called in. If it's being
> called for a Boolean value, it needs to return a Boolean value. If
> it's being called as a parameter to another === application, then it
> needs to somehow remember both it's truthiness and if true what value
> its already seen.

My thought is that Icon's notion of failure as an out-of-band result  
is best captured by the Monad instance for Maybe (or, perhaps more  
generally, MonadZero or whatever we're going to call it this time  
around; at the moment that means Monad).  Unfortunately, this can't be  
made especially clean:  given

 > (<==) :: Eq a => a -> a -> m Bool
 > a <== b = if a <= b then b else fail ">"

which causes -1 <== x <== 1 (say) to do the right thing, you have to  
either escape the monad to use it as a comparison or create a lifted  
if-then-else.

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH




More information about the Beginners mailing list