Haskell中的打印函数如何像python或scala一样?

时间:2012-10-14 04:33:23

标签: function haskell

我尝试在Haskell中打印函数只是为了好玩,就像这个例子:

{-# LANGUAGE FlexibleInstances #-}

instance Show (Int -> Bool) where
    show _ = "function: Int -> Bool"

在GHCi中加载并运行并举例:

λ> :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

但是,我希望看到每个函数都在调用时自行打印。

2 个答案:

答案 0 :(得分:9)

由于类型信息仅在编译时出现,因此您不能将其用于常规函数,但如果类型是Typeable类的实例,则使用Typeable类来编写足够接近的类。

import Data.Typeable

instance (Typeable a, Typeable b) => Show (a -> b) where
    show f = "Function: " ++ (show $ typeOf f)

在ghci中测试这个

*Main> (+)
Function: Integer -> Integer -> Integer
*Main> (+10)
Function: Integer -> Integer

但是,在将类型限制为具有Typeable实例的类型之前,这不适用于一般函数。

*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)]

答案 1 :(得分:7)

我假设您希望show方法打印函数的地址,这就是Python的作用:

>>> def foo(a):
...     return a
... 
>>> print foo
<function foo at 0xb76f679c>

实际上没有支持的方法(Haskell是一种安全的高级语言,从函数指针这样的低级细节中抽象出来),除非你愿意使用内部GHC函数{{1} }:

unpackClosure#

测试:

{-# LANGUAGE MagicHash,UnboxedTuples,FlexibleInstances #-}
module Main
       where

import GHC.Base
import Text.Printf

instance Show (a -> a) where
  show f = case unpackClosure# f of
    (# a, _, _ #) -> let addr = (I# (addr2Int# a))
                     in printf "<function ??? at %x>" addr

main :: IO ()
main = print (\a -> a)

不幸的是,没有办法获取函数的名称,因为它只是在编译的可执行文件中不存在(可能有调试信息,但你不能指望它的存在)。如果您的函数可以从C调用,您也可以使用C帮助程序获取其地址。