Haskell 预处理器中的 % 是什么?它与 Uint8 等类型有何关系(如果有的话)?

What is % in the Haskell preprocessor and how does it relate to types like Uint8 (if at all)?

我想从总体上更好地了解 following code

代码段中包含 MDouble 的实例以演示不使用预处理器的示例。

#let numarray t = "\
foreign import ccall unsafe mxIs%s :: MXArrayPtr -> IO CBool\n\
instance MXArrayComponent M%s where\n\
  isMXArray a = boolC =.< withMXArray a mxIs%s\n\
  createMXArray s = withNDims s (uncurry $ createNumericArray (mxClassOf (undefined :: M%s)) False) >>= mkMXArray\n\
  \
mxArrayGetOffset = arrayDataGet ;\
mxArraySetOffset = arrayDataSet ;\
mxArrayGetOffsetList = arrayDataGetList ;\
mxArraySetOffsetList = arrayDataSetList\
  \n\
instance MXArrayData MX%s M%s\
", #t, #t, #t, #t, #t, #t
foreign import ccall unsafe mxIsDouble :: MXArrayPtr -> IO CBool
foreign import ccall unsafe mxCreateDoubleScalar :: MXDouble -> IO MXArrayPtr
foreign import ccall unsafe mxGetScalar :: MXArrayPtr -> IO MXDouble
instance MXArrayComponent MDouble where
  isMXArray a = boolC =.< withMXArray a mxIsDouble
  createMXScalar = mxCreateDoubleScalar . hs2mx >=> mkMXArray
  mxScalarGet a = withMXArray a mxGetScalar
  createMXArray s = withNDims s (uncurry $ createNumericArray (mxClassOf (undefined :: Double)) False) >>= mkMXArray
  #arrayDataComponent
instance MXArrayData MXDouble MDouble
#numarray Single
#numarray Int8
#numarray Int16
#numarray Int32
#numarray Int64
#numarray Uint8
#numarray Uint16
#numarray Uint32
#numarray Uint64

但特别是,看起来他们正在使用某些类型,例如 Uint8 等,我不确定这些定义在哪里,或者为什么,例如Word8 未被使用。另外,不确定 %s 是如何使用的。我在 hsc2hs docs 中看到很多对 # 的引用,但到目前为止还没有提到 %.

关键是链接文档中加粗的句子:

#let ⟨name⟩ ⟨parameters⟩ = "⟨definition⟩"

Defines a macro to be applied to the Haskell source. Parameter names are comma-separated, not inside parens. Such macro is invoked as other #-constructs, starting with #name. The definition will be put in the C program inside parens as arguments of printf. To refer to a parameter, close the quote, put a parameter name and open the quote again, to let C string literals concatenate. Or use printf's format directives. Values of arguments must be given as strings, unless the macro stringifies them itself using the C preprocessor's #parameter syntax.

%sprintf 中指定了一个字符串参数(您可以看到更完整的解释,例如 here)。

所以不涉及 Uint8 类型;它只是一个字符串,当你将它代入 numarray 时,你会得到

foreign import ccall unsafe mxIsUint8 :: MXArrayPtr -> IO CBool
instance MXArrayComponent MUint8 where
  ...

并且记录了这种类型 here:

type MUint8 = Word8