確率的プログラミング 再 ハスケル

  • stackは入れておく
  • stackで専用環境を作る
stack new Prob
cd Prob
 module Main where

 import Control.Applicative
 import Control.Monad
 import Math.Probable

 import qualified Data.Vector.Unboxed as VU

 data Person = Person Int    -- ^ age
                      Double -- ^ weight (kgs)
                      Double -- ^ salary (e.g euros)
     deriving (Eq, Show)

 person :: RandT IO Person
 person =
     Person <$> uniformIn (1, 100)
            <*> uniformIn (2, 130)
            <*> uniformIn (500, 10000)

 randomPersons :: Int -> IO [Person]
 randomPersons n = mwc $ listOf n person

 randomDoubles :: Int -> IO (VU.Vector Double)
 randomDoubles n = mwc $ vectorOf n double

 main :: IO ()
 main = do
     randomPersons 10 >>= mapM_ print
     randomDoubles 10 >>= VU.mapM_ print
  • import内容を確認する
 import Control.Applicative
 import Control.Monad
 import Math.Probable

 import qualified Data.Vector.Unboxed as VU
  • 何もしないで
stack build
    • すると、おこられて、Math.ProbableとData.Vector.Unboxedがないらしいことがわかるので
    • Prob.cablファイルを以下のようにする
executable Prob-exe

  hs-source-dirs:      app
  main-is:             Main.hs
  ghc-options:         -threaded -rtsopts -with-rtsopts=-N
  build-depends:       base
                     , Prob
                     , probable
                     , unboxed-containers
  default-language:    Haskell2010
    • さらに、そうすると、yamlに書け、とエラーメッセージが出るので
packages:
- '.'
# Dependency packages to be pulled from upstream that are not in the resolver
# (e.g., acme-missiles-0.3)
extra-deps:
- probable-0.1.2
- unboxed-containers-0.0.2.4
    • とする
  • ここで
stack build
  • すると、vectorがhidden packageなので、それもbuild-dependsに加えろ、とエラーメッセージが出るので、Prob.cabalを書き換えて
executable Prob-exe

  hs-source-dirs:      app
  main-is:             Main.hs
  ghc-options:         -threaded -rtsopts -with-rtsopts=-N
  build-depends:       base
                     , Prob
                     , probable
                     , unboxed-containers
                     , vector
  default-language:    Haskell2010
  • とすると、stack build終了
stack exec Prob-exe
Person 83 37.873294788582555 996.2628381474327
Person 36 5.635874988642499 5164.250266228791
Person 62 67.09188922231682 5777.085177702781
Person 48 8.616523406348833 1218.3379987643195
Person 8 22.846032690283963 7865.1593404172645
Person 32 41.412364075589736 7199.331383547067
Person 69 4.515743921841548 9319.239235801304
Person 50 118.09027466799888 3669.605109370192
Person 81 21.658429900916232 5747.340092659839
Person 34 71.01180657376266 7525.583718106537
0.12737945567856823
0.8514567003306973
0.6541579405975521
2.0557125737774462e-2
0.6498155838990068
1.7956906238464354e-3
0.9096445185602616
0.8593450397435226
0.9864172274744293
0.6432395193236983