on master help sign in
Error if type has less parameters than required by 'param'
commit383f44106355cdb50f4b09b264df75ca39812cb5 authorCsongor Kiss <kiss.csongor.kiss@gmail.com> dateThu 8 Mar 2018, 21:09 (9 years ago) parent543c707
2 files changed+23−1
src/Data/Generics/Internal/Families/Changing.hs+5 src/Data/Generics/Product/Param.hs+18−1
src/Data/Generics/Internal/Families/Changing.hs+5
@@ -14,6 +14,7 @@ module Data.Generics.Internal.Families.Changing
1414 , PTag (..)
1515 , P
1616 , LookupParam
17+ , ArgCount
1718 ) where
1819
1920import GHC.TypeLits (Nat, type (-), type (+), TypeError, ErrorMessage (..))
@@ -118,6 +119,10 @@ type family IfEq (a :: k) (b :: k) (t :: l) (f :: l) :: l where
118119 IfEq a a t _ = t
119120 IfEq _ _ _ f = f
120121
122+type family ArgCount (t :: k) :: Nat where
123+ ArgCount (f a) = 1 + ArgCount f
124+ ArgCount a = 0
125+
121126-- Note [CPP in instance constraints]
122127-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123128--
src/Data/Generics/Product/Param.hs+18−1
@@ -1,3 +1,4 @@
1+{-# LANGUAGE TypeFamilies #-}
12{-# LANGUAGE AllowAmbiguousTypes #-}
23{-# LANGUAGE DataKinds #-}
34{-# LANGUAGE FlexibleContexts #-}
@@ -28,10 +29,11 @@ module Data.Generics.Product.Param
2829 , HasParam (..)
2930 ) where
3031
31−import GHC.TypeLits (Nat)
32+import GHC.TypeLits
3233import Data.Generics.Internal.Void
3334import Data.Generics.Internal.Families.Changing
3435import Data.Generics.Internal.VL.Traversal
36+import Data.Generics.Internal.VL.Lens
3537
3638import GHC.Generics
3739import Data.Kind
@@ -47,12 +49,27 @@ instance
4749 -- TODO: merge the old 'Changing' code with 'GenericN'
4850 , s ~ Infer t (P n b 'PTag) a
4951 , t ~ Infer s (P n a 'PTag) b
52+ , Error ((ArgCount s) <=? n) n (ArgCount s) s
5053 , GHasParam n (RepN s) (RepN t) a b
5154 ) => HasParam n s t a b where
5255
5356 param = confusing (\f s -> toN <$> gparam @n f (fromN s))
5457 {-# INLINE param #-}
5558
59+type family Error (b :: Bool) (expected :: Nat) (actual :: Nat) (s :: Type) :: Constraint where
60+ Error 'False _ _ _
61+ = ()
62+
63+ Error 'True expected actual typ
64+ = TypeError
65+ ( 'Text "Expected a type with at least "
66+ ':<>: 'ShowType (expected + 1)
67+ ':<>: 'Text " parameters, but "
68+ ':$$: 'ShowType typ
69+ ':<>: 'Text " only has "
70+ ':<>: 'ShowType actual
71+ )
72+
5673instance {-# OVERLAPPING #-} HasParam p (Void1 a) (Void1 b) a b where
5774 param = undefined
5875