-
Notifications
You must be signed in to change notification settings - Fork 9
/
StateTransformingLaxMorph.v
333 lines (249 loc) · 10.1 KB
/
StateTransformingLaxMorph.v
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
(* From Coq Require Import Program.Wf. *)
From Coq Require Import Relation_Definitions.
From SSProve.Relational Require Import OrderEnrichedCategory OrderEnrichedRelativeMonadExamples.
From SSProve.Mon Require Import SPropBase.
Set Warnings "-notation-overridden,-ambiguous-paths".
From mathcomp Require Import all_ssreflect (*boolp*).
Set Warnings "notation-overridden,ambiguous-paths".
From SSProve.Crypt Require Import Axioms OrderEnrichedRelativeAdjunctions LaxFunctorsAndTransf LaxMorphismOfRelAdjunctions TransformingLaxMorph OrderEnrichedRelativeAdjunctionsExamples ThetaDex SubDistr Theta_exCP ChoiceAsOrd FreeProbProg UniversalFreeMap RelativeMonadMorph_prod LaxComp choice_type Casts.
(* From SSProve.Crypt Require Import only_prob.Rules. *)
Import SPropNotations.
(*In this file we transform the lax relative monad morphism thetaDex : FreeProb² → Wrelprop
into a stateful effect observation stT_thetaDex : StT(FreeProb²) → StT(Wrelprop)*)
(*
We also subsequently make the domain of stT_thetaDex free by precomposing with a morphism
with type FreeStateProb² → StT(FreeProb²)
*)
Section StT_thetaDex_definition.
Let myThetaDex := thetaDex.
Context {S1 S2 : choiceType}.
Let TingAdj1_0 := Chi_DomainStateAdj S1 S2.
Let TingAdj2_0 := Chi_CodomainStateAdj S1 S2.
Let myJMWprod :=
@RelativeMonadMorph_prod.JMWprod _ _ (ord_functor_id TypeCat) _ _ (ord_functor_id TypeCat).
Program Definition state_beta' : lnatTrans
(lord_functor_comp (strict2laxFunc (binaryToTheS S1 S2))
(strict2laxFunc (ord_functor_comp myJMWprod Jprod)))
(lord_functor_comp (strict2laxFunc (ord_functor_comp myJMWprod Jprod))
(strict2laxFunc (ToTheS_OrdCat S1 S2))) :=
mkLnatTrans _ _.
Next Obligation.
move=> [A1 A2]. simpl.
unshelve econstructor.
move=> [g1 g2]. unshelve econstructor.
move=> [s1 s2]. exact ⟨ g1 s1 , g2 s2 ⟩.
cbv. move=> x y. move=> Hxy. destruct Hxy. reflexivity.
cbv. move=> x y. move=> Hxy. move=> [s1 s2]. destruct Hxy. reflexivity.
Defined.
(*this is a morphism between the transformed adjunctions. see rlmm_from_lmla below
to conclude the def of stT_thetaDex*)
Program Definition stT_thetaDex_adj :=
Transformed_lmla myThetaDex TingAdj1_0 TingAdj2_0 state_beta' _ _.
Next Obligation.
move=> [A1 A2 [a1 a2] [s1 s2]].
(*Unfortunately we have to manually destruct A1 and A2 because functions were defined like this in
Theta_exCP.v*)
move: A1 a1. move=> [A1 chA1] a1.
move: A2 a2. move=> [A2 chA2] a2.
cbv. reflexivity.
Qed.
Next Obligation.
move=> [[A1 chA1] [A2 chA2]] [Y1 Y2] [g1 g2]. simpl.
apply sig_eq. simpl. apply boolp.funext. move=> [[a1 s1] [a2 s2]].
simpl. reflexivity.
Qed.
Definition stT_thetaDex := rlmm_from_lmla stT_thetaDex_adj.
End StT_thetaDex_definition.
Section GetDomainAndCodomain.
Context {C D1 D2 : ord_category} {J1 : ord_functor C D1} {J12 : ord_functor D1 D2} {J2 : ord_functor C D2}
{phi : natIso J2 (ord_functor_comp J1 J12)} (psi := ni_inv phi)
{M1 : ord_relativeMonad J1} {M2: ord_relativeMonad J2}.
Definition rlmm_domain (smTheta : relativeLaxMonadMorphism J12 phi M1 M2)
: ord_relativeMonad J1
:= M1.
Definition rlmm_codomain (smTheta : relativeLaxMonadMorphism J12 phi M1 M2)
: ord_relativeMonad J2
:= M2.
End GetDomainAndCodomain.
(*
Here we build a unary choiceType relative monad, free, with a stateful-probabilistic
signature.
*)
Section FreeStateProbMonad.
Context {S : choiceType}. (*the set of states is itself a choiceType*)
(*old formulation*)
(* Inductive stateE : Type -> Type := *)
(* |gett : stateE S *)
(* |putt : S -> stateE unit. *)
Inductive S_OP :=
|sgett : S_OP
|sputt : S -> S_OP.
Definition S_AR : S_OP -> choiceType := fun sop =>
match sop with
|sgett => S
|sputt _ => chUnit
end.
(*Now how can we combine state and probabilities?*)
(*operations are either stateful or probabilistic...*)
Inductive SP_OP :=
|gett : SP_OP
|putt : S -> SP_OP
|samplee : P_OP -> SP_OP.
Definition SP_AR : SP_OP -> choiceType := fun stpOp =>
match stpOp with
|gett => S
|putt _ => chUnit
|samplee p_op => chElement (projT1 p_op)
end.
Definition op_iota : P_OP -> SP_OP := samplee.
Lemma computational_sliceMorph (o : P_OP) :
P_AR o = SP_AR ( samplee o ).
Proof.
reflexivity.
Qed.
(*retro comp*)
Definition ops_StP := SP_OP.
Definition ar_StP := SP_AR.
(*Here is our free choiceType relative monad with a stateful probabilistic signature*)
Definition FrStP := rFree ops_StP ar_StP.
End FreeStateProbMonad.
(*Now we build a relative monad morphism from FrStP to StT(Frp). In other words
we interpret the stateful part of the signature of FrStP, but not the probabilistic
one*)
Section UnaryInterpretState.
Context {S : choiceType}.
(*The domain of the intended morphism is FrStP ...*)
Let FrStP_filled := @FrStP S.
(*The codomain is StT(Frp)*)
Let prob_ops := P_OP.
Let prob_ar := P_AR.
Definition Frp := rFree prob_ops prob_ar.
Let myLflat := unaryTimesS1 S.
Let myR := ToTheS S.
Notation FrpF := (rFreeF prob_ops prob_ar).
Program Definition unaryStTransfromingAdj :
leftAdjunctionSituation choice_incl (ord_functor_comp myLflat choice_incl) myR :=
mkNatIso _ _ _ _ _ _ _.
Next Obligation. (*ni_map*)
move=> [A T]. simpl. unshelve econstructor.
move=> g a s. exact (g (a,s)).
cbv. move=> g1 g2. move=> Hg12. move=> a.
apply boolp.funext. move=> s.
move: Hg12 => /(_ (a,s)) Hg12.
assumption.
Defined.
Next Obligation. (*ni_inv*)
move=> [A T]. simpl. unshelve econstructor.
move=> g [a s]. exact (g a s).
cbv. move=> g1 g2. move=> Hg12. move=> [a s].
move: Hg12 => /(_ a) Hg12.
destruct Hg12. reflexivity.
Defined.
Next Obligation.
move=> [A T]. move=> [A' T'].
move=> [p q]. simpl in *.
apply sig_eq. simpl. apply boolp.funext. move=> g.
apply boolp.funext. move=> a'. apply boolp.funext.
move=> s. simpl.
unfold OrderEnrichedRelativeAdjunctionsExamples.ToTheS_obligation_1.
reflexivity.
Qed.
Next Obligation.
move=> [A T].
apply sig_eq. simpl.
apply boolp.funext. move=> g. cbv.
apply boolp.funext. move=> a. reflexivity.
Qed.
Next Obligation.
move=> [A T].
apply sig_eq. simpl.
apply boolp.funext. move=> g. cbv.
apply boolp.funext. move=> [a s].
reflexivity.
Qed.
(*This is StT(Frp)*)
Definition stT_Frp := AdjTransform Frp myLflat myR unaryStTransfromingAdj.
(*Let us define get and put in this monad ...*)
Let retrFree_filled (X:choiceType) := retrFree prob_ops prob_ar X.
Definition getStP : stT_Frp S :=
fun s : S => retrFree_filled (F_choice_prod_obj ⟨ S, S ⟩) (s, s).
Definition putStP : S -> stT_Frp unit_choiceType := fun new_s old_s =>
retrFree_filled (F_choice_prod ⟨ unit_choiceType, S ⟩) (tt, new_s).
Definition probopStP {T : choice_type} (sd: SDistr T) : stT_Frp (chElement T).
move=> s. simpl.
unshelve eapply ropr.
unshelve econstructor. exact T. exact sd.
cbn. move=> t. eapply retrFree_filled. simpl. exact ( (t,s) ).
Defined.
Let ops_StP_filled := @ops_StP S.
Let ar_StP_filled := @ar_StP S.
Definition sigMap : forall op : ops_StP_filled, stT_Frp( ar_StP_filled op ).
move=> op. cbv in op. destruct op.
- exact getStP.
- cbn. exact (putStP s).
- cbn. apply probopStP. destruct p. cbn. assumption.
Defined.
Definition unaryIntState
: relativeMonadMorphism _ _ (FrStP_filled) stT_Frp
:= @outOfFree (ops_StP_filled) (ar_StP_filled) stT_Frp sigMap.
End UnaryInterpretState.
(*now we square this morphism to get a relative monad morphism FrStP² → stT(Frp)²*)
Section SquareUnaryIntState.
Context {S1 S2 : choiceType}.
Let unaryIntState_filled_left := @unaryIntState S1.
Let unaryIntState_filled_right := @unaryIntState S2.
Definition preInterpretState :=
prod_relativeMonadMorphism
unaryIntState_filled_left unaryIntState_filled_right.
End SquareUnaryIntState.
(*We also need an additional bit stT(Frp)² → stT(Frp²), the latter being the domain
of stT_thetaDex *)
Section StT_vs_squaredMonads.
Context {S1 S2 : choiceType}.
Let preInterpretState_filled := @preInterpretState S1 S2.
Let stT_thetaDex_filled := @stT_thetaDex S1 S2.
(*domain of the additional morphism*)
Let squOf_stT_Frp := rlmm_codomain preInterpretState_filled.
(*and codomain*)
Let stT_squOf_Frp := rlmm_domain stT_thetaDex_filled.
(*base square*)
Definition BinaryTrivialChi :
natIso ( prod_functor choice_incl choice_incl )
(ord_functor_comp (prod_functor choice_incl choice_incl)
(ord_functor_id (prod_cat TypeCat TypeCat))).
apply natIso_sym. apply ord_functor_unit_right.
Defined.
Notation Frpp := (rFreeF P_OP P_AR).
Program Definition additionalIntState :
relativeMonadMorphism (ord_functor_id _) BinaryTrivialChi squOf_stT_Frp stT_squOf_Frp :=
mkRelMonMorph (ord_functor_id _) BinaryTrivialChi squOf_stT_Frp stT_squOf_Frp _ _ _.
Next Obligation.
move=> [A1 A2]. unshelve econstructor.
easy.
easy.
Defined.
End StT_vs_squaredMonads.
(*And now we are finally ready to define the intended theta *)
Section MakeTheDomainFree.
Context {S1 S2 : choiceType}.
Let preInterpretState_filled := @preInterpretState S1 S2.
Let addIntState_filled := @additionalIntState S1 S2.
Let stT_thetaDex_filled := @stT_thetaDex S1 S2.
(*FrStP² → stT(Frp²) part*)
Definition justInterpState :=
rlmm_comp _ _ _ _ _ _ _ preInterpretState_filled addIntState_filled.
(*the other part is stT_thetaDex : stT(Frp²) → Wstrelprop*)
(*we now combine those two morphisms *)
Definition thetaFstdex := rlmm_comp _ _ _ _ _ _ _ justInterpState stT_thetaDex_filled.
(*
Fstdex because:
start with theta dex = θex ∘ θdens : Frp² → Wrelprop
then state transform ; stT( θdex ) : StT( Frp² ) → WStRelprop
then make the domain free ; θFstdex : FrStP ² → WStRelprop
*)
(*thetaFdex : FrStP² → Wrelprop *)
(*
Eval hnf in rlmm_domain thetaFstdex.
Eval hnf in rlmm_codomain thetaFstdex.
*)
End MakeTheDomainFree.