From 05ceba5412a71626a5a88106d8f7d00e9b3ec08f Mon Sep 17 00:00:00 2001 From: Eitan Chatav Date: Tue, 4 Feb 2020 09:01:57 -0800 Subject: [PATCH 1/2] append and split NP Add functions to append and split n-ary products. --- sop-core/src/Data/SOP/NP.hs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/sop-core/src/Data/SOP/NP.hs b/sop-core/src/Data/SOP/NP.hs index 4d92cfb..c0fc9f4 100644 --- a/sop-core/src/Data/SOP/NP.hs +++ b/sop-core/src/Data/SOP/NP.hs @@ -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 @@ -10,6 +15,8 @@ module Data.SOP.NP , pure_POP , cpure_NP , cpure_POP + , (++) + , happend -- ** Construction from a list , fromList -- * Application @@ -21,6 +28,7 @@ module Data.SOP.NP , Projection , projections , shiftProjection + , hsplit -- * Lifting / mapping , liftA_NP , liftA_POP @@ -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. @@ -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'. From 554b070b20951106df383e7fccf14110b06d2bf8 Mon Sep 17 00:00:00 2001 From: Eitan Chatav Date: Tue, 4 Feb 2020 19:31:26 -0800 Subject: [PATCH 2/2] export type --- sop-core/src/Data/SOP/NP.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sop-core/src/Data/SOP/NP.hs b/sop-core/src/Data/SOP/NP.hs index c0fc9f4..196b117 100644 --- a/sop-core/src/Data/SOP/NP.hs +++ b/sop-core/src/Data/SOP/NP.hs @@ -15,7 +15,7 @@ module Data.SOP.NP , pure_POP , cpure_NP , cpure_POP - , (++) + , type (++) , happend -- ** Construction from a list , fromList