Skip to content

Commit ac9208c

Browse files
committed
TwoPhaseParticle
1 parent da80a60 commit ac9208c

File tree

5 files changed

+54
-18
lines changed

5 files changed

+54
-18
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
@@ -26,6 +26,7 @@ library
2626
Money.Theory.SemanticMoney
2727
Money.Theory.TokenModel
2828
Money.Theory.TokenModel.NaiveTokenModel
29+
Money.Theory.TokenModel.TwoPhaseTokenModel
2930
-- other-modules:
3031
-- other-extensions:
3132
build-depends:

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class (MonetaryTypes mt, Eq mu) =>
2828
MonetaryUnit mt mu | mu -> mt where
2929
settle :: t ~ MT_TIME mt => t -> mu -> mu
3030
settledAt :: t ~ MT_TIME mt => mu -> t
31-
flowRate :: fr ~ MT_FLOWRATE mt => mu -> fr
31+
flowRate :: MonetaryTypes'tr mt t fr => mu -> t -> fr
3232
rtb :: MonetaryTypes'tvr mt t v fr => mu -> t -> v
3333

3434
-- | An indexed monetary value and its operators (1-primitives).
35-
class (MonetaryUnit mt iv, Monoid iv, Eq iv) =>
35+
class (MonetaryUnit mt iv, Monoid iv) =>
3636
IndexedValue mt iv | iv -> mt where
3737
shift1 :: v ~ MT_VALUE mt => v -> iv -> (iv, v)
3838
flow1 :: fr ~ MT_FLOWRATE mt => fr -> iv -> (iv, fr)
@@ -57,7 +57,7 @@ flow2a, flow2b ::
5757
(MonetaryTypes'tr mt t fr, IndexedValue mt a, IndexedValue mt b) =>
5858
fr -> t -> (a, b) -> (a, b)
5959
flow2a dfr t (a, b) =
60-
let (b1, fr_a) = flow1 (flowRate a) (settle t mempty)
60+
let (b1, fr_a) = flow1 (flowRate a t) (settle t mempty)
6161
(b2, fr_a') = flow1 (-fr_a + dfr) (settle t mempty)
6262
(a', fr_a'') = flow1 (-fr_a') (settle t a)
6363
in assert (fr_a' == -fr_a'') (a', b <> b1 <> b2)
@@ -70,13 +70,13 @@ flow2b dfr t (a, b) = swap (flow2a (-dfr) t (b, a))
7070
-- 2) The adjustment must not produce new error term, or otherwise it would require recursive adjustments.
7171
align2a, align2b ::
7272
(IndexedValue mt a, IndexedValue mt b) =>
73-
MT_UNIT mt -> MT_UNIT mt -> (a, b) -> (a, b)
74-
align2a u u' (a, b) = (a', b')
75-
where fr = flowRate a
73+
MT_UNIT mt -> MT_UNIT mt -> MT_TIME mt -> (a, b) -> (a, b)
74+
align2a u u' t (a, b) = (a', b')
75+
where fr = flowRate a t
7676
(fr', e) = if u' == 0 then (0, fr `mt_fr_mul_u` u) else fr `mt_fr_mul_u_qr_u` (u, u')
7777
a' = fst (flow1 fr' a)
78-
b' = fst (flow1 (e + flowRate b) b)
79-
align2b u u' (a, b) = swap (align2a u u' (b, a))
78+
b' = fst (flow1 (e + flowRate b t) b)
79+
align2b u u' t (a, b) = swap (align2a u u' t (b, a))
8080

