haskell 中的亲切级别身份
kind level identity in haskell
是否可以构建一个种类级别的标识,将种类 a
映射到 haskell 中的同类 a
?
以下是一些未回答的问题:
type One :: * -> *
newtype One a = One {unOne :: a}
-- >>> :kind! 'One
-- 'One :: a -> One a -- not the identity
-- = 'One
type family Id a where -- *pointwise* identity, not a function (can't not apply it)
Id a = a
目前类型语言是first-order,意思是像Id
这样的无法匹配的函数必须完全饱和:Higher-Order Type-Level Programming in Haskell.
已接受补救建议:Unsaturated type families。
在这个提案下我们区分了One :: Type -> Type
和Id :: k -> k
的种类; One
可匹配,Id
不可匹配:
One :: Type -> @M Type
Id :: k -> @U k
我们将能够传递无与伦比的 type-level 函数作为参数:
data Foo :: (Type -> @U Type) -> Type
data Foo f = MkFoo (f Int) (f Bool) (f Char)
foo :: Foo Id
foo = MkFoo 10 False 'a'
这将允许您构造适当的恒等函子、函子组合和分类小工具。
type Functor :: (s -> @m t) -> Constraint
class (Category (Source f), Category (Target f))
=> Functor (f :: s -> @m t) where
type Source (f :: s -> @m t) :: Cat s
type Target (f :: s -> @m t) :: Cat t
fmap :: Source f a a' -> Target f (f a) (f a')
type IdFunctor :: Cat ob -> ob -> ob
type IdFunctor cat a = a
instance Category @ob cat => Functor (IdFunctor @ob cat) where
type Source (IdFunctor cat) = cat
type Target (IdFunctor cat) = cat
fmap :: cat a a' -> cat a a'
fmap = id
是否可以构建一个种类级别的标识,将种类 a
映射到 haskell 中的同类 a
?
以下是一些未回答的问题:
type One :: * -> *
newtype One a = One {unOne :: a}
-- >>> :kind! 'One
-- 'One :: a -> One a -- not the identity
-- = 'One
type family Id a where -- *pointwise* identity, not a function (can't not apply it)
Id a = a
目前类型语言是first-order,意思是像Id
这样的无法匹配的函数必须完全饱和:Higher-Order Type-Level Programming in Haskell.
已接受补救建议:Unsaturated type families。
在这个提案下我们区分了One :: Type -> Type
和Id :: k -> k
的种类; One
可匹配,Id
不可匹配:
One :: Type -> @M Type
Id :: k -> @U k
我们将能够传递无与伦比的 type-level 函数作为参数:
data Foo :: (Type -> @U Type) -> Type
data Foo f = MkFoo (f Int) (f Bool) (f Char)
foo :: Foo Id
foo = MkFoo 10 False 'a'
这将允许您构造适当的恒等函子、函子组合和分类小工具。
type Functor :: (s -> @m t) -> Constraint
class (Category (Source f), Category (Target f))
=> Functor (f :: s -> @m t) where
type Source (f :: s -> @m t) :: Cat s
type Target (f :: s -> @m t) :: Cat t
fmap :: Source f a a' -> Target f (f a) (f a')
type IdFunctor :: Cat ob -> ob -> ob
type IdFunctor cat a = a
instance Category @ob cat => Functor (IdFunctor @ob cat) where
type Source (IdFunctor cat) = cat
type Target (IdFunctor cat) = cat
fmap :: cat a a' -> cat a a'
fmap = id