需要一个类型,但“i”有一个类型“Nat”

Expected a type, but ‘i’ has kind ‘Nat’

我想用解释的类型限制大小来实现简单的命题逻辑。但是不知何故,我无法使用 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)’

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

您需要打开 PolyKinds 扩展程序或给 interpretationSize 一个善意的注释。