「Strictable」タイプのクラスを作成しようとしています。その理由は、次のように定義したいからです。
foldl'' f z = foldl' f (make_strict z)
そのため、型でfold''
使用すると、未評価のstrictable
サンクはありません。
だから私は次のことから始めました:
{-# LANGUAGE TypeFamilies #-}
class Strictable a where
type Strict a :: *
make_strict :: a -> Strict a
Int
s およびs のインスタンスを定義するFloat
のは簡単foldl'
です。これらは既に正常に機能しているため、何もする必要はありません。
instance Strictable Int where
type Strict Int = Int
make_strict = id
instance Strictable Float where
type Strict Float = Float
make_strict = id
ここがトリッキーな部分です。foldl'
最も外側のコンストラクターのみをラップ解除するため、たとえばペアを使用すると、foldl'
. 通常のペアから厳密なペアを作成したい。だから私はこれを試しました:
instance (Strictable a, Strictable b) => Strictable (a, b) where
type Strict (a, b) = (! Strict a, ! Strict b)
make_strict (x1, x2) = (make_strict x1, make_strict x2)
残念ながら、コンパイルエラーがたくさん発生しました。これをどのように実装すればよいですか?