]> 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: Cosimo Oliboni, oliboni@cs.unibo.it                   *)
19 (*     Cosimo Oliboni, oliboni@cs.unibo.it                                *)
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 nlet rec plus (n1,n2:nat) on n1 ≝ 
53  match n1 with
54   [ O ⇒ n2
55   | (S n1') ⇒ S (plus n1' n2) ].
56
57 interpretation "natural plus" 'plus x y = (plus x y).
58
59 nlet rec times (n1,n2:nat) on n1 ≝ 
60  match n1 with 
61   [ O ⇒ O
62   | (S n1') ⇒ n2 + (times n1' n2) ].
63
64 interpretation "natural times" 'times x y = (times x y).
65
66 nlet rec minus n m ≝ 
67  match n with 
68  [ O ⇒ O
69  | (S p) ⇒ 
70         match m with
71         [O ⇒ (S p)
72   | (S q) ⇒ minus p q ]].
73
74 interpretation "natural minus" 'minus x y = (minus x y).
75
76 nlet rec div_aux p m n : nat ≝
77 match (le_nat m n) with
78 [ true ⇒ O
79 | false ⇒
80   match p with
81   [ O ⇒ O
82   | (S q) ⇒ S (div_aux q (m-(S n)) n)]].
83
84 ndefinition div : nat → nat → nat ≝
85 λn,m.match m with 
86  [ O ⇒ S n
87  | (S p) ⇒ div_aux n n p]. 
88
89 interpretation "natural divide" 'divide x y = (div x y).