haskell - Anonymous Type Functions -
this followup previous question: type-level map datakinds, starting 2 answers received.
my goal take hlist
of arbitrary types , turn list of related/derived types
type family typemap (a :: * -> *) (xs :: [*]) :: [*] type instance typemap t '[] = '[] type instance typemap t (x ': xs) = t x ': typemap t xs data hlist :: [*] -> * hnil :: hlist '[] hcons :: -> hlist -> hlist (a ': as)
when tried few types, ran problem. type-function " argument typemap has take hlist
element type last argument , return new type. works okay sometimes:
test :: hlist rqs -> hlist (typemap ((,) int) rqs) test hnil = hnil test (hcons x xs) = hcons (3,x) $ test xs
but if wanted switch order of tuple in definition of test? first attempt define type synonym:
type revinttup b = (b,int) test2 :: hlist rqs -> hlist (typemap revinttup rqs) test2 hnil = hnil test2 (hcons x xs) = hcons (x,3) $ test2 xs
but of course, you can't partially apply type synonyms, trick. there (other) way achieve this?
you should able write fliptypemap
... that's not composable. better choice here might type-level version of map ($ 2) (map (/) [1,2,3])
instead of map (flip (/) 2) [1,2,3]
taking advantage of -xpolykinds
:
type family typemap (a :: j -> k) (xs :: [j]) :: [k] type instance typemap t '[] = '[] type instance typemap t (x ': xs) = t x ': typemap t xs type family aplist (xs :: [j -> k]) (a :: j) :: [k] type instance aplist '[] t = '[] type instance aplist (x ': xs) t = x t ': aplist xs t test2 :: hlist rqs -> hlist (typemap (,) rqs `aplist` int) test2 hnil = hnil test2 (hcons x xs) = hcons (x,3) $ test2 xs
Comments
Post a Comment