module SMCDEL.Other.MCTRIANGLE where
data Kind = Muddy | Clean
type State = (Int,Int)
data McModel = McM [State] [State] State deriving Int -> McModel -> ShowS
[McModel] -> ShowS
McModel -> String
(Int -> McModel -> ShowS)
-> (McModel -> String) -> ([McModel] -> ShowS) -> Show McModel
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> McModel -> ShowS
showsPrec :: Int -> McModel -> ShowS
$cshow :: McModel -> String
show :: McModel -> String
$cshowList :: [McModel] -> ShowS
showList :: [McModel] -> ShowS
Show
mcModel :: State -> McModel
mcModel :: State -> McModel
mcModel cur :: State
cur@(Int
c,Int
m) = [State] -> [State] -> State -> McModel
McM [State]
ostates [State]
fstates State
cur where
total :: Int
total = Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
m
ostates :: [State]
ostates = [ ((Int
totalInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
m',Int
m') | Int
m'<-[Int
0..(Int
totalInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)] ]
fstates :: [State]
fstates = [ (Int
totalInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
m', Int
m') | Int
m'<-[Int
0..Int
total ] ]
posFrom :: McModel -> State -> [State]
posFrom :: McModel -> State -> [State]
posFrom (McM [State]
_ [State]
fstates State
_) (Int
oc,Int
om) = (State -> Bool) -> [State] -> [State]
forall a. (a -> Bool) -> [a] -> [a]
filter (State -> [State] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [State]
fstates) [ (Int
ocInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1,Int
om), (Int
oc,Int
omInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) ]
obsFor :: McModel -> Kind -> State
obsFor :: McModel -> Kind -> State
obsFor (McM [State]
_ [State]
_ (Int
curc,Int
curm)) Kind
Clean = (Int
curcInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1,Int
curm)
obsFor (McM [State]
_ [State]
_ (Int
curc,Int
curm)) Kind
Muddy = (Int
curc,Int
curmInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
posFor :: McModel -> Kind -> [State]
posFor :: McModel -> Kind -> [State]
posFor McModel
m Kind
status = McModel -> State -> [State]
posFrom McModel
m (State -> [State]) -> State -> [State]
forall a b. (a -> b) -> a -> b
$ McModel -> Kind -> State
obsFor McModel
m Kind
status
type Quantifier = State -> Bool
some :: Quantifier
some :: State -> Bool
some (Int
_,Int
b) = Int
b Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0
data McFormula = Neg McFormula
| Conj [McFormula]
| Qf Quantifier
| KnowSelf Kind
| NotKnowSelf Kind
nobodyknows :: McFormula
nobodyknows :: McFormula
nobodyknows = [McFormula] -> McFormula
Conj [ Kind -> McFormula
NotKnowSelf Kind
Clean, Kind -> McFormula
NotKnowSelf Kind
Muddy ]
everyoneKnows :: McFormula
everyoneKnows :: McFormula
everyoneKnows = [McFormula] -> McFormula
Conj [ Kind -> McFormula
KnowSelf Kind
Clean, Kind -> McFormula
KnowSelf Kind
Muddy ]
eval :: McModel -> McFormula -> Bool
eval :: McModel -> McFormula -> Bool
eval McModel
m (Neg McFormula
f) = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ McModel -> McFormula -> Bool
eval McModel
m McFormula
f
eval McModel
m (Conj [McFormula]
fs) = (McFormula -> Bool) -> [McFormula] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (McModel -> McFormula -> Bool
eval McModel
m) [McFormula]
fs
eval (McM [State]
_ [State]
_ State
s) (Qf State -> Bool
q) = State -> Bool
q State
s
eval m :: McModel
m@(McM [State]
_ [State]
_ (Int
_,Int
curm)) (KnowSelf Kind
Muddy) = Int
curmInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
0 Bool -> Bool -> Bool
|| [State] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (McModel -> Kind -> [State]
posFor McModel
m Kind
Muddy) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1
eval m :: McModel
m@(McM [State]
_ [State]
_ (Int
curc,Int
_)) (KnowSelf Kind
Clean) = Int
curcInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
0 Bool -> Bool -> Bool
|| [State] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (McModel -> Kind -> [State]
posFor McModel
m Kind
Clean) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1
eval m :: McModel
m@(McM [State]
_ [State]
_ (Int
_,Int
curm)) (NotKnowSelf Kind
Muddy) = Int
curmInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
0 Bool -> Bool -> Bool
|| [State] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (McModel -> Kind -> [State]
posFor McModel
m Kind
Muddy) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2
eval m :: McModel
m@(McM [State]
_ [State]
_ (Int
curc,Int
_)) (NotKnowSelf Kind
Clean) = Int
curcInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
0 Bool -> Bool -> Bool
|| [State] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (McModel -> Kind -> [State]
posFor McModel
m Kind
Clean) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2
mcUpdate :: McModel -> McFormula -> McModel
mcUpdate :: McModel -> McFormula -> McModel
mcUpdate (McM [State]
ostates [State]
fstates State
cur) McFormula
f =
[State] -> [State] -> State -> McModel
McM [State]
ostates' [State]
fstates' State
cur where
fstates' :: [State]
fstates' = (State -> Bool) -> [State] -> [State]
forall a. (a -> Bool) -> [a] -> [a]
filter (\State
s -> McModel -> McFormula -> Bool
eval ([State] -> [State] -> State -> McModel
McM [State]
ostates [State]
fstates State
s) McFormula
f) [State]
fstates
ostates' :: [State]
ostates' = (State -> Bool) -> [State] -> [State]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (State -> Bool) -> State -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [State] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([State] -> Bool) -> (State -> [State]) -> State -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. McModel -> State -> [State]
posFrom ([State] -> [State] -> State -> McModel
McM [] [State]
fstates' State
cur)) [State]
ostates
step :: State -> Int -> McModel
step :: State -> Int -> McModel
step State
s Int
0 = McModel -> McFormula -> McModel
mcUpdate (State -> McModel
mcModel State
s) ((State -> Bool) -> McFormula
Qf State -> Bool
some)
step State
s Int
n = McModel -> McFormula -> McModel
mcUpdate (State -> Int -> McModel
step State
s (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)) McFormula
nobodyknows
showme :: State -> IO ()
showme :: State -> IO ()
showme s :: State
s@(Int
_,Int
m) = (Int -> IO ()) -> [Int] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\Int
n -> String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
n String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ McModel -> String
forall a. Show a => a -> String
show (State -> Int -> McModel
step State
s Int
n)) [Int
0..(Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)]