下面的代码片段从the Haskell wiki借用来携带一个类型类字典和一个存在类型:
{-# language ExistentialQuantification #-}
module Experiment1 where
data Showable = forall x. Show x => Showable x
instance Show Showable where showsPrec p (Showable x) = showsPrec p x
resultStr :: String
resultStr = show (Showable ()) -- "()"是否可以编写一个能够将Showable构造函数(或任何其他存在类型的数据构造函数)作为参数的函数f :: (forall x. x -> result) -> result?
一次失败的尝试如下所示:
{-# language ExistentialQuantification, RankNTypes, ConstraintKinds #-}
module Experiment2 where
-- import Data.Constraint (Dict(..), withDict)
data Showable = forall x. Show x => Showable x
instance Show Showable where showsPrec p (Showable x) = showsPrec p x
f :: (cxt (), cxt result) => (forall x. cxt x => x -> result) -> result
f mkResult = mkResult ()
resultStr :: String
resultStr = show (f Showable)正如我上面评论的导入所暗示的那样,我的印象是constraints包可能允许我传递必要的证据,但我看不出这是如何工作的?
发布于 2018-01-03 11:43:41
如果您提供了一种确定cxt的方法,则失败的尝试将起作用
import Data.Proxy
f :: (cxt (), cxt result) => p cxt -> (forall x. cxt x => x -> result) -> result
f _ mkResult = mkResult ()
resultStr :: String
resultStr = show (f (Proxy :: Proxy Show) Showable)发布于 2018-01-03 13:42:39
我很抱歉发布了我自己的答案,但这是我最终找到的另一个选择:
{-# language ExistentialQuantification, RankNTypes, ConstraintKinds, KindSignatures, TypeFamilies, FlexibleInstances #-}
module Experiment3 where
import GHC.Exts
data Showable (cxt :: * -> Constraint) = forall x. (cxt ~ Show, cxt x) => Showable x
instance Show (Showable Show) where showsPrec p (Showable x) = showsPrec p x
f :: cxt () => (forall x. cxt x => x -> result cxt) -> result cxt
f mkResult = mkResult ()
resultStr :: String
resultStr = show (f Showable :: Showable Show)不幸的是,它需要show (f Showable)中的显式类型签名,而我的目标是通过Showable获得类型推断(或者更确切地说,某种约束推断)。因此,这个答案本身并不是一个解决方案,而是我想要的另一个反例。
我会接受Cirdec的回答,因为它引导我得出这个结论。
https://stackoverflow.com/questions/48070211
复制相似问题