質問
また、機能類似ても済むの array
.に変換しますリストの配列リストのリストを2次元配列などを扱います。
この作品のようになります:
ghci> arrFromNestedLists ["hello", "world"] :: Array (Int, (Int, ())) Char
array ((0,(0,())),(1,(4,()))) [((0,(0,())),'h'),((0,(1,())),'e'),((0,(2,())),'l'),((0,(3,())),'l'),((0,(4,())),'o'),((1,(0,())),'w'),((1,(1,())),'o'),((1,(2,())),'r'),((1,(3,())),'l'),((1,(4,())),'d')]
(Int, (Int, ()))
ない (Int, Int)
がわからないのprogramaticを増加させる方法の長さのタプル.(側の質問:あるなど。)
の符号化のたくあったかい"の回避策"は、周りにダミーの引数能です。だいております。
このコード、中断に突回避策:
{-# LANGUAGE FlexibleInstances, ScopedTypeVariables, TypeFamilies #-}
type family ListOfIndex i a
type instance ListOfIndex () a = a
type instance ListOfIndex (Int, i) a = [ListOfIndex i a]
class Ix i => ArrConv i where
acBounds :: a -> ListOfIndex i a -> (i, i)
acFlatten :: i -> ListOfIndex i a -> [a]
acBounds
"べき"する :: ListOfIndex i a -> (i, i)
.と同様に、 acFlatten
.それぞれ与えたダミー変数undefined
常に価値を与え)ならないのでコンパイル:(
arrFromNestedLists :: forall i a. ArrConv i => ListOfIndex i a -> Array i a
arrFromNestedLists lst =
listArray
(acBounds (undefined :: a) lst)
(acFlatten (undefined :: i) lst)
上記のダミー undefined
引数で渡します。ようにGHCるインスタンス ListOfIndex
を利用します。
instance ArrConv () where
acBounds _ = const ((), ())
acFlatten _ = (: [])
以下の機能のすべての acBounds
機能のインスタンス ArrConv
, で宣言された外だからこそ私を利用する必要が ScopedTypeVariables
とがわからないので、機能のインスタンス定義..
acSucBounds
:: forall a i. ArrConv i
=> a -> [ListOfIndex i a] -> ((Int, i), (Int, i))
acSucBounds _ lst =
((0, inStart), (length lst - 1, inEnd))
where
(inStart, inEnd) = acBounds (undefined :: a) (head lst)
instance ArrConv i => ArrConv (Int, i) where
acBounds = acSucBounds
acFlatten _ = concatMap (acFlatten (undefined :: i))
解決
acBoundsとacFlattenに余分な引数が必要であることの理由は、種類のa
とi
はそれぞれListOfIndex i a -> (i, i)
とListOfIndex i a -> [a]
から回収することができないということです。回避策の一つは、型acArgs
の一つの方法のListOfIndex i a -> ((i, i), a)
に二つの方法を組み合わせることです。今、唯一の問題は、あまりにも多くの(例えば、我々は単に(Int, i)
を使用することはできません)前と同じ問題を引き起こして、その型を一般から型チェッカーを防ぐ方法でfst . acArgs
のインスタンスでそれを使用することです。
{-# LANGUAGE TypeFamilies, FlexibleInstances #-} import Data.Array type family ListOfIndex i a type instance ListOfIndex () a = a type instance ListOfIndex (Int, i) a = [ListOfIndex i a] class Ix i => ArrConv i where acArgs :: ListOfIndex i a -> ((i, i), [a]) instance ArrConv () where acArgs x = (((), ()), [x]) instance ArrConv i => ArrConv (Int, i) where acArgs lst = (((0, inStart), (length lst - 1, inEnd)), args >>= snd) where args = map acArgs lst (inStart, inEnd) = fst (head args) arrFromNestedLists :: ArrConv i => ListOfIndex i a -> Array i a arrFromNestedLists = uncurry listArray . acArgs
他のヒント
長期保存を希望する場合 acBounds
や acFlatten
別途、追加 タイプレベルのタグ引数 である acBounds
いタイプ acBounds :: Proxy a -> ListOfIndex i a -> (i, i)
.この除去の必要性の undefined
引数は、これまで通り過ぎ (Proxy :: SomeConcreteType)
なや acBounds
せを抽出す為に有値レベルの情報から、同型(untyped)のユニットタイプとなります。