8181
------------------------------------------------------------------------------------------------------------------------
8282
-- Basic Particle: building block for indexes
@@ -94,7 +94,7 @@ instance MonetaryTypes mt => Semigroup (BasicParticle mt) where
9494
a@(BasicParticle t1 _ _) <> b@(BasicParticle t2 _ _) = BasicParticle t' (sv1 + sv2) (r1 + r2)
9595
-- The binary operator supports negative time values while abiding the monoidal laws.
9696
-- The practical semantics of values of mixed-sign is not of the concern of this specification.
97-
where t' | t1 == 0 = t2 | t2 == 0 = t1 | otherwise = max t1 t2
97+
where t' = max t1 t2
9898
(BasicParticle _ sv1 r1) = settle t' a
9999
(BasicParticle _ sv2 r2) = settle t' b
100100

@@ -107,7 +107,7 @@ instance MonetaryTypes mt =>
107107
, bp_settled_value = rtb a t'
108108
}
109109
settledAt = bp_settled_at
110-
flowRate = bp_flow_rate
110+
flowRate = const . bp_flow_rate
111111
rtb (BasicParticle t s r) t' = r `mt_fr_mul_t` (t' - t) + s
112112

113113
instance MonetaryTypes mt =>
@@ -142,7 +142,7 @@ pdp_UpdateMember2 ::
142142
pdp_UpdateMember2 u' t' (a, (b, pm)) = (a'', (b'', pm''))
143143
where (PDP_Index tu mpi, pm'@(PDP_Member u _ _)) = settle t' (b, pm)
144144
tu' = tu + u' - u
145-
(mpi', a'') = align2b tu tu' (mpi, settle t' a)
145+
(mpi', a'') = align2b tu tu' t' (mpi, settle t' a)
146146
b'' = PDP_Index tu' mpi'
147147
pm'' = pm' { pdpm_owned_unit = u', pdpm_synced_wp = mpi' }
148148

@@ -199,7 +199,7 @@ instance MonetaryUnit mt wp =>
199199

200200
settledAt (_, PDP_Member _ _ mps) = settledAt mps
201201

202-
flowRate (PDP_Index _ mpi, PDP_Member u _ _) = flowRate mpi `mt_fr_mul_u` u
202+
flowRate (PDP_Index _ mpi, PDP_Member u _ _) t = flowRate mpi t `mt_fr_mul_u` u
203203

204204
rtb (PDP_Index _ mpi, PDP_Member u sv mps) t' = sv +
205205
-- let ti = bp_settled_at mpi
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Money.Theory.TokenModel.TwoPhaseTokenModel where
2+
-- containers
3+
import qualified Data.IntMap.Lazy as IntMap
4+
--
5+
import Money.Theory.SemanticMoney
6+
import Money.Theory.TokenModel
7+
8+
9+
data TwoPhaseParticle mt = MkTwoPhaseParticle
10+
{ confirmedParticle :: BasicParticle mt
11+
, pendingParticle :: BasicParticle mt
12+
}
13+
deriving Eq
14+
15+
instance MonetaryTypes mt => MonetaryUnit mt (TwoPhaseParticle mt) where
16+
settle t (MkTwoPhaseParticle p_c p_p) = MkTwoPhaseParticle p_c (settle t p_p)
17+
settledAt = settledAt . pendingParticle
18+
flowRate mu t =
19+
flowRate (confirmedParticle mu) t +
20+
if t > settledAt (confirmedParticle mu) then flowRate (pendingParticle mu) t else 0
21+
rtb mu t =
22+
rtb (confirmedParticle mu) t +
23+
if t > settledAt (confirmedParticle mu) then rtb (pendingParticle mu) t else 0
24+
25+
type Account = Int
26+
27+
data TwoPhaseTokenModel mt = MkTwoPhaseTokenModel
28+
{ accounts :: IntMap.IntMap (TwoPhaseParticle mt) -- ^ accounts indexed by Int
29+
, pools :: IntMap.IntMap (PDP_Index mt (TwoPhaseParticle mt)) -- ^ pools indexed by Int
30+
}
31+
32+
instance MonetaryTypes mt => TokenModel mt (TwoPhaseParticle mt) Account (TwoPhaseTokenModel mt) where
33+
-- initToken
34+
-- tokenAccounts
35+
-- processOneTokenEvent

packages/spec-haskell/pkgs/semantic-money/test/Money/Theory/SemanticMoney_prop.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@ one2n_pd_tests = describe "1toN proportional distribution 2-primitives" $ do
132132
--------------------------------------------------------------------------------
133133

134134
uu_flow2a (a :: TestUniversalIndex) (b :: TestUniversalIndex) t1 r1 t2 r2 t3 =
135-
flowRate b' - flowRate b == r1 + r2 && flowRate a' - flowRate a == -r1 -r2 &&
135+
flowRate b' t3 - flowRate b t3 == r1 + r2 && flowRate a' t3 - flowRate a t3 == -r1 -r2 &&
136136
rtb b' t3 - rtb b t3 == rtb a t3 - rtb a' t3 &&
137137
-- for shift flow semantics: rtb b' t3 - (rtb b t3 - rtb b t1) - rtb b t1 == rtb b' t3 - rtb b t3
138138
r1 `mt_fr_mul_t` (t2 - t1) + (r1 + r2) `mt_fr_mul_t` (t3 - t2) == rtb b' t3 - rtb b t3
139139
where (a', b') = flow2a r2 t2 (flow2a r1 t1 (a, b))
140140

141141
uu_flow2b (a :: TestUniversalIndex) (b :: TestUniversalIndex) t1 r1 t2 r2 t3 =
142-
flowRate b' - flowRate b == r1 + r2 && flowRate a' - flowRate a == -r1 -r2 &&
142+
flowRate b' t3 - flowRate b t3 == r1 + r2 && flowRate a' t3 - flowRate a t3 == -r1 -r2 &&
143143
rtb b' t3 - rtb b t3 == rtb a t3 - rtb a' t3 &&
144144
-- ditto
145145
r1 `mt_fr_mul_t` (t2 - t1) + (r1 + r2) `mt_fr_mul_t` (t3 - t2) == rtb b' t3 - rtb b t3
@@ -157,9 +157,9 @@ uu_flow2b (a :: TestUniversalIndex) (b :: TestUniversalIndex) t1 r1 t2 r2 t3 =
157157
updp_flow2b (a :: TestUniversalIndex) t1 r1 t2 r2 t3 =
158158
rtb (b'', b1') t3 - rtb (b', b1') t3 == rtb a' t3 - rtb a'' t3 &&
159159
rtb (b'', b1') t3 - rtb (b', b1') t3 == r1 `mt_fr_mul_t` (t2 - t1) + (r1 + r2) `mt_fr_mul_t` (t3 - t2) &&
160-
flowRate a'' - flowRate a' == -(r1 + r2) &&
161-
flowRate b'' - flowRate b' == r1 + r2 &&
162-
flowRate (b'', b1') == r1 + r2
160+
flowRate a'' t3 - flowRate a' t3 == -(r1 + r2) &&
161+
flowRate b'' t3 - flowRate b' t3 == r1 + r2 &&
162+
flowRate (b'', b1') t3 == r1 + r2
163163
where (a', (b', b1')) = pdp_UpdateMember2 1 t1 (a, (mempty :: TestPDP_Index, def))
164164
(a'', b'') = flow2b r2 t2 (flow2b r1 t1 (a', b'))
165165

packages/spec-haskell/pkgs/semantic-money/test/Money/Theory/TestMonetaryTypes.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Money.Theory.SemanticMoney
1111

1212
newtype TestTime = TestTime Integer deriving (Enum, Eq, Ord, Num, Real, Integral, Show)
1313
instance Arbitrary TestTime where
14-
arbitrary = TestTime <$> arbitrary -- choose (0, 2 ^ (32 :: Integer))
14+
arbitrary = TestTime <$> choose (0, 2 ^ (32 :: Integer))
1515

1616
newtype TestMValue = TestMValue Integer deriving (Enum, Eq, Ord, Num, Real, Integral, Show)
1717
instance Arbitrary TestMValue where

0 commit comments

Comments
 (0)