期待一种类型,但'我'有点'Nat'

时间:2016-09-23 22:41:19

标签: haskell types type-kinds

我想实现具有类型限制的解释大小的简单命题逻辑。但是somhow我无法使用natVal将类型级别的数字转换为值。示例代码:

import GHC.TypeLits
import Data.Proxy

newtype PropositionalVariable (interpretationSize) = PropositionalVariable {
  getVariable :: Int
} deriving (Show, Eq)

instance KnownNat i => Enum (PropositionalVariable i) where
  fromEnum = getVariable
  toEnum e | e <= 0 = error "Variable must be positive integer"
           | (toInteger e) > (natVal (Proxy :: Proxy i)) = error "Variable index overflow"
           | otherwise = PropositionalVariable e

给我错误,如:

• Expected a type, but ‘i’ has kind ‘Nat’
• In the first argument of ‘PropositionalVariable’, namely ‘i’
  In the first argument of ‘Enum’, namely ‘PropositionalVariable i’
  In the instance declaration for ‘Enum (PropositionalVariable i)’

在这种情况下,将类型级别整数转换为值的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

您需要打开PolyKindnds扩展名或给interpretationSize一种注释。