Guest User

Dependent vectors in Idris

a guest
May 30th, 2015
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. data Vec : (n : Nat) -> (t : Type) -> Type where
  2. Nil : Vec Z t
  3. (::) : t -> Vec n t -> Vec (S n) t
  4.  
  5. infixr 6 .++
  6. (.++) : Vec n t -> Vec m t -> Vec (n + m) t
  7. [] .++ b = b
  8. (.++) {n = S n} {m = m} (x :: xs) y = rewrite plusSuccRightSucc n m in xs .++ (x :: y)
  9.  
  10. infixl 7 .*
  11. (.*) : Num t => Vec n t -> Vec n t -> t
  12. (.*) [] [] = 0
  13. (.*) (x :: xs) (y :: ys) = (x * y) + (xs .* ys)
  14.  
  15. natVec : (n : Nat) -> Vec n Int
  16. natVec Z = Nil
  17. natVec (S n) = (cast n) :: natVec n
  18.  
  19. main : IO ()
  20. main = do
  21. n <- fromIntegerNat . cast `map` getLine
  22. m <- fromIntegerNat . cast `map` getLine
  23.  
  24. let a = natVec n
  25. let b = natVec m
  26.  
  27. print $ a .* a
  28.  
  29. --print $ a .* b
  30. --Error: cannot unify n with m
  31.  
  32. --print $ (a .++ b) .* (b .++ a)
  33. --Error: cannot unify (n + m) with (m + n)
  34.  
  35. print $ (a .++ b) .* (rewrite plusCommutative n m in b .++ a)
  36.  
  37. case n `decEq` m of
  38. Yes eq => print $ a .* (rewrite eq in b)
  39. _ => return ()
Advertisement
Add Comment
Please, Sign In to add comment