1 | include "arithmetics/nat.ma". |
---|

2 | |
---|

3 | inductive nat_compared : nat → nat → Type[0] ≝ |
---|

4 | | nat_lt : ∀n,m:nat. nat_compared n (n+S m) |
---|

5 | | nat_eq : ∀n:nat. nat_compared n n |
---|

6 | | nat_gt : ∀n,m:nat. nat_compared (m+S n) m. |
---|

7 | |
---|

8 | let rec nat_compare (n:nat) (m:nat) : nat_compared n m ≝ |
---|

9 | match n return λx. nat_compared x m with |
---|

10 | [ O ⇒ match m return λy. nat_compared O y with [ O ⇒ nat_eq ? | S m' ⇒ nat_lt ?? ] |
---|

11 | | S n' ⇒ |
---|

12 | match m return λy. nat_compared (S n') y with |
---|

13 | [ O ⇒ nat_gt n' O |
---|

14 | | S m' ⇒ match nat_compare n' m' return λx,y.λ_. nat_compared (S x) (S y) with |
---|

15 | [ nat_lt x y ⇒ nat_lt ?? |
---|

16 | | nat_eq x ⇒ nat_eq ? |
---|

17 | | nat_gt x y ⇒ nat_gt ? (S y) |
---|

18 | ] |
---|

19 | ] |
---|

20 | ]. |
---|

21 | |
---|

22 | lemma max_l : ∀m,n,o:nat. o ≤ m → o ≤ max m n. |
---|

23 | #m #n #o #H whd in ⊢ (??%) @leb_elim #H' |
---|

24 | [ @(transitive_le ? m ? H H') |
---|

25 | | @H |
---|

26 | ] qed. |
---|

27 | |
---|

28 | lemma max_r : ∀m,n,o:nat. o ≤ n → o ≤ max m n. |
---|

29 | #m #n #o #H whd in ⊢ (??%) @leb_elim #H' |
---|

30 | [ @H |
---|

31 | | @(transitive_le … H) @(transitive_le … (not_le_to_lt … H')) // |
---|

32 | ] qed. |
---|

33 | |
---|

34 | |
---|

35 | (* "Fast" proofs: some proofs get reduced during normalization (in particular, |
---|

36 | some functions which use a proof for rewriting are applied to constants and |
---|

37 | get reduced during a proof or while matita is searching for a term; |
---|

38 | they may also be normalized during testing), and so here are some more |
---|

39 | efficient versions. Perhaps they could be replaced using some kind of proof |
---|

40 | irrelevance? *) |
---|

41 | |
---|

42 | let rec plus_n_Sm_fast (n:nat) on n : ∀m:nat. S (n+m) = n+S m ≝ |
---|

43 | match n return λn'.∀m.S(n'+m) = n'+S m with |
---|

44 | [ O ⇒ λm.refl ?? |
---|

45 | | S n' ⇒ λm. ? |
---|

46 | ]. normalize @(match plus_n_Sm_fast n' m with [ refl ⇒ ? ]) @refl qed. |
---|

47 | |
---|

48 | let rec plus_n_O_faster (n:nat) : n = n + O ≝ |
---|

49 | match n return λn.n=n+O with |
---|

50 | [ O ⇒ refl ?? |
---|

51 | | S n' ⇒ match plus_n_O_faster n' return λx.λ_.S n'=S x with [ refl ⇒ refl ?? ] |
---|

52 | ]. |
---|

53 | |
---|

54 | let rec commutative_plus_faster (n,m:nat) : n+m = m+n ≝ |
---|

55 | match n return λn.n+m = m+n with |
---|

56 | [ O ⇒ plus_n_O_faster ? |
---|

57 | | S n' ⇒ ? |
---|

58 | ]. @(match plus_n_Sm_fast m n' return λx.λ_. ? = x with [ refl ⇒ ? ]) |
---|

59 | @(match commutative_plus_faster n' m return λx.λ_.? = S x with [refl ⇒ ?]) @refl qed. |
---|

60 | |
---|