आप इस सरलीकरण लागू करने के लिए एक RULES
pragma उपयोग कर सकते हैं, लेकिन आप a bit of extra work करना सुनिश्चित करें इससे पहले कि तुम्हारा करने का मौका दिया सामान्य विधि पुनर्लेखन नियम आग नहीं करना है:
{-# RULES
"protect toList" toList = toList';
"protect fromList" fromList = fromList';
"fromList/toList" forall x . fromList' (toList' x) = x; #-}
{-# NOINLINE [0] fromList' #-}
fromList' :: (ListIsomorphic l) => [a] -> l a
fromList' = fromList
{-# NOINLINE [0] toList' #-}
toList' :: (ListIsomorphic l) => l a -> [a]
toList' = toList
यहां एक मूर्खतापूर्ण उदाहरण है पता चलता है कि यह काम करता है:
instance ListIsomorphic Maybe where
toList = error "toList"
fromList = error "fromList"
test1 :: Maybe a -> Maybe a
test1 x = fromList (toList x)
main = print $ test1 $ Just "Hello"
यह Just "Hello"
प्रिंट बाहर erroring के बजाय। इसके अलावा, आप नियमों को फायरिंग देख सकते हैं:
$ ghc -O -ddump-rule-firings --make rewrite-method.hs
[1 of 1] Compiling Main (rewrite-method.hs, rewrite-method.o)
Rule fired: protect toList
Rule fired: protect fromList
Rule fired: unpack
Rule fired: unpack
Rule fired: protect toList
Rule fired: protect fromList
Rule fired: fromList/toList
Rule fired: unpack
Rule fired: Class op show
Rule fired: >#
Rule fired: tagToEnum#
Rule fired: Class op showsPrec
Rule fired: Class op showList
Rule fired: ++
Rule fired: unpack-list
Rule fired: foldr/app
Rule fired: unpack-list
Rule fired: unpack-list
Linking rewrite-method.exe ...
यह वास्तव में "स्पष्ट" नहीं है। यह एक ऐसी संपत्ति है जिसे आपको स्पष्ट रूप से मांगना है। उदाहरण के लिए, आपके पास एक वृक्ष संरचना हो सकती है जिसे आप एक सूची में परिवर्तित कर सकते हैं और आप एक सूची से भी निर्माण कर सकते हैं, लेकिन जहां यह पहचान जरूरी नहीं है। – Cubic
यह उचित लगता है। धन्यवाद। – MaiaVictor
इसके अलावा, 'toList' और 'सेलिस्ट' कॉल के बीच 'l' भिन्न नहीं हो सका? उदाहरण के लिए 'redBlackTreeToBTree = toList। से लिस्ट 'जहां' रेडब्लैक ट्रीटीओबीटी :: रेडब्लैक ट्री ए -> बीटी ए '। –