[Haskell-cafe] ghci and applicative
ryani.spam at gmail.com
Fri Jun 12 03:31:51 EDT 2009
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.
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.
On Fri, Jun 12, 2009 at 12:13 AM, Paul Keir<pkeir at dcs.gla.ac.uk> wrote:
> 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]
> 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?
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe