aboutsummaryrefslogtreecommitdiff
path: root/Data/Matrix/Core.agda
blob: d97cb2035f8b6ff630d48972aecbb97e7cf738d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
{-# OPTIONS --without-K --safe #-}

open import Level using (Level; _⊔_)
open import Relation.Binary using (Setoid; Rel; IsEquivalence)

module Data.Matrix.Core {c  : Level} (S : Setoid c ) where

import Data.Vec.Relation.Binary.Equality.Setoid as PW-≈
import Data.Vec.Relation.Binary.Pointwise.Inductive as PW
import Relation.Binary.Reasoning.Setoid as ≈-Reasoning

open import Data.Matrix.Vec using (transpose)
open import Data.Nat using (ℕ; _+_)
open import Data.Vec as Vec using (Vec; map; zipWith; head; tail; replicate)
open import Data.Vec.Properties using (map-cong; map-id)
open import Data.Vector.Core S using (Vector; Vectorₛ; _++_; ⟨⟩; ⟨⟩-!; _≊_)
open import Data.Vector.Vec using (zipWith-map; replicate-++)
open import Function using (id)
open import Relation.Binary.PropositionalEquality as  using (_≡_; module ≡-Reasoning)

open Setoid S
open open Vec.Vec

private
  variable
    n m p :     A B C : private

  module PW-≊ {n} = PW-≈ (Vectorₛ n)

opaque

  -- Matrices over a setoid
  Matrix : Rel  c
  Matrix n m = Vec (Vector n) m

  -- Pointwise equality of matrices
  _≋_ : Rel (Matrix n m) (c  )
  _≋_ {n} {m} A B = A PW-≊.≋ B

  -- Pointwise equivalence is an equivalence relation
  ≋-isEquiv : IsEquivalence (_≋_ {n} {m})
  ≋-isEquiv {n} {m} = PW-≊.≋-isEquivalence m

  mapRows : (Vector n  Vector m)  Matrix n p  Matrix m p
  mapRows = map

  _∥_ : Matrix A C  Matrix B C  Matrix (A + B) C
  _∥_ M N = zipWith _++_ M N

  infixr 7 _∥_

  _≑_ : Matrix A B  Matrix A C  Matrix A (B + C)
  _≑_ M N = M Vec.++ N

  infixr 6 _≑_

  _∷ᵥ_ : Vector A  Matrix A B  Matrix A (suc B)
  _∷ᵥ_ V M = V Vec.∷ M

  infixr 5 _∷ᵥ_

  opaque

    unfolding Vector

    _∷ₕ_ : Vector B  Matrix A B  Matrix (suc A) B
    _∷ₕ_ V M = zipWith _∷_ V M

    infixr 5 _∷ₕ_

    ∷ₕ-cong : {V V′ : Vector B} {M M′ : Matrix A B}  V  V′  M  M′  V ∷ₕ M  V′ ∷ₕ M′
    ∷ₕ-cong PW.[] PW.[] = PW.[]
    ∷ₕ-cong (≈x PW.∷ ≊V) (≊M₀ PW.∷ ≋M) = (≈x PW.∷ ≊M₀) PW.∷ (∷ₕ-cong ≊V ≋M)

    headₕ : Matrix (suc A) B  Vector B
    headₕ M = map Vec.head M

    tailₕ : Matrix (suc A) B  Matrix A B
    tailₕ M = map Vec.tail M

    head-∷-tailₕ : (M : Matrix (suc A) B)  headₕ M ∷ₕ tailₕ M  M
    head-∷-tailₕ M = begin
        zipWith _∷_ (map Vec.head M) (map Vec.tail M) ≡⟨ zipWith-map head tail _∷_ M         map (λ x  head x  tail x) M                 ≡⟨ map-cong (λ { (_  _)  ≡.refl }) M         map id M                                      ≡⟨ map-id M         M       where
        open ≡-Reasoning

    []ᵥ : Matrix 0 B
    []ᵥ = replicate _ []

    []ᵥ-! : (E : Matrix 0 B)  E  []ᵥ
    []ᵥ-! [] = ≡.refl
    []ᵥ-! ([]  E) = ≡.cong ([] ∷_) ([]ᵥ-! E)

    []ᵥ-≑ : []ᵥ {A}  []ᵥ {B}  []ᵥ
    []ᵥ-≑ {A} {B} = replicate-++ A B []

    []ᵥ-∥ : (M : Matrix A B)  []ᵥ  M  M
    []ᵥ-∥ [] = ≡.refl
    []ᵥ-∥ (M₀  M) = ≡.cong (M₀ ∷_) ([]ᵥ-∥ M)

    ∷ₕ-∥ : (V : Vector C) (M : Matrix A C) (N : Matrix B C)  V ∷ₕ (M  N)  (V ∷ₕ M)  N
    ∷ₕ-∥ [] [] [] = ≡.refl
    ∷ₕ-∥ (x  V) (M₀  M) (N₀  N) = ≡.cong ((x  M₀ ++ N₀) ∷_) (∷ₕ-∥ V M N)

    ∷ₕ-≑ : (V : Vector A) (W : Vector B) (M : Matrix C A) (N : Matrix C B)  (V ++ W) ∷ₕ (M  N)  (V ∷ₕ M)  (W ∷ₕ N)
    ∷ₕ-≑ [] W [] N = ≡.refl
    ∷ₕ-≑ (x  V) W (M₀  M) N = ≡.cong ((x  M₀) ∷_) (∷ₕ-≑ V W M N)

  headᵥ : Matrix A (suc B)  Vector A
  headᵥ (V  _) = V

  tailᵥ : Matrix A (suc B)  Matrix A B
  tailᵥ (_  M) = M

  head-∷-tailᵥ : (M : Matrix A (suc B))  headᵥ M ∷ᵥ tailᵥ M  M
  head-∷-tailᵥ (_  _) = ≡.refl

  []ₕ : Matrix A 0
  []ₕ = []

  []ₕ-! : (E : Matrix A 0)  E  []ₕ
  []ₕ-! [] = ≡.refl

  []ₕ-≑ : (M : Matrix A B)  []ₕ  M  M
  []ₕ-≑ _ = ≡.refl

  ∷ᵥ-≑ : (V : Vector A) (M : Matrix A B) (N : Matrix A C)  V ∷ᵥ (M  N)  (V ∷ᵥ M)  N
  ∷ᵥ-≑ V M N = ≡.refl

infix 4 _≋_

module ≋ {n} {m} = IsEquivalence (≋-isEquiv {n} {m})

Matrixₛ :     Setoid c (c  )
Matrixₛ n m = record
    { Carrier = Matrix n m
    ; _≈_ = _≋_ {n} {m}
    ; isEquivalence = ≋-isEquiv
  }

opaque

  unfolding Vector

  head′ : Vector (suc A)  Carrier
  head′ = head

  head-cong : {V V′ : Vector (suc A)}  V  V′  head′ V  head′ V′
  head-cong (≈x PW.∷ _) = ≈x

  tail′ : Vector (suc A)  Vector A
  tail′ = tail

  tail-cong : {V V′ : Vector (suc A)}  V  V′  tail′ V  tail′ V′
  tail-cong (_ PW.∷ ≊V) = ≊V

opaque

  unfolding headₕ head′

  ≋headₕ : {M M′ : Matrix (suc A) B}  M  M′  headₕ M  headₕ M′
  ≋headₕ M≋M′ = PW.map⁺ head-cong M≋M′

  ≋tailₕ : {M M′ : Matrix (suc A) B}  M  M′  tailₕ M  tailₕ M′
  ≋tailₕ M≋M′ = PW.map⁺ tail-cong M≋M′

opaque
  unfolding _≋_ _∥_ []ᵥ _∷ₕ_
  ∥-cong : {M M′ : Matrix A C} {N N′ : Matrix B C}  M  M′  N  N′  M  N  M′  N′
  ∥-cong {zero} {C} {B} {M} {M′} {N} {N′} ≋M ≋N
    rewrite []ᵥ-! M
    rewrite []ᵥ-! M′ = begin
      ([]ᵥ  N)   ≡⟨ []ᵥ-∥ N       N           ≈⟨ ≋N       N′          ≡⟨ []ᵥ-∥ N′       ([]ᵥ  N′)      where
      open ≈-Reasoning (Matrixₛ _ _)
  ∥-cong {suc A} {C} {B} {M} {M′} {N} {N′} ≋M ≋N
    rewrite ≡.sym (head-∷-tailₕ M)
    using M₀  headₕ M
    using M-  tailₕ M
    rewrite ≡.sym (head-∷-tailₕ M′)
    using M₀′  headₕ M′
    using M-′  tailₕ M′ = begin
      (M₀ ∷ₕ M-)  N     ≡⟨ ∷ₕ-∥ M₀ M- N       M₀ ∷ₕ M-  N       ≈⟨ ∷ₕ-cong ≊M₀ (∥-cong ≋M- ≋N)       M₀′ ∷ₕ M-′  N′    ≡⟨ ∷ₕ-∥ M₀′ M-′ N′       (M₀′ ∷ₕ M-′)  N′      where
      ≊M₀ : M₀  M₀′
      ≊M₀ = begin
          headₕ M             ≡⟨ ≡.cong headₕ (head-∷-tailₕ M)           headₕ (M₀ ∷ₕ M-)    ≈⟨ ≋headₕ ≋M           headₕ (M₀′ ∷ₕ M-′)  ≡⟨ ≡.cong headₕ (head-∷-tailₕ M′)           headₕ M′                    where
          open ≈-Reasoning (Vectorₛ _)
      ≋M- : M-  M-′
      ≋M- = begin
          tailₕ M             ≡⟨ ≡.cong tailₕ (head-∷-tailₕ M)           tailₕ (M₀ ∷ₕ M-)    ≈⟨ ≋tailₕ ≋M           tailₕ (M₀′ ∷ₕ M-′)  ≡⟨ ≡.cong tailₕ (head-∷-tailₕ M′)           tailₕ M′                    where
          open ≈-Reasoning (Matrixₛ _ _)
      open ≈-Reasoning (Matrixₛ _ _)

opaque
  unfolding _≑_
  ≑-cong : {M M′ : Matrix A B} {N N′ : Matrix A C}  M  M′  N  N′  M  N  M′  N′
  ≑-cong PW.[] ≋N = ≋N
  ≑-cong (M₀≊M₀′ PW.∷ ≋M) ≋N = M₀≊M₀′ PW.∷ ≑-cong ≋M ≋N

opaque

  unfolding Matrix

  _ᵀ : Matrix n m  Matrix m n
  _ᵀ [] = []ᵥ
  _ᵀ (M₀  M) = M₀ ∷ₕ M   infix 10 _ᵀ

  -ᵀ-cong : {M₁ M₂ : Matrix n m}  M₁  M₂  M₁   M₂   -ᵀ-cong PW.[] = ≋.refl
  -ᵀ-cong (≊M₀ PW.∷ ≋M) = ∷ₕ-cong ≊M₀ (-ᵀ-cong ≋M)

  opaque

    unfolding []ᵥ []ₕ

    []ᵥ-ᵀ : []ᵥ   []ₕ {A}
    []ᵥ-ᵀ {zero} = ≡.refl
    []ᵥ-ᵀ {suc A} = ≡.cong (zipWith _∷_ []) ([]ᵥ-ᵀ)

  opaque

    unfolding _∷ₕ_ Vector

    ∷ₕ-ᵀ : (V : Vector A) (M : Matrix B A)  (V ∷ₕ M)   V ∷ᵥ M     ∷ₕ-ᵀ [] [] = ≡.refl
    ∷ₕ-ᵀ (x  V) (M₀  M) = ≡.cong ((x  M₀) ∷ₕ_) (∷ₕ-ᵀ V M)

  ∷ᵥ-ᵀ : (V : Vector B) (M : Matrix B A)  (V ∷ᵥ M)   V ∷ₕ M   ∷ᵥ-ᵀ V M = ≡.refl

  opaque

    _ᵀᵀ : (M : Matrix n m)  M    M
    _ᵀᵀ [] = []ᵥ-ᵀ
    _ᵀᵀ (M₀  M) = begin
        (M₀ ∷ₕ M )  ≡⟨ ∷ₕ-ᵀ M₀ (M )          M₀ ∷ᵥ M     ≡⟨ ≡.cong (M₀ ∷ᵥ_) (M ᵀᵀ)         M₀ ∷ᵥ M             where
        open ≡-Reasoning

  infix 10 _ᵀᵀ