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

« back to all changes in this revision

Viewing changes to src/Algebra/Operations.agda

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2013-05-27 19:29:25 UTC
  • mfrom: (8.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130527192925-q2tadfousmn0xeav
Tags: 0.7-2
Upload to unstable 

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 
10
10
module Algebra.Operations {s₁ s₂} (S : Semiring s₁ s₂) where
11
11
 
12
 
open Semiring S hiding (zero)
13
 
open import Data.Nat using (zero; suc; ℕ)
 
12
open Semiring S renaming (zero to *-zero)
 
13
open import Data.Nat
 
14
  using (zero; suc; ℕ) renaming (_+_ to _ℕ+_; _*_ to _ℕ*_)
 
15
open import Data.Product using (module Σ)
14
16
open import Function
15
17
open import Relation.Binary
16
18
open import Relation.Binary.PropositionalEquality as PropEq using (_≡_)
25
27
infixr 7 _×_
26
28
 
27
29
_×_ : ℕ → Carrier → Carrier
28
 
zero  × x = 0#
 
30
0     × x = 0#
29
31
suc n × x = x + n × x
30
32
 
 
33
-- A variant that includes a "redundant" case which ensures that 1 × y
 
34
-- is definitionally equal to y.
 
35
 
 
36
_×′_ : ℕ → Carrier → Carrier
 
37
0     ×′ x = 0#
 
38
1     ×′ x = x
 
39
suc n ×′ x = x + n ×′ x
 
40
 
31
41
-- Exponentiation.
32
42
 
33
43
infixr 8 _^_
39
49
------------------------------------------------------------------------
40
50
-- Some properties
41
51
 
 
52
-- Unfolding lemma for _×′_.
 
53
 
 
54
1+×′ : ∀ n x → suc n ×′ x ≈ x + n ×′ x
 
55
1+×′ 0 x = begin
 
56
  x       ≈⟨ sym $ Σ.proj₂ +-identity x ⟩
 
57
  x + 0#  ∎
 
58
1+×′ (suc n) x = begin
 
59
  x + suc n ×′ x  ≡⟨⟩
 
60
  x + suc n ×′ x  ∎
 
61
 
 
62
-- _×_ and _×′_ are extensionally equal (up to the setoid
 
63
-- equivalence).
 
64
 
 
65
×≈×′ : ∀ n x → n × x ≈ n ×′ x
 
66
×≈×′ 0       x = begin 0# ∎
 
67
×≈×′ (suc n) x = begin
 
68
  x + n × x   ≈⟨ +-cong refl (×≈×′ n x) ⟩
 
69
  x + n ×′ x  ≈⟨ sym $ 1+×′ n x ⟩
 
70
  suc n ×′ x  ∎
 
71
 
 
72
-- _×_ is homomorphic with respect to _ℕ+_/_+_.
 
73
 
 
74
×-homo-+ : ∀ c m n → (m ℕ+ n) × c ≈ m × c + n × c
 
75
×-homo-+ c 0 n = begin
 
76
  n × c       ≈⟨ sym $ Σ.proj₁ +-identity (n × c) ⟩
 
77
  0# + n × c  ∎
 
78
×-homo-+ c (suc m) n = begin
 
79
  c + (m ℕ+ n) × c     ≈⟨ +-cong refl (×-homo-+ c m n) ⟩
 
80
  c + (m × c + n × c)  ≈⟨ sym $ +-assoc c (m × c) (n × c) ⟩
 
81
  c + m × c + n × c    ∎
 
82
 
 
83
-- _×′_ is homomorphic with respect to _ℕ+_/_+_.
 
84
 
 
85
×′-homo-+ : ∀ c m n → (m ℕ+ n) ×′ c ≈ m ×′ c + n ×′ c
 
86
×′-homo-+ c m n = begin
 
87
  (m ℕ+ n) ×′ c    ≈⟨ sym $ ×≈×′ (m ℕ+ n) c ⟩
 
88
  (m ℕ+ n) ×  c    ≈⟨ ×-homo-+ c m n ⟩
 
89
  m ×  c + n ×  c  ≈⟨ +-cong (×≈×′ m c) (×≈×′ n c) ⟩
 
90
  m ×′ c + n ×′ c  ∎
 
91
 
 
92
-- _× 1# is homomorphic with respect to _ℕ*_/_*_.
 
93
 
 
94
×1-homo-* : ∀ m n → (m ℕ* n) × 1# ≈ (m × 1#) * (n × 1#)
 
95
×1-homo-* 0 n = begin
 
96
  0#             ≈⟨ sym $ Σ.proj₁ *-zero (n × 1#) ⟩
 
97
  0# * (n × 1#)  ∎
 
98
×1-homo-* (suc m) n = begin
 
99
  (n ℕ+ m ℕ* n) × 1#                   ≈⟨ ×-homo-+ 1# n (m ℕ* n) ⟩
 
100
  n × 1# + (m ℕ* n) × 1#               ≈⟨ +-cong refl (×1-homo-* m n) ⟩
 
101
  n × 1# + (m × 1#) * (n × 1#)         ≈⟨ sym $ +-cong (Σ.proj₁ *-identity (n × 1#)) refl ⟩
 
102
  1# * (n × 1#) + (m × 1#) * (n × 1#)  ≈⟨ sym $ Σ.proj₂ distrib (n × 1#) 1# (m × 1#) ⟩
 
103
  (1# + m × 1#) * (n × 1#)             ∎
 
104
 
 
105
-- _×′ 1# is homomorphic with respect to _ℕ*_/_*_.
 
106
 
 
107
×′1-homo-* : ∀ m n → (m ℕ* n) ×′ 1# ≈ (m ×′ 1#) * (n ×′ 1#)
 
108
×′1-homo-* m n = begin
 
109
  (m ℕ* n) ×′ 1#         ≈⟨ sym $ ×≈×′ (m ℕ* n) 1# ⟩
 
110
  (m ℕ* n) ×  1#         ≈⟨ ×1-homo-* m n ⟩
 
111
  (m ×  1#) * (n ×  1#)  ≈⟨ *-cong (×≈×′ m 1#) (×≈×′ n 1#) ⟩
 
112
  (m ×′ 1#) * (n ×′ 1#)  ∎
 
113
 
 
114
-- _×_ preserves equality.
 
115
 
42
116
×-cong : _×_ Preserves₂ _≡_ ⟶ _≈_ ⟶ _≈_
43
 
×-cong {n} {n'} {x} {x'} n≡n' x≈x' = begin
44
 
  n  × x   ≈⟨ reflexive $ PropEq.cong (λ n → n × x) n≡n' ⟩
45
 
  n' × x   ≈⟨ ×-congʳ n' x≈x' ⟩
46
 
  n' × x'  ∎
 
117
×-cong {n} {n′} {x} {x′} n≡n′ x≈x′ = begin
 
118
  n  × x   ≈⟨ reflexive $ PropEq.cong (λ n → n × x) n≡n′ ⟩
 
119
  n′ × x   ≈⟨ ×-congʳ n′ x≈x′ ⟩
 
120
  n′ × x′  ∎
47
121
  where
48
122
  ×-congʳ : ∀ n → (_×_ n) Preserves _≈_ ⟶ _≈_
49
 
  ×-congʳ zero    x≈x' = refl
50
 
  ×-congʳ (suc n) x≈x' = x≈x' ⟨ +-cong ⟩ ×-congʳ n x≈x'
 
123
  ×-congʳ 0       x≈x′ = refl
 
124
  ×-congʳ (suc n) x≈x′ = x≈x′ ⟨ +-cong ⟩ ×-congʳ n x≈x′
 
125
 
 
126
-- _×′_ preserves equality.
 
127
 
 
128
×′-cong : _×′_ Preserves₂ _≡_ ⟶ _≈_ ⟶ _≈_
 
129
×′-cong {n} {n′} {x} {x′} n≡n′ x≈x′ = begin
 
130
  n  ×′ x   ≈⟨ sym $ ×≈×′ n x ⟩
 
131
  n  ×  x   ≈⟨ ×-cong n≡n′ x≈x′ ⟩
 
132
  n′ ×  x′  ≈⟨ ×≈×′ n′ x′ ⟩
 
133
  n′ ×′ x′  ∎
 
134
 
 
135
-- _^_ preserves equality.
51
136
 
52
137
^-cong : _^_ Preserves₂ _≈_ ⟶ _≡_ ⟶ _≈_
53
138
^-cong {x} {x'} {n} {n'} x≈x' n≡n' = begin