문제

Is there any way of defining a type level projection without using type families?

I do it like so:

{-# LANGUAGE TypeFamilies #-}
type family Project t :: *
type instance Project [r] = r ,

but I really only ever use one instance of it.

도움이 되었습니까?

해결책

You can use MultiParamTypeClass and FunctionalDependencies, though without knowing why you're using it it's hard to say if this is sufficient.

class Project k a | k -> a
instance Project [r] r

> :t undefined :: Project String r => r
undefined :: Project String r => r :: Char

다른 팁

I am not sure where you are going to use that exactly. But I tend to use type level projections only to satisfy the haskell type system. What I usually do is to define a function like this:

project :: [a] -> a
project = undefined

Now using project on an object of type [a] will give me an object of type a.

Another function which I use (although sometimes) along with the above is asTypeOf.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top