gpt4 book ai didi

haskell - GHC.Prim 中有问题的代码

转载 作者:行者123 更新时间:2023-12-04 22:05:02 32 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





what is the meaning of "let x = x in x" and "data Float#" in GHC.Prim in Haskell

(2 个回答)


8年前关闭。




我在 GHC.Prim 中遇到了以下代码:

...

negateFloat# :: Float# -> Float#
negateFloat# = let x = x in x

-- |Truncates a @Float#@ value to the nearest @Int#@.
-- Results are undefined if the truncation if truncation yields
-- a value outside the range of @Int#@.

float2Int# :: Float# -> Int#
float2Int# = let x = x in x

expFloat# :: Float# -> Float#
expFloat# = let x = x in x

logFloat# :: Float# -> Float#
logFloat# = let x = x in x

...

尤其:
  • float2Int#甚至不应该输入检查
  • 这些函数都没有参数,因此似乎格式不正确
  • 这些函数都没有应用他们声称的
  • 的数学运算。
  • 所有函数都是“无限递归的”:let x = x

  • 我知道扩展名为“MagicHash”,但它不可能那么神奇。是什么赋予了?

    (我想我应该添加免责声明,我不知道 -XMagicHash 实际上做了什么,我只是假设它允许 # 语法,仅此而已。)

    最佳答案

    线索位于模块顶部:

    {-
    This is a generated file (generated by genprimopcode).
    It is not code to actually be used. Its only purpose is to be
    consumed by haddock.
    -}

    顺便说一句, let x = x in x相当于 undefined , 并且对任何数据类型都有效; x不受以自引用以外的任何方式定义的限制,因此可以是任何类型。

    这些函数是“原始的”,意思是操作非常基本,它们没有在 Haskell 代码中定义,很可能只是直接翻译成机器指令。

    关于haskell - GHC.Prim 中有问题的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21767336/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com