]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/ng_assembly/common/nat.ma
freescale porting, work in progress
[helm.git] / helm / software / matita / contribs / ng_assembly / common / nat.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 (* ********************************************************************** *)
16 (*                          Progetto FreeScale                            *)
17 (*                                                                        *)
18 (*   Sviluppato da: Ing. Cosimo Oliboni, oliboni@cs.unibo.it              *)
19 (*   Ultima modifica: 05/08/2009                                          *)
20 (*                                                                        *)
21 (* ********************************************************************** *)
22
23 include "num/bool.ma".
24
25 (* ******** *)
26 (* NATURALI *)
27 (* ******** *)
28
29 inductive nat : Type ≝
30   O : nat
31 | S : nat → nat.
32
33 interpretation "Natural numbers" 'N = nat.
34
35 default "natural numbers" cic:/matita/common/nat/nat.ind.
36
37 alias num (instance 0) = "natural number".
38
39 nlet rec eq_nat (n1,n2:nat) on n1 ≝
40  match n1 with
41   [ O ⇒ match n2 with [ O ⇒ true | S _ ⇒ false ]
42   | S n1' ⇒ match n2 with [ O ⇒ false | S n2' ⇒ eq_nat n1' n2' ]
43   ].
44
45 nlet rec le_nat n m ≝ 
46  match n with 
47   [ O ⇒ true
48   | (S p) ⇒ match m with 
49    [ O ⇒ false | (S q) ⇒ le_nat p q ]
50   ].
51
52 interpretation "natural 'less or equal to'" 'leq x y = (le_nat x y).
53
54 ndefinition lt_nat ≝ λn1,n2:nat.(le_nat n1 n2) ⊗ (⊖ (eq_nat n1 n2)).
55
56 interpretation "natural 'less than'" 'lt x y = (lt_nat x y).
57
58 ndefinition ge_nat ≝ λn1,n2:nat.(⊖ (le_nat n1 n2)) ⊕ (eq_nat n1 n2).
59
60 interpretation "natural 'greater or equal to'" 'geq x y = (ge_nat x y).
61
62 ndefinition gt_nat ≝ λn1,n2:nat.⊖ (le_nat n1 n2).
63
64 interpretation "natural 'greater than'" 'gt x y = (gt_nat x y).
65
66 nlet rec plus (n1,n2:nat) on n1 ≝ 
67  match n1 with
68   [ O ⇒ n2
69   | (S n1') ⇒ S (plus n1' n2) ].
70
71 interpretation "natural plus" 'plus x y = (plus x y).
72
73 nlet rec times (n1,n2:nat) on n1 ≝ 
74  match n1 with 
75   [ O ⇒ O
76   | (S n1') ⇒ n2 + (times n1' n2) ].
77
78 interpretation "natural times" 'times x y = (times x y).
79
80 nlet rec minus n m ≝ 
81  match n with 
82  [ O ⇒ O
83  | (S p) ⇒ 
84         match m with
85         [O ⇒ (S p)
86   | (S q) ⇒ minus p q ]].
87
88 interpretation "natural minus" 'minus x y = (minus x y).
89
90 nlet rec div_aux p m n : nat ≝
91 match (le_nat m n) with
92 [ true ⇒ O
93 | false ⇒
94   match p with
95   [ O ⇒ O
96   | (S q) ⇒ S (div_aux q (m-(S n)) n)]].
97
98 ndefinition div : nat → nat → nat ≝
99 λn,m.match m with 
100  [ O ⇒ S n
101  | (S p) ⇒ div_aux n n p]. 
102
103 interpretation "natural divide" 'divide x y = (div x y).