Variadic составить функцию? - PullRequest
       23

Variadic составить функцию?

13 голосов
/ 11 марта 2012

Я пытаюсь написать композиционную функцию переменной функции.Который в основном (.), за исключением того, что функция второго аргумента является переменной.Это должно позволять такие выражения, как:

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 Ответ

9 голосов
/ 12 марта 2012

Можно ввести его в работу с полиморфными функциями:

{-# 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

...