유형이 [*]
인 유형 수준 목록이있는 공통 패턴이 있으며 목록의 각 요소에 유형이 * -> *
인 유형 생성자를 적용하고 싶습니다. 예를 들어 '[Int, Double, Integer]
을 '[Maybe Int, Maybe Double, Maybe Integer]
으로 변경하고 싶습니다.데이터 킨이있는 유형 수준 맵
여기 입력 수준 map
을 구현하려는 시도입니다.
{-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleInstances, FlexibleContexts, TypeOperators, DataKinds, ScopedTypeVariables, GADTs #-}
-- turns a type list '[b1, b2, b3]
-- into the type list '[a b1, a b2, a b3]
class TypeMap (a :: * -> *) (bs :: [*]) where
type Map a bs :: [*]
instance TypeMap a '[b] where
type Map a '[b] = '[a b]
instance TypeMap a (b1 ': b2 ': bs) where
type Map a (b1 ': b2 ': bs) = ((a b1) ': (Map a (b2 ': bs)))
data HList :: [*] -> * where
HNil :: HList '[]
HCons :: a -> HList as -> HList (a ': as)
class Foo as where
toLists :: HList as -> HList (Map [] as)
instance Foo '[a] where
toLists (HCons a HNil) = HCons [a] HNil
instance (Foo (a2 ': as)) => Foo (a1 ': a2 ': as) where
toLists (HCons a as) =
let as' = case (toLists as) of
(HCons a2 as'') -> HCons [head a2] as'' -- ERROR
in HCons [a] as'
이
Could not deduce (a3 ~ [t0])
from the context (Foo ((':) * a2 as))
bound by the instance declaration at Test.hs:35:10-50
or from ((':) * a1 ((':) * a2 as) ~ (':) * a as1)
bound by a pattern with constructor
HCons :: forall a (as :: [*]).
a -> HList as -> HList ((':) * a as),
in an equation for `toLists'
at Test.hs:36:14-23
or from (Map [] as1 ~ (':) * a3 as2)
bound by a pattern with constructor
HCons :: forall a (as :: [*]).
a -> HList as -> HList ((':) * a as),
in a case alternative
at Test.hs:38:22-34
`a3' is a rigid type variable bound by
a pattern with constructor
HCons :: forall a (as :: [*]).
a -> HList as -> HList ((':) * a as),
in a case alternative
at Test.hs:38:22
Expected type: HList (Map [] ((':) * a2 as))
Actual type: HList ((':) * [t0] as2)
In the return type of a call of `HCons'
In the expression: HCons [head a2] as''
In a case alternative: (HCons a2 as'') -> HCons [head a2] as''
내가 풍부한 유형 약어를 추가 해봤 오류가 발생하지만 오류가 더 많거나 적은 동일한 나오는 : GHC도 추측 할 수없는의 첫 번째 요소 HList는 (정상적인) 목록입니다. 내가 바보 같은 짓을하고 있니? 뭔가 불법인가? 아니면 주위에 어떤 방법이 있습니까?
왜 TypeMap [] 인스턴스가 없습니까? –
@DanielWagner 동의합니다. 해당 인스턴스는 '[]와 (a': as)에 대한 것일 것입니다. – crockeea