Skip to content

Commit

Permalink
Merge pull request #107 from miguel-negrao/type_safe_lens
Browse files Browse the repository at this point in the history
type-safe lens
  • Loading branch information
expipiplus1 authored Jun 26, 2021
2 parents 65fdcfc + 4fcf658 commit 45ef035
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 4 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## WIP

## [1.4.4] - 2021-06-26

- Add `ix'`

## [1.4.3.1] - 2020-12-13

- Fix Bits instance, shiftl and shiftr were incorrect
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: vector-sized
version: 1.4.3.1
version: 1.4.4
synopsis: Size tagged vectors
description: Please see README.md
category: Data
Expand Down
10 changes: 10 additions & 0 deletions src/Data/Vector/Generic/Sized.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE AllowAmbiguousTypes #-}

#if MIN_VERSION_base(4,12,0)
{-# LANGUAGE NoStarIsType #-}
Expand Down Expand Up @@ -111,6 +112,7 @@ module Data.Vector.Generic.Sized
, unsafeBackpermute
-- * Lenses
, ix
, ix'
, _head
, _last
-- * Elementwise operations
Expand Down Expand Up @@ -468,6 +470,14 @@ ix :: forall v n a f. (VG.Vector v a, Functor f)
ix n f vector = (\x -> vector // [(n, x)]) <$> f (index vector n)
{-# inline ix #-}

-- | Type-safe lens to access (/O(1)/) and update (/O(n)/) an arbitrary element by its index
-- which should be supplied via TypeApplications.
ix' :: forall i v n a f. (VG.Vector v a, Functor f,
KnownNat i, KnownNat n, i+1 <= n)
=> (a -> f a) -> Vector v n a -> f (Vector v n a)
ix' = ix (natToFinite (Proxy::Proxy i))
{-# inline ix' #-}

-- | Lens to access (/O(1)/) and update (/O(n)/) the first element of a non-empty vector.
_head :: forall v n a f. (VG.Vector v a, Functor f)
=> (a -> f a) -> Vector v (1+n) a -> f (Vector v (1+n) a)
Expand Down
12 changes: 12 additions & 0 deletions src/Data/Vector/Sized.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE CPP #-}

#if MIN_VERSION_base(4,12,0)
Expand Down Expand Up @@ -106,6 +109,7 @@ module Data.Vector.Sized
, unsafeBackpermute
-- * Lenses
, ix
, ix'
, _head
, _last
-- * Elementwise operations
Expand Down Expand Up @@ -331,6 +335,14 @@ ix :: forall n a f. Functor f
ix = V.ix
{-# inline ix #-}

-- | Type-safe lens to access (/O(1)/) and update (/O(n)/) an arbitrary element by its index
-- which should be supplied via TypeApplications.
ix' :: forall i n a f. (Functor f,
KnownNat i, KnownNat n, i+1 <= n)
=> (a -> f a) -> Vector n a -> f (Vector n a)
ix' = V.ix' @i
{-# inline ix' #-}

-- | Lens to access (/O(1)/) and update (/O(n)/) the first element of a non-empty vector.
_head :: forall n a f. Functor f
=> (a -> f a) -> Vector (1+n) a -> f (Vector (1+n) a)
Expand Down
12 changes: 12 additions & 0 deletions src/Data/Vector/Storable/Sized.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE CPP #-}

#if MIN_VERSION_base(4,12,0)
Expand Down Expand Up @@ -107,6 +110,7 @@ module Data.Vector.Storable.Sized
, unsafeBackpermute
-- * Lenses
, ix
, ix'
, _head
, _last
-- * Elementwise operations
Expand Down Expand Up @@ -335,6 +339,14 @@ ix :: forall n a f. (Storable a, Functor f)
ix = V.ix
{-# inline ix #-}

-- | Type-safe lens to access (/O(1)/) and update (/O(n)/) an arbitrary element by its index
-- which should be supplied via TypeApplications.
ix' :: forall i n a f. (Storable a, Functor f,
KnownNat i, KnownNat n, i+1 <= n)
=> (a -> f a) -> Vector n a -> f (Vector n a)
ix' = V.ix' @i
{-# inline ix' #-}

-- | Lens to access (/O(1)/) and update (/O(n)/) the first element of a non-empty vector.
_head :: forall n a f. (Storable a, Functor f)
=> (a -> f a) -> Vector (1+n) a -> f (Vector (1+n) a)
Expand Down
14 changes: 13 additions & 1 deletion src/Data/Vector/Unboxed/Sized.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE CPP #-}

#if MIN_VERSION_base(4,12,0)
Expand Down Expand Up @@ -107,6 +110,7 @@ module Data.Vector.Unboxed.Sized
, unsafeBackpermute
-- * Lenses
, ix
, ix'
, _head
, _last
-- * Elementwise operations
Expand Down Expand Up @@ -337,6 +341,14 @@ ix :: forall n a f. (Unbox a, Functor f)
ix = V.ix
{-# inline ix #-}

-- | Type-safe lens to access (/O(1)/) and update (/O(n)/) an arbitrary element by its index
-- which should be supplied via TypeApplications.
ix' :: forall i n a f. (Unbox a, Functor f,
KnownNat i, KnownNat n, i+1 <= n)
=> (a -> f a) -> Vector n a -> f (Vector n a)
ix' = V.ix' @i
{-# inline ix' #-}

-- | Lens to access (/O(1)/) and update (/O(n)/) the first element of a non-empty vector.
_head :: forall n a f. (Unbox a, Functor f)
=> (a -> f a) -> Vector (1+n) a -> f (Vector (1+n) a)
Expand Down
6 changes: 4 additions & 2 deletions vector-sized.cabal
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.34.2.
-- This file has been generated from package.yaml by hpack version 0.33.0.
--
-- see: https://github.com/sol/hpack
--
-- hash: e36af7d31e151c5e8d7ad5636dd5d31b47e82d6fe6384f8f76753ed0e17b61b1

name: vector-sized
version: 1.4.3.1
version: 1.4.4
synopsis: Size tagged vectors
description: Please see README.md
category: Data
Expand Down

0 comments on commit 45ef035

Please sign in to comment.