forked from SSProve/ssprove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstraMonadExamples.v
692 lines (572 loc) · 24.5 KB
/
DijkstraMonadExamples.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
From Coq Require Import ssreflect ssrfun.
From Mon Require Export Base.
From Mon Require Import SPropBase SPropMonadicStructures Monoid SpecificationMonads MonadExamples.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Set Primitive Projections.
(****************************************************************************)
(* Ordered monad algebras yield morphisms into the monotone continuations *)
(* monad. This gives a general way of generating many effect observations, *)
(* as described at the end of Section 4.1 *)
(****************************************************************************)
Section Algebra.
Context (M:Monad) (alg:OrderedAlgebra M).
Existing Instance oalg_order.
Definition WPSpecMonad := @MonoCont _ (@oalg_rel _ alg) _.
Program Definition mor_underlying X (m:M X) : WPSpecMonad X :=
exist _ (fun post => alg (bind m (fun x => ret (post x)))) _.
Next Obligation.
move=> ? ? H. apply oalg_mono ; assumption.
Qed.
Import FunctionalExtensionality.
Program Definition mor : MonadMorphism M WPSpecMonad :=
@mkMorphism _ _ mor_underlying _ _.
Next Obligation.
apply sig_eq; extensionality post; cbv ; rewrite monad_law1 malg_ret //.
Qed.
Next Obligation.
apply sig_eq ; extensionality post; cbv; rewrite monad_law3 malg_bind //.
Defined.
Definition D_wp := @Dθ _ _ mor.
Definition underlying {A spec} : D_wp A spec -> M A := @proj1_sig _ _.
End Algebra.
Section UpdateMonadEffectObservation.
Context (M:monoid) (X : monoid_action M).
Let UpdX := Upd X.
Let WUpdX := UpdSpec X.
Program Definition UpdEffObs : MonadMorphism UpdX WUpdX :=
@mkMorphism _ _ (fun A m => exist _ (fun p x => let mx := m x in p (nfst mx) (nsnd mx)) _) _ _.
Next Obligation.
move=> ? ? H ? ; apply H.
Qed.
Next Obligation.
compute. f_equal. apply ax_proof_irrel.
Qed.
Next Obligation.
apply sig_eq ; simpl. reflexivity.
Qed.
End UpdateMonadEffectObservation.
Section Option.
Program Definition Opt : Monad :=
@mkMonad option (@Some) (fun _ _ m f => Option.bind f m) _ _ _.
(* TODO : report this error of Solve Obligations *)
(* Solve Obligations with move: c => [?|] //=. *)
Next Obligation. move: c => [?|] //=. Qed.
Next Obligation. move: c => [?|] //=. Qed.
Program Definition TotAlg : OrderedAlgebra Opt :=
let alg := @mkMonadAlgebra Opt Prop
(fun c => match c with
| Some p => p
| None => False end) _ _ in
@mkOrderedAlgebra _ alg SProp_op_order _ _.
Next Obligation. move: m => [?|] //=. Qed.
Next Obligation. typeclasses eauto. Qed.
Next Obligation. move=> ? ? ? [?|] //= ; intuition. Qed.
Definition morWpTot := mor TotAlg.
Program Definition PartAlg : OrderedAlgebra Opt :=
let alg := @mkMonadAlgebra Opt Prop
(fun c => match c with
| Some p => p
| None => True end) _ _ in
@mkOrderedAlgebra _ alg SProp_op_order _ _.
Next Obligation. move: m => [?|] //=. Qed.
Next Obligation. typeclasses eauto. Qed.
Next Obligation. move=> ? ? ? [?|] //= ; intuition. Qed.
Definition morWpPart := mor PartAlg.
Section commenting_strongestPostconditions.
(*
Import StrongestPostcondition.
Let SP := ForwardPredTransformer.
Import FunctionalExtensionality.
Import SPropNotations.
Section TotalSPNotExactlyMorphism.
Program Definition θSP A (m : Opt A) : SP A :=
match m with
| Some a => ret a
| None => exist _ (fun pre a => @mkOver _ pre _) _
end.
Lemma θSP_ret A (a:A) : θSP (ret a) = ret a.
Proof. reflexivity. Qed.
Lemma θSP_bind A B (m:Opt A) (f : A -> Opt B) :
bind (θSP m) (fun b => θSP (f b)) ≤ θSP (bind m f).
Proof.
move: m => [a|] //=.
change SP_bind with (@monad_bind SP).
change SP_ret with (@monad_ret SP).
erewrite monad_law1.
move:(f a) => [?|] //=.
move=> pre b /= [a H] ; exact (over H).
Qed.
End TotalSPNotExactlyMorphism.
Program Definition morSpPart : MonadMorphism Opt SP :=
@mkMorphism Opt SP (fun A c => match c with
| Some a => ret a
| None => exist _ (fun pre a => @mkOver _ False _) _
end) _ _.
Next Obligation. destruct H. Qed.
Next Obligation.
move: m => [a|] //=.
change SP_bind with (@monad_bind SP).
change SP_ret with (@monad_ret SP).
erewrite monad_law1.
move:(f a) => [?|] //=.
apply sig_eq=> //=.
extensionality pre ; extensionality y.
apply SPropOver_eq.
split.
move=> H ; destruct H.
move=> [? H] ; move: (over H) => H0 ; destruct H0.
Qed.
Lemma part_pairing A m : wpsp_pairing (morWpPart A m) (morSpPart A m).
Proof.
cbv ; destruct m as [?|]; split ; try intuition ; subst ; intuition.
Qed.
Program Definition ranTotDiv A B (f : B -> Opt A) w
(H : morWpTot A (bind (None : Opt B) f) ≤ w) :
ran w (morWpTot B None) :=
exist _ (fun _ => exist _ (fun _ => False) _) _.
Next Obligation. cbv ; intuition. Qed.
Next Obligation. cbv ; split. cbv in H. assumption. intuition. Qed.
End Option.
*)
End commenting_strongestPostconditions.
End Option.
Section FreeMonadEffectObservation.
Context (S : Type) (P: S -> Type).
Context (W : OrderedMonad).
Context (OpContSpecs : forall s, W (P s)).
Let Free := Free P.
Fixpoint OpSpec_mor A (m : Free A) : W A :=
match m with
| retFree a => ret a
| @opr _ _ _ s k => bind (OpContSpecs s) (fun ps => OpSpec_mor (k ps))
end.
Import FunctionalExtensionality.
Program Definition OpSpecEffObs : MonadMorphism Free W :=
@mkMorphism _ _ OpSpec_mor _ _.
Next Obligation.
elim m=> [?|? ? IH] //=.
rewrite /bind /ret monad_law1 //.
rewrite /bind monad_law3 ; f_equal. extensionality ps ; apply IH.
Qed.
Definition OpSpecWP : Dijkstra W := Dθ OpSpecEffObs.
Program Definition cont_perform : forall s, OpSpecWP (P s) (@OpContSpecs s) :=
fun s => exist _ (@opr _ _ _ s (@retFree _ _ _)) _.
Next Obligation.
cbv ; rewrite monad_law2 ; reflexivity.
Qed.
Import SPropMonadicStructuresNotation.
Inductive ObsFree A : W A -> Type :=
| OFRet : forall a w, ret a ≤ w -> ObsFree w
| OFOp : forall s w w',
bind (OpContSpecs s) w' ≤ w ->
(forall x : P s, OpSpecWP A (w' x)) ->
ObsFree w.
Definition observeθ A w (m:OpSpecWP A w) : ObsFree w.
Proof.
destruct m as [[|] H].
exact (OFRet H).
apply (OFOp H)=> a. exists (f a). reflexivity.
Defined.
Context (OpPartialRan : forall s C (w : W C) w0,
bind (OpContSpecs s) w0 ≤ w -> ran w (OpContSpecs s)).
Definition observeRan A w (m:OpSpecWP A w) : ObsFree w.
Proof.
destruct m as [[|] H] ; simpl in H.
exact (OFRet H).
pose (OpPartialRan H) as H'.
destruct H' as [w0 H'].
simple refine (OFOp _ _).
exact s. exact w0. destruct H' ; assumption.
move=> a. exists (f a). destruct H' as [H1 H2].
apply (H2 (fun a => OpSpecEffObs A (f a))). assumption.
Defined.
End FreeMonadEffectObservation.
Section FreeDijkstraMonads.
Import SPropMonadicStructuresNotation.
Context (S : Type) (P: S -> Type).
Context (W : OrderedMonad).
Context (OpSpecs : forall s, W (P s)).
Context (OpPartialRan : forall s C (w : W C) w0,
bind (OpSpecs s) w0 ≤ w -> ran w (OpSpecs s)).
Inductive FreeD (A:Type) : W A -> Type :=
| FDRet : forall (a:A) (w:W A), ret a ≤ w -> FreeD w
| FDop : forall s w (w':ran w (OpSpecs s)),
(forall x:P s, FreeD (proj1_sig w' x)) -> FreeD w.
Definition fd_dret A (a:A) : FreeD (ret a) :=
@FDRet _ a (ret a) ltac:(reflexivity).
Definition fd_weaken A {w1 w2} (m : @FreeD A w1) (Hw : w1 ≤ w2)
: FreeD w2.
Proof.
revert w2 Hw ; induction m=> w2 Hw.
simple refine (@FDRet _ a _ _). etransitivity ; eassumption.
simple refine (@FDop _ s _ _ (fun x => _)).
apply (OpPartialRan (w0:=proj1_sig w')).
destruct w' as [? [? ?]] ; etransitivity ; eassumption.
simpl.
apply (X x).
apply ran_mono. assumption. reflexivity.
Defined.
Definition fd_bind A B wm wf (m:@FreeD A wm) (f:forall a, @FreeD B (wf a))
: FreeD (bind wm wf).
Proof.
revert wf f; induction m => wf0 f0.
apply (fd_weaken (f0 a)). rewrite -monad_law1;
apply omon_bind; [assumption| reflexivity].
simple refine (@FDop _ s _ _ (fun x => _)).
apply (OpPartialRan (w0:=fun x => bind (proj1_sig w' x) wf0)).
rewrite /bind -monad_law3.
apply omon_bind ; [|reflexivity].
destruct w' as [? [? ?]] ; assumption.
simpl.
apply (fd_weaken (X x wf0 f0)).
match goal with
| [|- _≤ proj1_sig ?X _] => destruct X as [w'' [H1 H2]]
end ; simpl in *.
apply (H2 (fun x => bind (proj1_sig w' x) wf0)).
rewrite /bind -monad_law3. apply omon_bind ; [|reflexivity].
destruct w' as [? [? ?]] ; assumption.
Defined.
(* So far, so good, we have a carrier type with return,
bind and weaken operations defined *)
(* The problems arise when trying to prove anything by induction
because right kan extension need not to be equal on the nose *)
(* Assuming that the preorder on W is actually an order should
be enough to go forward but that looks quite annoying to prove... *)
(* Import FunctionalExtensionality. *)
(* Import SPropNotations. *)
(* Notation "w1 ≃ w2" := (w1 ≤ w2 /\ w2 ≤ w1) (at level 65). *)
(* Context (Wantisym : forall A (w1 w2 : W A), w1 ≃ w2 -> w1 = w2). *)
(* Lemma kleisli_antisym A B (w1 w2 : A -> W B) : (forall a, w1 a ≃ w2 a) -> w1 = w2. *)
(* Proof. *)
(* move=> H ; extensionality a ; apply Wantisym ; apply H. *)
(* Qed. *)
(* Lemma fd_weaken_refl A w1 (m:@FreeD A w1) (H : w1 ≤ w1) : *)
(* fd_weaken m H = m. *)
(* Proof. *)
(* induction m=> //=. *)
(* Set Printing Implicit. *)
(* match goal with *)
(* | [|- FDop ?f1 = FDop ?f2] => enough (f1 = f2) as -> ; [reflexivity|] *)
(* end. *)
End FreeDijkstraMonads.
Section SumOfTheories.
Context (S1 S2 : Type) (P1: S1 -> Type) (P2: S2 -> Type).
Context (W : OrderedMonad).
Context (OpSpecs1 : forall s, W (P1 s)) (OpSpecs2 : forall s, W (P2 s)).
Definition sumS : Type := S1 + S2.
Definition sumP (s:sumS) : Type :=
match s with
| inl s1 => P1 s1
| inr s2 => P2 s2
end.
Definition sumOpSpecs (s:sumS) : W (sumP s) :=
match s with
| inl s1 => OpSpecs1 s1
| inr s2 => OpSpecs2 s2
end.
End SumOfTheories.
(****************************************************************************)
(* We use the approach of McBride'15, Turing completeness totally free, *)
(* in order to extend a Dijkstra monad on a free monad with an operation *)
(* corresponding to a recursive call. Then we provide a fix operation *)
(* (some kind of handler) to recover a function in the original Dijkstra *)
(* monad. *)
(****************************************************************************)
(** Deactivated temporarily *)
(* From Equations Require Import Equations. *)
(* From Mon Require Import WellFounded. *)
(* Derive NoConfusion for FreeF. *)
(* Derive Subterm for FreeF. *)
(* Section GeneralRecursion. *)
(* Context (S : Type) (P: S -> Type). *)
(* Context (W : OrderedMonad). *)
(* Context (OpSpecs : forall s, W (P s)) *)
(* (Hran_s : forall s C (w : W C) w0, bind (OpSpecs s) w0 ≤ w -> *)
(* ran w (OpSpecs s)). *)
(* Context `{forall A, aa (@omon_rel W A)}. *)
(* Context (DBase : Dijkstra W) *)
(* (perform_s : forall s, DBase (P s) (OpSpecs s)) *)
(* (intro_assume:forall A (pre:Prop) (w:W A), *)
(* (pre -> DBase A w) -> DBase A (assume_p pre w)). *)
(* Context (Dom:Type) (prec : Dom -> Dom -> Prop) `{WellFounded _ prec} *)
(* (Hsquash_prec : forall d1 d2, Squash (prec d1 d2) -> prec d1 d2). *)
(* Context (Cod : Dom -> Type) (inv : forall d:Dom, W (Cod d)). *)
(* Definition inv' d0 d := assume_p (Squash (prec d d0)) (inv d). *)
(* Context (Hran_inv : forall d0 d C (w : W C) w0, *)
(* bind (inv' d0 d) w0 ≤ w -> *)
(* ran w (inv' d0 d)). *)
(* Import SPropNotations. *)
(* Definition GenRecExtS : Type := sumS S Dom. *)
(* Definition GenRecExtP (s:GenRecExtS) : Type := sumP P Cod s. *)
(* Definition GenRecExtSpecs (d0:Dom) (s:GenRecExtS) : W (GenRecExtP s) := *)
(* sumOpSpecs OpSpecs (fun d => assume_p (Squash (prec d d0)) (inv d)) s. *)
(* Definition GenRecExt d0 := OpSpecWP (GenRecExtSpecs d0). *)
(* Section GenRecTelescopes. *)
(* Import Telescopes. *)
(* Definition GenRecTele := *)
(* ext Dom (fun d => ext Type (fun C => ext (W C) (fun w => tip (GenRecExt d C w)))). *)
(* Let T C := (FreeF GenRecExtP C). *)
(* Definition GenRec_to_FreeF : tele_fn GenRecTele {C : Type & T C}. *)
(* Proof. intros ? C ? [m] ; exists C ; exact m. Defined. *)
(* Definition GenRec_measure := *)
(* tele_measure GenRecTele {C : Type & T C} GenRec_to_FreeF *)
(* (lexdepprod Type T (empty_rel Type) *)
(* (@FreeF_subterm _ GenRecExtP)). *)
(* Definition to_tele C w d c : GenRecTele := *)
(* sigmaI _ d (sigmaI _ C (sigmaI _ w c)). *)
(* End GenRecTelescopes. *)
(* Import FunctionalExtensionality. *)
(* Context (f : forall (d:Dom), GenRecExt d (Cod d) (inv d)). *)
(* Equations(noind) eval {C w d} (c : GenRecExt d C w) : DBase C w *)
(* by wf (d, to_tele c) *)
(* (Subterm.lexprod Dom _ prec GenRec_measure) *)
(* := *)
(* eval (exist _ (retFree _ z) _) := wkn (dm_ret DBase z) _ ; *)
(* eval (exist _ (@opr _ _ _ (inl s) k) w) := *)
(* wkn (dm_bind (perform_s s) *)
(* (fun ps => @eval _ (proj1_sig (Hran_s w) ps) d (exist _ (k ps) _))) _ ; *)
(* eval (exist _ (@opr _ _ _ (inr d') k) w) := *)
(* let m (pre : Squash (prec d' d)) := @eval _ _ d' (f d') in *)
(* wkn (dm_bind (intro_assume m) (fun ps => @eval C (proj1_sig (Hran_inv w) ps) d (exist _ (k ps) _))) _. *)
(* Next Obligation. *)
(* match goal with *)
(* | [|- context [proj1_sig ?T]] => *)
(* let H0 := fresh "H" in *)
(* move: (proj2_sig T) => /= [_ H0] ; apply (H0 _ w ps) *)
(* end. *)
(* Qed. *)
(* Next Obligation. *)
(* do 2 right. do 2 constructor. *)
(* Qed. *)
(* Next Obligation. *)
(* match goal with *)
(* | [|- context [proj1_sig ?T]] => *)
(* let H0 := fresh "H" in *)
(* move: (proj2_sig T) => /= [H0 _] ; apply H0 *)
(* end. *)
(* Qed. *)
(* Next Obligation. *)
(* match goal with *)
(* | [|- context [proj1_sig ?T]] => *)
(* let H0 := fresh "H" in *)
(* move: (proj2_sig T) => /= [_ H0] ; apply (H0 _ w ps) *)
(* end. *)
(* Qed. *)
(* Next Obligation. *)
(* do 2 right. do 2 constructor. *)
(* Qed. *)
(* Next Obligation. *)
(* match goal with *)
(* | [|- context [proj1_sig ?T]] => *)
(* let H0 := fresh "H" in *)
(* move: (proj2_sig T) => /= [H0 _] ; apply H0 *)
(* end. *)
(* Qed. *)
(* Next Obligation. *)
(* unfold eval. *)
(* rewrite Subterm.FixWf_unfold_ext. *)
(* destruct c as [[?|[?|?]] ?] ; try reflexivity. *)
(* Defined. *)
(* Definition ffix (d:Dom) : DBase (Cod d) (inv d) := eval (f d). *)
(* Definition call {d0} d : GenRecExt d0 (Cod d) (inv' d0 d) := *)
(* cont_perform (GenRecExtSpecs d0) (inr d). *)
(* End GeneralRecursion. *)
(* Section NatLtProp. *)
(* Inductive leS : nat -> nat -> Prop := *)
(* | leSZ : forall n, leS 0 n *)
(* | leSS : forall n m, leS n m -> leS (S n) (S m). *)
(* Lemma leS_diag : forall n, leS n n. *)
(* Proof. elim=> [|] ; constructor. assumption. Qed. *)
(* Lemma leS_to_le : forall m n, leS n m -> le n m. *)
(* Proof. *)
(* induction m. move=> [|n H] //=. *)
(* assert (HF:False) by inversion H ; inversion HF. *)
(* move=> [?|n H]. exact (PeanoNat.Nat.le_0_l _). *)
(* apply Le.le_n_S ; apply IHm. inversion H ; assumption. *)
(* Qed. *)
(* Goal forall (p:Prop), Squash (Box p) -> p. *)
(* Proof. move=> p [[H]] ; exact H. Qed. *)
(* Lemma sq_le_to_leS : forall m n, Squash (le n m) -> leS n m. *)
(* Proof. *)
(* elim=> [|m IHm] [|n IHn]. *)
(* move=> ? ; constructor. *)
(* destruct IHn as [H] ; inversion H. *)
(* move=> ? ; constructor. *)
(* destruct IHn as [H] ; inversion H. *)
(* apply leS_diag. *)
(* constructor. apply IHm. *)
(* constructor. apply Le.le_Sn_le ; assumption. *)
(* Qed. *)
(* End NatLtProp. *)
(* Section EmptySignature. *)
(* Definition EmptyS : Type := False. *)
(* Definition EmptyP : EmptyS -> Type := False_rect Type. *)
(* Definition EmptyOpSpecs (W: OrderedMonad) (s : EmptyS) : W (EmptyP s) := *)
(* match s with end. *)
(* Definition EmptyRan (W:OrderedMonad) (s:EmptyS) : *)
(* forall (C : Type) (w : W C), ran w (EmptyOpSpecs W s) := *)
(* match s with end. *)
(* Definition EmptyRan' (W:OrderedMonad) (s:EmptyS) : *)
(* forall (C : Type) (w : W C) w0, *)
(* bind (EmptyOpSpecs W s) w0 ≤ w -> ran w (EmptyOpSpecs W s) := *)
(* match s with end. *)
(* End EmptySignature. *)
(* (****************************************************************************) *)
(* (* Dijkstra monad for Pure computations *) *)
(* (****************************************************************************) *)
(* (* TODO : there seems to be something funny to encode non-decidable partial maps *)
(* from A to B using A -> { p : Prop & p -> B } *)
(* This looks a bit like a Dijkstra monad, or maybe a graded monad (since the precondition *)
(* does not look like a spec monad but rather a monoid) *)
(* *) *)
(* Section Pure. *)
(* Let W := MonoContProp. *)
(* Import SPropNotations. *)
(* Import FunctionalExtensionality. *)
(* Definition Pure_car A (w:W A) : Type := *)
(* { f : forall p, proj1_sig w p -> { a : A | p a } *)
(* | forall p p' (H : Pred_op_order p' p) Hwp Hwp', *)
(* Squash (proj1_sig (f p Hwp) = proj1_sig (f p' Hwp')) }. *)
(* Program Definition Pure_ret A (a:A) : Pure_car (ret a) := *)
(* ⦑fun p Hpre => exist _ a Hpre⦒. *)
(* Next Obligation. constructor ; reflexivity. Qed. *)
(* Import EqNotations. *)
(* Lemma sprop_app_irr *)
(* : forall {p:SProp} {Z : Type} (f : p -> Z) (x1 x2 : p), f x1 = f x2. *)
(* Proof. reflexivity. Defined. *)
(* Program Definition Pure_bind A B wm wf (m:@Pure_car A wm) *)
(* (f: forall a, @Pure_car B (wf a)) : @Pure_car B (bind wm wf) := *)
(* ⦑fun p Hpre => *)
(* let m0 := proj1_sig m _ Hpre in *)
(* proj1_sig (f (proj1_sig m0)) p (proj2_sig m0)⦒. *)
(* Next Obligation. *)
(* unshelve epose (Hm := proj2_sig m _ _ _ Hwp Hwp'). *)
(* cbv ; intuition. *)
(* destruct Hm as [Hm]. *)
(* unshelve epose (Hf := proj2_sig (f (proj1_sig (proj1_sig m (fun a : A => proj1_sig (wf a) p) Hwp))) p p' H _ _). *)
(* exact (proj2_sig (proj1_sig m (fun a : A => proj1_sig (wf a) p) Hwp)). *)
(* unshelve eapply (proj2_sig (wf _) _ _). *)
(* exact p. *)
(* exact H. *)
(* exact (proj2_sig (proj1_sig m (fun a : A => proj1_sig (wf a) p) Hwp)). *)
(* destruct Hf as [Hf]. *)
(* constructor. *)
(* rewrite Hf. *)
(* assert (forall a a' p Hp Hp' (Ha:a = a'), proj1_sig (f a) p Hp = proj1_sig (f a') p Hp'). *)
(* intros. *)
(* refine (match Ha as H0 in _ = a0 return *)
(* forall (H:a' = a0), *)
(* proj1_sig (f a) p0 Hp = proj1_sig (f a0) p0 (eq_ind (fun a => proj1_sig (wf a) p0) Hp' H) *)
(* with *)
(* | eq_refl => _ *)
(* end eq_refl). *)
(* intros. apply sprop_app_irr. *)
(* erewrite (H0 _ _ p' _ _ Hm). *)
(* reflexivity. *)
(* Qed. *)
(* Program Definition Pure_wkn A w w' (m:@Pure_car A w) (Hww':w ≤ w') *)
(* : @Pure_car A w' *)
(* := ⦑fun p Hpre => proj1_sig m p (Hww' p Hpre)⦒. *)
(* Next Obligation. apply (proj2_sig m)=> //. Qed. *)
(* Lemma sprop_app_irr' *)
(* : forall {p:SProp} {Z : Type} (f : p -> Z) (x1 x2 : p), f x1 = f x2. *)
(* Proof. reflexivity. Defined. *)
(* Import SPropAxioms. *)
(* Program Definition Pure : Dijkstra W := *)
(* {| dm_tyop := Pure_car *)
(* ; dm_ret := Pure_ret *)
(* ; dm_bind := Pure_bind *)
(* ; dm_wkn := Pure_wkn *)
(* |}. *)
(* Next Obligation. hnf ; reflexivity. Qed. *)
(* Next Obligation. hnf ; reflexivity. Qed. *)
(* Next Obligation. hnf ; reflexivity. Qed. *)
(* Next Obligation. *)
(* cbv. *)
(* apply Monoid.sig_eq. *)
(* simpl ; funext p ; funext_s Hpre. *)
(* assert (forall a a' p Hp Hp' (Ha:a = a'), proj1_sig (f a) p Hp = proj1_sig (f a') p Hp'). *)
(* intros. *)
(* refine (match Ha as H0 in _ = a0 return *)
(* forall (H:a' = a0), *)
(* proj1_sig (f a) p0 Hp = proj1_sig (f a0) p0 (eq_sind _ _ (fun a _ => proj1_sig (wf a) p0) Hp' _ H) *)
(* with *)
(* | eq_refl _ => _ *)
(* end (eq_refl _)). *)
(* intros. apply sprop_app_irr'. *)
(* apply H. *)
(* epose (proj2_sig m (fun a : A => proj1_sig (wf' a) p) _ _ _ _). *)
(* destruct s as [s]. *)
(* rewrite s. *)
(* reflexivity. *)
(* Unshelve. *)
(* cbv in Hf |- *; move=> ? ; apply Hf. *)
(* cbv in Hm |- * ; apply Hm=> //. *)
(* Qed. *)
(* Program Definition pure_intro_assume A (pre:Prop) w (f : pre -> Pure A w) *)
(* : Pure A (assume_p pre w) := *)
(* ⦑fun p Hpre => proj1_sig (f _) p _⦒. *)
(* Next Obligation. destruct Hpre ; assumption. Qed. *)
(* Next Obligation. destruct Hpre ; assumption. Qed. *)
(* Next Obligation. apply (proj2_sig (f _))=> //. Qed. *)
(* End Pure. *)
(* (****************************************************************************) *)
(* (* We implement the fibonnacci function on top of Pure using General Recursion *) *)
(* (****************************************************************************) *)
(* Section Fibonnacci. *)
(* Let W := MonoContProp. *)
(* Definition fib_trivial_inv : W nat := PrePostSpec True (fun _ => True). *)
(* Let GenRecNatNat := *)
(* GenRecExt (EmptyOpSpecs W) lt (fun _ => fib_trivial_inv). *)
(* Let call {n0} (n:nat) : GenRecNatNat n0 nat _ := call _ _ _ n. *)
(* Import DijkstraMonadNotations. *)
(* Program Definition fib0 (n:nat) : GenRecNatNat n nat fib_trivial_inv := *)
(* match n with *)
(* | 0 | 1 => wkn (dret 1) _ *)
(* | S (S n) => wkn (r1 <- call (S n) ; r2 <- call n ; dret (r1 + r2)) _ *)
(* end. *)
(* Next Obligation. destruct H; cbv; auto. Qed. *)
(* Next Obligation. destruct H; cbv; auto. Qed. *)
(* Next Obligation. cbv ; intuition ; constructor. reflexivity. *)
(* constructor ; reflexivity. Qed. *)
(* Definition Empty_perform W (D: Dijkstra W) (s:EmptyS) : D (EmptyP s) (EmptyOpSpecs W s) := match s with end. *)
(* Program Definition fib := @ffix _ _ W _ (@EmptyRan' W) _ Pure (Empty_perform Pure) pure_intro_assume _ _ _ _ _ _ _ fib0. *)
(* Next Obligation. *)
(* apply leS_to_le. apply (sq_le_to_leS H). *)
(* Qed. *)
(* Next Obligation. *)
(* unshelve eapply (ran_iso (@MonoContAlongPrePost_ran _ _ w (Squash (S d <= d0)) (fun _ => True) _)). *)
(* cbv in H. *)
(* move=> Hw ; destruct (H _ Hw) ; assumption. *)
(* split ; sreflexivity. *)
(* cbv ; intuition. *)
(* Qed. *)
(* Program Definition fib' n := proj1_sig (@fib n) (fun _ => True) _. *)
(* Next Obligation. intuition. Qed. *)
(* (* Goal exists x, x = proj1_sig (fib' 2). *) *)
(* (* Proof. *) *)
(* (* eexists. *) *)
(* (* rewrite /fib' /fib /ffix. *) *)
(* (* time repeat (simp eval ; simpl). *) *)
(* (* (* Tactic call ran for 15.526 secs (15.437u,0.026s) (success) *) *) *)
(* (* reflexivity. *) *)
(* (* Qed. *) *)
(* (* Goal exists x, x = proj1_sig (fib' 0). *) *)
(* (* Proof. *) *)
(* (* eexists. *) *)
(* (* rewrite /fib' /fib /ffix. *) *)
(* (* time repeat (simp eval ; simpl). *) *)
(* (* (* Tactic call ran for 1.12 secs (1.115u,0.003s) (success) *) *) *)
(* (* reflexivity. *) *)
(* (* Qed. *) *)
(* (* Goal exists x, x = proj1_sig (fib' 3). *) *)
(* (* Proof. *) *)
(* (* eexists. *) *)
(* (* rewrite /fib' /fib /ffix. *) *)
(* (* time repeat (simp eval ; simpl). *) *)
(* (* (* Tactic call ran for 296.109 secs (295.803u,0.029s) (success) *) *) *)
(* (* reflexivity. *) *)
(* (* Qed. *) *)
(* End Fibonnacci. *)