为什么没有!-notation(注释)在Idris REPL中工作?

时间:2017-03-23 21:19:12

标签: monads idris

我预计这会评估为3,但却出现了错误:

Idris> :let x = Just 2
Idris> 1 + !x
(input):1:3-4:When checking an application of function Prelude.Interfaces.+:
        Type mismatch between
                Integer (Type of (_bindApp0))
        and
                Maybe b (Expected type)

我也尝试过没有顶级绑定并得到

Idris> let y = Just 2 in !y + 1
Maybe b is not a numeric type

1 个答案:

答案 0 :(得分:4)

! - 符号desugars的问题。

当你写1 + !x时,这基本上意味着x >>= \x' => 1 + x'。而且这个表达式没有打字。

Idris> :let x = Just 2
Idris> x >>= \x' => 1 + x'
(input):1:16-17:When checking an application of function Prelude.Interfaces.+:
        Type mismatch between
                Integer (Type of x')
        and
                Maybe b (Expected type)

但这完美无缺:

Idris> x >>= \x' => pure (1 + x')
Just 3 : Maybe Integer

所以你应该添加pure来使事情有效:

Idris> pure (1 + !x)
Just 3 : Maybe Integer

Idris repl没什么特别的,它只是类型检查器的工作原理。这就是为什么 Idris 教程的pure函数中有m_add的原因:

m_add : Maybe Int -> Maybe Int -> Maybe Int
m_add x y = pure (!x + !y)