显示Haskell中的函数列表 [英] Show a list of functions in Haskell
问题描述
有没有一种方法可以让 show
Haskell中的函数列表?当我尝试
ghci> let functions = [(+),( - ),(*)]
ghci>功能
GHCi说:
< interactive>:17:1:
(Show(Num a0 = > a0 - > a0 - > a0))
因使用'print'而产生
>
可能的修正:
添加一个实例声明(Show(Num a0 => a0 - > ; a0 - > a0))
在一个交互式GHCi命令中:print it
我不确定如何为此添加实例声明。任何帮助将不胜感激。谢谢。
您无法轻松显示功能。标准答案是:
pre code> instance显示(a - > b)其中
显示_ =< function>
前奏> [(+),( - ),(*)]
[< function>,< function>,< function>]
这可以让你有一个show实例,但不提供有用的信息。这通常是正确的,因为可能没有紧凑的方式来显示函数的效果。此外,值得注意的是,虽然考虑函数un - show
-able的标准做法,但如果您确实定义了该实例,则可能会得到重叠的实例条件,如任何实例像 instance(...)=>显示(a - > b)
既是一个孤儿,也是一个 。经验法则应该是在应用程序代码中是可以的,但在库中是危险的。
但是,通过这种方式,我们可以为<$如果你知道你的函数有一个有界的输入,那么你可以这样做:c $ c> instance Show(a - > b)。
稍微好一点 - |如果'universe'是一个
- 实例化类型
class的Universe,则实例化'Universe'Universe a
universe :: [a]
实例Universe Bool其中
universe = [True,False]
实例(Universe a,Show a,Show b)=>显示(a - > b)其中
显示f =显示$ map(\ a - >(a,f a))universe
Prelude> (&& $)
[(True,[(True,True)
,(False,False)
])
,(False,[(True,False)
,(False,False)
])
]
最后,如果可接受,我们可以使用 Data.Typeable
机制为函数获得更好的摘要 show
。 / p>
import Data.Typeable
instance(Typeable a,Typeable b)=>显示(a - > b)其中
显示f ={Function ::++(show $ typeOf f)++}
Prelude Data.Typeable> [(+),( - ),(*)]
[{Function :: Integer - >整数 - >整数}
,{函数::整数 - >整数 - >整数}
,{函数::整数 - >整数 - >整数}
]
但是要注意,这样会失败多态函数。
Prelude Data.Typeable> ($)
< interactive>:7:1:
由于使用'print'而产生(Typeable b0)
类型变量'b0'不明确的
可能的修复:添加修复这些类型变量的类型签名
注意:有几个潜在的实例:
实例[overlap ok] Typeable()
- 在`Data.Typeable.Internal'中定义
实例[overlap ok]可键入的Bool
- 在`Data.Typeable.Internal'中定义
实例[overlap ok]可键入的char
- 定义于`Data.Typeable.Internal'
...加上18个
在一个交互式GHCi命令中:print it
Prelude Data.Typeable> ($)::(() - >()) - > () - > ()
{Function ::(() - >()) - > () - > ()}
Is there a way to show
a list of functions in Haskell? When I try
ghci> let functions = [(+), (-), (*)]
ghci> functions
GHCi says:
<interactive>:17:1:
No instance for (Show (Num a0 => a0 -> a0 -> a0))
arising from a use of 'print'
Possible fix:
add an instance declaration for (Show (Num a0 => a0 -> a0 -> a0))
In a stmt of an interactive GHCi command: print it
I'm not sure how to add an instance declaration for this. Any help will be appreciated. Thanks.
You can't easily show a function. The standard answer is
instance Show (a -> b) where
show _ = "<function>"
Prelude> [(+), (-), (*)]
[<function>,<function>,<function>]
Which lets you have a show instance but provides no useful information. This is generally true since there may be no compact way to show the effect of a function. Further, it's worth noting that while it's standard practice to consider functions un-show
-able, if you do define that instance you'll be likely to get an overlapping instance condition as any instance like instance (...) => Show (a -> b)
is both an orphan and very general. Rule of thumb should be that it's OK in application code, but dangerous in a library.
But with that out of the way, we can make much nicer functions for instance Show (a -> b)
.
If you know that your function has a bounded input then you can do slightly better
-- | A type instantiates 'Universe' if 'universe' is a
-- list of every value instantiating the type
class Universe a where
universe :: [a]
instance Universe Bool where
universe = [True, False]
instance (Universe a, Show a, Show b) => Show (a -> b) where
show f = show $ map (\a -> (a, f a)) universe
Prelude> (&&)
[ (True, [ (True,True)
, (False,False)
])
, (False, [ (True,False)
, (False,False)
])
]
Finally, we can use the Data.Typeable
machinery to get a nicer summary show
for functions, if that's acceptable.
import Data.Typeable
instance (Typeable a, Typeable b) => Show (a -> b) where
show f = "{ Function :: " ++ (show $ typeOf f) ++ " }"
Prelude Data.Typeable> [(+), (-), (*)]
[ { Function :: Integer -> Integer -> Integer }
, { Function :: Integer -> Integer -> Integer }
, { Function :: Integer -> Integer -> Integer }
]
But beware that this will fail on polymorphic functions.
Prelude Data.Typeable> ($)
<interactive>:7:1:
No instance for (Typeable b0) arising from a use of `print'
The type variable `b0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance [overlap ok] Typeable ()
-- Defined in `Data.Typeable.Internal'
instance [overlap ok] Typeable Bool
-- Defined in `Data.Typeable.Internal'
instance [overlap ok] Typeable Char
-- Defined in `Data.Typeable.Internal'
...plus 18 others
In a stmt of an interactive GHCi command: print it
Prelude Data.Typeable> ($) :: (() -> ()) -> () -> ()
{ Function :: (() -> ()) -> () -> () }
这篇关于显示Haskell中的函数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!