@@ -1,3 +1,4 @@
1+{-# LANGUAGE GADTs #-}
12{-# LANGUAGE DeriveGeneric #-}
23{-# LANGUAGE FlexibleContexts #-}
34{-# LANGUAGE TypeOperators #-}
@@ -15,13 +16,13 @@ import LAoP.Utils
1516import LAoP.Dist
1617import GHC.TypeLits
1718import Data.Coerce
18−import GHC.Generics
19+import qualified GHC.Generics as G
1920import Control.Category hiding (id)
2021import Prelude hiding ((.))
2122
2223-- Monty Hall Problem
2324data Outcome = Win | Lose
24− deriving (Bounded, Enum, Eq, Show, Generic)
25+ deriving (Bounded, Enum, Eq, Show, G.Generic)
2526
2627switch :: Outcome -> Outcome
2728switch Win = Lose
@@ -56,32 +57,41 @@ die :: Matrix Double () SS
5657die = col $ map (const (1/6)) [nat @1 @6 1 .. nat 6]
5758
5859-- Sprinkler
60+data G = Dry | Wet
61+ deriving (Bounded, Enum, Eq, Show, G.Generic)
5962
60−rain :: Matrix Double () Bool
63+data S = Off | On
64+ deriving (Bounded, Enum, Eq, Show, G.Generic)
65+
66+data R = No | Yes
67+ deriving (Bounded, Enum, Eq, Show, G.Generic)
68+
69+rain :: Matrix Double () R
6170rain = col [0.8, 0.2]
6271
63−sprinkler :: Matrix Double Bool Bool
72+sprinkler :: Matrix Double R S
6473sprinkler = fromLists [[0.6, 0.99], [0.4, 0.01]]
6574
66−grass :: Matrix Double (Bool, Bool) Bool
75+grass :: Matrix Double (S, R) G
6776grass = fromLists [[1, 0.2, 0.1, 0.01], [0, 0.8, 0.9, 0.99]]
6877
69−state :: Matrix Double () (Bool, (Bool, Bool))
70−state = khatri grass identity . khatri sprinkler identity . rain
78+tag f = khatri f identity
79+
80+state g s r = tag g `comp` tag s `comp` r
7181
72−grass_wet :: Matrix Double (Bool, (Bool, Bool)) One
82+grass_wet :: Matrix Double (G, (S, R)) One
7383grass_wet = row [0,1] . kp1
7484
75−rainning :: Matrix Double (Bool, (Bool, Bool)) One
85+rainning :: Matrix Double (G, (S, R)) One
7686rainning = row [0,1] . kp2 . kp2
7787
7888-- Alcuin Puzzle
7989
8090data Being = Farmer | Fox | Goose | Beans
81− deriving (Bounded, Enum, Eq, Show, Generic)
91+ deriving (Bounded, Enum, Eq, Show, G.Generic)
8292
8393data Bank = LeftB | RightB
84− deriving (Bounded, Enum, Eq, Show, Generic)
94+ deriving (Bounded, Enum, Eq, Show, G.Generic)
8595
8696eats :: Being -> Being -> Bool
8797eats Fox Goose = True
@@ -152,8 +162,8 @@ exec = do
152162 putStrLn "\n Checking that the last result is indeed a distribution: "
153163 prettyPrint (bang . sumSSM . khatri die die)
154164 putStrLn "\n Probability of grass being wet:"
155− prettyPrint (grass_wet . state)
165+ prettyPrint (grass_wet . state grass sprinkler rain)
156166 putStrLn "\n Probability of rain:"
157− prettyPrint (rainning . state)
167+ prettyPrint (rainning . state grass sprinkler rain)
158168 putStrLn "\n Is the arbitrary state a valid state? (Alcuin Puzzle)"
159169 print (inv bankStateR)