Haskellで多変量関数を書くことについてのこの記事を読んだ後、私は自分自身のいくつかを書こうとしました。
最初はそれを一般化しようと思ったので、与えられた引数を折りたたむことで可変個引数関数を返す関数を作成できました。
{-# OPTIONS -fglasgow-exts #-}
module Collapse where
class Collapse a r | r -> a where
collapse :: (a -> a -> a) -> a -> r
instance Collapse a a where
collapse _ = id
instance (Collapse a r) => Collapse a (a -> r) where
collapse f a a' = collapse f (f a a')
しかし、コンパイラはそれを気に入らなかった:
Collapse.hs:5:9:
Functional dependencies conflict between instance declarations:
instance Collapse a a -- Defined at Collapse.hs:5:9-20
instance (Collapse a r) => Collapse a (a -> r)
-- Defined at Collapse.hs:7:9-43
ただし、戻って最終結果のラッパータイプを追加すると、次のように機能しました。
module Collapse where
class Collapse a r | r -> a where
collapse :: (a -> a -> a) -> a -> r
data C a = C a
instance Collapse a (C a) where
collapse _ = C . id
instance (Collapse a r) => Collapse a (a -> r) where
collapse f a a' = collapse f (f a a')
sum :: (Num a, Collapse a r) => a -> r
sum = collapse (+)
この変更を行うと、正常にコンパイルされ、でcollapse
関数を使用できるようになりましたghci
。
ghci> let C s = Collapse.sum 1 2 3 in s
6
最終結果にラッパータイプが必要な理由がわかりません。誰かがそれを説明できれば、私はそれを高く評価します。コンパイラーが機能依存性に問題があると言っているのはわかりますが、fundepsの適切な使用法についてはまだ理解していません。
後で、私は別の方法を取り、リストを取得して値を返す関数の可変個引数関数発生器を定義しようとしました。私は同じコンテナトリックをしなければならず、また許可しなければなりませんでしたUndecidableInstances
。
{-# OPTIONS -fglasgow-exts #-}
{-# LANGUAGE UndecidableInstances #-}
module Variadic where
class Variadic a b r | r -> a, r -> b where
variadic :: ([a] -> b) -> r
data V a = V a
instance Variadic a b (V b) where
variadic f = V $ f []
instance (Variadic a b r) => Variadic a b (a -> r) where
variadic f a = variadic (f . (a:))
list :: Variadic a [a] r => r
list = variadic . id
foldl :: (Variadic b a r) => (a -> b -> a) -> a -> r
foldl f a = variadic (Prelude.foldl f a)
UndecidableInstances
コンパイラが私のインスタンス宣言が違法であると文句を言うことを許可せずに:
Variadic.hs:7:0:
Illegal instance declaration for `Variadic a b (V b)'
(the Coverage Condition fails for one of the functional dependencies;
Use -XUndecidableInstances to permit this)
In the instance declaration for `Variadic a b (V b)'
Variadic.hs:9:0:
Illegal instance declaration for `Variadic a b (a -> r)'
(the Coverage Condition fails for one of the functional dependencies;
Use -XUndecidableInstances to permit this)
In the instance declaration for `Variadic a b (a -> r)'
ただし、コンパイルすると、ghciで正常に使用できます。
ghci> let V l = Variadic.list 1 2 3 in l
[1,2,3]
ghci> let vall p = Variadic.foldl (\b a -> b && (p a)) True
ghci> :t vall
vall :: (Variadic b Bool r) => (b -> Bool) -> r
ghci> let V b = vall (>0) 1 2 3 in b
True
私が探しているのは、最終値のコンテナータイプが必要な理由と、さまざまな機能依存性がすべて必要な理由の説明だと思います。
また、これは奇妙に思えました:
ghci> let vsum = Variadic.foldl (+) 0
<interactive>:1:10:
Ambiguous type variables `a', `r' in the constraint:
`Variadic a a r'
arising from a use of `Variadic.foldl' at <interactive>:1:10-29
Probable fix: add a type signature that fixes these type variable(s)
<interactive>:1:10:
Ambiguous type variable `a'in the constraint:
`Num a' arising from the literal `0' at <interactive>:1:29
Probable fix: add a type signature that fixes these type variable(s)
ghci> let vsum' = Variadic.foldl (+)
ghci> :t vsum'
(Num a, Variadic a a r) => t -> a -> r
ghci> :t vsum' 0
(Num a, Variadic a a r) => a -> r
ghci> let V s = vsum' 0 1 2 3 in s
6
それは許可の結果UndecidableInstances
だと思いますが、わかりません。何が起こっているのかをもっとよく理解したいと思います。