Skip to content

Commit 7abf1a3

Browse files
authored
Merge pull request #150 from well-typed/improve-cata-ana
Generalise type signatures of cata- and anamorphisms.
2 parents 42f433c + 688c115 commit 7abf1a3

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

sop-core/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 0.6.0.0
2+
3+
* Generalise the type signatures of cata- and anamorphisms
4+
to pass down the constraint dictionaries, as suggested
5+
in #144.
6+
17
# 0.5.0.2 (2022-01-02)
28

39
* Compatibility with GHC-9.0 and GHC-9.2.

sop-core/src/Data/SOP/NP.hs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
{-# LANGUAGE PolyKinds, StandaloneDeriving, UndecidableInstances #-}
1+
{-# LANGUAGE PolyKinds #-}
2+
{-# LANGUAGE StandaloneDeriving #-}
3+
{-# LANGUAGE UndecidableInstances #-}
24
-- | n-ary products (and products of products)
35
module Data.SOP.NP
46
( -- * Datatypes
@@ -713,15 +715,14 @@ ctraverse_POP = hctraverse
713715
--
714716
cata_NP ::
715717
forall r f xs .
716-
r '[]
717-
-> (forall y ys . f y -> r ys -> r (y ': ys))
718+
SListI xs
719+
=> r '[]
720+
-> (forall y ys . SListI ys => f y -> r ys -> r (y ': ys))
718721
-> NP f xs
719722
-> r xs
720-
cata_NP nil cons = go
721-
where
722-
go :: forall ys . NP f ys -> r ys
723-
go Nil = nil
724-
go (x :* xs) = cons x (go xs)
723+
cata_NP nil cons =
724+
ccata_NP topP nil cons
725+
{-# INLINE cata_NP #-}
725726

726727
-- | Constrained catamorphism for 'NP'.
727728
--
@@ -733,10 +734,11 @@ cata_NP nil cons = go
733734
-- @since 0.2.3.0
734735
--
735736
ccata_NP ::
736-
forall c proxy r f xs . (All c xs)
737+
forall c proxy r f xs .
738+
All c xs
737739
=> proxy c
738740
-> r '[]
739-
-> (forall y ys . c y => f y -> r ys -> r (y ': ys))
741+
-> (forall y ys . (c y, All c ys) => f y -> r ys -> r (y ': ys))
740742
-> NP f xs
741743
-> r xs
742744
ccata_NP _ nil cons = go
@@ -759,7 +761,7 @@ ccata_NP _ nil cons = go
759761
ana_NP ::
760762
forall s f xs .
761763
SListI xs
762-
=> (forall y ys . s (y ': ys) -> (f y, s ys))
764+
=> (forall y ys . SListI ys => s (y ': ys) -> (f y, s ys))
763765
-> s xs
764766
-> NP f xs
765767
ana_NP uncons =
@@ -775,14 +777,15 @@ ana_NP uncons =
775777
-- @since 0.2.3.0
776778
--
777779
cana_NP ::
778-
forall c proxy s f xs . (All c xs)
780+
forall c proxy s f xs .
781+
All c xs
779782
=> proxy c
780-
-> (forall y ys . c y => s (y ': ys) -> (f y, s ys))
783+
-> (forall y ys . (c y, All c ys) => s (y ': ys) -> (f y, s ys))
781784
-> s xs
782785
-> NP f xs
783786
cana_NP _ uncons = go sList
784787
where
785-
go :: forall ys . (All c ys) => SList ys -> s ys -> NP f ys
788+
go :: forall ys . All c ys => SList ys -> s ys -> NP f ys
786789
go SNil _ = Nil
787790
go SCons s = case uncons s of
788791
(x, s') -> x :* go sList s'

sop-core/src/Data/SOP/NS.hs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -770,30 +770,29 @@ ctraverse_SOP = hctraverse
770770
--
771771
cata_NS ::
772772
forall r f xs .
773-
(forall y ys . f y -> r (y ': ys))
774-
-> (forall y ys . r ys -> r (y ': ys))
773+
SListI xs
774+
=> (forall y ys . SListI ys => f y -> r (y ': ys))
775+
-> (forall y ys . SListI ys => r ys -> r (y ': ys))
775776
-> NS f xs
776777
-> r xs
777-
cata_NS z s = go
778-
where
779-
go :: forall ys . NS f ys -> r ys
780-
go (Z x) = z x
781-
go (S i) = s (go i)
778+
cata_NS z s =
779+
ccata_NS topP z s
782780

783781
-- | Constrained catamorphism for 'NS'.
784782
--
785783
-- @since 0.2.3.0
786784
--
787785
ccata_NS ::
788-
forall c proxy r f xs . (All c xs)
786+
forall c proxy r f xs .
787+
All c xs
789788
=> proxy c
790-
-> (forall y ys . c y => f y -> r (y ': ys))
791-
-> (forall y ys . c y => r ys -> r (y ': ys))
789+
-> (forall y ys . (c y, All c ys) => f y -> r (y ': ys))
790+
-> (forall y ys . (c y, All c ys) => r ys -> r (y ': ys))
792791
-> NS f xs
793792
-> r xs
794793
ccata_NS _ z s = go
795794
where
796-
go :: forall ys . (All c ys) => NS f ys -> r ys
795+
go :: forall ys . All c ys => NS f ys -> r ys
797796
go (Z x) = z x
798797
go (S i) = s (go i)
799798

@@ -802,9 +801,10 @@ ccata_NS _ z s = go
802801
-- @since 0.2.3.0
803802
--
804803
ana_NS ::
805-
forall s f xs . (SListI xs)
804+
forall s f xs .
805+
SListI xs
806806
=> (forall r . s '[] -> r)
807-
-> (forall y ys . s (y ': ys) -> Either (f y) (s ys))
807+
-> (forall y ys . SListI ys => s (y ': ys) -> Either (f y) (s ys))
808808
-> s xs
809809
-> NS f xs
810810
ana_NS refute decide =
@@ -816,15 +816,15 @@ ana_NS refute decide =
816816
-- @since 0.2.3.0
817817
--
818818
cana_NS :: forall c proxy s f xs .
819-
(All c xs)
819+
All c xs
820820
=> proxy c
821821
-> (forall r . s '[] -> r)
822-
-> (forall y ys . c y => s (y ': ys) -> Either (f y) (s ys))
822+
-> (forall y ys . (c y, All c ys) => s (y ': ys) -> Either (f y) (s ys))
823823
-> s xs
824824
-> NS f xs
825825
cana_NS _ refute decide = go sList
826826
where
827-
go :: forall ys . (All c ys) => SList ys -> s ys -> NS f ys
827+
go :: forall ys . All c ys => SList ys -> s ys -> NS f ys
828828
go SNil s = refute s
829829
go SCons s = case decide s of
830830
Left x -> Z x

0 commit comments

Comments
 (0)