Add Show instance for NP

This commit is contained in:
Kiana Sheibani 2022-08-04 15:08:17 -04:00
parent 6cdb22a6ed
commit 1ebf0dcbe9
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
4 changed files with 24 additions and 10 deletions

View file

@ -13,8 +13,17 @@ data NP : (f : k -> Type) -> (ts : Vect n k) -> Type where
public export
(all : NP (Eq . f) ts) => Eq (NP f ts) where
(==) {all = []} [] [] = True
(==) {all = _ :: _} (x :: xs) (y :: ys) = (x == y) && (xs == ys)
(==) {all=[]} [] [] = True
(==) {all=_::_} (x :: xs) (y :: ys) = (x == y) && (xs == ys)
public export
(all : NP (Show . f) ts) => Show (NP f ts) where
show xs = "[" ++ elems xs ++ "]"
where
elems : {0 f,ts : _} -> (all : NP (Show . f) ts) => NP f ts -> String
elems {all=[]} [] = ""
elems {all=[_]} [x] = show x
elems {all=_::_} (x :: xs) = show x ++ ", " ++ elems xs

View file

@ -276,8 +276,7 @@ indexSet is = indexUpdate is . const
||| coordinates are out of bounds.
export
indexNB : Vect rk Nat -> Array {rk} s a -> Maybe a
indexNB is arr with (viewShape arr)
_ | Shape s = if concat @{All} $ zipWith (<) is s
indexNB is arr = if and $ map delay $ zipWith (<) is (shape arr)
then Just $ index (getLocation' (strides arr) is) (getPrim arr)
else Nothing
@ -391,6 +390,14 @@ resizeLTE : (s' : Vect rk Nat) -> (0 ok : NP Prelude.id (zipWith LTE s' s)) =>
resizeLTE s' arr = resize s' (believe_me ()) arr
||| List all of the values in an array in row-major order.
export
elements : Array {rk} s a -> Vect (product s) a
elements (MkArray _ sts sh p) =
let elems = map (flip index p . getLocation' sts) (getAllCoords' sh)
in assert_total $ case toVect (product sh) elems of Just v => v
||| List all of the values in an array along with their coordinates.
export
enumerateNB : Array {rk} s a -> List (Vect rk Nat, a)

View file

@ -103,8 +103,8 @@ namespace CoordsRange
getNewPos : {s : _} -> (rs : CoordsRange {rk} s) -> Vect rk Nat -> Vect (newRank rs) Nat
getNewPos [] [] = []
getNewPos (r :: rs) (i :: is) with (cRangeToBounds r)
getNewPos (r :: rs) (i :: is) | Left _ = getNewPos rs is
getNewPos (r :: rs) (i :: is) | Right (x, _) = minus i x :: getNewPos rs is
_ | Left _ = getNewPos rs is
_ | Right (x, _) = minus i x :: getNewPos rs is
export
getCoordsList : {s : Vect rk Nat} -> (rs : CoordsRange s) -> List (Vect rk Nat, Vect (newRank rs) Nat)

View file

@ -11,11 +11,9 @@ import Data.Vect
||| memory location.
public export
data Order : Type where
||| C-like order, or contiguous order. This order stores elements in a
||| row-major fashion (the last axis is the least significant).
COrder : Order
||| Fortran-like order. This order stores elements in a column-major
||| fashion (the first axis is the least significant).
FOrder : Order