Create Data.NP

This commit is contained in:
Kiana Sheibani 2022-06-15 22:36:33 -04:00
parent cb0157cd8c
commit de66efe75b
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
2 changed files with 30 additions and 8 deletions

24
src/Data/NP.idr Normal file
View file

@ -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

View file

@ -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)