【发布时间】:2011-07-05 15:32:19
【问题描述】:
我有以下使用重叠实例的 Haskell 代码;我试图实现一个函数,它将函数的类型生成为字符串,或者——更一般地说——为不同的函数类型做不同的事情:
{-# OPTIONS_GHC -fglasgow-exts #-}
{-# LANGUAGE OverlappingInstances, IncoherentInstances #-}
module Test
where
data TypeString = MKTS String
instance Show TypeString where
show (MKTS s) = s
class ShowType b c where
theType :: (b -> c) -> TypeString
instance ShowType b b where
theType _ = MKTS "b -> b"
instance ShowType b c where
theType _ = MKTS "b -> c"
instance ShowType (b, b') c where
theType _ = MKTS "(b, b') -> c"
class Problem a where
showProblem :: a -> TypeString
instance Problem (b -> c) where
showProblem f = theType f
Haskell 在我输入时显示预期的行为
> theType (uncurry (+))
(b,b') -> c
但是:谁能解释一下:
> showProblem (uncurry (+))
b -> c
...并解释一下,如何避免 Haskell 选择过于笼统的实例的情况...
【问题讨论】: