Idris是否有非终止条款?

时间:2018-05-28 03:03:42

标签: idris

关于官方伊德里斯维基的非官方常见问题解答(官方称其为语言' s git repo),它是stated that

  

总体语言[例如Idris]我们没有未定义和非终止条款   所以我们不用担心评估它们。

但是,ones的以下定义(使用List而不是Stream)肯定似乎没有终止:

ones: List Int
ones = 1 :: ones
-- ...
printLn(head ones) -- seg fault!

所以,我不确定wiki条目是否错误,或者我是否误解了上下文。请注意Stream解决方法已经described in the Idris tutorial

1 个答案:

答案 0 :(得分:5)

如果你认为伊德里斯是完全的,那么这只是总数。您可以编写%default total%default covering%default partial(默认值)之一,之后的所有声明都将采用给定的整体注释:

%default total

-- implicitly total
ones1 : List Int
ones1 = 1 :: ones1
-- ERROR: ones1 is not total

-- total
ZNeverHeardOfIt : Nat -> Nat
ZNeverHeardOfIt (S n) = n
-- ERROR: missing cases in ZNeverHeardOfIt

covering
natRoulette : Nat -> Nat
natRoulette Z = Z
natRoulette (S n) = natRoulette (S (S n))
-- covering means all possible inputs are covered by an equation
-- but the function isn't checked for termination
-- natRoulette has cases for all inputs, but it might go into an infinite loop
-- it's morally equivalent to just partial, as a function that loops forever
-- on an input isn’t very different from one missing the case
-- it just gets the compiler to complain more

partial
ones : List Int
ones = 1 :: ones
-- no checks at all
-- Idris, being strict, needs to evaluate ones strictly before it can evaluate ones.
-- Oh wait, that's impossible. Idris promptly vanishes in a segfault.