14

可変関数合成関数を書こうとしています。これは基本的に(.)、2 番目の引数関数が可変長であることを除いてです。これにより、次のような式が可能になります。

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

何か案は?それは可能ですか?

4

1 に答える 1

10

タイプハックして、多相関数を操作することができます。

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

于 2012-03-12T10:33:47.013 に答える