关于mod的Idris证明

时间:2018-10-03 02:49:02

标签: idris theorem-proving

我试图在Idris中编写有关以下基于减法的mod运算符的证明:

mod : (x, y : Nat) -> Not (y = Z) -> Nat
mod x Z p = void (p Refl)
mod x (S k) _ = if lt x (S k) then x else helper x (minus x (S k)) (S k)
  where total
        helper : Nat -> Nat -> Nat -> Nat
        helper Z x y = x
        helper (S k) x y = if lt x y then x else helper k (minus x y) y

我想证明的定理是,上面“ mod”产生的余数总是小于分频器。即

mod_prop : (x, y : Nat) -> (p : Not (y=0))-> LT (mod x y p) y

我构造了一个证明,但被最后一个洞卡住了。我的完整代码粘贴在下面

mod : (x, y : Nat) -> Not (y = Z) -> Nat
mod x Z p = void (p Refl)
mod x (S k) _ = if lt x (S k) then x else helper x (minus x (S k)) (S k)
  where total
        helper : Nat -> Nat -> Nat -> Nat
        helper Z x y = x
        helper (S k) x y = if lt x y then x else helper k (minus x y) y

lteZK : LTE Z k
lteZK {k = Z} = LTEZero
lteZK {k = (S k)} = let ih = lteZK {k=k} in
                    lteSuccRight {n=Z} {m=k} ih 

lte2LTE_True : True = lte a b -> LTE a b
lte2LTE_True {a = Z} prf = lteZK
lte2LTE_True {a = (S _)} {b = Z} Refl impossible
lte2LTE_True {a = (S k)} {b = (S j)} prf = 
  let ih = lte2LTE_True {a=k} {b=j} prf in LTESucc ih


lte2LTE_False : False = lte a b -> GT a b
lte2LTE_False {a = Z} Refl impossible
lte2LTE_False {a = (S k)} {b = Z} prf = LTESucc lteZK
lte2LTE_False {a = (S k)} {b = (S j)} prf = 
  let ih = lte2LTE_False {a=k} {b=j} prf in (LTESucc ih)

total
mod_prop : (x, y : Nat) -> (p : Not (y=0))-> LT (mod x y p) y
mod_prop x Z p = void (p Refl)
mod_prop x (S k) p with (lte x k) proof lxk
  mod_prop x (S k) p | True  = LTESucc (lte2LTE_True lxk)
  mod_prop Z (S k) p | False = LTESucc lteZK 
  mod_prop (S x) (S k) p | False with (lte (minus x k) k)  proof lxk'
    mod_prop (S x) (S k) p | False | True = LTESucc (lte2LTE_True lxk')
    mod_prop (S x) (S Z) p | False | False  = LTESucc ?hole

运行类型检查器后,该孔的描述如下:

- + Main.hole [P]
 `--          x : Nat
              p : (1 = 0) -> Void
            lxk : False = lte (S x) 0
           lxk' : False = lte (minus x 0) 0
     --------------------------------------------------------------------------
      Main.hole : LTE (Main.mod, helper (S x) 0 p x (minus (minus x 0) 1) 1) 0

我不熟悉 idris-holes 窗口中给出的Main.mod, helper (S x) 0 p x (minus (minus x 0) 1) 1的语法。我猜(S x) 0 p是“ mod”的三个参数,而(minus (minus x 0) 1) 1是“ mod”的本地“ helper”功能的三个参数?

似乎是时候利用归纳假设了。但是如何使用归纳法完成证明呢?

1 个答案:

答案 0 :(得分:1)

(Main.mod, helper (S x) 0 p x (minus (minus x 0) 1) 1)

可以读为

  • Main.mod, helper-helper函数的限定名称,在where函数的mod子句中定义(Main是模块名称) ;
  • mod的参数也传递给helper-(S x)0p(请参阅docs):
  

在外部作用域中可见的任何名称也将在   where子句(除非已重新定义它们,例如xs)。一种   仅出现在类型中的名称将在范围内   子句,如果它是其中一种类型的参数,即它是固定的   整个结构。

  • helper本身的参数-x(minus (minus x 0) 1)1

下面还提供了mod的另一种实现方式,该方式将Fin n类型用于除以n的余数。事实证明,这样做更容易,因为Fin n的任何值总是小于n

import Data.Fin

%default total


mod' : (x, y : Nat) -> {auto ok: GT y Z} -> Fin y
mod' Z (S _) = FZ
mod' (S x) (S y) with (strengthen $ mod' x (S y))
    | Left _ = FZ
    | Right rem = FS rem

mod : (x, y : Nat) -> {auto ok: GT y Z} -> Nat
mod x y = finToNat $ mod' x y

finLessThanBound : (f : Fin n) -> LT (finToNat f) n
finLessThanBound FZ = LTESucc LTEZero
finLessThanBound (FS f) = LTESucc (finLessThanBound f)

mod_prop : (x, y : Nat) -> {auto ok: GT y Z} -> LT (mod x y) y
mod_prop x y = finLessThanBound (mod' x y)

为方便起见,我使用auto implicits来证明y > 0