2012-03-11 15 views
13

मैं एक वैरैडिक फ़ंक्शन संरचना फ़ंक्शन लिखने की कोशिश कर रहा हूं। जो मूल रूप से (.) है, सिवाय इसके कि दूसरा तर्क फ़ंक्शन विविधतापूर्ण है। इस भाव की तरह की अनुमति चाहिए:वैराडिक रचना समारोह?

map even . zipWith (+) 

या सिर्फ

map even . zipWith 

वर्तमान में मैं क्या काम करता है पहुँच गए हैं अगर मैं IncoherentInstances जोड़ सकते हैं और पहला तर्क समारोह के लिए एक गैर बहुरूपी उदाहरण की आवश्यकता है।

{-# LANGUAGE FlexibleInstances, OverlappingInstances, MultiParamTypeClasses, 
FunctionalDependencies, UndecidableInstances, KindSignatures #-} 

class Comp a b c d | c -> d where 
    comp :: (a -> b) -> c -> d 

instance Comp a b (a :: *) (b :: *) where 
    comp f g = f g 

instance Comp c d b e => Comp c d (a -> b) (a -> e) where 
    comp f g = comp f . g 

कोई विचार? क्या यह भी संभव है?

+1

से बच सकते हैं आप आप "variadic समारोह रचना क्या मतलब है और अधिक एक सा समझा सकता "? शायद कुछ उदाहरण जोड़ें। –

+0

मैंने अंतिम संपादन में थोड़ा सा स्पष्ट किया। इसके अलावा, दो दिए गए उदाहरणों में क्या गलत है? – is7s

+0

ओह, क्षमा करें। उदाहरण ठीक हैं। यह मेरे लिए स्पष्ट नहीं था कि वे टाइपशेक नहीं करते हैं। –

उत्तर

9

यह संभव है यह बहुरूपी कार्यों के साथ काम करने में टाइप-हैक करने:

{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, 
    IncoherentInstances, UndecidableInstances, 
    FunctionalDependencies, TypeFamilies, 
    NoMonomorphismRestriction #-} 


class Comp a b c | a b -> c where 
    (...) :: a -> b -> c 

instance (a ~ c, r ~ b) => Comp (a -> b) c r where 
    f ... g = f g 

instance (Comp (a -> b) d r1, r ~ (c -> r1)) => Comp (a -> b) (c -> d) r where 
    f ... g = \c -> f ... g c 

t1 = map even ... zipWith (+) 
t2 = map even ... zipWith 
t3 = (+1) ... foldr 

लेकिन मुझे शक है आप IncoherentInstances

संबंधित मुद्दे