我如何约束 Maybe a where Eq a?它必须是种类 * -> 约束
我尝试过的:
class (a ~ Maybe b, Eq b) => K a where
instance (a ~ Maybe b, Eq b) => K a where
Error:
Not in scope: type variable ‘b’
用法示例:
data Test c = forall a. (c a) => Test a
r :: Test K -> Maybe Bool
r (Test o) = (==) <$> o <*> o -- I need GHC to infer that o is Maybe Eq
有效的案例:
pp :: Test ((~) String) -> String
pp (Test o) = o ++ "X" -- GHC infers that o is a string
hh :: Test Eq -> Bool
hh (Test o) = o == o -- GHC infers that o is Eq
通用答案在这里:是否有通用方法将约束应用于类型应用程序? https://stackoverflow.com/questions/41178589/is-there-a-general-way-to-apply-constraints-to-a-type-application/41178590#41178590
以下内容在我的机器上编译。不知道这有多明智。
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE ConstraintKinds #-}
class (Eq (UnMaybe a), a ~ Maybe (UnMaybe a)) => EqMaybe a where
type UnMaybe a
instance Eq a => EqMaybe (Maybe a) where
type UnMaybe (Maybe a) = a
data Test c = forall a. c a => Test a
r :: Test EqMaybe -> Maybe Bool
r (Test o) = (==) <$> o <*> o
f :: Test Eq -> Test EqMaybe
f (Test o) = Test (Just o)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)