我正在嘗試編寫一個類型族,我可以使用它來約束類型級別列表的元素。我有這樣的代碼:約束Haskell中的類型級別列表的元素
{-# LANGUAGE PolyKinds, DataKinds, TypeOperators, TypeFamilies #-}
import GHC.TypeLits (KnownSymbol, symbolVal)
import GHC.Exts (Constraint)
import Data.Proxy (Proxy(..))
type family AllHave (c :: k -> Constraint) (xs :: [k]) :: Constraint
type instance AllHave c '[] =()
type instance AllHave c (x ': xs) = (c x, AllHave c xs)
type family Head (xs :: [k]) :: k where
Head (x ': xs) = x
headProxy :: proxy xs -> Proxy (Head xs)
headProxy _ = Proxy
test :: AllHave KnownSymbol xs => proxy xs -> String
test p = symbolVal (headProxy p)
main :: IO()
main = putStrLn $ test (Proxy :: Proxy '["a", "b"])
據我所知這應該工作,但是當我編譯GHC吐出了這一點:
Test.hs:18:10:
Could not deduce (KnownSymbol (Head xs))
arising from a use of ‘symbolVal’
from the context (AllHave KnownSymbol xs)
bound by the type signature for
test :: AllHave KnownSymbol xs => proxy xs -> String
at Test.hs:17:9-52
In the expression: symbolVal (headProxy p)
In an equation for ‘test’: test p = symbolVal (headProxy p)
謝謝!這就說得通了。作爲一個後續問題 - 如果我想爲空'xs'提供一個基本情況,並且在整個列表中遞歸地定義'test'而不是僅僅頭部,這會是什麼樣子? – Nol