From a6b95556c3593c2cdb4047f59fada1aa5d62a068 Mon Sep 17 00:00:00 2001 From: M Farkas-Dyck Date: Wed, 27 Sep 2017 13:21:54 -0800 Subject: [PATCH 1/7] make `Map` not associated to class; it needn't be --- Data/OpenRecords.hs | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/Data/OpenRecords.hs b/Data/OpenRecords.hs index b130b9a..916babe 100644 --- a/Data/OpenRecords.hs +++ b/Data/OpenRecords.hs @@ -374,51 +374,47 @@ class Forall (r :: Row *) (c :: * -> Constraint) where labels :: forall r s . (Forall r Unconstrained1, IsString s) => Proxy r -> [s] labels _ = getConst $ rinitAWithLabel @r (Proxy @Unconstrained1) (Const . pure . show') -class RowMap (f :: * -> *) (r :: Row *) where - type Map f r :: Row * +type family Map (f :: a -> b) (r :: Row a) :: Row b where Map f (R r) = R (RM f r) + +class RowMap (r :: Row *) where rmap :: Proxy f -> (forall a. a -> f a) -> Rec r -> Rec (Map f r) rsequence :: Applicative f => Proxy f -> Rec (Map f r) -> f (Rec r) -instance RowMapx f r => RowMap f (R r) where - type Map f (R r) = R (RM f r) +instance RowMapx r => RowMap (R r) where rmap = rmap' rsequence = rsequence' -class RowMapx (f :: * -> *) (r :: [LT *]) where - type RM f r :: [LT *] +type family RM (f :: a -> b) (r :: [LT a]) :: [LT b] where + RM f '[] = '[] + RM f (l :-> v ': t) = l :-> f v ': RM f t + +class RowMapx (r :: [LT *]) where rmap' :: Proxy f -> (forall a. a -> f a) -> Rec (R r) -> Rec (R (RM f r)) rsequence' :: Applicative f => Proxy f -> Rec (R (RM f r)) -> f (Rec (R r)) -instance RowMapx f '[] where - type RM f '[] = '[] +instance RowMapx '[] where rmap' _ _ _ = empty rsequence' _ _ = pure empty -instance (KnownSymbol l, RowMapx f t) => RowMapx f (l :-> v ': t) where - type RM f (l :-> v ': t) = l :-> f v ': RM f t +instance (KnownSymbol l, RowMapx t) => RowMapx (l :-> v ': t) where rmap' w f r = unsafeInjectFront l (f (r .! l)) (rmap' w f (r .- l)) where l = Label :: Label l rsequence' w r = unsafeInjectFront l <$> r .! l <*> rsequence' w (r .- l) where l = Label :: Label l -class RowMapC (c :: * -> Constraint) (f :: * -> *) (r :: Row *) where - type MapC c f r :: Row * - rmapc :: Proxy c -> Proxy f -> (forall a. c a => a -> f a) -> Rec r -> Rec (MapC c f r) +class RowMapC (c :: * -> Constraint) (r :: Row *) where + rmapc :: Proxy c -> Proxy f -> (forall a. c a => a -> f a) -> Rec r -> Rec (Map f r) -instance RMapc c f r => RowMapC c f (R r) where - type MapC c f (R r) = R (RMapp c f r) +instance RMapc c r => RowMapC c (R r) where rmapc = rmapc' -class RMapc (c :: * -> Constraint) (f :: * -> *) (r :: [LT *]) where - type RMapp c f r :: [LT *] - rmapc' :: Proxy c -> Proxy f -> (forall a. c a => a -> f a) -> Rec (R r) -> Rec (R (RMapp c f r)) +class RMapc (c :: * -> Constraint) (r :: [LT *]) where + rmapc' :: Proxy c -> Proxy f -> (forall a. c a => a -> f a) -> Rec (R r) -> Rec (R (RM f r)) -instance RMapc c f '[] where - type RMapp c f '[] = '[] +instance RMapc c '[] where rmapc' _ _ _ _ = empty -instance (KnownSymbol l, c v, RMapc c f t) => RMapc c f (l :-> v ': t) where - type RMapp c f (l :-> v ': t) = l :-> f v ': RMapp c f t +instance (KnownSymbol l, c v, RMapc c t) => RMapc c (l :-> v ': t) where rmapc' c w f r = unsafeInjectFront l (f (r .! l)) (rmapc' c w f (r .- l)) where l = Label :: Label l From 3f6a18f8a7bdb7b79b02181a5efdd0799c085d3b Mon Sep 17 00:00:00 2001 From: M Farkas-Dyck Date: Mon, 2 Oct 2017 11:17:55 -0800 Subject: [PATCH 2/7] move `rmapc` into `Forall` --- Data/OpenRecords.hs | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/Data/OpenRecords.hs b/Data/OpenRecords.hs index 916babe..58c1ded 100644 --- a/Data/OpenRecords.hs +++ b/Data/OpenRecords.hs @@ -371,6 +371,8 @@ class Forall (r :: Row *) (c :: * -> Constraint) where -- with the same label and collect the result in a list. eraseZip :: Proxy c -> (forall a. c a => a -> a -> b) -> Rec r -> Rec r -> [b] + rmapc :: Proxy c -> Proxy f -> (forall a. c a => a -> f a) -> Rec r -> Rec (Map f r) + labels :: forall r s . (Forall r Unconstrained1, IsString s) => Proxy r -> [s] labels _ = getConst $ rinitAWithLabel @r (Proxy @Unconstrained1) (Const . pure . show') @@ -402,28 +404,13 @@ instance (KnownSymbol l, RowMapx t) => RowMapx (l :-> v ': t) where rsequence' w r = unsafeInjectFront l <$> r .! l <*> rsequence' w (r .- l) where l = Label :: Label l -class RowMapC (c :: * -> Constraint) (r :: Row *) where - rmapc :: Proxy c -> Proxy f -> (forall a. c a => a -> f a) -> Rec r -> Rec (Map f r) - -instance RMapc c r => RowMapC c (R r) where - rmapc = rmapc' - -class RMapc (c :: * -> Constraint) (r :: [LT *]) where - rmapc' :: Proxy c -> Proxy f -> (forall a. c a => a -> f a) -> Rec (R r) -> Rec (R (RM f r)) - -instance RMapc c '[] where - rmapc' _ _ _ _ = empty - -instance (KnownSymbol l, c v, RMapc c t) => RMapc c (l :-> v ': t) where - rmapc' c w f r = unsafeInjectFront l (f (r .! l)) (rmapc' c w f (r .- l)) - where l = Label :: Label l - instance Forall (R '[]) c where rinit _ _ = empty rinitAWithLabel _ _ = pure empty eraseWithLabels _ _ _ = [] eraseToHashMap _ _ _ = M.empty eraseZip _ _ _ _ = [] + rmapc _ _ _ _ = empty instance (KnownSymbol l, Forall (R t) c, c a) => Forall (R (l :-> a ': t)) c where rinit c f = unsafeInjectFront l f (rinit c f) where l = Label :: Label l @@ -439,6 +426,9 @@ instance (KnownSymbol l, Forall (R t) c, c a) => Forall (R (l :-> a ': t)) c whe eraseZip c f x y = f (x .! l) (y .! l) : eraseZip c f (x .- l) (y .- l) where l = Label :: Label l + rmapc c w f r = unsafeInjectFront l (f (r .! l)) (rmapc c w f (r .- l)) + where l = Label :: Label l + show' :: (IsString s, Show a) => a -> s show' = fromString . show From 22ba8611a5701fe7ea8c4ada952c13de3bc94992 Mon Sep 17 00:00:00 2001 From: M Farkas-Dyck Date: Mon, 2 Oct 2017 11:31:08 -0800 Subject: [PATCH 3/7] delete some unneeded `Proxy` parametres --- CTRex.cabal | 3 ++- Data/OpenRecords.hs | 23 ++++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/CTRex.cabal b/CTRex.cabal index a09e88f..2dd1d21 100644 --- a/CTRex.cabal +++ b/CTRex.cabal @@ -39,7 +39,8 @@ Library TypeFamilies, TypeOperators, ViewPatterns, - UndecidableInstances + UndecidableInstances, + UnicodeSyntax source-repository head type: git diff --git a/Data/OpenRecords.hs b/Data/OpenRecords.hs index 58c1ded..69476f9 100644 --- a/Data/OpenRecords.hs +++ b/Data/OpenRecords.hs @@ -371,7 +371,7 @@ class Forall (r :: Row *) (c :: * -> Constraint) where -- with the same label and collect the result in a list. eraseZip :: Proxy c -> (forall a. c a => a -> a -> b) -> Rec r -> Rec r -> [b] - rmapc :: Proxy c -> Proxy f -> (forall a. c a => a -> f a) -> Rec r -> Rec (Map f r) + rmapc :: Proxy c -> (forall a. c a => a -> f a) -> Rec r -> Rec (Map f r) labels :: forall r s . (Forall r Unconstrained1, IsString s) => Proxy r -> [s] labels _ = getConst $ rinitAWithLabel @r (Proxy @Unconstrained1) (Const . pure . show') @@ -379,8 +379,8 @@ labels _ = getConst $ rinitAWithLabel @r (Proxy @Unconstrained1) (Const . pure . type family Map (f :: a -> b) (r :: Row a) :: Row b where Map f (R r) = R (RM f r) class RowMap (r :: Row *) where - rmap :: Proxy f -> (forall a. a -> f a) -> Rec r -> Rec (Map f r) - rsequence :: Applicative f => Proxy f -> Rec (Map f r) -> f (Rec r) + rmap :: (forall a. a -> f a) -> Rec r -> Rec (Map f r) + rsequence :: Applicative f => Rec (Map f r) -> f (Rec r) instance RowMapx r => RowMap (R r) where rmap = rmap' @@ -391,17 +391,17 @@ type family RM (f :: a -> b) (r :: [LT a]) :: [LT b] where RM f (l :-> v ': t) = l :-> f v ': RM f t class RowMapx (r :: [LT *]) where - rmap' :: Proxy f -> (forall a. a -> f a) -> Rec (R r) -> Rec (R (RM f r)) - rsequence' :: Applicative f => Proxy f -> Rec (R (RM f r)) -> f (Rec (R r)) + rmap' :: (forall a. a -> f a) -> Rec (R r) -> Rec (R (RM f r)) + rsequence' :: Applicative f => Rec (R (RM f r)) -> f (Rec (R r)) instance RowMapx '[] where - rmap' _ _ _ = empty - rsequence' _ _ = pure empty + rmap' _ _ = empty + rsequence' _ = pure empty instance (KnownSymbol l, RowMapx t) => RowMapx (l :-> v ': t) where - rmap' w f r = unsafeInjectFront l (f (r .! l)) (rmap' w f (r .- l)) + rmap' f r = unsafeInjectFront l (f (r .! l)) (rmap' f (r .- l)) where l = Label :: Label l - rsequence' w r = unsafeInjectFront l <$> r .! l <*> rsequence' w (r .- l) + rsequence' r = unsafeInjectFront l <$> r .! l <*> rsequence' (r .- l) where l = Label :: Label l instance Forall (R '[]) c where @@ -410,7 +410,7 @@ instance Forall (R '[]) c where eraseWithLabels _ _ _ = [] eraseToHashMap _ _ _ = M.empty eraseZip _ _ _ _ = [] - rmapc _ _ _ _ = empty + rmapc _ _ _ = empty instance (KnownSymbol l, Forall (R t) c, c a) => Forall (R (l :-> a ': t)) c where rinit c f = unsafeInjectFront l f (rinit c f) where l = Label :: Label l @@ -426,7 +426,8 @@ instance (KnownSymbol l, Forall (R t) c, c a) => Forall (R (l :-> a ': t)) c whe eraseZip c f x y = f (x .! l) (y .! l) : eraseZip c f (x .- l) (y .- l) where l = Label :: Label l - rmapc c w f r = unsafeInjectFront l (f (r .! l)) (rmapc c w f (r .- l)) + rmapc :: ∀ f . Proxy c -> (forall a. c a => a -> f a) -> Rec (R (l :-> a ': t)) -> Rec (Map f (R (l :-> a ': t))) + rmapc c f r = unsafeInjectFront l (f (r .! l)) (rmapc @_ @_ @f c f (r .- l)) where l = Label :: Label l show' :: (IsString s, Show a) => a -> s From 388c907a0a91af2cf6839f58727e389bd3551c3e Mon Sep 17 00:00:00 2001 From: M Farkas-Dyck Date: Mon, 2 Oct 2017 11:38:44 -0800 Subject: [PATCH 4/7] export `Map`, `RowMap` --- Data/OpenRecords.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/Data/OpenRecords.hs b/Data/OpenRecords.hs index 69476f9..34ee13f 100644 --- a/Data/OpenRecords.hs +++ b/Data/OpenRecords.hs @@ -50,6 +50,7 @@ module Data.OpenRecords -- * Row constraints (:\), Disjoint, Labels, Forall(..), -- * Row only operations + Map, RowMap (..), -- * Syntactic sugar RecOp(..), RowOp(..), (.|), (:|), -- * Labels From 5105151cf5bba571a55395338db40545e613bc1b Mon Sep 17 00:00:00 2001 From: M Farkas-Dyck Date: Mon, 2 Oct 2017 13:39:00 -0800 Subject: [PATCH 5/7] export `RowZip` --- Data/OpenRecords.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data/OpenRecords.hs b/Data/OpenRecords.hs index 34ee13f..a4991a4 100644 --- a/Data/OpenRecords.hs +++ b/Data/OpenRecords.hs @@ -50,7 +50,7 @@ module Data.OpenRecords -- * Row constraints (:\), Disjoint, Labels, Forall(..), -- * Row only operations - Map, RowMap (..), + Map, RowMap (..), RowZip (..), -- * Syntactic sugar RecOp(..), RowOp(..), (.|), (:|), -- * Labels From e303d1bbdc0316541202f4daa7b7d3f81a2b3fd4 Mon Sep 17 00:00:00 2001 From: M Farkas-Dyck Date: Mon, 2 Oct 2017 12:24:18 -0800 Subject: [PATCH 6/7] unmethodize `rmap` --- Data/OpenRecords.hs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Data/OpenRecords.hs b/Data/OpenRecords.hs index a4991a4..98d4cef 100644 --- a/Data/OpenRecords.hs +++ b/Data/OpenRecords.hs @@ -50,7 +50,7 @@ module Data.OpenRecords -- * Row constraints (:\), Disjoint, Labels, Forall(..), -- * Row only operations - Map, RowMap (..), RowZip (..), + Map, RowMap (..), RowZip (..), rmap, -- * Syntactic sugar RecOp(..), RowOp(..), (.|), (:|), -- * Labels @@ -377,14 +377,15 @@ class Forall (r :: Row *) (c :: * -> Constraint) where labels :: forall r s . (Forall r Unconstrained1, IsString s) => Proxy r -> [s] labels _ = getConst $ rinitAWithLabel @r (Proxy @Unconstrained1) (Const . pure . show') +rmap :: Forall r Unconstrained1 => (forall a. a -> f a) -> Rec r -> Rec (Map f r) +rmap = rmapc (Proxy @Unconstrained1) + type family Map (f :: a -> b) (r :: Row a) :: Row b where Map f (R r) = R (RM f r) class RowMap (r :: Row *) where - rmap :: (forall a. a -> f a) -> Rec r -> Rec (Map f r) rsequence :: Applicative f => Rec (Map f r) -> f (Rec r) instance RowMapx r => RowMap (R r) where - rmap = rmap' rsequence = rsequence' type family RM (f :: a -> b) (r :: [LT a]) :: [LT b] where @@ -392,16 +393,12 @@ type family RM (f :: a -> b) (r :: [LT a]) :: [LT b] where RM f (l :-> v ': t) = l :-> f v ': RM f t class RowMapx (r :: [LT *]) where - rmap' :: (forall a. a -> f a) -> Rec (R r) -> Rec (R (RM f r)) rsequence' :: Applicative f => Rec (R (RM f r)) -> f (Rec (R r)) instance RowMapx '[] where - rmap' _ _ = empty rsequence' _ = pure empty instance (KnownSymbol l, RowMapx t) => RowMapx (l :-> v ': t) where - rmap' f r = unsafeInjectFront l (f (r .! l)) (rmap' f (r .- l)) - where l = Label :: Label l rsequence' r = unsafeInjectFront l <$> r .! l <*> rsequence' (r .- l) where l = Label :: Label l From 55abb439469482dfe396a509a22ed5e83f7a8241 Mon Sep 17 00:00:00 2001 From: M Farkas-Dyck Date: Mon, 2 Oct 2017 12:31:08 -0800 Subject: [PATCH 7/7] define `rxform` --- Data/OpenRecords.hs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Data/OpenRecords.hs b/Data/OpenRecords.hs index 98d4cef..416513d 100644 --- a/Data/OpenRecords.hs +++ b/Data/OpenRecords.hs @@ -50,7 +50,7 @@ module Data.OpenRecords -- * Row constraints (:\), Disjoint, Labels, Forall(..), -- * Row only operations - Map, RowMap (..), RowZip (..), rmap, + Map, RowMap (..), RowZip (..), rmap, rxform, -- * Syntactic sugar RecOp(..), RowOp(..), (.|), (:|), -- * Labels @@ -373,6 +373,7 @@ class Forall (r :: Row *) (c :: * -> Constraint) where eraseZip :: Proxy c -> (forall a. c a => a -> a -> b) -> Rec r -> Rec r -> [b] rmapc :: Proxy c -> (forall a. c a => a -> f a) -> Rec r -> Rec (Map f r) + rxformc :: Proxy c -> (forall a. c a => f a -> g a) -> Rec (Map f r) -> Rec (Map g r) labels :: forall r s . (Forall r Unconstrained1, IsString s) => Proxy r -> [s] labels _ = getConst $ rinitAWithLabel @r (Proxy @Unconstrained1) (Const . pure . show') @@ -380,6 +381,9 @@ labels _ = getConst $ rinitAWithLabel @r (Proxy @Unconstrained1) (Const . pure . rmap :: Forall r Unconstrained1 => (forall a. a -> f a) -> Rec r -> Rec (Map f r) rmap = rmapc (Proxy @Unconstrained1) +rxform :: ∀ r f g . Forall r Unconstrained1 => (forall a. f a -> g a) -> Rec (Map f r) -> Rec (Map g r) +rxform = rxformc @r (Proxy @Unconstrained1) + type family Map (f :: a -> b) (r :: Row a) :: Row b where Map f (R r) = R (RM f r) class RowMap (r :: Row *) where @@ -409,6 +413,7 @@ instance Forall (R '[]) c where eraseToHashMap _ _ _ = M.empty eraseZip _ _ _ _ = [] rmapc _ _ _ = empty + rxformc _ _ _ = empty instance (KnownSymbol l, Forall (R t) c, c a) => Forall (R (l :-> a ': t)) c where rinit c f = unsafeInjectFront l f (rinit c f) where l = Label :: Label l @@ -428,6 +433,10 @@ instance (KnownSymbol l, Forall (R t) c, c a) => Forall (R (l :-> a ': t)) c whe rmapc c f r = unsafeInjectFront l (f (r .! l)) (rmapc @_ @_ @f c f (r .- l)) where l = Label :: Label l + rxformc :: ∀ f g . Proxy c -> (forall a. c a => f a -> g a) -> Rec (Map f (R (l :-> a ': t))) -> Rec (Map g (R (l :-> a ': t))) + rxformc c f r = unsafeInjectFront l (f (r .! l)) (rxformc @(R t) @_ @f @g c f (r .- l)) + where l = Label :: Label l + show' :: (IsString s, Show a) => a -> s show' = fromString . show