関数型プログラミング言語Haskell Part11at TECH
関数型プログラミング言語Haskell Part11 - 暇つぶし2ch11:デフォルトの名無しさん
09/09/12 16:14:07
質問していい?
以下のモナドのなかで、union は Ord a であるところの Set a にしか使えません、と怒られるんだが、どこで制約課したらいいの?

---------------------------------------------------------------------------
import qualified Data.Set as S

data Distribution a =
Dist { sample :: a, support :: (S.Set a), expectation :: ((a -> Double) -> Double) }

always :: a -> Distribution a
always x = Dist { sample = x, support = S.singleton x, expectation = \f -> f x }

newtype Probability a = Prob { runProbability :: Distribution a }

instance Monad Probability where

  p >>= f =

    let x = runProbability p in

    Prob $ Dist { sample = sample $ runProbability $ f (sample x),

             support = S.fold (\d -> S.union $ support (runProbability $ f d)) (S.empty) (support x),

             expectation = \g -> (expectation x) (\x -> (expectation (runProbability $ f x)) g) }

  return a = Prob $ always a

----------------------------------------------------------------------------
Could not deduce (Ord b) from the context ()
arising from a use of `S.union'
Possible fix:  add (Ord b) to the context of the type signature for `>>='


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch