Type-level Map with DataKinds

萝らか妹 提交于 2019-12-01 11:44:17
aavogt

When you wrote TypeMap a (b1 ': b2 ': bs), that isn't consistent with the recursion you did to define Map... which only leads to an error when you try to TypeMap lists that aren't 1 or 2 elements long. Also, it's cleaner in your case to just have a type family for this.

type family TypeMap (a :: * -> *) (xs :: [*]) :: [*]
type instance TypeMap t '[] = '[]
type instance TypeMap t (x ': xs) = t x ': TypeMap t xs

Note this is pretty much a direct translation of:

map f [] = []
map f (x:xs) = f x : map f xs

The minimal change that makes your code compile is to change your instances for [a] and b1:b2:bs into instances for [] and b:bs.

instance TypeMap a '[] where
    type Map a '[] = '[]

instance TypeMap a (b ': bs) where
    type Map a (b ': bs) = a b ': Map a bs
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!