가정하자 나는 어떤 종류의 [*]
, 유형의 목록이 있습니다 그래서 지금까지다형성 함수는
type family Tupled (ts :: [*]) z :: *
type instance Tupled (t ': ts) z = (t, Tupled ts z)
type instance Tupled '[] z = z
:
let Ts = '[Int, Bool, Char]
내가 튜플의 체인이를 변환 할 좋은 :
> :kind! Tupled Ts()
Tupled Ts() :: *
= (Int, (Bool, (Char,())))
는 지금은 기능을 표현하는 유형 Fun
을 쓸 수 있도록하고 싶습니다 그 이 사슬의 "바닥"에 다형성이있다.
(Int, (Bool, (Char, (String,()))))
(Int, (Bool, (Char, (Word, (ByteString,())))))
내가이 시도 : 예를 들어, Fun Ts Ts
는 이러한 유형 중 하나에 작업을해야
newtype Fun as bs = Fun
{ unfun :: forall z. Tupled as z -> Tupled bs z }
를하지만 유형 체킹 실패 : 내가 사용하는 권장 사항을 본 적이
Couldn't match type ‘Tupled bs z’ with ‘Tupled bs z0’
NB: ‘Tupled’ is a type function, and may not be injective
The type variable ‘z0’ is ambiguous
Expected type: Tupled as z -> Tupled bs z
Actual type: Tupled as z0 -> Tupled bs z0
In the ambiguity check for the type of the constructor ‘Fun’:
Fun :: forall z. Tupled as z -> Tupled bs z
To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
In the definition of data constructor ‘Fun’
In the newtype declaration for ‘Fun’
분사 성 문제를 피하는 데이터 패밀리 :
data family Tupled (ts :: [*]) z :: *
data instance Tupled (t ': ts) z = Cons t (Tupled ts z)
data instance Tupled '[] z = Nil
Fun $ \ (i, (b, (c, z))) -> (succ i, (not b, (pred c, z)))
내가 해결할 수 있습니다
:Fun
컴파일하게,하지만이 같은 튜플 작업을 찾고 있어요 때
Cons
및
Nil
의 땅에서 그 날을 얻는다 같이 "붙어"보이는
그리고 실제로 어떻게 든이?
왜 'type fun as bs = forall z? z로 연결됨 -> 합쳐서 bs z'? –
@ Li-yaoXia : 'Fun'을 'Fun'에 인수로 전달할 수 있기를 원하기 때문에 impredicativity를 숨기려면 'newtype'이 필요합니다. –