function - 了解偏函数的输出

标签 function idris

给定以下部分函数(Nothing 输入没有输出):

f : Maybe Int -> Maybe Int
f (Just 42) = Just 42

REPL 显示以下内容:

*Lecture> f $ Just 42
Just 42 : Maybe Int

*Lecture> f Nothing
f Nothing : Maybe Int

f Nothing 的输出含义是什么?

最佳答案

Idris 不会减少涉及调用不匹配模式的部分函数的表达式。换句话说,这只是 REPL 呈现未定义或“底部”值的方式。据推测,如果您在可执行文件中进行该调用,那么您将收到运行时错误。

来自the tutorial :

And although [a partial function] typechecks and compiles, it will not reduce (that is, evaluation of the function will cause it to change):

-- Unsafe head example!
unsafeHead : List a -> a
unsafeHead (x::xs) = x

unsafe> the Integer $ unsafeHead [1, 2, 3]
1 : Integer
unsafe> the Integer $ unsafeHead []
unsafeHead [] : Integer

关于function - 了解偏函数的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42477227/

相关文章:

lazy-evaluation - 我可以在 Idris 中证明 (s : Stream a) -> (head s::tail s = s) 吗?

javascript - X 单击后如何禁用按钮?

函数内的 Javascript Post

python - 如何将所有被调用的函数参数变成一个(Python 3)

postgresql - 在 PostgreSQL 的 SQL 函数中将元素转换为用户定义的类型

idris - 在接口(interface)中约束函数参数

bash - 在 bash 中调用自定义函数导致 "Command not found"

idris - 在 Idris 中生成库而不是可执行文件?

dependent-type - idris 的实际例子

syntax-error - idris 接口(interface)语法