~ubuntu-branches/ubuntu/wily/agda-stdlib/wily-proposed

« back to all changes in this revision

Viewing changes to src/Data/Integer/Addition/Properties.agda

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2011-12-30 20:02:46 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111230200246-xl31fi4bnippohay
Tags: 0.6-1
* [a88bdc0] Imported Upstream version 0.6
* [7aea5f2] Update copyright for new copyright holders and for new DEP5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
------------------------------------------------------------------------
 
2
-- The Agda standard library
 
3
--
 
4
-- Properties related to addition of integers
 
5
------------------------------------------------------------------------
 
6
 
 
7
module Data.Integer.Addition.Properties where
 
8
 
 
9
open import Algebra
 
10
import Algebra.FunctionProperties
 
11
open import Algebra.Structures
 
12
open import Data.Integer hiding (suc)
 
13
open import Data.Nat using (suc; zero) renaming (_+_ to _ℕ+_)
 
14
import Data.Nat.Properties as ℕ
 
15
open import Relation.Binary.PropositionalEquality
 
16
 
 
17
open Algebra.FunctionProperties (_≡_ {A = ℤ})
 
18
open CommutativeSemiring ℕ.commutativeSemiring
 
19
  using (+-comm; +-assoc; +-identity)
 
20
 
 
21
------------------------------------------------------------------------
 
22
-- Addition and zero form a commutative monoid
 
23
 
 
24
private
 
25
 
 
26
  comm : Commutative _+_
 
27
  comm -[1+ a ] -[1+ b ] rewrite +-comm a b = refl
 
28
  comm (+   a ) (+   b ) rewrite +-comm a b = refl
 
29
  comm -[1+ _ ] (+   _ ) = refl
 
30
  comm (+   _ ) -[1+ _ ] = refl
 
31
 
 
32
  identityˡ : LeftIdentity (+ 0) _+_
 
33
  identityˡ -[1+ _ ] = refl
 
34
  identityˡ (+   _ ) = refl
 
35
 
 
36
  identityʳ : RightIdentity (+ 0) _+_
 
37
  identityʳ x rewrite comm x (+ 0) = identityˡ x
 
38
 
 
39
  distribˡ-⊖-+-neg : ∀ a b c → b ⊖ c + -[1+ a ] ≡ b ⊖ (suc c ℕ+ a)
 
40
  distribˡ-⊖-+-neg _ zero    zero    = refl
 
41
  distribˡ-⊖-+-neg _ zero    (suc _) = refl
 
42
  distribˡ-⊖-+-neg _ (suc _) zero    = refl
 
43
  distribˡ-⊖-+-neg a (suc b) (suc c) = distribˡ-⊖-+-neg a b c
 
44
 
 
45
  distribʳ-⊖-+-neg : ∀ a b c → -[1+ a ] + (b ⊖ c) ≡ b ⊖ (suc a ℕ+ c)
 
46
  distribʳ-⊖-+-neg a b c
 
47
    rewrite comm -[1+ a ] (b ⊖ c)
 
48
          | distribˡ-⊖-+-neg a b c
 
49
          | +-comm a c
 
50
          = refl
 
51
 
 
52
  distribˡ-⊖-+-pos : ∀ a b c → b ⊖ c + + a ≡ b ℕ+ a ⊖ c
 
53
  distribˡ-⊖-+-pos _ zero    zero    = refl
 
54
  distribˡ-⊖-+-pos _ zero    (suc _) = refl
 
55
  distribˡ-⊖-+-pos _ (suc _) zero    = refl
 
56
  distribˡ-⊖-+-pos a (suc b) (suc c) = distribˡ-⊖-+-pos a b c
 
57
 
 
58
  distribʳ-⊖-+-pos : ∀ a b c → + a + (b ⊖ c) ≡ a ℕ+ b ⊖ c
 
59
  distribʳ-⊖-+-pos a b c
 
60
    rewrite comm (+ a) (b ⊖ c)
 
61
          | distribˡ-⊖-+-pos a b c
 
62
          | +-comm a b
 
63
          = refl
 
64
 
 
65
  assoc : Associative _+_
 
66
  assoc (+ zero) y z rewrite identityˡ      y  | identityˡ (y + z) = refl
 
67
  assoc x (+ zero) z rewrite identityʳ  x      | identityˡ      z  = refl
 
68
  assoc x y (+ zero) rewrite identityʳ (x + y) | identityʳ  y      = refl
 
69
  assoc -[1+ a ]  -[1+ b ]  (+ suc c) = sym (distribʳ-⊖-+-neg a c b)
 
70
  assoc -[1+ a ]  (+ suc b) (+ suc c) = distribˡ-⊖-+-pos (suc c) b a
 
71
  assoc (+ suc a) -[1+ b ]  -[1+ c ]  = distribˡ-⊖-+-neg c a b
 
72
  assoc (+ suc a) -[1+ b ] (+ suc c)
 
73
    rewrite distribˡ-⊖-+-pos (suc c) a b
 
74
          | distribʳ-⊖-+-pos (suc a) c b
 
75
          | sym (+-assoc a 1 c)
 
76
          | +-comm a 1
 
77
          = refl
 
78
  assoc (+ suc a) (+ suc b) -[1+ c ]
 
79
    rewrite distribʳ-⊖-+-pos (suc a) b c
 
80
          | sym (+-assoc a 1 b)
 
81
          | +-comm a 1
 
82
          = refl
 
83
  assoc -[1+ a ] -[1+ b ] -[1+ c ]
 
84
    rewrite sym (+-assoc a 1 (b ℕ+ c))
 
85
          | +-comm a 1
 
86
          | +-assoc a b c
 
87
          = refl
 
88
  assoc -[1+ a ] (+ suc b) -[1+ c ]
 
89
    rewrite distribʳ-⊖-+-neg a b c
 
90
          | distribˡ-⊖-+-neg c b a
 
91
          = refl
 
92
  assoc (+ suc a) (+ suc b) (+ suc c)
 
93
    rewrite +-assoc (suc a) (suc b) (suc c)
 
94
          = refl
 
95
 
 
96
  isSemigroup : IsSemigroup _≡_ _+_
 
97
  isSemigroup = record
 
98
    { isEquivalence = isEquivalence
 
99
    ; assoc         = assoc
 
100
    ; ∙-cong        = cong₂ _+_
 
101
    }
 
102
 
 
103
  isCommutativeMonoid : IsCommutativeMonoid _≡_ _+_ (+ 0)
 
104
  isCommutativeMonoid = record
 
105
    { isSemigroup = isSemigroup
 
106
    ; identityˡ   = identityˡ
 
107
    ; comm        = comm
 
108
    }
 
109
 
 
110
commutativeMonoid : CommutativeMonoid _ _
 
111
commutativeMonoid = record
 
112
  { Carrier             = ℤ
 
113
  ; _≈_                 = _≡_
 
114
  ; _∙_                 = _+_
 
115
  ; ε                   = + 0
 
116
  ; isCommutativeMonoid = isCommutativeMonoid
 
117
  }