@@ -41,6 +41,7 @@ This is still a work in progress, any feedback is welcome!
4141## Example
4242
4343```Haskell
44+{-# LANGUAGE DeriveGeneric #-}
4445{-# LANGUAGE FlexibleContexts #-}
4546{-# LANGUAGE TypeOperators #-}
4647{-# LANGUAGE TypeApplications #-}
@@ -48,29 +49,32 @@ This is still a work in progress, any feedback is welcome!
4849
4950module Main where
5051
51−import Matrix.Nat
52+import Matrix.Type
5253import Utils
5354import Dist
5455import GHC.TypeLits
5556import Data.Coerce
57+import GHC.Generics
58+import Control.Category hiding (id)
59+import Prelude hiding ((.))
5660
5761-- Monty Hall Problem
5862data Outcome = Win | Lose
59− deriving (Bounded, Enum, Eq, Show)
63+ deriving (Bounded, Enum, Eq, Show, Generic)
6064
6165switch :: Outcome -> Outcome
6266switch Win = Lose
6367switch Lose = Win
6468
65−firstChoice :: Dist Outcome m
69+firstChoice :: Matrix Double () Outcome
6670firstChoice = col [1/3, 2/3]
6771
68−secondChoice :: Matrix Double 2 2
69−secondChoice = fromF switch
72+secondChoice :: Matrix Double Outcome Outcome
73+secondChoice = fromF' switch
7074
7175-- Dice sum
7276
73−type SS = Natural 1 6
77+type SS = Natural 1 6 -- Sample Space
7478
7579sumSS :: SS -> SS -> Natural 2 12
7680sumSS = coerceNat (+)
@@ -85,49 +89,52 @@ condition (fst, snd) thrd = if fst == snd
8589conditionSS :: (SS, SS) -> SS -> Natural 3 18
8690conditionSS = coerceNat2 condition
8791
88−conditionalThrows = fromF' (uncurry conditionSS) `comp` khatri die (khatri die die)
92+conditionalThrows = fromF' (uncurry conditionSS) . khatri (khatri die die) die
8993
90−die :: Dist (Natural 1 6) 6
91−die = uniform [nat @1 @6 1 .. nat 6]
94+die :: Matrix Double () SS
95+die = col $ map (const (1/6)) [nat @1 @6 1 .. nat 6]
9296
9397-- Sprinkler
9498
95−rain :: Dist Bool 2
96−rain = choose 0.8
99+rain :: Matrix Double () Bool
100+rain = col [0.8, 0.2]
97101
98−sprinkler :: Matrix Double 2 2
102+sprinkler :: Matrix Double Bool Bool
99103sprinkler = fromLists [[0.6, 0.99], [0.4, 0.01]]
100104
101−grass :: Matrix Double 4 2
105+grass :: Matrix Double (Bool, Bool) Bool
102106grass = fromLists [[1, 0.2, 0.1, 0.01], [0, 0.8, 0.9, 0.99]]
103107
104−state = khatri grass identity `comp` khatri sprinkler identity `comp` rain
108+state :: Matrix Double () (Bool, (Bool, Bool))
109+state = khatri grass identity . khatri sprinkler identity . rain
105110
106−grass_wet = row [0,1] `comp` kp1 @Double @2 @4
111+grass_wet :: Matrix Double (Bool, (Bool, Bool)) One
112+grass_wet = row [0,1] . kp1
107113
108−rainning = row [0,1] `comp` kp2 @Double @2 @2 `comp` kp2 @Double @2 @4
114+rainning :: Matrix Double (Bool, (Bool, Bool)) One
115+rainning = row [0,1] . kp2 . kp2
109116
110117main :: IO ()
111118main = do
112119 putStrLn "Monty Hall Problem solution:"
113− prettyPrint (p1 @Double @1 `comp` secondChoice `comp` firstChoice)
120+ prettyPrint (secondChoice . firstChoice)
114121 putStrLn "\n Sum of dices probability:"
115122 prettyPrint (sumSSM `comp` khatri die die)
116− prettyPrintDist @(Natural 2 12) (sumSSM `comp` khatri die die)
117123 putStrLn "\n Conditional dice throw:"
118− prettyPrintDist @(Natural 3 18) conditionalThrows
124+ prettyPrint conditionalThrows
119125 putStrLn "\n Checking that the last result is indeed a distribution: "
120− prettyPrint (bang `comp` sumSSM `comp` khatri die die)
126+ prettyPrint (bang . sumSSM . khatri die die)
121127 putStrLn "\n Probability of grass being wet:"
122− prettyPrint (grass_wet `comp` state)
128+ prettyPrint (grass_wet . state)
123129 putStrLn "\n Probability of rain:"
124− prettyPrint (rainning `comp` state)
130+ prettyPrint (rainning . state)
125131```
126132
127133```Shell
128134Monty Hall Problem solution:
129135┌ ┐
130136│ 0.6666666666666666 │
137+│ 0.3333333333333333 │
131138└ ┘
132139
133140 Sum of dices probability:
@@ -144,37 +151,26 @@ Monty Hall Problem solution:
144151│ 5.555555555555555e-2 │
145152│ 2.7777777777777776e-2 │
146153└ ┘
147−Nat 7 16.666666666666664%
148−Nat 6 13.88888888888889%
149−Nat 8 13.88888888888889%
150−Nat 5 11.11111111111111%
151−Nat 9 11.11111111111111%
152−Nat 4 8.333333333333332%
153−Nat 10 8.333333333333332%
154−Nat 3 5.555555555555555%
155−Nat 11 5.555555555555555%
156−Nat 2 2.7777777777777777%
157−Nat 12 2.7777777777777777%
158−
159154
160155 Conditional dice throw:
161−Nat 9 12.962962962962962%
162−Nat 12 12.962962962962962%
163−Nat 10 11.11111111111111%
164−Nat 11 11.11111111111111%
165−Nat 8 8.333333333333332%
166−Nat 13 8.333333333333332%
167−Nat 6 6.481481481481481%
168−Nat 15 6.481481481481481%
169−Nat 7 5.555555555555555%
170−Nat 14 5.555555555555555%
171−Nat 3 2.7777777777777777%
172−Nat 18 2.7777777777777777%
173−Nat 5 1.8518518518518516%
174−Nat 16 1.8518518518518516%
175−Nat 4 0.9259259259259258%
176−Nat 17 0.9259259259259258%
177−
156+┌ ┐
157+│ 2.7777777777777776e-2 │
158+│ 9.259259259259259e-3 │
159+│ 1.8518518518518517e-2 │
160+│ 6.481481481481481e-2 │
161+│ 5.555555555555555e-2 │
162+│ 8.333333333333333e-2 │
163+│ 0.12962962962962962 │
164+│ 0.1111111111111111 │
165+│ 0.1111111111111111 │
166+│ 0.12962962962962962 │
167+│ 8.333333333333333e-2 │
168+│ 5.555555555555555e-2 │
169+│ 6.481481481481481e-2 │
170+│ 1.8518518518518517e-2 │
171+│ 9.259259259259259e-3 │
172+│ 2.7777777777777776e-2 │
173+└ ┘
178174
179175 Checking that the last result is indeed a distribution:
180176┌ ┐
@@ -182,13 +178,12 @@ Nat 17 0.9259259259259258%
182178└ ┘
183179
184180 Probability of grass being wet:
185−┌ ┐
186−│ 0.44838 │
187−└ ┘
181+┌ ┐
182+│ 0.4483800000000001 │
183+└ ┘
188184
189185 Probability of rain:
190−┌ ┐
191−│ 0.19999999999999998 │
192−└ ┘
193−λ>
186+┌ ┐
187+│ 0.2 │
188+└ ┘
194189```