【发布时间】:2011-12-26 18:07:00
【问题描述】:
我将 GHCi 7.0.3 与以下实现类型级列表的程序一起使用:
{-# LANGUAGE TypeOperators #-}
data True
data False
-- List
data Nil
data Cons x xs
-- Type-level infix operator must begin with ':'
data x ::: xs
infixr 5 ::: -- set precedence level to 5 (tight)
它可以编译,但是当我测试它时:
:t (undefined :: True:::Nil)
(当转换为 True:::Nil 类型时,undefined 的类型是什么?)我收到此错误:
Illegal operator `:::' in type `True ::: Nil'
Use -XTypeOperators to allow operators in types
确实,当我使用标志启动 GHCi 时
-XTypeOperators
我得到了预期的结果:
(undefined :: True ::: Nil) :: True ::: Nil
我的问题是:为什么等效的 pragma 不起作用:
{-# LANGUAGE TypeOperators #-}
编辑:如果编译指示没有扩展到 GHCi 环境,那么我还有另一个难题。我试过这个程序:
class And b1 b2 b | b1 b2 -> b where
andf :: b1 -> b2 -> b
-- truth table
instance And True True True where andf = undefined
instance And True False False where andf = undefined
instance And False True False where andf = undefined
instance And False False False where andf = undefined
它需要以下编译指示:
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
但是一旦编译,我可以在 GHCi 中使用它:
*Main> :t andf (undefined::True) (undefined::False)
andf (undefined::True) (undefined::False) :: False
我猜在列表的情况下,解释器甚至无法使用类型级运算符::: 解析表达式,而在多参数类的情况下,命令行是可解析的。但是,想想看,GHCi 使用多参数类和函数依赖执行类型推断,不是吗?这种类型推断是在 GHCi 中完成的,而不是通过在编译代码中调用某些函数来完成的,对吧?
【问题讨论】: