Skip to content

Instantly share code, notes, and snippets.

@gabro
Created December 19, 2014 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabro/f631c6ab4a02b4aee201 to your computer and use it in GitHub Desktop.
Save gabro/f631c6ab4a02b4aee201 to your computer and use it in GitHub Desktop.
dependent type Matrix with implicit conversion from Vect m (Vect n a)
module Matrix
data Matrix : Nat -> Nat -> Type -> Type where
Matr : Vect m (Vect n a) -> Matrix n m a
(+) : Matrix n m a -> Vect n a -> Matrix n (m + 1) a
(+) (Matr rows) row = Matr (rows ++ [row])
implicit vectVectToMatr : Vect m (Vect n a) -> Matrix n m a
vectVectToMatr = Matr
@gabro
Copy link
Author

gabro commented Dec 19, 2014

example usage:

[[1,2], [3,4]] + [5,6]
-- Matr [[1, 2], [3, 4], [5, 6]] : Matrix 2 3 Integer

and of course you can't add mismatching rows

[[1,2], [3,4]] + [5,6,7]
--         Can't unify
--                Vect (S n) a
--        with
--                Vect 0 a
--
--        Specifically:
--                Can't unify
--                        S n
--                with
--                        0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment