Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

append and split NP #115

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion sop-core/src/Data/SOP/NP.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{-# LANGUAGE PolyKinds, StandaloneDeriving, UndecidableInstances #-}
{-# LANGUAGE
PolyKinds
, StandaloneDeriving
, TypeApplications
, UndecidableInstances
#-}
-- | n-ary products (and products of products)
module Data.SOP.NP
( -- * Datatypes
Expand All @@ -10,6 +15,8 @@ module Data.SOP.NP
, pure_POP
, cpure_NP
, cpure_POP
, type (++)
, happend
-- ** Construction from a list
, fromList
-- * Application
Expand All @@ -21,6 +28,7 @@ module Data.SOP.NP
, Projection
, projections
, shiftProjection
, hsplit
-- * Lifting / mapping
, liftA_NP
, liftA_POP
Expand Down Expand Up @@ -273,6 +281,17 @@ instance HPure POP where
hpure = pure_POP
hcpure = cpure_POP

-- | Append type level lists with `++` type operator.
type family (++) xs ys where
'[] ++ ys = ys
(x ': xs) ++ ys = x ': (xs ++ ys)
infixr 5 ++

-- | Append n-ary products with `happend`.
happend :: NP f xs -> NP f ys -> NP f (xs ++ ys)
happend Nil ys = ys
happend (x :* xs) ys = x :* happend xs ys

-- ** Construction from a list

-- | Construct a homogeneous n-ary product from a normal Haskell list.
Expand Down Expand Up @@ -360,6 +379,16 @@ projections = case sList :: SList xs of
shiftProjection :: Projection f xs a -> Projection f (x ': xs) a
shiftProjection (Fn f) = Fn $ f . K . tl . unK

-- | Split an n-ary product with `hsplit`.
hsplit
:: forall xs ys f. SListI xs
=> NP f (xs ++ ys)
-> (NP f xs, NP f ys)
hsplit = case sList @xs of
SNil -> \ys -> (Nil, ys)
SCons -> \(x :* xsys) ->
case hsplit xsys of (xs,ys) -> (x :* xs, ys)

-- * Lifting / mapping

-- | Specialization of 'hliftA'.
Expand Down