summaryrefslogtreecommitdiff
path: root/backends/hol4/divDefProtoScript.sml
blob: 64d4b9f0d1fed3694bfd82e57332fd8d77b4d230 (plain)
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
(* Prototype: divDefLib but with general combinators *)

open HolKernel boolLib bossLib Parse
open boolTheory arithmeticTheory integerTheory intLib listTheory stringTheory

open primitivesArithTheory primitivesBaseTacLib ilistTheory primitivesTheory
open primitivesLib

val _ = new_theory "divDefProto"

(*
 * Test with a general validity predicate.
 *
 * TODO: this works! Cleanup.
 *)
val fix_fuel_def = Define 
  (fix_fuel (0 : num) (f : ('a -> 'b result) -> 'a -> 'b result) (x : 'a) : 'b result = Diverge) 
  (fix_fuel (SUC n) (f : ('a -> 'b result) -> 'a -> 'b result) (x : 'a) : 'b result = f (fix_fuel n f) x)


val fix_fuel_P_def = Define 
  fix_fuel_P f x n = ~(is_diverge (fix_fuel n f x))


val fix_def = Define 
  fix (f : ('a -> 'b result) -> 'a -> 'b result) (x : 'a) : 'b result =
    if ( n. fix_fuel_P f x n) then fix_fuel ($LEAST (fix_fuel_P f x)) f x else Diverge


val is_valid_fp_body_def = Define 
  is_valid_fp_body (f : ('a -> 'b result) -> 'a -> 'b result) =
    ∀x. (∀g h. f g x = f h x)  ( y. ∀g. f g x = g y)


Theorem fix_fuel_mono:
  ∀f. is_valid_fp_body f 
    ∀n x. fix_fuel_P f x n 
      m. n  m 
       fix_fuel n f x = fix_fuel m f x
Proof
  ntac 2 strip_tac >>
  Induct_on ‘n’ >> rpt strip_tac
  >- (fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def]) >>
  Cases_on ‘m’ >- int_tac >> fs [] >>
    fs [is_valid_fp_body_def] >>
  fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>

  (* Use the validity property *)
  last_x_assum (qspec_assume ‘x’) >>
  (* This makes a case disjunction on the validity property *)
  rw []
  >-((* Case 1: the continuation doesn't matter *) fs []) >>
  (* Case 2: the continuation *does* matter (i.e., there is a recursive call *)
  (* Instantiate the validity property with the different continuations *)
  first_assum (qspec_assume ‘fix_fuel n f’) >>
  first_assum (qspec_assume ‘fix_fuel n' f’) >>
  last_assum (qspec_assume ‘y’) >>
  fs []
QED

(* TODO: remove? *)
Theorem fix_fuel_mono_least:
  ∀f. is_valid_fp_body f 
    ∀n x. fix_fuel_P f x n 
      fix_fuel n f x = fix_fuel ($LEAST (fix_fuel_P f x)) f x
Proof
  rw [] >>
  pure_once_rewrite_tac [EQ_SYM_EQ] >>
  irule fix_fuel_mono >> fs [] >>
  (* Use the "fundamental" property about $LEAST *)
  qspec_assume ‘fix_fuel_P f x whileTheory.LEAST_EXISTS_IMP >>
  (* Prove the premise *)
  pop_assum sg_premise_tac >- metis_tac [] >> fs [] >>
  spose_not_then assume_tac >>
  fs [not_le_eq_gt]
QED

Theorem fix_fixed_diverges:
  ∀f. is_valid_fp_body f  ∀x. ~( n. fix_fuel_P f x n)  fix f x = f (fix f) x
Proof
  rw [fix_def] >>
  (* Case disjunction on the validity hypothesis *)
  fs [is_valid_fp_body_def] >>
  last_x_assum (qspec_assume ‘x’) >>
  rw []
  >- (
    last_assum (qspec_assume ‘SUC n’) >>
    fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>
    first_assum (qspecl_assume [‘fix f’, ‘fix_fuel n f’]) >>
    Cases_on ‘f (fix_fuel n f) x >> fs []
  ) >>
  first_assum (qspec_assume ‘fix f’) >> fs [] >>
  fs [fix_def] >>
  (* Case disjunction on the ‘∃n. ...’ *)
  rw [] >>
  (* Use the hypothesis that there is no fuel s.t. ... *)
  last_assum (qspec_assume ‘SUC n’) >>
  fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>
  first_assum (qspec_assume ‘fix_fuel n f’) >>
  Cases_on ‘fix_fuel n f y >> fs []
QED

(* TODO: I think we can merge this with the theorem below *)
Theorem fix_fixed_termination_rec_case:
  ∀x y n m.
  is_valid_fp_body f 
  (∀g. f g x = g y) 
  fix_fuel_P f x n 
  fix_fuel_P f y m 
  fix_fuel n f x = fix_fuel m f y
Proof
  rw [] >>
  Cases_on ‘n’ >- fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>
  pure_asm_rewrite_tac [fix_fuel_def] >>
  sg ‘fix_fuel_P f y n' >- rfs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>
  imp_res_tac fix_fuel_mono_least >>
  fs []
QED

Theorem fix_fixed_termination_rec_case:
   x y n m.
  is_valid_fp_body f 
  (∀g. f g x = g y) 
  fix_fuel_P f x n 
  fix_fuel_P f y m 
  fix_fuel ($LEAST (fix_fuel_P f x)) f x =
  fix_fuel ($LEAST (fix_fuel_P f y)) f y
Proof
  rw [] >>
  imp_res_tac fix_fuel_mono_least >>
  irule fix_fixed_termination_rec_case >>
  fs [] >>
  (* TODO: factorize *)
  qspec_assume ‘fix_fuel_P f x whileTheory.LEAST_EXISTS_IMP >>
  pop_assum sg_premise_tac >- metis_tac [] >>
  qspec_assume ‘fix_fuel_P f y whileTheory.LEAST_EXISTS_IMP >>
  pop_assum sg_premise_tac >- metis_tac [] >>
  rw []
QED

Theorem fix_fixed_terminates:
  ∀f. is_valid_fp_body f  ∀x n. fix_fuel_P f x n  fix f x = f (fix f) x
Proof
  rpt strip_tac >>
  (* Case disjunction on the validity hypothesis - we don't want to consume it *)
  last_assum mp_tac >>
  pure_rewrite_tac [is_valid_fp_body_def] >> rpt strip_tac >>
  pop_assum (qspec_assume ‘x’) >>
  rw []
  >-( (* No recursive call *)
    fs [fix_def] >> rw [] >> fs [] >>
    imp_res_tac fix_fuel_mono_least >>
    Cases_on ‘$LEAST (fix_fuel_P f x) >> fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def]
  ) >>
  (* There exists a recursive call *)
  SUBGOAL_THEN “∃m. fix_fuel_P f y m assume_tac >-(
    Cases_on ‘n’ >>
    fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>
    metis_tac []
  ) >>
  fs [fix_def] >> rw [] >> fs [] >>
  irule fix_fixed_termination_rec_case >>
  fs [] >>
  metis_tac []
QED

Theorem fix_fixed_eq:
  ∀f. is_valid_fp_body f  ∀x. fix f x = f (fix f) x
Proof
  rw [] >>
  Cases_on ‘∃n. fix_fuel_P f x n
  >- (irule fix_fixed_terminates >> metis_tac []) >>
  irule fix_fixed_diverges >>
  fs []
QED  

(* Testing on an example *)
Datatype:
  list_t =
    ListCons 't list_t
  | ListNil
End

val nth_body_def = Define 
  nth_body (f : ('t list_t # u32) -> 't result) (x : ('t list_t # u32)) : 't result =
    let (ls, i) = x in
    case ls of
    | ListCons x tl =>
      if u32_to_int i = (0:int)
      then (Return x)
      else
        do
        i0 <- u32_sub i (int_to_u32 1);
        f (tl, i0)
        od
    | ListNil => Fail Failure


(* TODO: move *)
Theorem is_valid_suffice:
  ∃y. ∀g. g x = g y
Proof
  metis_tac []
QED

(* The general proof of is_valid_fp_body *)
Theorem nth_body_is_valid:
  is_valid_fp_body nth_body
Proof
  pure_rewrite_tac [is_valid_fp_body_def] >>
  gen_tac >>
  (* TODO: automate this *)
  Cases_on ‘x’ >> fs [] >>
  (* Expand *)
  fs [nth_body_def, bind_def] >>
  (* Explore all paths *)
  Cases_on ‘q’ >> fs [is_valid_suffice] >>
  Cases_on ‘u32_to_int r = 0 >> fs [is_valid_suffice] >>
  Cases_on ‘u32_sub r (int_to_u32 1) >> fs [is_valid_suffice]
QED

val nth_raw_def = Define 
  nth (ls : 't list_t) (i : u32) = fix nth_body (ls, i)


Theorem nth_def:
  ∀ls i. nth (ls : 't list_t) (i : u32) : 't result =
    case ls of
    | ListCons x tl =>
      if u32_to_int i = (0:int)
      then (Return x)
      else
        do
        i0 <- u32_sub i (int_to_u32 1);
        nth tl i0
        od
    | ListNil => Fail Failure
Proof
  rpt strip_tac >>
  (* Expand the raw definition *)
  pure_rewrite_tac [nth_raw_def] >>
  (* Use the fixed-point equality *)
  sg ‘fix nth_body (ls,i) = nth_body (fix nth_body) (ls,i)
  >- simp_tac bool_ss [HO_MATCH_MP fix_fixed_eq nth_body_is_valid] >>
  pure_asm_rewrite_tac [] >>
  (* Expand the body definition *)
  qspecl_assume [‘fix nth_body’, ‘(ls, i)’] nth_body_def >>
  pure_asm_rewrite_tac [LET_THM] >>
  simp_tac arith_ss []
QED

(************************** FAILED TESTS *)

(*
Datatype:
  fresult = FReturn 'b | FFail error | FRec 'a
End

(* TODO: monad converter from fresult to result + rewrite rules *)
val fres_to_res_def = Define ‘
  fres_to_res (f : 'a -> 'b result) (r : ('a, 'b) fresult) : 'b result =
    case r of
    | FReturn x => Return x
    | FFail e => Fail e
    | FRec y => f y


val fixa_fuel_def = Define ‘
  (fixa_fuel (0 : num) (f : 'a -> ('a, 'b) fresult) (x : 'a) : 'b result = Diverge) ∧
  (fixa_fuel (SUC n) (f : 'a -> ('a, 'b) fresult) (x : 'a) : 'b result =
    fres_to_res (fixa_fuel n f) (f x))


val fixa_fuel_P_def = Define ‘
  fixa_fuel_P f x n = ~(is_diverge (fixa_fuel n f x))


val fixa_def = Define ‘
  fixa (f : 'a -> ('a, 'b) fresult) (x : 'a) : 'b result =
    if (∃ n. fixa_fuel_P f x n) then fixa_fuel ($LEAST (fixa_fuel_P f x)) f x else Diverge


Theorem fixa_fuel_mono:
  ∀n m. n <= m ⇒ (∀f x. fixa_fuel_P f x n ⇒ fixa_fuel n f x = fixa_fuel m f x)
Proof
  Induct_on ‘n’ >> rpt strip_tac
  >- (fs [fixa_fuel_P_def, is_diverge_def, fixa_fuel_def, fres_to_res_def]) >>
  Cases_on ‘m’ >- int_tac >> fs [] >>
  last_x_assum imp_res_tac >>
  fs [fixa_fuel_P_def, is_diverge_def, fixa_fuel_def, fres_to_res_def] >>
  Cases_on ‘f x’ >> fs []
QED

(* TODO: remove ? *)
Theorem fixa_fuel_P_least:
  ∀f x n. fixa_fuel_P f x n ⇒ fixa_fuel_P f x ($LEAST (fixa_fuel_P f x))
Proof
  rw [] >>
  (* Use the "fundamental" property about $LEAST *)
  qspec_assume ‘fixa_fuel_P f x’ whileTheory.LEAST_EXISTS_IMP >>
  (* Prove the premise *)
  pop_assum sg_premise_tac >- metis_tac [] >>
  rw []
QED

Theorem fixa_fuel_mono_least:
  ∀n f x. fixa_fuel_P f x n ⇒ fixa_fuel n f x = fixa_fuel ($LEAST (fixa_fuel_P f x)) f x
Proof
  rw [] >>
  (* Use the "fundamental" property about $LEAST *)
  qspec_assume ‘fixa_fuel_P f x’ whileTheory.LEAST_EXISTS_IMP >>
  (* Prove the premise *)
  pop_assum sg_premise_tac >- metis_tac [] >>
  (* Prove that $LEAST ... ≤ n *)
  sg ‘n >= $LEAST (fixa_fuel_P f x)’ >-(
    spose_not_then assume_tac >>
    fs [not_ge_eq_lt]) >>
  pure_once_rewrite_tac [EQ_SYM_EQ] >>
  irule fixa_fuel_mono >>
  fs []
QED

Theorem fixa_fixed_eq:
  ∀f x. fixa f x = fres_to_res (fixa f) (f x)
Proof
  rw [fixa_def, fres_to_res_def]
  >- (
    (* Termination case *)
    imp_res_tac fixa_fuel_P_least >>
    Cases_on ‘$LEAST (fixa_fuel_P f x)’ >>
    fs [fixa_fuel_P_def, is_diverge_def, fixa_fuel_def, fres_to_res_def] >>
    Cases_on ‘f x’ >> fs [] >>
    rw [] >> fs [] >>
    irule fixa_fuel_mono_least >>
    fs [fixa_fuel_P_def, is_diverge_def, fixa_fuel_def]) >>
  (* Divergence case *)
  fs [fres_to_res_def] >>
  sg ‘∀n. ~(fixa_fuel_P f x (SUC n))’ >- fs [] >>
  Cases_on ‘f x’ >> fs [] >>
  fs [fixa_fuel_P_def, is_diverge_def, fixa_fuel_def, fres_to_res_def]  >>
  rw []
QED

val fix_fuel_def = Define ‘
  (fix_fuel (0 : num) (f : ('a -> 'b result) -> 'a -> 'b result) (x : 'a) : 'b result = Diverge) ∧
  (fix_fuel (SUC n) (f : ('a -> 'b result) -> 'a -> 'b result) (x : 'a) : 'b result = f (fix_fuel n f) x)


val fix_fuel_P_def = Define ‘
  fix_fuel_P f x n = ~(is_diverge (fix_fuel n f x))


val fix_def = Define ‘
  fix (f : ('a -> 'b result) -> 'a -> 'b result) (x : 'a) : 'b result =
    if (∃ n. fix_fuel_P f x n) then fix_fuel ($LEAST (fix_fuel_P f x)) f x else Diverge


Theorem fixa_fuel_eq_fix_fuel:
  ∀n (f : 'a -> ('a, 'b) fresult) (x : 'a).
    fixa_fuel n f x = fix_fuel n (\g y. fres_to_res g (f y)) x
Proof
  Induct_on ‘n’ >> rw [fixa_fuel_def, fix_fuel_def, fres_to_res_def]
QED

Theorem fixa_fuel_P_eq_fix_fuel_P:
  ∀(f : 'a -> ('a, 'b) fresult) (x : 'a).
    fixa_fuel_P f x = fix_fuel_P (\g y. fres_to_res g (f y)) x
Proof
  rw [] >> irule EQ_EXT >> rw [] >>
  qspecl_assume [‘x'’, ‘f’, ‘x’] fixa_fuel_eq_fix_fuel >>
  fs [fixa_fuel_P_def, fix_fuel_P_def]  
QED

Theorem fixa_fuel_eq_fix_fuel:
  ∀(f : 'a -> ('a, 'b) fresult) (x : 'a).
    fixa f x = fix (\g y. fres_to_res g (f y)) x
Proof
  rw [fixa_def, fix_def, fixa_fuel_P_eq_fix_fuel_P, fixa_fuel_eq_fix_fuel]
QED

(*
  The annoying thing is that to make this work, we need to rewrite the
  body by swapping matches, which is not easy to automate.
*)

(*
f x =
  case _ of
  | _ => Fail e
  | _ => Return x
  | _ => f y

f x (g : ('a, 'b) result => 'c)

f x (fres_to_res g) = f x g

*)

(*
val fix_fuel_def = Define ‘
  (fix_fuel (0 : num) (f : ('a -> 'b result) -> 'a -> 'b result) (x : 'a) : 'b result = Diverge) ∧
  (fix_fuel (SUC n) (f : ('a -> 'b result) -> 'a -> 'b result) (x : 'a) : 'b result = f (fix_fuel n f) x)


val fix_fuel_P_def = Define ‘
  fix_fuel_P f x n = ~(is_diverge (fix_fuel n f x))

*)


(*
val is_valid_FP_body_def = Define ‘
  is_valid_FP_body (f : (('a, 'b) fresult -> 'c) -> ('a, 'b) fresult -> 'c) =
    ∃ (fa: (('a, 'b) fresult -> ('a, 'b) fresult) -> ('a, 'b) fresult -> ('a, 'b) fresult).
      ∀ (g : ('a, 'b) fresult -> 'c) x.
        f g x = g (fa (\x. x) x)

val fix_fuel_def = Define ‘
  (fix_fuel (0 : num) (f : (('a, 'b) fresult -> 'b result) -> ('a, 'b) fresult -> 'b result) (x : ('a, 'b) fresult) : 'b result = Diverge) ∧
  (fix_fuel (SUC n) (f : (('a, 'b) fresult -> 'b result) -> ('a, 'b) fresult -> 'b result) (x : ('a, 'b) fresult) : 'b result = f (fix_fuel n f) x)


val fix_fuel_P_def = Define ‘
  fix_fuel_P f x n = ~(is_diverge (fix_fuel n f x))


val fix_def = Define ‘
  fix f x : 'b result =
    if (∃ n. fix_fuel_P f x n) then fix_fuel ($LEAST (fix_fuel_P f x)) f x else Diverge


Theorem fix_fuel_mono:
  ∀f. is_valid_FP_body f ⇒
  ∀n m. n <= m ⇒ (∀x. fix_fuel_P f x n ⇒ fix_fuel n f x = fix_fuel m f x)
Proof
  strip_tac >> strip_tac >>
  Induct_on ‘n’ >> rpt strip_tac
  >- (fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def]) >>
  fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>
  Cases_on ‘m’ >- int_tac >>
  fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>
  fs [is_valid_FP_body] >>
  last_x_assum (qspec_assume ‘fix_fuel n f’) >>
  fs []
QED

’*)

(*
 * Test with a validity predicate which gives monotonicity.

   TODO: not enough to prove the fixed-point equation.
 *)
val fix_fuel_def = Define ‘
  (fix_fuel (0 : num) (f : ('a -> 'b result) -> 'a -> 'b result) (x : 'a) : 'b result = Diverge) ∧
  (fix_fuel (SUC n) (f : ('a -> 'b result) -> 'a -> 'b result) (x : 'a) : 'b result = f (fix_fuel n f) x)


val fix_fuel_P_def = Define ‘
  fix_fuel_P f x n = ~(is_diverge (fix_fuel n f x))


val fix_def = Define ‘
  fix (f : ('a -> 'b result) -> 'a -> 'b result) (x : 'a) : 'b result =
    if (∃ n. fix_fuel_P f x n) then fix_fuel ($LEAST (fix_fuel_P f x)) f x else Diverge


Theorem fix_eq:
  fix f = \x. if (∃ n. fix_fuel_P f x n) then fix_fuel ($LEAST (fix_fuel_P f x)) f x else Diverge
Proof
  irule EQ_EXT >> fs [fix_def]
QED

val is_valid_fp_body_def = Define ‘
  is_valid_fp_body (f : ('a -> 'b result) -> 'a -> 'b result) =
    ∀n m. n ≤ m ⇒ ∀x. fix_fuel_P f x n ⇒ fix_fuel n f x = fix_fuel m f x


Theorem fix_fuel_mono_least:
  ∀f. is_valid_fp_body f ⇒
    ∀n x. fix_fuel_P f x n ⇒ fix_fuel n f x = fix_fuel ($LEAST (fix_fuel_P f x)) f x
Proof
  rw [] >>
  (* Use the "fundamental" property about $LEAST *)
  qspec_assume ‘fix_fuel_P f x’ whileTheory.LEAST_EXISTS_IMP >>
  (* Prove the premise *)
  pop_assum sg_premise_tac >- metis_tac [] >> fs [] >>
  (* Prove that $LEAST ... ≤ n *)
  sg ‘n >= $LEAST (fix_fuel_P f x)’ >-(
    spose_not_then assume_tac >>
    fs [not_ge_eq_lt]) >>
  pure_once_rewrite_tac [EQ_SYM_EQ] >>
  (* Finish by using the monotonicity property *)
  fs [is_valid_fp_body_def]
QED


(* TODO: remove ? *)
Theorem fix_fuel_P_least:
  ∀f x n. fix_fuel_P f x n ⇒ fix_fuel_P f x ($LEAST (fix_fuel_P f x))
Proof
  rw [] >>
  (* Use the "fundamental" property about $LEAST *)
  qspec_assume ‘fix_fuel_P f x’ whileTheory.LEAST_EXISTS_IMP >>
  (* Prove the premise *)
  pop_assum sg_premise_tac >- metis_tac [] >>
  rw []
QED

(*Theorem test:
  fix_fuel_P f x (SUC n) ⇒
  (∀m. n ≤ m ⇒ f (fix_fuel n f) x = f (fix_fuel m f) x) ⇒
  f (fix_fuel n f) x = f (fix f) x
Proof
  rw [] >>
  spose_not_then assume_tac >>
  fs [fix_eq]
*)
Theorem fix_fixed_eq:
  ∀f. is_valid_fp_body f ⇒ ∀x. fix f x = f (fix f) x
Proof
  rw [fix_def] >> CASE_TAC >> fs []
  >- (
    (* Termination case *)
    sg ‘$LEAST (fix_fuel_P f x) <= n’ >- cheat >>
    imp_res_tac fix_fuel_P_least >>
    Cases_on ‘$LEAST (fix_fuel_P f x)’ >>
    fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>
    Cases_on ‘n’ >>
    fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>
    (* Use the validity assumption *)
    fs [is_valid_fp_body_def] >>
  
(*    last_x_assum imp_res_tac >> *)
    last_assum (qspecl_assume [‘SUC n'’, ‘SUC n''’]) >> fs [] >>
    pop_assum imp_res_tac >>
    pop_assum (qspec_assume ‘x’) >>
    pop_assum sg_premise_tac >- fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>
    
    fs [fix_fuel_def] >>

    
    fs [fix_fuel_P_def] >>
    rfs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>
    
    fs [fix_eq] >>

    Cases_on ‘fix_fuel n' f x’ >> fs [] >>
    last_assum (qspecl_then [‘n'’, ‘’
    f (fix_fuel n' f) x >>

    Cases_on ‘f x’ >> fs [] >>
    rw [] >> fs [] >>
    irule fix_fuel_mono_least >>
    fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def]) >>
  (* Divergence case *)
  fs [fres_to_res_def] >>
  sg ‘∀n. ~(fix_fuel_P f x (SUC n))’ >- fs [] >>
  Cases_on ‘f x’ >> fs [] >>
  fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def]  >>
  rw []
QED


(* A slightly weaker and more precise validity criteria.

   We simply extracted the part of the inductive proof that we can't prove without
   knowing ‘f’.
 *)
val is_valid_fp_body_weak_def = Define ‘
   is_valid_fp_body_weak f =
   ∀n m x.
   ((∀x. fix_fuel_P f x n ⇒ fix_fuel n f x = fix_fuel m f x) ⇒
    n ≤ m ⇒
    fix_fuel_P f x (SUC n) ⇒
    fix_fuel (SUC n) f x = fix_fuel (SUC m) f x)


Theorem is_valid_fp_body_weak_imp_is_valid:
  ∀f. is_valid_fp_body_weak f ⇒ is_valid_fp_body f
Proof
  rpt strip_tac >> fs [is_valid_fp_body_def] >>
  Induct_on ‘n’ >- fs [fix_fuel_P_def, fix_fuel_def, is_diverge_def] >>
  Cases_on ‘m’ >> fs [] >>
  fs [is_valid_fp_body_weak_def]
QED

(* Testing on an example *)
Datatype:
  list_t =
    ListCons 't list_t
  | ListNil
End

val nth_body_def = Define ‘
  nth_body (f : ('t list_t # u32) -> 't result) (x : ('t list_t # u32)) : 't result =
    let (ls, i) = x in
    case ls of
    | ListCons x tl =>
      if u32_to_int i = (0:int)
      then (Return x)
      else
        do
        i0 <- u32_sub i (int_to_u32 1);
        f (tl, i0)
        od
    | ListNil => (Fail Failure)


(* The general proof of is_valid_fp_body. We isolate a more precise property below. *)
Theorem nth_body_is_valid:
  is_valid_fp_body nth_body
Proof
  pure_rewrite_tac [is_valid_fp_body_def] >>
  Induct_on ‘n’ >- fs [fix_fuel_P_def, fix_fuel_def, is_diverge_def] >>
  Cases_on ‘m’ >- fs [] >>
  rpt strip_tac >>
  (* TODO: here *)
  last_assum imp_res_tac >>
  (* TODO: here? *)
  fs [fix_fuel_def, nth_body_def, fix_fuel_P_def, is_diverge_def, bind_def] >>
  Cases_on ‘x’ >> fs [] >> (* TODO: automate this *)
  (* Explore all paths *)
  Cases_on ‘q’ >> fs [] >>
  Cases_on ‘u32_to_int r = 0’ >> fs [] >>
  Cases_on ‘u32_sub r (int_to_u32 1)’ >> fs []
QED


Theorem nth_body_is_valid_weak:
  is_valid_fp_body_weak nth_body
Proof
  pure_rewrite_tac [is_valid_fp_body_weak_def] >>
  rpt strip_tac >>
  (* TODO: automate this - we may need to destruct a variable number of times *)
  Cases_on ‘x’ >> fs [] >>
  (* Expand all *)
  fs [fix_fuel_def, nth_body_def, fix_fuel_P_def, is_diverge_def, bind_def] >>
  (* Explore all paths *)
  Cases_on ‘q’ >> fs [] >>
  Cases_on ‘u32_to_int r = 0’ >> fs [] >>
  Cases_on ‘u32_sub r (int_to_u32 1)’ >> fs []
QED

(*
 * Test with a more general validity predicate.
 *)
(* We need to control the way calls to the continuation are performed, so
   that we can inspect the inputs (otherwise we can't prove anything).
 *)
val is_valid_FP_body_def = Define ‘
  is_valid_FP_body (f : (('a, 'b) fresult -> 'c) -> 'a -> 'c) =
    ∃ (fa: (('a, 'b) fresult -> ('a, 'b) fresult) -> 'a -> ('a, 'b) fresult).
      ∀ (g : ('a, 'b) fresult -> 'c) x.
        f g x = g (fa (\x. x) x)



val fix_fuel_def = Define ‘
  (fix_fuel (0 : num) (f : (('a, 'b) fresult -> 'b result) -> 'a -> 'b result) (x : 'a) : 'b result = Diverge) ∧
  (fix_fuel (SUC n) (f : (('a, 'b) fresult -> 'b result) -> 'a -> 'b result) (x : 'a) : 'b result =
    f (fres_to_res (fix_fuel n f)) x)


val fix_fuel_P_def = Define ‘
  fix_fuel_P f x n = ~(is_diverge (fix_fuel n f x))


val fix_def = Define ‘
  fix f x : 'b result =
    if (∃ n. fix_fuel_P f x n) then fix_fuel ($LEAST (fix_fuel_P f x)) f x else Diverge


Theorem fix_fuel_mono:
  ∀f. is_valid_FP_body f ⇒
  ∀n m. n <= m ⇒ (∀x. fix_fuel_P f x n ⇒ fix_fuel n f x = fix_fuel m f x)
Proof
  strip_tac >> strip_tac >>
  Induct_on ‘n’ >> rpt strip_tac
  >- (fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def]) >>
  fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def, fres_to_res_def] >>
  Cases_on ‘m’ >- int_tac >>
  fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>
  fs [is_valid_FP_body_def, fres_to_res_def] >>
  CASE_TAC >>
  last_x_assum (qspec_assume ‘fres_to_res (fix_fuel n f)’) >>
  fs [fres_to_res_def]
QED

(* Tests *)



Datatype:
  list_t =
    ListCons 't list_t
  | ListNil
End

(*
val def_qt = ‘
  nth_mut_fwd (ls : 't list_t) (i : u32) : 't result =
  case ls of
  | ListCons x tl =>
    if u32_to_int i = (0:int)
    then Return x
    else
      do
      i0 <- u32_sub i (int_to_u32 1);
      nth_mut_fwd tl i0
      od
  | ListNil =>
    Fail Failure

*)

val nth_body_def = Define ‘
  nth_body (f : ('t list_t # u32, 't) fresult -> 'c) (ls : 't list_t, i : u32) : 'c =
    case ls of
    | ListCons x tl =>
      if u32_to_int i = (0:int)
      then f (FReturn x)
      else
        do
        i0 <- u32_sub i (int_to_u32 1);
        f (FRec (tl, i0))
        od
    | ListNil => f (FFail Failure)


Theorem nth_body_is_valid_FP_body:
  is_valid_FP_body nth_body
Proof
  fs [is_valid_FP_body_def] >>
  exists_tac “nth_body” >>
QED

“nth_body”

(* TODO: abbreviation for ‘(\g y. fres_to_res g (f y))’ *)
Theorem fix_fixed_eq:
  ∀f x. fix (\g y. fres_to_res g (f y)) x =
     (f x)
    fres_to_res (\g y. fres_to_res g (f y))



(*
TODO: can't prove that

Theorem fix_fuel_mono:
  ∀n m. n <= m ⇒ (∀f x. fix_fuel_P f x n ⇒ fix_fuel n f x = fix_fuel m f x)
Proof
  Induct_on ‘n’ >> rpt strip_tac
  >- (fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def]) >>
  fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>
  Cases_on ‘m’ >- int_tac >>
  fs [fix_fuel_P_def, is_diverge_def, fix_fuel_def] >>

  sg ‘fix_fuel n f = fix_fuel n' f’ >>

  last_x_assum imp_res_tac >>
  pop_assum (qspecl_assume [‘x’, ‘f’]) >>

Theorem fix_def_eq:
  ∀f x. fix f x = f (fix f) x
Proof
  
*)
*)


val _ = export_theory ()