我想要一个表示已修正 Data.Map.Map
中的成员身份的类型类。像这样:
class Reifies s (Map Text v) => IsMember (x :: Symbol) s where
value :: Proxy s -> Proxy x -> v
然后我想实现一个函数,该函数在出现符号时返回此类的Dict
实例:
checkMember :: forall s x v. (KnownSymbol x, Reifies s (Map Text v))
=> proxy x -> Maybe (Dict (IsMember x s))
checkMember sx =
let m = reflect (Proxy @s)
in (_ :: v -> Dict (IsMember x s)) <$> Map.lookup (symbolVal sx) m
我不介意使用unsafeCoerce
来实现checkMember
,但即使如此,我仍然很难弄清楚如何做到这一点(填入类型孔)。
大概的前言:
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}
import Data.Constraint(Dict(..))
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Proxy (Proxy(..))
import Data.Reflection (Reifies, reflect)
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
答案 0 :(得分:1)
为什么必须是类IsMember
?简单类型如何:
newtype Member x s v = Member v
checkMember :: ... => proxy x -> Maybe (Member x s v)
保留Member
抽象允许保留类型Member x s v
的值属于与s
关联的字典的不变性。您不需要unsafeCoerce
。
从那里开始,也许还有某种方法可以使用反射将Member
提升到类型级别,但这听起来有些工程化。
编辑:在讨论中,似乎要求是外部的,对此没有太多要做。这是一种实现checkMember
的方法。
(reflection
也实现了自己的机制。)
我们可以滥用以下事实:GHC将单一方法和无超类class C a where m :: v
的类直接使用到解包方法m :: v
中,并将值C a => b
约束到函数v -> b
IsMember
)的IsMember0
版本IsMember0 x s v => r
包装为新类型,以便将其强制转换为IsMember0 x s v -> r
(UnsafeMember
){-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}
import Data.Constraint(Dict(..))
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Proxy (Proxy(..))
import Data.Reflection (Reifies, reflect, reify)
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
import Unsafe.Coerce (unsafeCoerce)
type Text = String
class IsMember0 (x :: Symbol) s v | s -> v where
value0 :: Proxy s -> Proxy x -> v
class (Reifies s (Map Text v), IsMember0 x s v) => IsMember (x :: Symbol) s v | s -> v
instance (Reifies s (Map Text v), IsMember0 x s v) => IsMember (x :: Symbol) s v
value :: IsMember x s v => Proxy s -> Proxy x -> v
value = value0
newtype UnsafeMember x s v = UnsafeMember (IsMember0 x s v => Dict (IsMember x s v))
unsafeMember :: forall x s v. Reifies s (Map Text v) => v -> Dict (IsMember x s v)
unsafeMember v = unsafeCoerce (UnsafeMember @x @s @v Dict) (\ _ _ -> v)
checkMember :: forall s x v proxys proxyx. (KnownSymbol x, Reifies s (Map Text v))
=> proxys s -> proxyx x -> Maybe (Dict (IsMember x s v))
checkMember _ sx =
let m = reflect (Proxy @s)
in unsafeMember <$> Map.lookup (symbolVal sx) m
-- Executable example
main :: IO ()
main = do
let d = Map.fromList [("foo", 33 :: Int)]
foo = Proxy :: Proxy "foo"
reify d (\p ->
case checkMember p foo of
Nothing -> fail "Not found"
Just Dict -> print (value0 p foo))