Commit 718d043a by simonmar

### [project @ 2001-09-13 15:54:43 by simonmar]

Back out the change to remove Ord as a superclass of Ix; the revised
Haskell 98 report will no longer have this change.
parent 37707670
 ... ... @@ -155,7 +155,7 @@ array :: (Ix a) => (a,a) -> [(a,b)] -> Array a b array b ivs = if and [inRange b i | (i,_) <- ivs] then MkArray b (\j -> case [v | (i,v) <- ivs, index b i == index b j] of (\j -> case [v | (i,v) <- ivs, i == j] of [v] -> v [] -> error "Array.!: \ \undefined array element" ... ... @@ -183,11 +183,8 @@ assocs a = [(i, a!i) | i <- indices a] (//) :: (Ix a) => Array a b -> [(a,b)] -> Array a b a // us = array (bounds a) ([(i,a!i) | i <- indices a, index b i notElem new_indices] ([(i,a!i) | i <- indices a \\ [i | (i,_) <- us]] ++ us) where b = bounds a new_indices = map (index b . fst) us accum :: (Ix a) => (b -> c -> b) -> Array a b -> [(a,c)] -> Array a b ... ... @@ -204,10 +201,10 @@ ixmap b f a = array b [(i, a ! f i) | i <- range b] instance (Ix a) => Functor (Array a) where fmap fn (MkArray b f) = MkArray b (fn . f) instance (Ix a, Eq a, Eq b) => Eq (Array a b) where instance (Ix a, Eq b) => Eq (Array a b) where a == a' = assocs a == assocs a' instance (Ix a, Ord a, Ord b) => Ord (Array a b) where instance (Ix a, Ord b) => Ord (Array a b) where a <= a' = assocs a <= assocs a' instance (Ix a, Show a, Show b) => Show (Array a b) where ... ... @@ -216,7 +213,7 @@ instance (Ix a, Show a, Show b) => Show (Array a b) where shows (bounds a) . showChar ' ' . shows (assocs a) ) instance (Eq a, Ix a, Read a, Read b) => Read (Array a b) where instance (Ix a, Read a, Read b) => Read (Array a b) where readsPrec p = readParen (p > 9) (\r -> [(array b as, u) | ("array",s) <- lex r, (b,t) <- reads s, ... ... @@ -509,4 +506,4 @@ instance Observable StdGen where { observer = observeBase } instance Observable a => Observable (Gen a) where observer (Gen a) = send "Gen" (return (Gen) << a) -} -} \ No newline at end of file
 ... ... @@ -199,7 +199,7 @@ mapAccumL f s (b:bs) = (c:cs,s'') -- Here is a version of @elem@ that uses partual application -- to optimise lookup. arrElem :: (Ord a, Ix a) => [a] -> a -> Bool arrElem :: (Ix a) => [a] -> a -> Bool arrElem obj = \x -> inRange size x && arr ! x where size = (maximum obj,minimum obj) ... ... @@ -209,7 +209,7 @@ arrElem obj = \x -> inRange size x && arr ! x -- again using arrays, of course. Remember @b@ can be a function ! -- Note again the use of partiual application. arrCond :: (Eq a, Ix a) arrCond :: (Ix a) => (a,a) -- the bounds -> [(Assoc [a] b)] -- the simple lookups -> [(Assoc (a -> Bool) b)] -- the functional lookups ... ...
 ... ... @@ -185,7 +185,7 @@ trMatrix (Matrix x) = matrix ((l,l'),(u',u)) [((j,i), x!(i,j)) | j <- range (l', row :: (Ix a, Ix b) => a -> Array (a,b) c -> Array b c row i x = ixmap (l',u') (\j->(i,j)) x where ((l,l'),(u,u')) = bounds x zipArr :: (Eq a,Ix a) => String -> (b -> c -> d) -> Array a b -> Array a c -> Array a d zipArr :: (Ix a) => String -> (b -> c -> d) -> Array a b -> Array a c -> Array a d zipArr s f a b | bounds a == bounds b = array (bounds a) [(i, f (a!i) (b!i)) | i <- indices a] | otherwise = error ("zipArr: " ++ s ++ ": unconformable arrays") \end{code} ... ... @@ -193,7 +193,7 @@ zipArr s f a b | bounds a == bounds b = array (bounds a) [(i, f (a!i) (b!i)) | i Valid only for b -> c -> b functions. \begin{code} zipArr' :: (Eq a,Ix a) => String -> (b -> c -> b) -> Array a b -> Array a c -> Array a b zipArr' :: (Ix a) => String -> (b -> c -> b) -> Array a b -> Array a c -> Array a b zipArr' s f a b | bounds a == bounds b = accum f a (assocs b) | otherwise = error ("zipArr': " ++ s ++ ": unconformable arrays") \end{code} ... ... @@ -257,7 +257,7 @@ mapindexed3 f = mapindexed (\i -> mapindexed (\j -> mapindexed (\k -> f (i, j, k Overload arithmetical operators to work on arrays. \begin{code} instance (Eq a, Ix a, Show a, Num b) => Num (Array a b) where instance (Ix a, Show a, Num b) => Num (Array a b) where (+) = zipArr "+" (+) (-) = zipArr "-" (-) negate = fmap negate ... ... @@ -281,7 +281,7 @@ arraySize = rangeSize . bounds \end{code} \begin{code} matMult :: (Eq a, Eq b, Ix a, Ix b, Ix c, Num d) => matMult :: (Ix a, Ix b, Ix c, Num d) => Array (a,b) d -> Array (b,c) d -> Array (a,c) d matMult x y = array resultBounds [((i,j), sum [x!(i,k) * y!(k,j) | k <- range (lj,uj)]) ... ... @@ -316,7 +316,7 @@ outerVector (Vector v) (Vector w) = if (l,u) == (l',u') \end{code} \begin{code} outerArr :: (Eq a, Ix a, Num b) => Array a b -> Array a b -> Array (a,a) b outerArr :: (Ix a, Num b) => Array a b -> Array a b -> Array (a,a) b outerArr v w = if (l,u) == (l',u') then array ((l,l'),(u,u')) [((i,j), v!i * w!j) | i <- range (l,u), j <- range (l',u')] else error "nn.outer: inconformable vectors" ... ... @@ -326,7 +326,7 @@ outerArr v w = if (l,u) == (l',u') Inner product of a matrix and a vector. \begin{code} matvec :: (Eq a, Ix a, Num b) => Array (a,a) b -> Array a b -> Array a b matvec :: (Ix a, Num b) => Array (a,a) b -> Array a b -> Array a b matvec w x | bounds x == (l',u') = array (l,u) [(i, sum [w!(i,j) * x!j | j <- range (l',u')]) | i <- range (l,u)] ... ...
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!