【发布时间】:2016-05-04 02:46:56
【问题描述】:
类型类方法是否有 Haskell 语言扩展来“使用唯一可用的潜在实例”?
我要编译如下
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
class Foo a r where
foo :: a -> r
instance Foo a (Bool -> a) where
foo x _ = x
-- This compiles
-- bar :: Int -> Bool
-- This does not
bar :: a -> Bool
bar _ = True
use :: Bool
use = bar $ foo _5 True
where
_5 :: Int
_5 = 5
现在我收到以下错误:
No instance for (Foo Int (Bool -> r0))
(maybe you haven't applied enough arguments to a function?)
arising from a use of ‘foo’
The type variable ‘r0’ is ambiguous
Note: there is a potential instance available:
instance Foo a (Bool -> a) -- Defined at tests/pos/Fixme.hs:9:10
但是由于只有一个潜在的实例可用,有没有办法强制 ghc 使用该实例?或者当类型不明确时,有没有办法将某个实例声明为默认实例?
【问题讨论】:
-
这个 typeclass + 实例让我畏缩。你确定你在这里表达的是正确的吗?
标签: haskell