From de66efe75ba02c66cece33d161e429809fa9058b Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Wed, 15 Jun 2022 22:36:33 -0400 Subject: [PATCH] Create Data.NP --- src/Data/NP.idr | 24 ++++++++++++++++++++++++ src/Data/NumIdr/Array/Coords.idr | 14 ++++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 src/Data/NP.idr diff --git a/src/Data/NP.idr b/src/Data/NP.idr new file mode 100644 index 0000000..d026024 --- /dev/null +++ b/src/Data/NP.idr @@ -0,0 +1,24 @@ +module Data.NP + +import Data.Vect + +%default total + + +public export +data NP : (f : k -> Type) -> (ts : Vect n k) -> Type where + Nil : NP {k} f [] + (::) : f t -> NP {k} f ts -> NP {k} f (t :: ts) + + +public export +(all : NP (Eq . f) ts) => Eq (NP f ts) where + (==) {all = []} [] [] = True + (==) {all = _ :: _} (x :: xs) (y :: ys) = (x == y) && (xs == ys) + + + +public export +index : (i : Fin n) -> NP {n} f ts -> f (index i ts) +index FZ (x :: xs) = x +index (FS i) (x :: xs) = index i xs diff --git a/src/Data/NumIdr/Array/Coords.idr b/src/Data/NumIdr/Array/Coords.idr index db4b8e7..e1bc6c5 100644 --- a/src/Data/NumIdr/Array/Coords.idr +++ b/src/Data/NumIdr/Array/Coords.idr @@ -2,6 +2,7 @@ module Data.NumIdr.Array.Coords import Data.Either import Data.Vect +import public Data.NP %default total @@ -11,9 +12,8 @@ namespace Coords ||| A type-safe coordinate system for an array. The coordinates are ||| values of `Fin dim`, where `dim` is the dimension of each axis. public export - data Coords : (s : Vect rk Nat) -> Type where - Nil : Coords Nil - (::) : Fin dim -> Coords s -> Coords (dim :: s) + Coords : (s : Vect rk Nat) -> Type + Coords = NP Fin ||| Forget the shape of the array by converting each index to type `Nat`. export @@ -29,7 +29,7 @@ namespace Coords export collapse : {s : _} -> Vects s a -> List a - collapse {s=[]} = (::[]) + collapse {s=[]} = pure collapse {s=_::_} = concat . map collapse @@ -72,10 +72,8 @@ namespace CoordsRange public export - data CoordsRange : Vect rk Nat -> Type where - Nil : CoordsRange [] - (::) : CRange d -> CoordsRange s -> CoordsRange (d :: s) - + CoordsRange : (s : Vect rk Nat) -> Type + CoordsRange = NP CRange public export cRangeToBounds : {n : Nat} -> CRange n -> Either Nat (Nat, Nat)