Vec.agda 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. {-# OPTIONS --guardedness #-}
  2. -- Written by P. Hausmann
  3. module Vec where
  4. open import IO
  5. open import Data.Vec
  6. open import Data.Nat
  7. open import Data.Nat.Show
  8. open import Level using (0ℓ)
  9. Matrix : Set -> ℕ -> ℕ -> Set
  10. Matrix a n m = Vec (Vec a m) n
  11. madd : {n m : ℕ} -> Matrix ℕ m n -> Matrix ℕ m n -> Matrix ℕ m n
  12. madd a b = map (λ x → \y -> map _+_ x ⊛ y) a ⊛ b
  13. idMatrix : {n : ℕ} -> Matrix ℕ n n
  14. idMatrix {zero} = []
  15. idMatrix {suc n} = (1 ∷ (replicate zero)) ∷ (map (λ x → zero ∷ x) idMatrix)
  16. transposeM : {n m : ℕ} {a : Set} -> Matrix a m n -> Matrix a n m
  17. transposeM {zero} {zero} a₁ = []
  18. transposeM {zero} {suc m} {a} x = []
  19. transposeM {suc n} {zero} a₁ = replicate []
  20. transposeM {suc n} {suc m} {a} (_∷_ x₁ x₂) with map head (x₁ ∷ x₂)
  21. ... | vm = vm ∷ (map _∷_ (tail x₁) ⊛ transposeM (map tail x₂))
  22. -- We use quite small numbers right now, as with big number the computation
  23. -- gets very slow (at least in MAlonzo)
  24. -- correct result : 109
  25. compute : ℕ
  26. compute = sum (map sum g)
  27. where m : Matrix ℕ 3 3
  28. m = (3 ∷ 5 ∷ 9 ∷ []) ∷
  29. (12 ∷ 0 ∷ 7 ∷ []) ∷ (11 ∷ 2 ∷ 4 ∷ []) ∷ []
  30. g : Matrix ℕ 3 3
  31. g = madd (transposeM (transposeM m)) (transposeM (madd m idMatrix))
  32. main = run {0ℓ} (putStrLn (show compute))