[Haskell-cafe] Partial Application

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Thu Jan 18 16:35:31 EST 2007


On Jan 18, 2007, at 16:15 , Philippe de Rochambeau wrote:

> When I read this, I thought that you could partially apply  
> "multiply" by typing "multiply 2" at the ghci prompt. However, this  
> generated an error:
>
> <interactive>:1:0:
>     No instance for (Show (Int -> Int))
>       arising from use of `print' at <interactive>:1:0-9
>     Possible fix: add an instance declaration for (Show (Int -> Int))
>     In the expression: print it
>     In a 'do' expression: print it

Well, yes and no.  Partial application at the prompt works fine;  
displaying the value of a partially applied function doesn't work so  
well, because un-applied or partially applied functions don't have  
nice printable representations (the "No instance for (Show (Int ->  
Int))" error) by default.

You can actually change that to some extent, by loading something  
like this into ghci:

 > import Data.Typeable
 >
 > instance (Typeable a, Typeable b) => Show (a -> b) where
 >     show f = "<" ++ show (typeOf f) ++ ">"

Now your partially applied function should print as <Integer ->  
Integer> instead of producing an error.  (And because Haskell  
functions are curried and functions are themselves Typeable, this  
automatically generalizes to functions taking more than one argument.

(This isn't perfect, since polymorphic functions are not Typeable.)

-- 
brandon s. allbery    [linux,solaris,freebsd,perl]     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 Haskell-Cafe mailing list