{-# LANGUAGE FlexibleInstances, FlexibleContexts #-}
module SMCDEL.Other.Planning where
import Data.Dynamic
import Data.HasCacBDD hiding (Top,Bot)
import Data.List (intersect,nub,sort,(\\))
import qualified Data.Map as M
import SMCDEL.Internal.Help (apply)
import SMCDEL.Language
import qualified SMCDEL.Symbolic.S5 as Sym
import qualified SMCDEL.Symbolic.K as SymK
import qualified SMCDEL.Explicit.S5 as Exp
import qualified SMCDEL.Explicit.K as ExpK
class IsPlan a where
reaches :: a -> Form -> Form
reachesOn :: (Semantics o) => a -> Form -> o -> Bool
reachesOn a
plan Form
f o
start = o
start o -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
|= a -> Form -> Form
forall a. IsPlan a => a -> Form -> Form
reaches a
plan Form
f
type OfflinePlan = [Form]
instance IsPlan OfflinePlan where
reaches :: OfflinePlan -> Form -> Form
reaches [] Form
goal = Form
goal
reaches (Form
step:OfflinePlan
rest) Form
goal = OfflinePlan -> Form
Conj [Form
step, Form -> Form -> Form
PubAnnounce Form
step (OfflinePlan -> Form -> Form
forall a. IsPlan a => a -> Form -> Form
reaches OfflinePlan
rest Form
goal)]
offlineSearch :: (Eq a, Semantics a, Update a Form) =>
Int ->
a ->
[Form] ->
[Form] ->
Form ->
[OfflinePlan]
offlineSearch :: forall a.
(Eq a, Semantics a, Update a Form) =>
Int -> a -> OfflinePlan -> OfflinePlan -> Form -> [OfflinePlan]
offlineSearch Int
roundsLeft a
now OfflinePlan
acts OfflinePlan
safety Form
goal
| a
now a -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
|= Form
goal = [ [] ]
| Int
roundsLeft Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = [ ]
| Bool
otherwise = [ Form
a Form -> OfflinePlan -> OfflinePlan
forall a. a -> [a] -> [a]
: OfflinePlan
rest
| Form
a <- OfflinePlan
acts
, a
now a -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
|= Form
a
, let new :: a
new = a
now a -> Form -> a
forall a b. Update a b => a -> b -> a
`update` Form
a
, a
new a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
now
, (Form -> Bool) -> OfflinePlan -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (a
new a -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
|=) OfflinePlan
safety
, OfflinePlan
rest <- Int -> a -> OfflinePlan -> OfflinePlan -> Form -> [OfflinePlan]
forall a.
(Eq a, Semantics a, Update a Form) =>
Int -> a -> OfflinePlan -> OfflinePlan -> Form -> [OfflinePlan]
offlineSearch (Int
roundsLeftInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) a
new OfflinePlan
acts OfflinePlan
safety Form
goal ]
data Plan a = Stop
| Do String a (Plan a)
| Check Form (Plan a)
| IfThenElse Form (Plan a) (Plan a)
deriving (Plan a -> Plan a -> Bool
(Plan a -> Plan a -> Bool)
-> (Plan a -> Plan a -> Bool) -> Eq (Plan a)
forall a. Eq a => Plan a -> Plan a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Plan a -> Plan a -> Bool
== :: Plan a -> Plan a -> Bool
$c/= :: forall a. Eq a => Plan a -> Plan a -> Bool
/= :: Plan a -> Plan a -> Bool
Eq,Eq (Plan a)
Eq (Plan a) =>
(Plan a -> Plan a -> Ordering)
-> (Plan a -> Plan a -> Bool)
-> (Plan a -> Plan a -> Bool)
-> (Plan a -> Plan a -> Bool)
-> (Plan a -> Plan a -> Bool)
-> (Plan a -> Plan a -> Plan a)
-> (Plan a -> Plan a -> Plan a)
-> Ord (Plan a)
Plan a -> Plan a -> Bool
Plan a -> Plan a -> Ordering
Plan a -> Plan a -> Plan a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (Plan a)
forall a. Ord a => Plan a -> Plan a -> Bool
forall a. Ord a => Plan a -> Plan a -> Ordering
forall a. Ord a => Plan a -> Plan a -> Plan a
$ccompare :: forall a. Ord a => Plan a -> Plan a -> Ordering
compare :: Plan a -> Plan a -> Ordering
$c< :: forall a. Ord a => Plan a -> Plan a -> Bool
< :: Plan a -> Plan a -> Bool
$c<= :: forall a. Ord a => Plan a -> Plan a -> Bool
<= :: Plan a -> Plan a -> Bool
$c> :: forall a. Ord a => Plan a -> Plan a -> Bool
> :: Plan a -> Plan a -> Bool
$c>= :: forall a. Ord a => Plan a -> Plan a -> Bool
>= :: Plan a -> Plan a -> Bool
$cmax :: forall a. Ord a => Plan a -> Plan a -> Plan a
max :: Plan a -> Plan a -> Plan a
$cmin :: forall a. Ord a => Plan a -> Plan a -> Plan a
min :: Plan a -> Plan a -> Plan a
Ord,Int -> Plan a -> ShowS
[Plan a] -> ShowS
Plan a -> String
(Int -> Plan a -> ShowS)
-> (Plan a -> String) -> ([Plan a] -> ShowS) -> Show (Plan a)
forall a. Show a => Int -> Plan a -> ShowS
forall a. Show a => [Plan a] -> ShowS
forall a. Show a => Plan a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Plan a -> ShowS
showsPrec :: Int -> Plan a -> ShowS
$cshow :: forall a. Show a => Plan a -> String
show :: Plan a -> String
$cshowList :: forall a. Show a => [Plan a] -> ShowS
showList :: [Plan a] -> ShowS
Show)
execute :: Update state a => Plan a -> state -> Maybe state
execute :: forall state a. Update state a => Plan a -> state -> Maybe state
execute Plan a
Stop state
s = state -> Maybe state
forall a. a -> Maybe a
Just state
s
execute (Do String
_ a
action Plan a
rest) state
s | state
s state -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
|= a -> Form
forall a. HasPrecondition a => a -> Form
preOf a
action = Plan a -> state -> Maybe state
forall state a. Update state a => Plan a -> state -> Maybe state
execute Plan a
rest (state
s state -> a -> state
forall a b. Update a b => a -> b -> a
`update` a
action)
| Bool
otherwise = Maybe state
forall a. Maybe a
Nothing
execute (Check Form
f Plan a
rest) state
s = if state
s state -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
|= Form
f then Plan a -> state -> Maybe state
forall state a. Update state a => Plan a -> state -> Maybe state
execute Plan a
rest state
s else Maybe state
forall a. Maybe a
Nothing
execute (IfThenElse Form
f Plan a
pa Plan a
pb) state
s = if state
s state -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
|= Form
f then Plan a -> state -> Maybe state
forall state a. Update state a => Plan a -> state -> Maybe state
execute Plan a
pa state
s else Plan a -> state -> Maybe state
forall state a. Update state a => Plan a -> state -> Maybe state
execute Plan a
pb state
s
instance IsPlan (Plan Form) where
reaches :: Plan Form -> Form -> Form
reaches Plan Form
Stop Form
goal = Form
goal
reaches (Do String
_ Form
toBeAn Plan Form
next) Form
goal = OfflinePlan -> Form
Conj [Form
toBeAn, Form -> Form -> Form
PubAnnounce Form
toBeAn (Plan Form -> Form -> Form
forall a. IsPlan a => a -> Form -> Form
reaches Plan Form
next Form
goal)]
reaches (Check Form
toBeChecked Plan Form
next) Form
goal = OfflinePlan -> Form
Conj [Form
toBeChecked, Plan Form -> Form -> Form
forall a. IsPlan a => a -> Form -> Form
reaches Plan Form
next Form
goal]
reaches (IfThenElse Form
condition Plan Form
planA Plan Form
planB) Form
goal =
OfflinePlan -> Form
Conj [ Form
condition Form -> Form -> Form
`Impl` Plan Form -> Form -> Form
forall a. IsPlan a => a -> Form -> Form
reaches Plan Form
planA Form
goal, Form -> Form
Neg Form
condition Form -> Form -> Form
`Impl` Plan Form -> Form -> Form
forall a. IsPlan a => a -> Form -> Form
reaches Plan Form
planB Form
goal ]
instance IsPlan (Plan Sym.MultipointedEvent) where
reaches :: Plan MultipointedEvent -> Form -> Form
reaches Plan MultipointedEvent
Stop Form
goal = Form
goal
reaches (Do String
actLabel MultipointedEvent
action Plan MultipointedEvent
next) Form
goal = DynamicOp -> Form -> Form
dix (String -> Dynamic -> DynamicOp
Dyn String
actLabel (MultipointedEvent -> Dynamic
forall a. Typeable a => a -> Dynamic
toDyn MultipointedEvent
action)) (Plan MultipointedEvent -> Form -> Form
forall a. IsPlan a => a -> Form -> Form
reaches Plan MultipointedEvent
next Form
goal)
reaches (Check Form
toBeChecked Plan MultipointedEvent
next) Form
goal = OfflinePlan -> Form
Conj [Form
toBeChecked, Plan MultipointedEvent -> Form -> Form
forall a. IsPlan a => a -> Form -> Form
reaches Plan MultipointedEvent
next Form
goal]
reaches (IfThenElse Form
check Plan MultipointedEvent
planA Plan MultipointedEvent
planB) Form
goal =
OfflinePlan -> Form
Conj [ Form
check Form -> Form -> Form
`Impl` Plan MultipointedEvent -> Form -> Form
forall a. IsPlan a => a -> Form -> Form
reaches Plan MultipointedEvent
planA Form
goal, Form -> Form
Neg Form
check Form -> Form -> Form
`Impl` Plan MultipointedEvent -> Form -> Form
forall a. IsPlan a => a -> Form -> Form
reaches Plan MultipointedEvent
planB Form
goal ]
dix :: DynamicOp -> Form -> Form
dix :: DynamicOp -> Form -> Form
dix DynamicOp
op Form
f = OfflinePlan -> Form
Conj [DynamicOp -> Form -> Form
Dia DynamicOp
op Form
Top, DynamicOp -> Form -> Form
box DynamicOp
op Form
f]
data Task state action = Task state [(String,action)] Form
deriving (Task state action -> Task state action -> Bool
(Task state action -> Task state action -> Bool)
-> (Task state action -> Task state action -> Bool)
-> Eq (Task state action)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall state action.
(Eq state, Eq action) =>
Task state action -> Task state action -> Bool
$c== :: forall state action.
(Eq state, Eq action) =>
Task state action -> Task state action -> Bool
== :: Task state action -> Task state action -> Bool
$c/= :: forall state action.
(Eq state, Eq action) =>
Task state action -> Task state action -> Bool
/= :: Task state action -> Task state action -> Bool
Eq,Eq (Task state action)
Eq (Task state action) =>
(Task state action -> Task state action -> Ordering)
-> (Task state action -> Task state action -> Bool)
-> (Task state action -> Task state action -> Bool)
-> (Task state action -> Task state action -> Bool)
-> (Task state action -> Task state action -> Bool)
-> (Task state action -> Task state action -> Task state action)
-> (Task state action -> Task state action -> Task state action)
-> Ord (Task state action)
Task state action -> Task state action -> Bool
Task state action -> Task state action -> Ordering
Task state action -> Task state action -> Task state action
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall state action.
(Ord state, Ord action) =>
Eq (Task state action)
forall state action.
(Ord state, Ord action) =>
Task state action -> Task state action -> Bool
forall state action.
(Ord state, Ord action) =>
Task state action -> Task state action -> Ordering
forall state action.
(Ord state, Ord action) =>
Task state action -> Task state action -> Task state action
$ccompare :: forall state action.
(Ord state, Ord action) =>
Task state action -> Task state action -> Ordering
compare :: Task state action -> Task state action -> Ordering
$c< :: forall state action.
(Ord state, Ord action) =>
Task state action -> Task state action -> Bool
< :: Task state action -> Task state action -> Bool
$c<= :: forall state action.
(Ord state, Ord action) =>
Task state action -> Task state action -> Bool
<= :: Task state action -> Task state action -> Bool
$c> :: forall state action.
(Ord state, Ord action) =>
Task state action -> Task state action -> Bool
> :: Task state action -> Task state action -> Bool
$c>= :: forall state action.
(Ord state, Ord action) =>
Task state action -> Task state action -> Bool
>= :: Task state action -> Task state action -> Bool
$cmax :: forall state action.
(Ord state, Ord action) =>
Task state action -> Task state action -> Task state action
max :: Task state action -> Task state action -> Task state action
$cmin :: forall state action.
(Ord state, Ord action) =>
Task state action -> Task state action -> Task state action
min :: Task state action -> Task state action -> Task state action
Ord,Int -> Task state action -> ShowS
[Task state action] -> ShowS
Task state action -> String
(Int -> Task state action -> ShowS)
-> (Task state action -> String)
-> ([Task state action] -> ShowS)
-> Show (Task state action)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall state action.
(Show state, Show action) =>
Int -> Task state action -> ShowS
forall state action.
(Show state, Show action) =>
[Task state action] -> ShowS
forall state action.
(Show state, Show action) =>
Task state action -> String
$cshowsPrec :: forall state action.
(Show state, Show action) =>
Int -> Task state action -> ShowS
showsPrec :: Int -> Task state action -> ShowS
$cshow :: forall state action.
(Show state, Show action) =>
Task state action -> String
show :: Task state action -> String
$cshowList :: forall state action.
(Show state, Show action) =>
[Task state action] -> ShowS
showList :: [Task state action] -> ShowS
Show)
findPlan :: (Eq state, Update state action) => Int -> Task state action -> [Plan action]
findPlan :: forall state action.
(Eq state, Update state action) =>
Int -> Task state action -> [Plan action]
findPlan Int
d (Task state
now [(String, action)]
acts Form
goal)
| state
now state -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
|= Form
goal = [ Plan action
forall a. Plan a
Stop ]
| Int
d Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = [ ]
| Bool
otherwise = [ String -> action -> Plan action -> Plan action
forall a. String -> a -> Plan a -> Plan a
Do String
lbl action
act Plan action
continue
| (String
lbl,action
act) <- [(String, action)]
acts
, state -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
isTrue state
now (action -> Form
forall a. HasPrecondition a => a -> Form
preOf action
act)
, state
now state -> state -> Bool
forall a. Eq a => a -> a -> Bool
/= state -> action -> state
forall a b. Update a b => a -> b -> a
update state
now action
act
, Plan action
continue <- Int -> Task state action -> [Plan action]
forall state action.
(Eq state, Update state action) =>
Int -> Task state action -> [Plan action]
findPlan (Int
dInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) (state -> [(String, action)] -> Form -> Task state action
forall state action.
state -> [(String, action)] -> Form -> Task state action
Task (state -> action -> state
forall a b. Update a b => a -> b -> a
update state
now action
act) [(String, action)]
acts Form
goal) ]
class Eq o => HasPerspective o where
asSeenBy :: o -> Agent -> o
isLocalFor :: o -> Agent -> Bool
isLocalFor o
state String
i = o
state o -> String -> o
forall o. HasPerspective o => o -> String -> o
`asSeenBy` String
i o -> o -> Bool
forall a. Eq a => a -> a -> Bool
== o
state
instance HasPerspective Exp.MultipointedModelS5 where
asSeenBy :: MultipointedModelS5 -> String -> MultipointedModelS5
asSeenBy (m :: KripkeModelS5
m@(Exp.KrMS5 [Int]
_ [(String, Partition)]
rel [(Int, Assignment)]
_), [Int]
actualWorlds) String
agent = (KripkeModelS5
m, [Int]
seenWorlds) where
seenWorlds :: [Int]
seenWorlds = [Int] -> [Int]
forall a. Ord a => [a] -> [a]
sort ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ Partition -> [Int]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (Partition -> [Int]) -> Partition -> [Int]
forall a b. (a -> b) -> a -> b
$ ([Int] -> Bool) -> Partition -> Partition
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([Int] -> Bool) -> [Int] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Int] -> Bool) -> ([Int] -> [Int]) -> [Int] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> [Int] -> [Int]
forall a. Eq a => [a] -> [a] -> [a]
intersect [Int]
actualWorlds) ([(String, Partition)] -> String -> Partition
forall a b. (Show a, Show b, Eq a) => Rel a b -> a -> b
apply [(String, Partition)]
rel String
agent)
instance HasPerspective ExpK.MultipointedModel where
asSeenBy :: MultipointedModel -> String -> MultipointedModel
asSeenBy (ExpK.KrM Map Int (Map Prp Bool, Map String [Int])
m, [Int]
actualWorlds) String
agent = (Map Int (Map Prp Bool, Map String [Int]) -> KripkeModel
ExpK.KrM Map Int (Map Prp Bool, Map String [Int])
m, [Int]
seenWorlds) where
seenWorlds :: [Int]
seenWorlds = [Int] -> [Int]
forall a. Ord a => [a] -> [a]
sort ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ [Int] -> [Int]
forall a. Eq a => [a] -> [a]
nub ([Int] -> [Int]) -> [Int] -> [Int]
forall a b. (a -> b) -> a -> b
$ ([Int] -> Int -> (Map Prp Bool, Map String [Int]) -> [Int])
-> [Int] -> Map Int (Map Prp Bool, Map String [Int]) -> [Int]
forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
M.foldlWithKey
(\ [Int]
vs Int
w (Map Prp Bool
_,Map String [Int]
rel) -> [Int]
vs [Int] -> [Int] -> [Int]
forall a. [a] -> [a] -> [a]
++ Partition -> [Int]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ Map String [Int]
rel Map String [Int] -> String -> [Int]
forall k a. Ord k => Map k a -> k -> a
M.! String
agent | Int
w Int -> [Int] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Int]
actualWorlds ])
[]
Map Int (Map Prp Bool, Map String [Int])
m
instance HasPerspective Sym.MultipointedKnowScene where
asSeenBy :: MultipointedKnowScene -> String -> MultipointedKnowScene
asSeenBy (Sym.KnS [Prp]
props Bdd
lawbdd [(String, [Prp])]
obs, Bdd
statesBdd) String
agent =
([Prp] -> Bdd -> [(String, [Prp])] -> KnowStruct
Sym.KnS [Prp]
props Bdd
lawbdd [(String, [Prp])]
obs, Bdd
seenStatesBdd) where
seenStatesBdd :: Bdd
seenStatesBdd = [Int] -> Bdd -> Bdd
existsSet [Int]
otherps Bdd
statesBdd
otherps :: [Int]
otherps = (Prp -> Int) -> [Prp] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Prp -> Int
forall a. Enum a => a -> Int
fromEnum ([Prp]
props [Prp] -> [Prp] -> [Prp]
forall a. Eq a => [a] -> [a] -> [a]
\\ [(String, [Prp])] -> String -> [Prp]
forall a b. (Show a, Show b, Eq a) => Rel a b -> a -> b
apply [(String, [Prp])]
obs String
agent)
flipRelBdd :: [Prp] -> SymK.RelBDD -> SymK.RelBDD
flipRelBdd :: [Prp] -> RelBDD -> RelBDD
flipRelBdd [Prp]
props = (Bdd -> Bdd) -> RelBDD -> RelBDD
forall a b. (a -> b) -> Tagged Dubbel a -> Tagged Dubbel b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Bdd -> Bdd) -> RelBDD -> RelBDD)
-> (Bdd -> Bdd) -> RelBDD -> RelBDD
forall a b. (a -> b) -> a -> b
$ [(Prp, Prp)] -> Bdd -> Bdd
Sym.relabelWith [(Prp -> Prp
SymK.mvP Prp
p, Prp -> Prp
SymK.cpP Prp
p) | Prp
p <- [Prp]
props ]
instance HasPerspective SymK.MultipointedBelScene where
asSeenBy :: MultipointedBelScene -> String -> MultipointedBelScene
asSeenBy (SymK.BlS [Prp]
props Bdd
lawbdd Map String RelBDD
obsBdds, Bdd
statesBdd) String
agent =
([Prp] -> Bdd -> Map String RelBDD -> BelStruct
SymK.BlS [Prp]
props Bdd
lawbdd Map String RelBDD
obsBdds, Bdd
seenStatesBdd) where
flippedObsBdd :: RelBDD
flippedObsBdd = [Prp] -> RelBDD -> RelBDD
flipRelBdd [Prp]
props (Map String RelBDD
obsBdds Map String RelBDD -> String -> RelBDD
forall k a. Ord k => Map k a -> k -> a
M.! String
agent)
seenStatesBdd :: Bdd
seenStatesBdd = RelBDD -> Bdd
SymK.unmvBdd (RelBDD -> Bdd) -> RelBDD -> Bdd
forall a b. (a -> b) -> a -> b
$ [Int] -> Bdd -> Bdd
existsSet ((Prp -> Int) -> [Prp] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Prp -> Int
forall a. Enum a => a -> Int
fromEnum ([Prp] -> [Int]) -> [Prp] -> [Int]
forall a b. (a -> b) -> a -> b
$ [Prp] -> [Prp]
SymK.cp [Prp]
props) (Bdd -> Bdd) -> RelBDD -> RelBDD
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
(Bdd -> Bdd -> Bdd
con (Bdd -> Bdd -> Bdd) -> RelBDD -> Tagged Dubbel (Bdd -> Bdd)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bdd -> RelBDD
SymK.cpBdd Bdd
statesBdd Tagged Dubbel (Bdd -> Bdd) -> RelBDD -> RelBDD
forall a b.
Tagged Dubbel (a -> b) -> Tagged Dubbel a -> Tagged Dubbel b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> RelBDD
flippedObsBdd)
data CoopTask state action = CoopTask state [Owned action] Form
deriving (CoopTask state action -> CoopTask state action -> Bool
(CoopTask state action -> CoopTask state action -> Bool)
-> (CoopTask state action -> CoopTask state action -> Bool)
-> Eq (CoopTask state action)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall state action.
(Eq state, Eq action) =>
CoopTask state action -> CoopTask state action -> Bool
$c== :: forall state action.
(Eq state, Eq action) =>
CoopTask state action -> CoopTask state action -> Bool
== :: CoopTask state action -> CoopTask state action -> Bool
$c/= :: forall state action.
(Eq state, Eq action) =>
CoopTask state action -> CoopTask state action -> Bool
/= :: CoopTask state action -> CoopTask state action -> Bool
Eq,Eq (CoopTask state action)
Eq (CoopTask state action) =>
(CoopTask state action -> CoopTask state action -> Ordering)
-> (CoopTask state action -> CoopTask state action -> Bool)
-> (CoopTask state action -> CoopTask state action -> Bool)
-> (CoopTask state action -> CoopTask state action -> Bool)
-> (CoopTask state action -> CoopTask state action -> Bool)
-> (CoopTask state action
-> CoopTask state action -> CoopTask state action)
-> (CoopTask state action
-> CoopTask state action -> CoopTask state action)
-> Ord (CoopTask state action)
CoopTask state action -> CoopTask state action -> Bool
CoopTask state action -> CoopTask state action -> Ordering
CoopTask state action
-> CoopTask state action -> CoopTask state action
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall state action.
(Ord state, Ord action) =>
Eq (CoopTask state action)
forall state action.
(Ord state, Ord action) =>
CoopTask state action -> CoopTask state action -> Bool
forall state action.
(Ord state, Ord action) =>
CoopTask state action -> CoopTask state action -> Ordering
forall state action.
(Ord state, Ord action) =>
CoopTask state action
-> CoopTask state action -> CoopTask state action
$ccompare :: forall state action.
(Ord state, Ord action) =>
CoopTask state action -> CoopTask state action -> Ordering
compare :: CoopTask state action -> CoopTask state action -> Ordering
$c< :: forall state action.
(Ord state, Ord action) =>
CoopTask state action -> CoopTask state action -> Bool
< :: CoopTask state action -> CoopTask state action -> Bool
$c<= :: forall state action.
(Ord state, Ord action) =>
CoopTask state action -> CoopTask state action -> Bool
<= :: CoopTask state action -> CoopTask state action -> Bool
$c> :: forall state action.
(Ord state, Ord action) =>
CoopTask state action -> CoopTask state action -> Bool
> :: CoopTask state action -> CoopTask state action -> Bool
$c>= :: forall state action.
(Ord state, Ord action) =>
CoopTask state action -> CoopTask state action -> Bool
>= :: CoopTask state action -> CoopTask state action -> Bool
$cmax :: forall state action.
(Ord state, Ord action) =>
CoopTask state action
-> CoopTask state action -> CoopTask state action
max :: CoopTask state action
-> CoopTask state action -> CoopTask state action
$cmin :: forall state action.
(Ord state, Ord action) =>
CoopTask state action
-> CoopTask state action -> CoopTask state action
min :: CoopTask state action
-> CoopTask state action -> CoopTask state action
Ord,Int -> CoopTask state action -> ShowS
[CoopTask state action] -> ShowS
CoopTask state action -> String
(Int -> CoopTask state action -> ShowS)
-> (CoopTask state action -> String)
-> ([CoopTask state action] -> ShowS)
-> Show (CoopTask state action)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall state action.
(Show state, Show action) =>
Int -> CoopTask state action -> ShowS
forall state action.
(Show state, Show action) =>
[CoopTask state action] -> ShowS
forall state action.
(Show state, Show action) =>
CoopTask state action -> String
$cshowsPrec :: forall state action.
(Show state, Show action) =>
Int -> CoopTask state action -> ShowS
showsPrec :: Int -> CoopTask state action -> ShowS
$cshow :: forall state action.
(Show state, Show action) =>
CoopTask state action -> String
show :: CoopTask state action -> String
$cshowList :: forall state action.
(Show state, Show action) =>
[CoopTask state action] -> ShowS
showList :: [CoopTask state action] -> ShowS
Show)
instance (HasPerspective state, Eq action) => HasPerspective (CoopTask state action) where
asSeenBy :: CoopTask state action -> String -> CoopTask state action
asSeenBy (CoopTask state
start [Owned action]
acts Form
goal) String
agent = state -> [Owned action] -> Form -> CoopTask state action
forall state action.
state -> [Owned action] -> Form -> CoopTask state action
CoopTask (state
start state -> String -> state
forall o. HasPerspective o => o -> String -> o
`asSeenBy` String
agent) [Owned action]
acts Form
goal
type Labelled a = (String,a)
type Owned action = (Agent,Labelled action)
type ICPlan action = [Owned action]
instance Typeable a => IsPlan (ICPlan a) where
reaches :: ICPlan a -> Form -> Form
reaches [] Form
goal = Form
goal
reaches ((String
agent,(String
label,a
action)):ICPlan a
rest) Form
goal =
String -> Form -> Form
K String
agent (DynamicOp -> Form -> Form
dix (String -> Dynamic -> DynamicOp
Dyn String
label (a -> Dynamic
forall a. Typeable a => a -> Dynamic
toDyn a
action)) (ICPlan a -> Form -> Form
forall a. IsPlan a => a -> Form -> Form
reaches ICPlan a
rest Form
goal))
ppICPlan :: ICPlan action -> String
ppICPlan :: forall action. ICPlan action -> String
ppICPlan [] = String
""
ppICPlan [(String
agent,(String
label,action
_))] = String
agent String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
":" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
label String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"."
ppICPlan ((String
agent,(String
label,action
_)):[Owned action]
rest) = String
agent String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
":" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
label String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"; " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [Owned action] -> String
forall action. ICPlan action -> String
ppICPlan [Owned action]
rest
icSolves :: (Typeable action, Semantics state) => ICPlan action -> CoopTask state action -> Bool
icSolves :: forall action state.
(Typeable action, Semantics state) =>
ICPlan action -> CoopTask state action -> Bool
icSolves ICPlan action
plan (CoopTask state
start ICPlan action
acts Form
goal) =
((String, Labelled action) -> Bool) -> ICPlan action -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ((String, Labelled action) -> String) -> ICPlan action -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String, Labelled action) -> String
forall a b. (a, b) -> a
fst ICPlan action
acts) (String -> Bool)
-> ((String, Labelled action) -> String)
-> (String, Labelled action)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String, Labelled action) -> String
forall a b. (a, b) -> a
fst) ICPlan action
plan Bool -> Bool -> Bool
&& ICPlan action -> Form -> state -> Bool
forall o. Semantics o => ICPlan action -> Form -> o -> Bool
forall a o. (IsPlan a, Semantics o) => a -> Form -> o -> Bool
reachesOn ICPlan action
plan Form
goal state
start
findSequentialIcPlan :: (Typeable action, Eq state, Update state action) => Int -> CoopTask state action -> [ICPlan action]
findSequentialIcPlan :: forall action state.
(Typeable action, Eq state, Update state action) =>
Int -> CoopTask state action -> [ICPlan action]
findSequentialIcPlan Int
d (CoopTask state
now [Owned action]
acts Form
goal)
| state
now state -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
|= Form
goal = [ [] ]
| Int
d Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = [ ]
| Bool
otherwise = [ (String
agent,(String
label, action
act)) Owned action -> [Owned action] -> [Owned action]
forall a. a -> [a] -> [a]
: [Owned action]
continue
| a :: Owned action
a@(String
agent,(String
label,action
act)) <- [Owned action]
acts
, state
now state -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
|= action -> Form
forall a. HasPrecondition a => a -> Form
preOf action
act
, state
now state -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
|= String -> Form -> Form
K String
agent (action -> Form
forall a. HasPrecondition a => a -> Form
preOf action
act)
, state
now state -> state -> Bool
forall a. Eq a => a -> a -> Bool
/= state -> action -> state
forall a b. Update a b => a -> b -> a
update state
now action
act
, [Owned action]
continue <- Int -> CoopTask state action -> [[Owned action]]
forall action state.
(Typeable action, Eq state, Update state action) =>
Int -> CoopTask state action -> [ICPlan action]
findSequentialIcPlan (Int
dInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) (state -> [Owned action] -> Form -> CoopTask state action
forall state action.
state -> [Owned action] -> Form -> CoopTask state action
CoopTask (state -> action -> state
forall a b. Update a b => a -> b -> a
update state
now action
act) [Owned action]
acts Form
goal)
, (Owned action
aOwned action -> [Owned action] -> [Owned action]
forall a. a -> [a] -> [a]
:[Owned action]
continue) [Owned action] -> CoopTask state action -> Bool
forall action state.
(Typeable action, Semantics state) =>
ICPlan action -> CoopTask state action -> Bool
`icSolves` state -> [Owned action] -> Form -> CoopTask state action
forall state action.
state -> [Owned action] -> Form -> CoopTask state action
CoopTask state
now [Owned action]
acts Form
goal ]
findSequentialIcPlanBFS :: (Typeable action, Eq state, Update state action) => Int -> CoopTask state action -> Maybe (ICPlan action)
findSequentialIcPlanBFS :: forall action state.
(Typeable action, Eq state, Update state action) =>
Int -> CoopTask state action -> Maybe (ICPlan action)
findSequentialIcPlanBFS Int
maxDepth (CoopTask state
start [Owned action]
acts Form
goal) = [([Owned action], state)] -> Maybe [Owned action]
forall {a}.
(Eq a, Update a action) =>
[([Owned action], a)] -> Maybe [Owned action]
loop [([],state
start)] where
loop :: [([Owned action], a)] -> Maybe [Owned action]
loop [] = Maybe [Owned action]
forall a. Maybe a
Nothing
loop (([Owned action]
done,a
now):[([Owned action], a)]
rest)
| a
now a -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
|= Form
goal = [Owned action] -> Maybe [Owned action]
forall a. a -> Maybe a
Just [Owned action]
done
| Bool
otherwise = [([Owned action], a)] -> Maybe [Owned action]
loop ([([Owned action], a)] -> Maybe [Owned action])
-> [([Owned action], a)] -> Maybe [Owned action]
forall a b. (a -> b) -> a -> b
$ [([Owned action], a)]
rest [([Owned action], a)]
-> [([Owned action], a)] -> [([Owned action], a)]
forall a. [a] -> [a] -> [a]
++
[ ([Owned action]
done [Owned action] -> [Owned action] -> [Owned action]
forall a. [a] -> [a] -> [a]
++ [Owned action
a], a -> action -> a
forall a b. Update a b => a -> b -> a
update a
now action
act)
| [Owned action] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Owned action]
done Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
maxDepth
, a :: Owned action
a@(String
agent,(String
_,action
act)) <- [Owned action]
acts
, a
now a -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
|= action -> Form
forall a. HasPrecondition a => a -> Form
preOf action
act
, a
now a -> Form -> Bool
forall a. Semantics a => a -> Form -> Bool
|= String -> Form -> Form
K String
agent (action -> Form
forall a. HasPrecondition a => a -> Form
preOf action
act)
, a
now a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a -> action -> a
forall a b. Update a b => a -> b -> a
update a
now action
act
]