मैं हाल ही में vinyl
पर पढ़ रहा हूं, जो कि अजीब "प्रकारों की सूची" प्रकारों का उपयोग करता है। प्रकार और विनाइल पर थोड़ा पढ़ने के बाद, मैं उनमें से एक सहज ज्ञान युक्त समझ के कुछ मिल गया है, और मैं इस एक साथदयालु सूचियां कैसे काम करती हैं?
{-# LANGUAGE DataKinds,
TypeOperators,
FlexibleInstances,
FlexibleContexts,
KindSignatures,
GADTs #-}
module Main where
-- from the data kinds page, with HCons replaced with :+:
data HList :: [*] -> * where
HNil :: HList '[]
(:+:) :: a -> HList t -> HList (a ': t)
infixr 8 :+:
instance Show (HList '[]) where
show _ = "[]"
instance (Show a, Show (HList t)) => Show (HList (a ': t)) where
show (x :+: xs) = show x ++ " : " ++ show xs
class ISum a where
isum :: Integral t => a -> t
instance ISum (HList '[]) where
isum _ = 0
instance (Integral a, ISum (HList t)) => ISum (HList (a ': t)) where
isum (x :+: xs) = fromIntegral x + isum xs
-- explicit type signatures just to check if I got them right
alist :: HList '[Integer]
alist = (3::Integer) :+: HNil
blist :: HList '[Integer,Int]
blist = (3::Integer) :+: (3::Int) :+: HNil
main :: IO()
main = do
print alist
print (isum alist :: Int)
print blist
print (isum blist :: Integer)
:i HList
हैक कर लिया है पैदावार
data HList $a where
HNil :: HList ('[] *)
(:+:) :: a -> (HList t) -> HList ((':) * a t)
-- Defined at /tmp/test.hs:10:6
instance Show (HList ('[] *)) -- Defined at /tmp/test.hs:17:10
instance (Show a, Show (HList t)) => Show (HList ((':) * a t))
-- Defined at /tmp/test.hs:19:10
instance ISum (HList ('[] *)) -- Defined at /tmp/test.hs:25:10
instance (Integral a, ISum (HList t)) => ISum (HList ((':) * a t))
-- Defined at /tmp/test.hs:29:10
*Main> :i HList
data HList $a where
HNil :: HList ('[] *)
(:+:) :: a -> (HList t) -> HList ((':) * a t)
-- Defined at /tmp/test.hs:10:6
instance Show (HList ('[] *)) -- Defined at /tmp/test.hs:17:10
instance (Show a, Show (HList t)) => Show (HList ((':) * a t))
-- Defined at /tmp/test.hs:19:10
instance ISum (HList ('[] *)) -- Defined at /tmp/test.hs:25:10
instance (Integral a, ISum (HList t)) => ISum (HList ((':) * a t))
-- Defined at /tmp/test.hs:29:10
जहां से मैं '[]
'[] *
और x ': y
के लिए (':) * x y
के लिए चीनी है। वह क्या है * वहां कर रहा है? क्या यह सूची तत्वों की तरह है? इसके अलावा, वैसे भी ऐसी सूची क्या है? क्या यह भाषा में कुछ बनाया गया है?
बहुत स्पष्ट! मैं अब कुछ समय के लिए इस वाक्यविन्यास को पूरी तरह से प्राप्त करने के साथ संघर्ष कर रहा हूं। –
मैं वास्तव में इस उत्तर के अधिकांश को समझ नहीं पा रहा हूं।आपका उदाहरण केवल मेरे लिए -XPolyKinds के साथ काम करता है - एक और एक्सटेंशन जो मुझे समझ में नहीं आता है - और मेरे लिए 'y foo'' y -> Foo * * x y z' जो आपको मिला है उसके बजाय है। यदि मैं एक फ़ंक्शन foo पेश करने का प्रयास करता हूं - आपने परिभाषा नहीं दी है, इसलिए मैंने "foo x y = foo y' - जीएचसी पैनिक्स" असंभव हुआ "के साथ प्रयास किया। – Cubic