[Haskell-cafe] ghci and applicative

Ryan Ingram ryani.spam at gmail.com
Fri Jun 12 03:31:51 EDT 2009


:set -XNoMonomorphismRestriction

or eta-expand:

let op x y = x+y

The problem is that "op" looks like a value to the user, but it's a
function (based on the dictionary passed in), which means that any
evaluation it does isn't shared between instances.

Consider:

f1 = let v = fib 10000 in \x -> x + v

f1 :: Integer -> Integer
only calculates "fib 10000" once, but,
f1 :: Num a => a -> a
calculates "fib 10000" every time you call it.

This can lead some programs to take exponentially longer than they
seem like they should.

  -- ryan

On Fri, Jun 12, 2009 at 12:13 AM, Paul Keir<pkeir at dcs.gla.ac.uk> wrote:
> Hi,
>
> I'm finding that some data types which use Applicative to
> instantiate the Num class, give responses I wasn't expecting
> at the ghci prompt. A simple example is list:
>
> import Control.Applicative
>
> instance (Num a) => Num [a] where
>  as + bs = (+) <$> as <*> bs
>  (*) = undefined;    abs = undefined
>  signum = undefined; fromInteger = undefined
>
> f1 = let op = (+) in [1,2,3] `op` [1,1,1]
> f2 = let op = (+) in op [1,2,3] [1,1,1]
>
> Functions f1 and f2 give no problems at the ghci prompt.
> However if I instead type the body of either interactively,
> I get an error:
>
> *Main> let op = (+)
> *Main> [1,2,3] `op` [1,1,1]
>
> <interactive>:1:0:
>     Couldn't match expected type `Integer' against inferred type `[a]'
>     In the first argument of `op', namely `[1, 2, 3]'
>     In the expression: [1, 2, 3] `op` [1, 1, 1]
>     In the definition of `it': it = [1, 2, 3] `op` [1, 1, 1]
>
> I get the same error message with op [1,2,3] [1,1,1]. Any thoughts?
>
> Thanks,
> Paul
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


More information about the Haskell-Cafe mailing list