-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.hs
More file actions
50 lines (41 loc) · 1.41 KB
/
Copy pathClient.hs
File metadata and controls
50 lines (41 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{-# LANGUAGE
DeriveDataTypeable
, TemplateHaskell
#-}
module GameEngine.AI.Client
(Client(..)
,mkClient
,emptyClient
,observe
,act
,client
,clientObservations
,clientUpdate
)
where
import Control.Lens
import Data.Typeable
-- A client is a type 't' that can:
-- - Be observed to produce 'ob'
-- - Be updated by an action 'ac' to produce a new 't'.
data Client t ob ac o = Client
{_client :: t -- The client
,_clientObservations :: t -> ob -- An observation type. Properties an agent can inspect
,_clientUpdate :: ac -> t -> (t,o) -- Update a client by an action, producing some output
}
deriving Typeable
makeLenses ''Client
instance Show t => Show (Client t ob ac o) where
show (Client t _ _) = "Client " ++ show t
instance Eq t => Eq (Client t ob ac o) where
(Client t0 _ _) == (Client t1 _ _) = t0 == t1
mkClient :: t -> (t -> ob) -> (ac -> t -> (t,o)) -> Client t ob ac o
mkClient thing observeThing updateThing = Client thing observeThing updateThing
emptyClient :: t -> Client t () () ()
emptyClient t0 = mkClient t0 (const ()) (\() t -> (t,()))
-- Make an observation about the current state of the Client
observe :: Client t ob ac o -> ob
observe (Client c obF _) = obF c
-- Given an action, apply it to the Client
act :: ac -> Client t ob ac o -> (Client t ob ac o,o)
act ac (Client t obF upF) = let (t',o) = upF ac t in (Client t' obF upF,o)