Skip to content

Commit 5015f46

Browse files
committed
added NaiveTokenModel
1 parent 7facce8 commit 5015f46

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

packages/spec-haskell/pkgs/semantic-money/semantic-money.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ library
2929
-- other-extensions:
3030
build-depends:
3131
base >=4.16.0.0 && <5,
32+
containers >= 0.8,
3233
data-default
3334

3435
test-suite semantic-money-test-suite

packages/spec-haskell/pkgs/semantic-money/src/Money/Theory/MonetaryTypes.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Money.Theory.MonetaryTypes
88
, mt_v_mul_u, mt_v_div_u, mt_v_mul_u_qr_u
99
, mt_fr_mul_u, mt_fr_div_u, mt_fr_mul_u_qr_u
1010
)
11-
, MonetaryTypes'tv, MonetaryTypes'tvr, MonetaryTypes'tvru
11+
, MonetaryTypes'tv, MonetaryTypes'tr, MonetaryTypes'tvr, MonetaryTypes'tvru
1212
) where
1313
-- base
1414
import Data.Kind (Type)
@@ -73,5 +73,6 @@ class ( Eq (MT_TIME mt), Ord (MT_TIME mt), Num (MT_TIME mt)
7373
type family MT_UNIT mt = (u :: Type) | u -> mt
7474

7575
type MonetaryTypes'tv mt t v = (MonetaryTypes mt, t ~ MT_TIME mt, v ~ MT_VALUE mt)
76-
type MonetaryTypes'tvr mt t v fr = (MonetaryTypes'tv mt t v, fr ~ MT_FLOWRATE mt)
76+
type MonetaryTypes'tr mt t fr = (MonetaryTypes mt, t ~ MT_TIME mt, fr ~ MT_FLOWRATE mt)
77+
type MonetaryTypes'tvr mt t v fr = (MonetaryTypes'tv mt t v, MonetaryTypes'tr mt t fr)
7778
type MonetaryTypes'tvru mt t v fr u = (MonetaryTypes'tvr mt t v fr, u ~ MT_UNIT mt)

packages/spec-haskell/pkgs/semantic-money/src/Money/Theory/SemanticMoney.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Money.Theory.SemanticMoney
33
( -- * Semantic Money Classes & Primitives
44
MonetaryUnit (settle, settledAt, flowRate, rtb)
55
, IndexedValue (shift1, flow1)
6-
, shift2b, flow2a, flow2b, align2a, align2b
6+
, shift2a, shift2b, flow2a, flow2b, align2a, align2b
77
-- * Semantic Money Instances
88
, BasicParticle (..)
99
, PDP_Index (..), PDP_Member (..), PDP_MemberMU, pdp_UpdateMember2
Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
{-# LANGUAGE LambdaCase #-}
12
module Money.Theory.TokenModel where
3+
-- containers
4+
import qualified Data.IntMap as IntMap
25
--
36
import Money.Theory.SemanticMoney
47

@@ -7,8 +10,46 @@ data TokenEvent mt acc where
710
TransferEvent :: forall mt acc {t} {v}.
811
MonetaryTypes'tv mt t v =>
912
t -> acc -> acc -> v -> TokenEvent mt acc
10-
-- UpdateFlowEvent :: forall mt acc {t} {v} {u}.
11-
-- MonetaryTypes'tvu mt t v u =>
12-
-- t -> acc -> acc -> v -> TokenEvent mt acc
13+
UpdateFlowEvent :: forall mt acc {t} {fr}.
14+
MonetaryTypes'tr mt t fr =>
15+
t -> acc -> acc -> fr -> TokenEvent mt acc
1316

14-
data TokenModel mt acc
17+
type Account = Int
18+
19+
------------------------------------------------------------------------------------------------------------------------
20+
-- NaiveTokenModel
21+
------------------------------------------------------------------------------------------------------------------------
22+
23+
data NaiveTokenModel mt acc = MkNaiveTokenModel
24+
{ accounts :: IntMap.IntMap (BasicParticle mt) -- ^ accounts indexed by Int
25+
, pools :: IntMap.IntMap (PDP_Index mt (BasicParticle mt)) -- ^ pools indexed by Int
26+
}
27+
28+
naiveProcessOneEvent ::
29+
MonetaryTypes mt =>
30+
NaiveTokenModel mt Account ->
31+
TokenEvent mt Account ->
32+
NaiveTokenModel mt Account
33+
naiveProcessOneEvent (MkNaiveTokenModel accs pools) = \case
34+
TransferEvent t from to amount -> go2 from to (shift2a amount t)
35+
UpdateFlowEvent t from to rate -> go2 from to (flow2a rate t)
36+
where go2 from to op =
37+
let sender = IntMap.findWithDefault mempty from accs
38+
receiver = IntMap.findWithDefault mempty from accs
39+
(sender', receiver') = op (sender, receiver)
40+
accs' = IntMap.insert from sender'
41+
$ IntMap.insert to receiver'
42+
$ accs
43+
in MkNaiveTokenModel accs' pools
44+
45+
naiveProcessEvents ::
46+
MonetaryTypes mt =>
47+
[TokenEvent mt Account] ->
48+
NaiveTokenModel mt Account
49+
naiveProcessEvents = foldl' naiveProcessOneEvent (MkNaiveTokenModel IntMap.empty IntMap.empty)
50+
51+
naiveSystemSnapshot ::
52+
MonetaryTypes'tv mt t v =>
53+
NaiveTokenModel mt Account ->
54+
IntMap.IntMap (t -> v)
55+
naiveSystemSnapshot = (rtb <$>) . accounts

0 commit comments

Comments
 (0)