假设
type family F (x :: Nat) (y :: Nat) :: (Nat, Nat) where
F x y = '(x, y)
然后
:t Proxy :: Proxy '[ (F 1 2) ]
Proxy :: Proxy '[ (F 1 2) ] :: Proxy '['(1, 2)]
但现在假设
type family ZipWith (f :: a -> b -> c) (as :: [a]) (bs :: [b]) :: [c] where
ZipWith f (a ': as) (b ': bs) = (f a b) ': (ZipWith f as bs)
ZipWith f as bs = '[]
:t Proxy :: Proxy (ZipWith F ('[1,2]) ('[3,4]))
Proxy :: Proxy (ZipWith F ('[1,2]) ('[3,4])) :: Proxy '[F 1 3, F 2 4]
为什么不是类型
Proxy :: Proxy (ZipWith F ('[1,2]) ('[3,4])) :: Proxy '['(1, 3), '(2, 4)]
如果我说
:t (Proxy :: Proxy (ZipWith F ('[1,2]) ('[3,4]))) :: Proxy '[ '(1, 3), '(2, 4)]
然后我收到类型错误
Couldn't match type ‘F’ with ‘'(,)’
Expected type: Proxy (ZipWith F '[1, 2] '[3, 4])
Actual type: Proxy '['(1, 3), '(2, 4)]
In the expression:
(Proxy :: Proxy (ZipWith F ('[1, 2]) ('[3, 4]))) ::
Proxy '['(1, 3), '(2, 4)]
但是F是'(,)所以发生了什么?这与注入性有关吗?
编辑回应评论,这是一个真正的答案:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE PolyKinds #-}