Last active
April 19, 2017 19:11
-
-
Save 46bit/a7c23cce6d7326dc57708ce993aaa2bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Prelude hiding (take) | |
| import Data.List (sort) | |
| import Test.LeanCheck | |
| import Test.LeanCheck.Utils | |
| import Test.FitSpec | |
| class Set s where | |
| empty :: s | |
| single :: Eq a => a -> s | |
| class MatrixSpec m where | |
| load :: (Num a) => [[a]] -> m a | |
| dims :: m a -> (Int, Int) | |
| rows :: (Num a) => m a -> [[a]] | |
| elementwise :: (Num a) => (a -> a -> a) -> m a -> m a -> Maybe (m a) | |
| add :: (Num a) => m a -> m a -> Maybe (m a) | |
| add = elementwise (+) | |
| sub :: (Num a) => m a -> m a -> Maybe (m a) | |
| sub = elementwise (-) | |
| ---------------------------------------- | |
| data Mat a = Mat [[a]] | |
| unmat (Mat rs) = rs | |
| instance MatrixSpec Mat where | |
| load rs = Mat rs | |
| dims (Mat rs) = (length rs, length (head rs)) | |
| rows (Mat rs) = rs | |
| elementwise f a b = if dims a == dims b | |
| then Just (Mat elledRows) | |
| else Nothing | |
| where | |
| elledRows = elRows (unmat a) (unmat b) | |
| elRows [] [] = [] | |
| elRows (a:as) (b:bs) = (elRow a b) : elRows as bs | |
| elRow [] [] = [] | |
| elRow (a:as) (b:bs) = (f a b) : elRow as bs | |
| instance Show a => Show (Mat a) where | |
| show (Mat rs) = show rs | |
| -- -- prop_takes_nothing_from_nothing :: Nat -> Bool | |
| -- -- prop_takes_nothing_from_nothing n = length (take n []) == 0 | |
| -- -- prop_takes_correct_length :: Nat -> [Int] -> Bool | |
| -- -- prop_takes_correct_length (Nat n) xs = length (take (Nat n) xs) == (min n (length xs)) | |
| -- -- prop_takes_correct_items :: Nat -> [Int] -> Bool | |
| -- -- prop_takes_correct_items _ [] = True | |
| -- -- prop_takes_correct_items n (x:xs) = (n == 0) || (head (take n (x:xs)) == x) && prop_takes_correct_items (n-1) xs | |
| -- properties :: ([[Int]] -> Mat Int) -> [Property] | |
| -- properties load = | |
| -- [ property $ \rs -> rs == Main.rows (load rs) | |
| -- ] | |
| -- main = mainWith args {names = ["load rows"]} | |
| -- load | |
| -- properties |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment