1

鉴于下面的代码在其中查找类型的特定于类型的信息Data.HashMap,是否可以定义getMapVal2注释中记录的新函数,以构建TypeKey给定类型的参数?

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DataKinds #-}

import Data.Monoid ((<>))
import Data.Proxy (Proxy(Proxy))
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
import qualified Data.HashMap.Strict as Map (HashMap, empty, insert, lookup)
import Data.Dynamic
import GHC.Generics
import Data.Maybe (fromJust, isNothing, maybe)

type family TypeKey (a :: *) :: Symbol where
    TypeKey Int = "int"
    TypeKey T = "trec"

data T = T { aInt :: Int} deriving (Show, Generic, Typeable)

extract ::(s ~ TypeKey a, Typeable a, KnownSymbol s) => Maybe Dynamic -> Maybe a
extract dyn = if (isNothing dyn) then Nothing else fromDynamic . fromJust $ dyn   

getMapVal :: (s ~ TypeKey a, Typeable a, KnownSymbol s) => Map.HashMap String Dynamic -> String -> Maybe a
getMapVal m k = extract $ Map.lookup k m 

{-- How do we get the TypeKey lookup for type a?
getMapVal2 :: (s ~ TypeKey a, Typeable a, KnownSymbol s) => Map.HashMap String Dynamic -> a -> Maybe a
getMapVal2 m ty = extract $ Map.lookup (symbolVal (Proxy :: Proxy (TypeKey ???))) m
--}

main = do
  let map = Map.insert (symbolVal (Proxy :: Proxy (TypeKey T))) (toDyn $ T {aInt=5}) Map.empty -- we insert some value in hashmap for type T - it is of same type
      val = getMapVal map (symbolVal (Proxy :: Proxy (TypeKey T))) :: Maybe T -- now let us retrieve the value in map for Type T. We pass the SymbolVal ourselves
    --val =  getMapVal2 map (T {aInt = 2}) -- now we want to lookup map value given something of a type T. Need getMapVal2 to build symbolval given the input type
  print $ maybe "" show val -- prints value stored in Hashmap for type T which is: T {aInt=5}

这只是一个玩具代码,用于测试在运行时通过类型特定配置传递Data.HashMap给作用于类型类类型的多态函数。

4

1 回答 1

4

使用扩展ScopedTypeVariablesforall这允许您在绑定它们的定义主体中引用-bound 类型变量。

{-# LANGUAGE ScopedTypeVariables #-}

getMapVal2 :: forall a s. (s ~ TypeKey a, Typeable a, KnownSymbol s) => Map.HashMap String Dynamic -> a -> Maybe a
getMapVal2 m ty = extract $ Map.lookup (symbolVal (Proxy :: Proxy (TypeKey a))) m
于 2016-04-02T16:19:29.830 回答