ram*_*ion 12 haskell type-families forall
我试图理解使用forall量化两个类型变量和使用forall量化元组类型的单个类型变量之间的区别.
例如,给定以下类型系列:
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE DataKinds #-}
type family Fst (p :: (a,b)) :: a where
Fst '(a,_) = a
type family Snd (p :: (a,b)) :: b where
Snd '(_,b) = b
type family Pair (p :: (Type,Type)) :: Type where
Pair '(a,b) = (a,b)
Run Code Online (Sandbox Code Playgroud)
我可以使用两个类型变量在对上定义一个标识,并让它在GHC 8.0.1上编译:
ex0 :: forall (a :: Type) (b :: Type). Pair '(a,b) -> (Fst '(a,b), Snd '(a,b))
ex0 = id
Run Code Online (Sandbox Code Playgroud)
如果我使用元组类型的单个类型变量,则相同的定义不会编译,但是:
ex1 :: forall (p :: (Type,Type)). Pair p -> (Fst p, Snd p)
ex1 = id
-- Ex.hs:20:7: error:
-- • Couldn't match type ‘Pair p’ with ‘(Fst p, Snd p)’
-- Expected type: Pair p -> (Fst p, Snd p)
-- Actual type: (Fst p, Snd p) -> (Fst p, Snd p)
-- • In the expression: id
-- In an equation for ‘ex1’: ex1 = id
-- • Relevant bindings include
-- ex1 :: Pair p -> (Fst p, Snd p) (bound at Ex.hs:20:1)
Run Code Online (Sandbox Code Playgroud)
问题p可能是?什么?
原因很简单,没有对类型级别进行 eta 转换检查。首先,没有机制可以将data定义与可能具有 eta 定律的单构造函数记录/产品区分开来。我不认为p存在\xe2\x8a\xa5是一个合理的理由。即使在部分惰性语言中,对的 eta 相等性也成立(相对于观察等价性)。
| 归档时间: |
|
| 查看次数: |
105 次 |
| 最近记录: |