[Haskell-beginners] Monad instances and type synonyms

Christopher Howard christopher.howard at frigidcode.com
Tue Apr 16 07:51:11 CEST 2013


On 04/14/2013 04:09 AM, Brent Yorgey wrote:
> On Sat, Apr 13, 2013 at 05:03:57PM -0800, Christopher Howard wrote:
> 
> Sorry, what you're trying to do is simply not possible.  Type synonyms
> must always be fully applied.  So if you want to make Adjustment an
> instance of Monad then you have to make it a newtype.
> 
> However... Adjustment already *is* an instance of Monad!  (In
> particular ((->) e) is an instance for any type e.)  So there's no
> need for you to redeclare an instance yourself.  These days I think
> you just have to import Control.Monad to bring the instance in scope.
> 
> -Brent
> 

Thank you everyone for your patience. I believe what you say about it
already being an instance of Monad, but I don't seem to have convinced
the compiler:

code:
--------
import Control.Monad

-- ...snip...

type Adjustment = (->) SaleVariables

addTax :: Cash -> Adjustment Cash
addTax cash = \v -> cash * (1 + salesTax v)

-- obviously silly to add taxes twice, but work with me here
testrun = addTax 10.00 >>= \c -> addTax c
--------

gives me

code:
--------
    No instance for (Monad ((->) SaleVariables))
      arising from a use of `>>='
    Possible fix:
      add an instance declaration for (Monad ((->) SaleVariables))
    In the expression: addTax 10.00 >>= \ c -> addTax c
    In an equation for `testrun':
        testrun = addTax 10.00 >>= \ c -> addTax c
--------

-- 
frigidcode.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 555 bytes
Desc: OpenPGP digital signature
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130415/a924beb3/attachment.pgp>


More information about the Beginners mailing list