{-# LANGUAGE AllowAmbiguousTypes #-}

-- | Open games in a copy/discard category. Strategies are finite catalogues of
-- monoidal lenses; contexts retain the joint state of the environment.
--
-- The categorical laws identify games up to bijections of profiles preserving
-- play and evaluation, with diagnostic lists compared up to permutation.
-- Enumeration order and diagnostic order are not categorical invariants.
-- Strictification handles profile parenthesisation, not arbitrary equivalence
-- of strategies. There is no 'Eq' instance for games containing opaque arrows.
module Proarrow.Category.Instance.OpenGame where

import Data.Kind (Type)
import Prelude (Bool, Eq, Ord, Rational, Show, String, ($), type (~))
import Prelude qualified as P

import Proarrow.Category.Monoidal
  ( Monoidal (..)
  , MonoidalProfunctor (..)
  , SymMonoidal (..)
  )
import Proarrow.Category.Monoidal.CopyDiscard (CopyDiscard, fst, snd)
import Proarrow.Category.Monoidal.Strictified (Fold, IsList (withIsList2), Strictified (..), concatFold, splitFold)
import Proarrow.Core (CAT, CategoryOf (..), Profunctor (..), Promonad (..), dimapDefault, obj)
import Proarrow.Object (pattern Objs, type (++))
import Proarrow.Optic (iso)
import Proarrow.Optic.Getter (view)
import Proarrow.Optic.MonoidalLens (MonLensFl, MonoidalLens, monLens)
import Proarrow.Optic.Setter (over)
import Proarrow.Optic.TensorOps (mapContinuation, retainView, tensorOptic)

type data GAME k = G k k

type Observation :: GAME k -> k
type family Observation a where
  Observation (G x s) = x

type Coutility :: GAME k -> k
type family Coutility a where
  Coutility (G x s) = s

-- | A joint state and continuation represented directly as an optic.
type Context (a :: GAME k) (b :: GAME k) =
  MonoidalLens Unit (Coutility b) (Observation a) (Observation b)

-- | Exact utilities used by the finite decision primitives.
type Payoff = Rational

-- | A local best-response check, including all tied optimal actions.
data Diagnostic = Diagnostic
  { Diagnostic -> String
player :: String
  , Diagnostic -> String
observation :: String
  , Diagnostic -> String
chosenAction :: String
  , Diagnostic -> Payoff
payoff :: Payoff
  , Diagnostic -> [String]
optimalActions :: [String]
  , Diagnostic -> Payoff
optimalPayoff :: Payoff
  , Diagnostic -> Bool
equilibrium :: Bool
  }
  deriving (Diagnostic -> Diagnostic -> Bool
(Diagnostic -> Diagnostic -> Bool)
-> (Diagnostic -> Diagnostic -> Bool) -> Eq Diagnostic
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Diagnostic -> Diagnostic -> Bool
== :: Diagnostic -> Diagnostic -> Bool
$c/= :: Diagnostic -> Diagnostic -> Bool
/= :: Diagnostic -> Diagnostic -> Bool
Eq, Eq Diagnostic
Eq Diagnostic =>
(Diagnostic -> Diagnostic -> Ordering)
-> (Diagnostic -> Diagnostic -> Bool)
-> (Diagnostic -> Diagnostic -> Bool)
-> (Diagnostic -> Diagnostic -> Bool)
-> (Diagnostic -> Diagnostic -> Bool)
-> (Diagnostic -> Diagnostic -> Diagnostic)
-> (Diagnostic -> Diagnostic -> Diagnostic)
-> Ord Diagnostic
Diagnostic -> Diagnostic -> Bool
Diagnostic -> Diagnostic -> Ordering
Diagnostic -> Diagnostic -> Diagnostic
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
$ccompare :: Diagnostic -> Diagnostic -> Ordering
compare :: Diagnostic -> Diagnostic -> Ordering
$c< :: Diagnostic -> Diagnostic -> Bool
< :: Diagnostic -> Diagnostic -> Bool
$c<= :: Diagnostic -> Diagnostic -> Bool
<= :: Diagnostic -> Diagnostic -> Bool
$c> :: Diagnostic -> Diagnostic -> Bool
> :: Diagnostic -> Diagnostic -> Bool
$c>= :: Diagnostic -> Diagnostic -> Bool
>= :: Diagnostic -> Diagnostic -> Bool
$cmax :: Diagnostic -> Diagnostic -> Diagnostic
max :: Diagnostic -> Diagnostic -> Diagnostic
$cmin :: Diagnostic -> Diagnostic -> Diagnostic
min :: Diagnostic -> Diagnostic -> Diagnostic
Ord, Int -> Diagnostic -> ShowS
[Diagnostic] -> ShowS
Diagnostic -> String
(Int -> Diagnostic -> ShowS)
-> (Diagnostic -> String)
-> ([Diagnostic] -> ShowS)
-> Show Diagnostic
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Diagnostic -> ShowS
showsPrec :: Int -> Diagnostic -> ShowS
$cshow :: Diagnostic -> String
show :: Diagnostic -> String
$cshowList :: [Diagnostic] -> ShowS
showList :: [Diagnostic] -> ShowS
Show)

isEquilibrium :: [Diagnostic] -> Bool
isEquilibrium :: [Diagnostic] -> Bool
isEquilibrium = (Diagnostic -> Bool) -> [Diagnostic] -> Bool
forall (t :: Type -> Type) a.
Foldable t =>
(a -> Bool) -> t a -> Bool
P.all Diagnostic -> Bool
equilibrium

-- | A game with an explicit finite, lazy profile catalogue. Custom constructors
-- should supply each admissible profile once; enumeration can be exponential.
-- Profiles use 'Fold' of a strictified strategy list, so composing games
-- concatenates their lists without introducing parenthesisation into profiles.
data OpenGame (ss :: [Type]) (a :: GAME k) (b :: GAME k) where
  OpenGame
    :: (Ob ss, Ob a, Ob b)
    => { forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> [Fold ss]
profiles :: [Fold ss]
       , forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Fold ss -> Strategy a b
strategy :: Fold ss -> Strategy a b
       }
    -> OpenGame ss a b

-- | Hide the strategy list to obtain arrows of 'GAME'. Use 'strategies' to
-- access profiles with their play and evaluators, or 'withGame' for typed access.
type Game :: CAT (GAME k)
data Game a b where
  Game :: OpenGame ss a b -> Game a b

-- | A profile closed over its game's play and evaluator. It can be stored and
-- evaluated in different contexts without exposing the hidden profile type.
data Strategy (a :: GAME k) (b :: GAME k) where
  Strategy
    :: (Ob a, Ob b)
    => { forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b
-> MonoidalLens
     (Observation a) (Coutility a) (Observation b) (Coutility b)
playStrategy :: ~(MonoidalLens (Observation a) (Coutility a) (Observation b) (Coutility b))
       , forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b -> Context a b -> [Diagnostic]
evaluateStrategy :: Context a b -> [Diagnostic]
       }
    -> Strategy a b

-- | Enumerate profiles with their play and evaluator closed over the game.
strategies :: Game a b -> [Strategy a b]
strategies :: forall {k} (a :: GAME k) (b :: GAME k). Game a b -> [Strategy a b]
strategies (Game g :: OpenGame ss a b
g@OpenGame{}) = (Fold ss -> Strategy a b) -> [Fold ss] -> [Strategy a b]
forall a b. (a -> b) -> [a] -> [b]
P.map (OpenGame ss a b -> Fold ss -> Strategy a b
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Fold ss -> Strategy a b
strategy OpenGame ss a b
g) (OpenGame ss a b -> [Fold ss]
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> [Fold ss]
profiles OpenGame ss a b
g)

-- | Play the selected profile's bundled strategy.
play :: OpenGame ss a b -> Fold ss -> MonoidalLens (Observation a) (Coutility a) (Observation b) (Coutility b)
play :: forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b
-> Fold ss
-> MonoidalLens
     (Observation a) (Coutility a) (Observation b) (Coutility b)
play OpenGame ss a b
g Fold ss
s = Strategy a b
-> Optic_
     (OPT (Observation b) (Coutility b))
     (OPT (Observation a) (Coutility a))
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b
-> MonoidalLens
     (Observation a) (Coutility a) (Observation b) (Coutility b)
playStrategy (OpenGame ss a b -> Fold ss -> Strategy a b
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Fold ss -> Strategy a b
strategy OpenGame ss a b
g Fold ss
s)
{-# INLINE play #-}

-- | Evaluate the selected profile's bundled strategy in a context.
evaluate :: OpenGame ss a b -> Fold ss -> Context a b -> [Diagnostic]
evaluate :: forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Fold ss -> Context a b -> [Diagnostic]
evaluate OpenGame ss a b
g Fold ss
s = Strategy a b
-> Optic_
     (OPT (Observation a) (Observation b)) (OPT Unit (Coutility b))
-> [Diagnostic]
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b -> Context a b -> [Diagnostic]
evaluateStrategy (OpenGame ss a b -> Fold ss -> Strategy a b
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Fold ss -> Strategy a b
strategy OpenGame ss a b
g Fold ss
s)
{-# INLINE evaluate #-}

-- | Share each profile's play and evaluator closures across repeated uses.
-- The lazy catalogue retains its order and multiplicity; its hidden profile
-- representation becomes the corresponding first-class 'Strategy'.
memoisePlay :: forall k (a :: GAME k) b. Game a b -> Game a b
memoisePlay :: forall k (a :: GAME k) (b :: GAME k). Game a b -> Game a b
memoisePlay game :: Game a b
game@(Game OpenGame{}) =
  OpenGame '[Strategy a b] a b -> Game a b
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Game a b
Game (forall (ss :: [Type]) (a :: GAME k) (b :: GAME k).
(Ob ss, Ob a, Ob b) =>
[Fold ss] -> (Fold ss -> Strategy a b) -> OpenGame ss a b
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
(Ob ss, Ob a, Ob b) =>
[Fold ss] -> (Fold ss -> Strategy a b) -> OpenGame ss a b
OpenGame @'[Strategy a b] (Game a b -> [Strategy a b]
forall {k} (a :: GAME k) (b :: GAME k). Game a b -> [Strategy a b]
strategies Game a b
game) Fold '[Strategy a b] -> Strategy a b
Strategy
  (G (Observation a) (Coutility a)) (G (Observation b) (Coutility b))
-> Strategy
     (G (Observation a) (Coutility a)) (G (Observation b) (Coutility b))
forall a. Ob a => a -> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id)

-- | Equilibria within the supplied finite strategy catalogue.
equilibria :: Game a b -> Context a b -> [Strategy a b]
equilibria :: forall {k} (a :: GAME k) (b :: GAME k).
Game a b -> Context a b -> [Strategy a b]
equilibria Game a b
g Context a b
c = (Strategy a b -> Bool) -> [Strategy a b] -> [Strategy a b]
forall a. (a -> Bool) -> [a] -> [a]
P.filter (\Strategy a b
s -> [Diagnostic] -> Bool
isEquilibrium (Strategy a b -> Context a b -> [Diagnostic]
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b -> Context a b -> [Diagnostic]
evaluateStrategy Strategy a b
s Context a b
c)) (Game a b -> [Strategy a b]
forall {k} (a :: GAME k) (b :: GAME k). Game a b -> [Strategy a b]
strategies Game a b
g)

withGame :: Game a b -> (forall ss. OpenGame ss a b -> r) -> r
withGame :: forall {k} (a :: GAME k) (b :: GAME k) r.
Game a b -> (forall (ss :: [Type]). OpenGame ss a b -> r) -> r
withGame (Game OpenGame ss a b
g) forall (ss :: [Type]). OpenGame ss a b -> r
k = OpenGame ss a b -> r
forall (ss :: [Type]). OpenGame ss a b -> r
k OpenGame ss a b
g

outputs :: (CopyDiscard k) => Strategy (a :: GAME k) b -> Observation a ~> Observation b
outputs :: forall k (a :: GAME k) (b :: GAME k).
CopyDiscard k =>
Strategy a b -> Observation a ~> Observation b
outputs Strategy a b
s = Optic
  (Prostrong MonLensFl)
  (Observation a)
  (Coutility a)
  (Observation b)
  (Coutility b)
-> Observation a ~> Observation b
forall {j} {k} (c :: (k -> j -> Type) -> Constraint) (s :: k)
       (t :: j) (a :: k) (b :: j).
(CategoryOf j, CategoryOf k,
 (Ob a, Ob b) => c (ExOptic GetterFl a b)) =>
Optic c s t a b -> s ~> a
view (Strategy a b
-> Optic
     (Prostrong MonLensFl)
     (Observation a)
     (Coutility a)
     (Observation b)
     (Coutility b)
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b
-> MonoidalLens
     (Observation a) (Coutility a) (Observation b) (Coutility b)
playStrategy Strategy a b
s)

-- | Execute both passes with the same residual, retaining joint outcomes.
runStrategy
  :: forall k (a :: GAME k) b. (CopyDiscard k) => Strategy a b -> Context a b -> Unit ~> Observation b ** Coutility a
runStrategy :: forall k (a :: GAME k) (b :: GAME k).
CopyDiscard k =>
Strategy a b
-> Context a b -> Unit ~> (Observation b ** Coutility a)
runStrategy s :: Strategy a b
s@Strategy{} Context a b
ctx = Optic
  (Prostrong MonLensFl)
  Unit
  (Observation b ** Coutility a)
  (Observation a)
  (Observation a)
-> (Observation a ~> Observation a)
-> Unit ~> (Observation b ** Coutility a)
forall {k} (c :: (k -> k -> Type) -> Constraint) (s :: k) (t :: k)
       (a :: k) (b :: k).
(CategoryOf k, (Ob a, Ob b) => c (ExOptic SetterFl a b)) =>
Optic c s t a b -> (a ~> b) -> s ~> t
over (Optic
  (Prostrong MonLensFl)
  (Observation a)
  (Observation b ** Coutility a)
  (Observation b)
  (Coutility b)
-> Context a b
-> Optic
     (Prostrong MonLensFl)
     Unit
     (Observation b ** Coutility a)
     (Observation a)
     (Observation a)
forall {k} (input :: (k -> k -> Type) -> Constraint)
       (output :: (k -> k -> Type) -> Constraint) (s :: k) (x :: k)
       (y :: k) (r :: k) (z :: k) (t :: k).
(CategoryOf k, input (Continuation output s x)) =>
Optic input y r z t -> Optic output s t x z -> Optic output s r x y
mapContinuation (forall {k}
       (w :: (k -> k -> Type) -> (k -> k -> Type) -> Constraint)
       (input :: (k -> k -> Type) -> Constraint) (s :: k) (t :: k)
       (a :: k) (b :: k).
(Comonoid a,
 w (Rep (ActionAt Tensor a)) (Corep (ActionAt Tensor a)),
 forall (p :: k -> k -> Type).
 (Prostrong w p, Profunctor p) =>
 input (Backward p a)) =>
Optic input s t a b -> Optic (Prostrong w) s (a ** t) a b
forall (w :: (k +-> k) -> (k +-> k) -> Constraint)
       (input :: (k +-> k) -> Constraint) (s :: k) (t :: k) (a :: k)
       (b :: k).
(Comonoid a,
 w (Rep (ActionAt Tensor a)) (Corep (ActionAt Tensor a)),
 forall (p :: k +-> k).
 (Prostrong w p, Profunctor p) =>
 input (Backward p a)) =>
Optic input s t a b -> Optic (Prostrong w) s (a ** t) a b
retainView @MonLensFl (Strategy a b
-> Optic
     (Prostrong MonLensFl)
     (Observation a)
     (Coutility a)
     (Observation b)
     (Coutility b)
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b
-> MonoidalLens
     (Observation a) (Coutility a) (Observation b) (Coutility b)
playStrategy Strategy a b
s)) Context a b
ctx) Observation a ~> Observation a
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id

instance (CopyDiscard k) => CategoryOf (GAME k) where
  type (~>) = Game
  type Ob a = (a ~ G (Observation a) (Coutility a), Ob (Observation a), Ob (Coutility a))

instance (CopyDiscard k) => Profunctor (Game :: CAT (GAME k)) where
  dimap :: forall (c :: GAME k) (a :: GAME k) (b :: GAME k) (d :: GAME k).
(c ~> a) -> (b ~> d) -> Game a b -> Game c d
dimap = (c ~> a) -> (b ~> d) -> Game a b -> Game c d
Game c a -> Game b d -> Game a b -> Game c d
forall {k} (p :: CAT k) (c :: k) (a :: k) (b :: k) (d :: k).
Promonad p =>
p c a -> p b d -> p a b -> p c d
dimapDefault
  (Ob a, Ob b) => r
r \\ :: forall (a :: GAME k) (b :: GAME k) r.
((Ob a, Ob b) => r) -> Game a b -> r
\\ Game OpenGame{} = r
(Ob a, Ob b) => r
r

instance (CopyDiscard k) => Promonad (Game :: CAT (GAME k)) where
  id :: forall (a :: GAME k). Ob a => Game a a
id = OpenGame '[] a a -> Game a a
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Game a b
Game ((Observation a ~> Observation a)
-> (Coutility a ~> Coutility a)
-> OpenGame
     '[]
     (G (Observation a) (Coutility a))
     (G (Observation a) (Coutility a))
forall k (x :: k) (y :: k) (r :: k) (s :: k).
CopyDiscard k =>
(x ~> y) -> (r ~> s) -> OpenGame '[] (G x s) (G y r)
fromArrows Observation a ~> Observation a
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id Coutility a ~> Coutility a
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id)

  -- Sequential play transports upstream deviations through the downstream lens.
  Game (h :: OpenGame ss b c
h@OpenGame{} :: OpenGame ts b c) . :: forall (b :: GAME k) (c :: GAME k) (a :: GAME k).
Game b c -> Game a b -> Game a c
. Game (g :: OpenGame ss a b
g@OpenGame{} :: OpenGame ss a b) =
    forall (as :: [Type]) (bs :: [Type]) r.
(IsList as, IsList bs) =>
(IsList (as ++ bs) => r) -> r
forall {k} (as :: [k]) (bs :: [k]) r.
(IsList as, IsList bs) =>
(IsList (as ++ bs) => r) -> r
withIsList2 @ss @ts ((IsList (ss ++ ss) => Game a c) -> Game a c)
-> (IsList (ss ++ ss) => Game a c) -> Game a c
forall a b. (a -> b) -> a -> b
$
      OpenGame (ss ++ ss) a c -> Game a c
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Game a b
Game (OpenGame (ss ++ ss) a c -> Game a c)
-> OpenGame (ss ++ ss) a c -> Game a c
forall a b. (a -> b) -> a -> b
$
        forall (ss :: [Type]) (a :: GAME k) (b :: GAME k).
(Ob ss, Ob a, Ob b) =>
[Fold ss] -> (Fold ss -> Strategy a b) -> OpenGame ss a b
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
(Ob ss, Ob a, Ob b) =>
[Fold ss] -> (Fold ss -> Strategy a b) -> OpenGame ss a b
OpenGame @(ss ++ ts)
          [forall (as :: [Type]) (bs :: [Type]).
(Ob as, Ob bs, Monoidal Type) =>
(Fold as ** Fold bs) ~> Fold (as ++ bs)
forall {k} (as :: [k]) (bs :: [k]).
(Ob as, Ob bs, Monoidal k) =>
(Fold as ** Fold bs) ~> Fold (as ++ bs)
concatFold @ss @ts (Fold ss
s, Fold ss
t) | Fold ss
s <- OpenGame ss a b -> [Fold ss]
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> [Fold ss]
profiles OpenGame ss a b
g, Fold ss
t <- OpenGame ss b c -> [Fold ss]
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> [Fold ss]
profiles OpenGame ss b c
h]
          ( \Fold (ss ++ ss)
st ->
              let (Fold ss
s, Fold ss
t) = forall (as :: [Type]) (bs :: [Type]).
(Ob as, Ob bs, Monoidal Type) =>
Fold (as ++ bs) ~> (Fold as ** Fold bs)
forall {k} (as :: [k]) (bs :: [k]).
(Ob as, Ob bs, Monoidal k) =>
Fold (as ++ bs) ~> (Fold as ** Fold bs)
splitFold @ss @ts Fold (ss ++ ss)
st
                  gs :: Strategy a b
gs = OpenGame ss a b -> Fold ss -> Strategy a b
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Fold ss -> Strategy a b
strategy OpenGame ss a b
g Fold ss
s
                  hs :: Strategy b c
hs = OpenGame ss b c -> Fold ss -> Strategy b c
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Fold ss -> Strategy a b
strategy OpenGame ss b c
h Fold ss
t
              in MonoidalLens
  (Observation a) (Coutility a) (Observation c) (Coutility c)
-> (Context a c -> [Diagnostic]) -> Strategy a c
forall {k} (a :: GAME k) (b :: GAME k).
(Ob a, Ob b) =>
MonoidalLens
  (Observation a) (Coutility a) (Observation b) (Coutility b)
-> (Context a b -> [Diagnostic]) -> Strategy a b
Strategy
                   (Strategy a b
-> Optic_
     (OPT (Observation b) (Coutility b))
     (OPT (Observation a) (Coutility a))
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b
-> MonoidalLens
     (Observation a) (Coutility a) (Observation b) (Coutility b)
playStrategy Strategy a b
gs Optic_
  (OPT (Observation b) (Coutility b))
  (OPT (Observation a) (Coutility a))
-> Optic_
     (OPT (Observation c) (Coutility c))
     (OPT (Observation b) (Coutility b))
-> MonoidalLens
     (Observation a) (Coutility a) (Observation c) (Coutility c)
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
forall (b :: OPTIC k k (Prostrong MonLensFl))
       (c :: OPTIC k k (Prostrong MonLensFl))
       (a :: OPTIC k k (Prostrong MonLensFl)).
Optic_ b c -> Optic_ a b -> Optic_ a c
. Strategy b c
-> Optic_
     (OPT (Observation c) (Coutility c))
     (OPT (Observation b) (Coutility b))
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b
-> MonoidalLens
     (Observation a) (Coutility a) (Observation b) (Coutility b)
playStrategy Strategy b c
hs)
                   ( \Context a c
ctx ->
                       Strategy a b -> Context a b -> [Diagnostic]
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b -> Context a b -> [Diagnostic]
evaluateStrategy Strategy a b
gs (Optic_
  (OPT (Observation c) (Coutility c))
  (OPT (Observation b) (Coutility b))
-> Context a c -> Context a b
forall {k} (input :: (k -> k -> Type) -> Constraint)
       (output :: (k -> k -> Type) -> Constraint) (s :: k) (x :: k)
       (y :: k) (r :: k) (z :: k) (t :: k).
(CategoryOf k, input (Continuation output s x)) =>
Optic input y r z t -> Optic output s t x z -> Optic output s r x y
mapContinuation (Strategy b c
-> Optic_
     (OPT (Observation c) (Coutility c))
     (OPT (Observation b) (Coutility b))
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b
-> MonoidalLens
     (Observation a) (Coutility a) (Observation b) (Coutility b)
playStrategy Strategy b c
hs) Context a c
ctx)
                         [Diagnostic] -> [Diagnostic] -> [Diagnostic]
forall a. [a] -> [a] -> [a]
P.++ Strategy b c -> Context b c -> [Diagnostic]
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b -> Context a b -> [Diagnostic]
evaluateStrategy Strategy b c
hs (Context a c
ctx Context a c
-> Optic_
     (OPT (Observation b) (Observation c))
     (OPT (Observation a) (Observation c))
-> Context b c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
forall (b :: OPTIC k k (Prostrong MonLensFl))
       (c :: OPTIC k k (Prostrong MonLensFl))
       (a :: OPTIC k k (Prostrong MonLensFl)).
Optic_ b c -> Optic_ a b -> Optic_ a c
. (Observation a ~> Observation b)
-> (Observation c ~> Observation c)
-> Optic_
     (OPT (Observation b) (Observation c))
     (OPT (Observation a) (Observation c))
forall {j} {k} (c :: (j +-> k) -> Constraint) (s :: k) (t :: j)
       (a :: k) (b :: j).
(CategoryOf j, CategoryOf k) =>
(s ~> a) -> (b ~> t) -> Optic c s t a b
iso (Optic_
  (OPT (Observation b) (Coutility b))
  (OPT (Observation a) (Coutility a))
-> Observation a ~> Observation b
forall {j} {k} (c :: (k -> j -> Type) -> Constraint) (s :: k)
       (t :: j) (a :: k) (b :: j).
(CategoryOf j, CategoryOf k,
 (Ob a, Ob b) => c (ExOptic GetterFl a b)) =>
Optic c s t a b -> s ~> a
view (Strategy a b
-> Optic_
     (OPT (Observation b) (Coutility b))
     (OPT (Observation a) (Coutility a))
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b
-> MonoidalLens
     (Observation a) (Coutility a) (Observation b) (Coutility b)
playStrategy Strategy a b
gs)) Observation c ~> Observation c
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id)
                   )
          )

-- | A nonstrategic component with one empty profile and no decision checks.
fromMonoidalLens
  :: (CopyDiscard k) => MonoidalLens (x :: k) s y r -> OpenGame '[] (G x s) (G y r)
fromMonoidalLens :: forall k (x :: k) (s :: k) (y :: k) (r :: k).
CopyDiscard k =>
MonoidalLens x s y r -> OpenGame '[] (G x s) (G y r)
fromMonoidalLens optic :: MonoidalLens x s y r
optic@MonoidalLens x s y r
Objs = [Fold '[]]
-> (Fold '[] -> Strategy (G x s) (G y r))
-> OpenGame '[] (G x s) (G y r)
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
(Ob ss, Ob a, Ob b) =>
[Fold ss] -> (Fold ss -> Strategy a b) -> OpenGame ss a b
OpenGame [()] (Strategy (G x s) (G y r) -> () -> Strategy (G x s) (G y r)
forall a b. a -> b -> a
P.const (MonoidalLens
  (Observation (G x s))
  (Coutility (G x s))
  (Observation (G y r))
  (Coutility (G y r))
-> (Context (G x s) (G y r) -> [Diagnostic])
-> Strategy (G x s) (G y r)
forall {k} (a :: GAME k) (b :: GAME k).
(Ob a, Ob b) =>
MonoidalLens
  (Observation a) (Coutility a) (Observation b) (Coutility b)
-> (Context a b -> [Diagnostic]) -> Strategy a b
Strategy MonoidalLens x s y r
MonoidalLens
  (Observation (G x s))
  (Coutility (G x s))
  (Observation (G y r))
  (Coutility (G y r))
optic (\Context (G x s) (G y r)
_ -> [])))

fromArrows :: (CopyDiscard k) => (x :: k) ~> y -> r ~> s -> OpenGame '[] (G x s) (G y r)
fromArrows :: forall k (x :: k) (y :: k) (r :: k) (s :: k).
CopyDiscard k =>
(x ~> y) -> (r ~> s) -> OpenGame '[] (G x s) (G y r)
fromArrows x ~> y
f r ~> s
g = MonoidalLens x s y r -> OpenGame '[] (G x s) (G y r)
forall k (x :: k) (s :: k) (y :: k) (r :: k).
CopyDiscard k =>
MonoidalLens x s y r -> OpenGame '[] (G x s) (G y r)
fromMonoidalLens ((x ~> y) -> (r ~> s) -> MonoidalLens x s y r
forall {j} {k} (c :: (j +-> k) -> Constraint) (s :: k) (t :: j)
       (a :: k) (b :: j).
(CategoryOf j, CategoryOf k) =>
(s ~> a) -> (b ~> t) -> Optic c s t a b
iso x ~> y
f r ~> s
g)

-- | Independent parallel play; each deviation holds the other strategy fixed.
parallel
  :: forall k ss ts (a :: GAME k) b c d
   . (CopyDiscard k) => OpenGame ss a b -> OpenGame ts c d -> OpenGame (ss ++ ts) (a ** c) (b ** d)
parallel :: forall k (ss :: [Type]) (ts :: [Type]) (a :: GAME k) (b :: GAME k)
       (c :: GAME k) (d :: GAME k).
CopyDiscard k =>
OpenGame ss a b
-> OpenGame ts c d -> OpenGame (ss ++ ts) (a ** c) (b ** d)
parallel g :: OpenGame ss a b
g@OpenGame{} h :: OpenGame ts c d
h@OpenGame{} =
  forall (as :: [Type]) (bs :: [Type]) r.
(IsList as, IsList bs) =>
(IsList (as ++ bs) => r) -> r
forall {k} (as :: [k]) (bs :: [k]) r.
(IsList as, IsList bs) =>
(IsList (as ++ bs) => r) -> r
withIsList2 @ss @ts ((IsList (ss ++ ts) => OpenGame (ss ++ ts) (a ** c) (b ** d))
 -> OpenGame (ss ++ ts) (a ** c) (b ** d))
-> (IsList (ss ++ ts) => OpenGame (ss ++ ts) (a ** c) (b ** d))
-> OpenGame (ss ++ ts) (a ** c) (b ** d)
forall a b. (a -> b) -> a -> b
$
    forall k (a :: k) (b :: k) r.
(Monoidal k, Ob a, Ob b) =>
(Ob (a ** b) => r) -> r
withOb2 @(GAME k) @a @c ((Ob (a ** c) => OpenGame (ss ++ ts) (a ** c) (b ** d))
 -> OpenGame (ss ++ ts) (a ** c) (b ** d))
-> (Ob (a ** c) => OpenGame (ss ++ ts) (a ** c) (b ** d))
-> OpenGame (ss ++ ts) (a ** c) (b ** d)
forall a b. (a -> b) -> a -> b
$
      forall k (a :: k) (b :: k) r.
(Monoidal k, Ob a, Ob b) =>
(Ob (a ** b) => r) -> r
withOb2 @(GAME k) @b @d ((Ob (b ** d) => OpenGame (ss ++ ts) (a ** c) (b ** d))
 -> OpenGame (ss ++ ts) (a ** c) (b ** d))
-> (Ob (b ** d) => OpenGame (ss ++ ts) (a ** c) (b ** d))
-> OpenGame (ss ++ ts) (a ** c) (b ** d)
forall a b. (a -> b) -> a -> b
$
        [Fold (ss ++ ts)]
-> (Fold (ss ++ ts)
    -> Strategy
         (G (Observation a ** Observation c) (Coutility a ** Coutility c))
         (G (Observation b ** Observation d) (Coutility b ** Coutility d)))
-> OpenGame
     (ss ++ ts)
     (G (Observation a ** Observation c) (Coutility a ** Coutility c))
     (G (Observation b ** Observation d) (Coutility b ** Coutility d))
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
(Ob ss, Ob a, Ob b) =>
[Fold ss] -> (Fold ss -> Strategy a b) -> OpenGame ss a b
OpenGame
          [forall (as :: [Type]) (bs :: [Type]).
(Ob as, Ob bs, Monoidal Type) =>
(Fold as ** Fold bs) ~> Fold (as ++ bs)
forall {k} (as :: [k]) (bs :: [k]).
(Ob as, Ob bs, Monoidal k) =>
(Fold as ** Fold bs) ~> Fold (as ++ bs)
concatFold @ss @ts (Fold ss
s, Fold ts
t) | Fold ss
s <- OpenGame ss a b -> [Fold ss]
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> [Fold ss]
profiles OpenGame ss a b
g, Fold ts
t <- OpenGame ts c d -> [Fold ts]
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> [Fold ss]
profiles OpenGame ts c d
h]
          ( \Fold (ss ++ ts)
st ->
              let (Fold ss
s, Fold ts
t) = forall (as :: [Type]) (bs :: [Type]).
(Ob as, Ob bs, Monoidal Type) =>
Fold (as ++ bs) ~> (Fold as ** Fold bs)
forall {k} (as :: [k]) (bs :: [k]).
(Ob as, Ob bs, Monoidal k) =>
Fold (as ++ bs) ~> (Fold as ** Fold bs)
splitFold @ss @ts Fold (ss ++ ts)
st
                  gs :: Strategy a b
gs = OpenGame ss a b -> Fold ss -> Strategy a b
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Fold ss -> Strategy a b
strategy OpenGame ss a b
g Fold ss
s
                  hs :: Strategy c d
hs = OpenGame ts c d -> Fold ts -> Strategy c d
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Fold ss -> Strategy a b
strategy OpenGame ts c d
h Fold ts
t
                  v :: MonoidalLens
  (Observation a) (Coutility a) (Observation b) (Coutility b)
v = Strategy a b
-> MonoidalLens
     (Observation a) (Coutility a) (Observation b) (Coutility b)
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b
-> MonoidalLens
     (Observation a) (Coutility a) (Observation b) (Coutility b)
playStrategy Strategy a b
gs
                  w :: MonoidalLens
  (Observation c) (Coutility c) (Observation d) (Coutility d)
w = Strategy c d
-> MonoidalLens
     (Observation c) (Coutility c) (Observation d) (Coutility d)
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b
-> MonoidalLens
     (Observation a) (Coutility a) (Observation b) (Coutility b)
playStrategy Strategy c d
hs
              in MonoidalLens
  (Observation
     (G (Observation a ** Observation c) (Coutility a ** Coutility c)))
  (Coutility
     (G (Observation a ** Observation c) (Coutility a ** Coutility c)))
  (Observation
     (G (Observation b ** Observation d) (Coutility b ** Coutility d)))
  (Coutility
     (G (Observation b ** Observation d) (Coutility b ** Coutility d)))
-> (Context
      (G (Observation a ** Observation c) (Coutility a ** Coutility c))
      (G (Observation b ** Observation d) (Coutility b ** Coutility d))
    -> [Diagnostic])
-> Strategy
     (G (Observation a ** Observation c) (Coutility a ** Coutility c))
     (G (Observation b ** Observation d) (Coutility b ** Coutility d))
forall {k} (a :: GAME k) (b :: GAME k).
(Ob a, Ob b) =>
MonoidalLens
  (Observation a) (Coutility a) (Observation b) (Coutility b)
-> (Context a b -> [Diagnostic]) -> Strategy a b
Strategy
                   (MonoidalLens
  (Observation a) (Coutility a) (Observation b) (Coutility b)
-> MonoidalLens
     (Observation c) (Coutility c) (Observation d) (Coutility d)
-> Optic
     (Prostrong MonLensFl)
     (Observation a ** Observation c)
     (Coutility a ** Coutility c)
     (Observation b ** Observation d)
     (Coutility b ** Coutility d)
forall {k} (input1 :: (k +-> k) -> Constraint)
       (input2 :: (k +-> k) -> Constraint)
       (output :: (k +-> k) -> Constraint) (s :: k) (t :: k) (a :: k)
       (b :: k) (u :: k) (v :: k) (c :: k) (d :: k).
(SymMonoidal k,
 forall (p :: k +-> k).
 (output p, Profunctor p) =>
 input1 (TensorRight p u v),
 forall (p :: k +-> k).
 (output p, Profunctor p) =>
 input2 (TensorRight p a b)) =>
Optic input1 s t a b
-> Optic input2 u v c d
-> Optic output (s ** u) (t ** v) (a ** c) (b ** d)
tensorOptic MonoidalLens
  (Observation a) (Coutility a) (Observation b) (Coutility b)
v MonoidalLens
  (Observation c) (Coutility c) (Observation d) (Coutility d)
w)
                   ( \Context
  (G (Observation a ** Observation c) (Coutility a ** Coutility c))
  (G (Observation b ** Observation d) (Coutility b ** Coutility d))
ctx ->
                       let left :: Optic_
  (OPT (Observation a) (Observation b)) (OPT Unit (Coutility b))
left =
                             (Unit ~> Unit)
-> ((Coutility b ** Coutility d) ~> Coutility b)
-> Optic
     (Prostrong MonLensFl)
     Unit
     (Coutility b)
     Unit
     (Coutility b ** Coutility d)
forall {j} {k} (c :: (j +-> k) -> Constraint) (s :: k) (t :: j)
       (a :: k) (b :: j).
(CategoryOf j, CategoryOf k) =>
(s ~> a) -> (b ~> t) -> Optic c s t a b
iso Unit ~> Unit
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id (forall (a :: k) (b :: k).
(CopyDiscard k, Ob a, Ob b) =>
(a ** b) ~> a
forall {k} (a :: k) (b :: k).
(CopyDiscard k, Ob a, Ob b) =>
(a ** b) ~> a
fst @(Coutility b) @(Coutility d))
                               Optic
  (Prostrong MonLensFl)
  Unit
  (Coutility b)
  Unit
  (Coutility b ** Coutility d)
-> Optic_
     (OPT (Observation a) (Observation b))
     (OPT Unit (Coutility b ** Coutility d))
-> Optic_
     (OPT (Observation a) (Observation b)) (OPT Unit (Coutility b))
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
forall (b :: OPTIC k k (Prostrong MonLensFl))
       (c :: OPTIC k k (Prostrong MonLensFl))
       (a :: OPTIC k k (Prostrong MonLensFl)).
Optic_ b c -> Optic_ a b -> Optic_ a c
. Optic_
  (OPT
     (Observation a ** Observation c) (Observation b ** Observation d))
  (OPT Unit (Coutility b ** Coutility d))
Context
  (G (Observation a ** Observation c) (Coutility a ** Coutility c))
  (G (Observation b ** Observation d) (Coutility b ** Coutility d))
ctx
                               Optic_
  (OPT
     (Observation a ** Observation c) (Observation b ** Observation d))
  (OPT Unit (Coutility b ** Coutility d))
-> Optic_
     (OPT (Observation a) (Observation b))
     (OPT
        (Observation a ** Observation c) (Observation b ** Observation d))
-> Optic_
     (OPT (Observation a) (Observation b))
     (OPT Unit (Coutility b ** Coutility d))
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
forall (b :: OPTIC k k (Prostrong MonLensFl))
       (c :: OPTIC k k (Prostrong MonLensFl))
       (a :: OPTIC k k (Prostrong MonLensFl)).
Optic_ b c -> Optic_ a b -> Optic_ a c
. forall (m :: k) (s :: k) (t :: k) (a :: k) (b :: k).
(Comonoid m, Ob a, Ob b) =>
(s ~> (m ** a)) -> ((m ** b) ~> t) -> MonoidalLens s t a b
forall {k} (m :: k) (s :: k) (t :: k) (a :: k) (b :: k).
(Comonoid m, Ob a, Ob b) =>
(s ~> (m ** a)) -> ((m ** b) ~> t) -> MonoidalLens s t a b
monLens @(Observation c)
                                 (forall k (a :: k) (b :: k).
(SymMonoidal k, Ob a, Ob b) =>
(a ** b) ~> (b ** a)
swap @k @(Observation a) @(Observation c))
                                 (forall k (a :: k) (b :: k).
(SymMonoidal k, Ob a, Ob b) =>
(a ** b) ~> (b ** a)
swap @k @(Observation d) @(Observation b) ((Observation d ** Observation b)
 ~> (Observation b ** Observation d))
-> ((Observation c ** Observation b)
    ~> (Observation d ** Observation b))
-> (Observation c ** Observation b)
   ~> (Observation b ** Observation d)
forall (b :: k) (c :: k) (a :: k). (b ~> c) -> (a ~> b) -> a ~> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. (MonoidalLens
  (Observation c) (Coutility c) (Observation d) (Coutility d)
-> Observation c ~> Observation d
forall {j} {k} (c :: (k -> j -> Type) -> Constraint) (s :: k)
       (t :: j) (a :: k) (b :: j).
(CategoryOf j, CategoryOf k,
 (Ob a, Ob b) => c (ExOptic GetterFl a b)) =>
Optic c s t a b -> s ~> a
view MonoidalLens
  (Observation c) (Coutility c) (Observation d) (Coutility d)
w (Observation c ~> Observation d)
-> (Observation b ~> Observation b)
-> (Observation c ** Observation b)
   ~> (Observation d ** Observation b)
forall (x1 :: k) (x2 :: k) (y1 :: k) (y2 :: k).
(x1 ~> x2) -> (y1 ~> y2) -> (x1 ** y1) ~> (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
       (y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
** forall (a :: k). (CategoryOf k, Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @(Observation b)))
                           right :: Optic_
  (OPT (Observation c) (Observation d)) (OPT Unit (Coutility d))
right =
                             (Unit ~> Unit)
-> ((Coutility b ** Coutility d) ~> Coutility d)
-> Optic
     (Prostrong MonLensFl)
     Unit
     (Coutility d)
     Unit
     (Coutility b ** Coutility d)
forall {j} {k} (c :: (j +-> k) -> Constraint) (s :: k) (t :: j)
       (a :: k) (b :: j).
(CategoryOf j, CategoryOf k) =>
(s ~> a) -> (b ~> t) -> Optic c s t a b
iso Unit ~> Unit
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id (forall (a :: k) (b :: k).
(CopyDiscard k, Ob a, Ob b) =>
(a ** b) ~> b
forall {k} (a :: k) (b :: k).
(CopyDiscard k, Ob a, Ob b) =>
(a ** b) ~> b
snd @(Coutility b) @(Coutility d))
                               Optic
  (Prostrong MonLensFl)
  Unit
  (Coutility d)
  Unit
  (Coutility b ** Coutility d)
-> Optic_
     (OPT (Observation c) (Observation d))
     (OPT Unit (Coutility b ** Coutility d))
-> Optic_
     (OPT (Observation c) (Observation d)) (OPT Unit (Coutility d))
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
forall (b :: OPTIC k k (Prostrong MonLensFl))
       (c :: OPTIC k k (Prostrong MonLensFl))
       (a :: OPTIC k k (Prostrong MonLensFl)).
Optic_ b c -> Optic_ a b -> Optic_ a c
. Optic_
  (OPT
     (Observation a ** Observation c) (Observation b ** Observation d))
  (OPT Unit (Coutility b ** Coutility d))
Context
  (G (Observation a ** Observation c) (Coutility a ** Coutility c))
  (G (Observation b ** Observation d) (Coutility b ** Coutility d))
ctx
                               Optic_
  (OPT
     (Observation a ** Observation c) (Observation b ** Observation d))
  (OPT Unit (Coutility b ** Coutility d))
-> Optic_
     (OPT (Observation c) (Observation d))
     (OPT
        (Observation a ** Observation c) (Observation b ** Observation d))
-> Optic_
     (OPT (Observation c) (Observation d))
     (OPT Unit (Coutility b ** Coutility d))
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
forall (b :: OPTIC k k (Prostrong MonLensFl))
       (c :: OPTIC k k (Prostrong MonLensFl))
       (a :: OPTIC k k (Prostrong MonLensFl)).
Optic_ b c -> Optic_ a b -> Optic_ a c
. forall (m :: k) (s :: k) (t :: k) (a :: k) (b :: k).
(Comonoid m, Ob a, Ob b) =>
(s ~> (m ** a)) -> ((m ** b) ~> t) -> MonoidalLens s t a b
forall {k} (m :: k) (s :: k) (t :: k) (a :: k) (b :: k).
(Comonoid m, Ob a, Ob b) =>
(s ~> (m ** a)) -> ((m ** b) ~> t) -> MonoidalLens s t a b
monLens @(Observation a) (Observation a ** Observation c)
~> (Observation a ** Observation c)
forall (a :: k). Ob a => a ~> a
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
id (MonoidalLens
  (Observation a) (Coutility a) (Observation b) (Coutility b)
-> Observation a ~> Observation b
forall {j} {k} (c :: (k -> j -> Type) -> Constraint) (s :: k)
       (t :: j) (a :: k) (b :: j).
(CategoryOf j, CategoryOf k,
 (Ob a, Ob b) => c (ExOptic GetterFl a b)) =>
Optic c s t a b -> s ~> a
view MonoidalLens
  (Observation a) (Coutility a) (Observation b) (Coutility b)
v (Observation a ~> Observation b)
-> (Observation d ~> Observation d)
-> (Observation a ** Observation d)
   ~> (Observation b ** Observation d)
forall (x1 :: k) (x2 :: k) (y1 :: k) (y2 :: k).
(x1 ~> x2) -> (y1 ~> y2) -> (x1 ** y1) ~> (x2 ** y2)
forall {j} {k} (p :: j +-> k) (x1 :: k) (x2 :: j) (y1 :: k)
       (y2 :: j).
MonoidalProfunctor p =>
p x1 x2 -> p y1 y2 -> p (x1 ** y1) (x2 ** y2)
** forall (a :: k). (CategoryOf k, Ob a) => Obj a
forall {k} (a :: k). (CategoryOf k, Ob a) => Obj a
obj @(Observation d))
                       in Strategy a b
-> Optic_
     (OPT (Observation a) (Observation b)) (OPT Unit (Coutility b))
-> [Diagnostic]
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b -> Context a b -> [Diagnostic]
evaluateStrategy Strategy a b
gs Optic_
  (OPT (Observation a) (Observation b)) (OPT Unit (Coutility b))
left [Diagnostic] -> [Diagnostic] -> [Diagnostic]
forall a. [a] -> [a] -> [a]
P.++ Strategy c d
-> Optic_
     (OPT (Observation c) (Observation d)) (OPT Unit (Coutility d))
-> [Diagnostic]
forall {k} (a :: GAME k) (b :: GAME k).
Strategy a b -> Context a b -> [Diagnostic]
evaluateStrategy Strategy c d
hs Optic_
  (OPT (Observation c) (Observation d)) (OPT Unit (Coutility d))
right
                   )
          )

reindex :: [Fold ss] -> Strictified ss ts -> OpenGame ts a b -> OpenGame ss a b
reindex :: forall {k} (ss :: [Type]) (ts :: [Type]) (a :: GAME k)
       (b :: GAME k).
[Fold ss]
-> Strictified ss ts -> OpenGame ts a b -> OpenGame ss a b
reindex [Fold ss]
ss (Str Fold ss ~> Fold ts
f) g :: OpenGame ts a b
g@OpenGame{} = [Fold ss] -> (Fold ss -> Strategy a b) -> OpenGame ss a b
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
(Ob ss, Ob a, Ob b) =>
[Fold ss] -> (Fold ss -> Strategy a b) -> OpenGame ss a b
OpenGame [Fold ss]
ss (OpenGame ts a b -> Fold ts -> Strategy a b
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Fold ss -> Strategy a b
strategy OpenGame ts a b
g (Fold ts
 -> Strategy
      (G (Observation a) (Coutility a))
      (G (Observation b) (Coutility b)))
-> (Fold ss -> Fold ts)
-> Fold ss
-> Strategy
     (G (Observation a) (Coutility a)) (G (Observation b) (Coutility b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (p :: CAT k) (b :: k) (c :: k) (a :: k).
Promonad p =>
p b c -> p a b -> p a c
. Fold ss ~> Fold ts
Fold ss -> Fold ts
f)

instance (CopyDiscard k) => MonoidalProfunctor (Game :: CAT (GAME k)) where
  one :: Game Unit Unit
one = Game Unit Unit
Game (G Unit Unit) (G Unit Unit)
forall {k} (p :: CAT k) (a :: k). (Promonad p, Ob a) => p a a
forall (a :: GAME k). Ob a => Game a a
id
  Game OpenGame ss x1 x2
g ** :: forall (x1 :: GAME k) (x2 :: GAME k) (y1 :: GAME k) (y2 :: GAME k).
Game x1 x2 -> Game y1 y2 -> Game (x1 ** y1) (x2 ** y2)
** Game OpenGame ss y1 y2
h = OpenGame
  (ss ++ ss)
  (G (Observation x1 ** Observation y1)
     (Coutility x1 ** Coutility y1))
  (G (Observation x2 ** Observation y2)
     (Coutility x2 ** Coutility y2))
-> Game
     (G (Observation x1 ** Observation y1)
        (Coutility x1 ** Coutility y1))
     (G (Observation x2 ** Observation y2)
        (Coutility x2 ** Coutility y2))
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Game a b
Game (OpenGame ss x1 x2
-> OpenGame ss y1 y2 -> OpenGame (ss ++ ss) (x1 ** y1) (x2 ** y2)
forall k (ss :: [Type]) (ts :: [Type]) (a :: GAME k) (b :: GAME k)
       (c :: GAME k) (d :: GAME k).
CopyDiscard k =>
OpenGame ss a b
-> OpenGame ts c d -> OpenGame (ss ++ ts) (a ** c) (b ** d)
parallel OpenGame ss x1 x2
g OpenGame ss y1 y2
h)

instance (CopyDiscard k) => Monoidal (GAME k) where
  type Unit = G Unit Unit
  type a ** b = G (Observation a ** Observation b) (Coutility a ** Coutility b)
  withOb2 :: forall (a :: GAME k) (b :: GAME k) r.
(Ob a, Ob b) =>
(Ob (a ** b) => r) -> r
withOb2 @a @b Ob (a ** b) => r
r = forall k (a :: k) (b :: k) r.
(Monoidal k, Ob a, Ob b) =>
(Ob (a ** b) => r) -> r
withOb2 @k @(Observation a) @(Observation b) (forall k (a :: k) (b :: k) r.
(Monoidal k, Ob a, Ob b) =>
(Ob (a ** b) => r) -> r
withOb2 @k @(Coutility a) @(Coutility b) r
Ob (Coutility a ** Coutility b) => r
Ob (a ** b) => r
r)
  leftUnitor :: forall (a :: GAME k). Ob a => (Unit ** a) ~> a
leftUnitor @a = OpenGame
  '[]
  (G (Unit ** Observation a) (Unit ** Coutility a))
  (G (Observation a) (Coutility a))
-> Game
     (G (Unit ** Observation a) (Unit ** Coutility a))
     (G (Observation a) (Coutility a))
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Game a b
Game (((Unit ** Observation a) ~> Observation a)
-> (Coutility a ~> (Unit ** Coutility a))
-> OpenGame
     '[]
     (G (Unit ** Observation a) (Unit ** Coutility a))
     (G (Observation a) (Coutility a))
forall k (x :: k) (y :: k) (r :: k) (s :: k).
CopyDiscard k =>
(x ~> y) -> (r ~> s) -> OpenGame '[] (G x s) (G y r)
fromArrows (forall k (a :: k). (Monoidal k, Ob a) => (Unit ** a) ~> a
leftUnitor @k @(Observation a)) (forall k (a :: k). (Monoidal k, Ob a) => a ~> (Unit ** a)
leftUnitorInv @k @(Coutility a)))
  leftUnitorInv :: forall (a :: GAME k). Ob a => a ~> (Unit ** a)
leftUnitorInv @a = OpenGame
  '[]
  (G (Observation a) (Coutility a))
  (G (Unit ** Observation a) (Unit ** Coutility a))
-> Game
     (G (Observation a) (Coutility a))
     (G (Unit ** Observation a) (Unit ** Coutility a))
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Game a b
Game ((Observation a ~> (Unit ** Observation a))
-> ((Unit ** Coutility a) ~> Coutility a)
-> OpenGame
     '[]
     (G (Observation a) (Coutility a))
     (G (Unit ** Observation a) (Unit ** Coutility a))
forall k (x :: k) (y :: k) (r :: k) (s :: k).
CopyDiscard k =>
(x ~> y) -> (r ~> s) -> OpenGame '[] (G x s) (G y r)
fromArrows (forall k (a :: k). (Monoidal k, Ob a) => a ~> (Unit ** a)
leftUnitorInv @k @(Observation a)) (forall k (a :: k). (Monoidal k, Ob a) => (Unit ** a) ~> a
leftUnitor @k @(Coutility a)))
  rightUnitor :: forall (a :: GAME k). Ob a => (a ** Unit) ~> a
rightUnitor @a = OpenGame
  '[]
  (G (Observation a ** Unit) (Coutility a ** Unit))
  (G (Observation a) (Coutility a))
-> Game
     (G (Observation a ** Unit) (Coutility a ** Unit))
     (G (Observation a) (Coutility a))
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Game a b
Game (((Observation a ** Unit) ~> Observation a)
-> (Coutility a ~> (Coutility a ** Unit))
-> OpenGame
     '[]
     (G (Observation a ** Unit) (Coutility a ** Unit))
     (G (Observation a) (Coutility a))
forall k (x :: k) (y :: k) (r :: k) (s :: k).
CopyDiscard k =>
(x ~> y) -> (r ~> s) -> OpenGame '[] (G x s) (G y r)
fromArrows (forall k (a :: k). (Monoidal k, Ob a) => (a ** Unit) ~> a
rightUnitor @k @(Observation a)) (forall k (a :: k). (Monoidal k, Ob a) => a ~> (a ** Unit)
rightUnitorInv @k @(Coutility a)))
  rightUnitorInv :: forall (a :: GAME k). Ob a => a ~> (a ** Unit)
rightUnitorInv @a = OpenGame
  '[]
  (G (Observation a) (Coutility a))
  (G (Observation a ** Unit) (Coutility a ** Unit))
-> Game
     (G (Observation a) (Coutility a))
     (G (Observation a ** Unit) (Coutility a ** Unit))
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Game a b
Game ((Observation a ~> (Observation a ** Unit))
-> ((Coutility a ** Unit) ~> Coutility a)
-> OpenGame
     '[]
     (G (Observation a) (Coutility a))
     (G (Observation a ** Unit) (Coutility a ** Unit))
forall k (x :: k) (y :: k) (r :: k) (s :: k).
CopyDiscard k =>
(x ~> y) -> (r ~> s) -> OpenGame '[] (G x s) (G y r)
fromArrows (forall k (a :: k). (Monoidal k, Ob a) => a ~> (a ** Unit)
rightUnitorInv @k @(Observation a)) (forall k (a :: k). (Monoidal k, Ob a) => (a ** Unit) ~> a
rightUnitor @k @(Coutility a)))
  associator :: forall (a :: GAME k) (b :: GAME k) (c :: GAME k).
(Ob a, Ob b, Ob c) =>
((a ** b) ** c) ~> (a ** (b ** c))
associator @a @b @c =
    OpenGame
  '[]
  (G ((Observation a ** Observation b) ** Observation c)
     ((Coutility a ** Coutility b) ** Coutility c))
  (G (Observation a ** (Observation b ** Observation c))
     (Coutility a ** (Coutility b ** Coutility c)))
-> Game
     (G ((Observation a ** Observation b) ** Observation c)
        ((Coutility a ** Coutility b) ** Coutility c))
     (G (Observation a ** (Observation b ** Observation c))
        (Coutility a ** (Coutility b ** Coutility c)))
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Game a b
Game
      ( (((Observation a ** Observation b) ** Observation c)
 ~> (Observation a ** (Observation b ** Observation c)))
-> ((Coutility a ** (Coutility b ** Coutility c))
    ~> ((Coutility a ** Coutility b) ** Coutility c))
-> OpenGame
     '[]
     (G ((Observation a ** Observation b) ** Observation c)
        ((Coutility a ** Coutility b) ** Coutility c))
     (G (Observation a ** (Observation b ** Observation c))
        (Coutility a ** (Coutility b ** Coutility c)))
forall k (x :: k) (y :: k) (r :: k) (s :: k).
CopyDiscard k =>
(x ~> y) -> (r ~> s) -> OpenGame '[] (G x s) (G y r)
fromArrows
          (forall k (a :: k) (b :: k) (c :: k).
(Monoidal k, Ob a, Ob b, Ob c) =>
((a ** b) ** c) ~> (a ** (b ** c))
associator @k @(Observation a) @(Observation b) @(Observation c))
          (forall k (a :: k) (b :: k) (c :: k).
(Monoidal k, Ob a, Ob b, Ob c) =>
(a ** (b ** c)) ~> ((a ** b) ** c)
associatorInv @k @(Coutility a) @(Coutility b) @(Coutility c))
      )
  associatorInv :: forall (a :: GAME k) (b :: GAME k) (c :: GAME k).
(Ob a, Ob b, Ob c) =>
(a ** (b ** c)) ~> ((a ** b) ** c)
associatorInv @a @b @c =
    OpenGame
  '[]
  (G (Observation a ** (Observation b ** Observation c))
     (Coutility a ** (Coutility b ** Coutility c)))
  (G ((Observation a ** Observation b) ** Observation c)
     ((Coutility a ** Coutility b) ** Coutility c))
-> Game
     (G (Observation a ** (Observation b ** Observation c))
        (Coutility a ** (Coutility b ** Coutility c)))
     (G ((Observation a ** Observation b) ** Observation c)
        ((Coutility a ** Coutility b) ** Coutility c))
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Game a b
Game
      ( ((Observation a ** (Observation b ** Observation c))
 ~> ((Observation a ** Observation b) ** Observation c))
-> (((Coutility a ** Coutility b) ** Coutility c)
    ~> (Coutility a ** (Coutility b ** Coutility c)))
-> OpenGame
     '[]
     (G (Observation a ** (Observation b ** Observation c))
        (Coutility a ** (Coutility b ** Coutility c)))
     (G ((Observation a ** Observation b) ** Observation c)
        ((Coutility a ** Coutility b) ** Coutility c))
forall k (x :: k) (y :: k) (r :: k) (s :: k).
CopyDiscard k =>
(x ~> y) -> (r ~> s) -> OpenGame '[] (G x s) (G y r)
fromArrows
          (forall k (a :: k) (b :: k) (c :: k).
(Monoidal k, Ob a, Ob b, Ob c) =>
(a ** (b ** c)) ~> ((a ** b) ** c)
associatorInv @k @(Observation a) @(Observation b) @(Observation c))
          (forall k (a :: k) (b :: k) (c :: k).
(Monoidal k, Ob a, Ob b, Ob c) =>
((a ** b) ** c) ~> (a ** (b ** c))
associator @k @(Coutility a) @(Coutility b) @(Coutility c))
      )

instance (CopyDiscard k) => SymMonoidal (GAME k) where
  swap :: forall (a :: GAME k) (b :: GAME k).
(Ob a, Ob b) =>
(a ** b) ~> (b ** a)
swap @a @b = OpenGame
  '[]
  (G (Observation a ** Observation b) (Coutility a ** Coutility b))
  (G (Observation b ** Observation a) (Coutility b ** Coutility a))
-> Game
     (G (Observation a ** Observation b) (Coutility a ** Coutility b))
     (G (Observation b ** Observation a) (Coutility b ** Coutility a))
forall {k} (ss :: [Type]) (a :: GAME k) (b :: GAME k).
OpenGame ss a b -> Game a b
Game (((Observation a ** Observation b)
 ~> (Observation b ** Observation a))
-> ((Coutility b ** Coutility a) ~> (Coutility a ** Coutility b))
-> OpenGame
     '[]
     (G (Observation a ** Observation b) (Coutility a ** Coutility b))
     (G (Observation b ** Observation a) (Coutility b ** Coutility a))
forall k (x :: k) (y :: k) (r :: k) (s :: k).
CopyDiscard k =>
(x ~> y) -> (r ~> s) -> OpenGame '[] (G x s) (G y r)
fromArrows (forall k (a :: k) (b :: k).
(SymMonoidal k, Ob a, Ob b) =>
(a ** b) ~> (b ** a)
swap @k @(Observation a) @(Observation b)) (forall k (a :: k) (b :: k).
(SymMonoidal k, Ob a, Ob b) =>
(a ** b) ~> (b ** a)
swap @k @(Coutility b) @(Coutility a)))