- length,
- map,
- replicate,
- sum,
- zipWith
- )
-
-import Normed
-
--- | The Vn newtype simply wraps (Vector v a) so that we avoid
--- undecidable instances.
-newtype Vn v a = Vn (v a)
-
--- | Declare the dimension of the wrapper to be the dimension of what
--- it contains.
-type instance Dim (Vn v) = Dim v
-
-instance (Vector v a) => Vector (Vn v) a where
- -- | Fortunately, 'Fun' is an instance of 'Functor'. The
- -- 'construct' defined on our contained type will return a
- -- 'Fun', and we simply slap our constructor on top with fmap.
- construct = fmap Vn construct
-
- -- | Defer to the inspect defined on the contained type.
- inspect (Vn v1) = inspect v1
-
-instance (Show a, Vector v a) => Show (Vn v a) where
- -- | Display vectors as ordinary tuples. This is poor practice, but
- -- these results are primarily displayed interactively and
- -- convenience trumps correctness (said the guy who insists his
- -- vector lengths be statically checked at compile-time).
- --
- -- Examples:
- --
- -- >>> let v1 = make2d (1,2)
- -- >>> show v1
- -- (1,2)
- --
- show (Vn v1) =
- "(" ++ (intercalate "," element_strings) ++ ")"
- where
- v1l = toList v1
- element_strings = Prelude.map show v1l
-
-
--- | We would really like to say, "anything that is a vector of
--- equatable things is itself equatable." The 'Vn' class
--- allows us to express this without a GHC battle.
---
--- Examples:
---
--- >>> let v1 = make2d (1,2)
--- >>> let v2 = make2d (1,2)
--- >>> let v3 = make2d (3,4)
--- >>> v1 == v2
--- True
--- >>> v1 == v3
--- False
---
-instance (Eq a, Vector v a) => Eq (Vn v a) where
- (Vn v1) == (Vn v2) = v1 `V.eq` v2
-
-
--- | The use of 'Num' here is of course incorrect (otherwise, we
--- wouldn't have to throw errors). But it's really nice to be able
--- to use normal addition/subtraction.
-instance (Num a, Vector v a) => Num (Vn v a) where
- -- | Componentwise addition.
- --
- -- Examples:
- --
- -- >>> let v1 = make2d (1,2)
- -- >>> let v2 = make2d (3,4)
- -- >>> v1 + v2
- -- (4,6)
- --
- (Vn v1) + (Vn v2) = Vn $ V.zipWith (+) v1 v2