How print functions in Haskell like a python or scala?
function, haskell
Solution
You can not have this for a general function as type information is present only at compile time, but you use `Typeable` class for writing something close enough if the type is an instance for `Typeable` class.
import Data.Typeable
instance (Typeable a, Typeable b) => Show (a -> b) where
show f = "Function: " ++ (show $ typeOf f)
Testing this in ghci
*Main> (+)
Function: Integer -> Integer -> Integer
*Main> (+10)
Function: Integer -> Integer
But this will not work for general functions until the type is restricted to a type that has `Typeable` instance.
*Main> zip
<interactive>:3:1:
Ambiguous type variable `a0' in the constraint:
(Typeable a0) arising from a use of `print'
Probable fix: add a type signature that fixes these type variable(s)
In a stmt of an interactive GHCi command: print it
<interactive>:3:1:
Ambiguous type variable `b0' in the constraint:
(Typeable b0) arising from a use of `print'
Probable fix: add a type signature that fixes these type variable(s)
In a stmt of an interactive GHCi command: print it
*Main> zip :: [Int] -> [Bool] -> [(Int,Bool)]
Function: [Int] -> [Bool] -> [(Int,Bool)]
Problem
I try to print functions in Haskell only for fun, like this example: ``` {-# LANGUAGE FlexibleInstances #-} instance Show (Int -> Bool) where show _ = "function: Int -> Bool" ``` loading in GHCi and run and example: ``` λ> :l foo [1 of 1] Compiling Main ( foo.hs, interpreted ) foo.hs:2:1: Warning: Unrecognised pragma Ok, modules loaded: Main. λ> (==2) :: Int -> Bool function: Int -> Bool ``` But, I wish to see that every function print yourself at invocation.