在IDRIS 1中应用已知证据

发布于 01-18 04:34 字数 1752 浏览 6 评论 0原文

我试图通过练习来熟悉 Idris1 中的定理证明,但遇到了麻烦。

假设我有以下自然数的定义和我想要证明的以下定理:

data Natural = Z | S Natural

plus : Natural -> Natural -> Natural
plus x Z = x
plus x (S y) = S (plus x y)

succBoth : {a : Natural} -> {b : Natural} -> (a = b) -> (S a = S b)
succBoth = ?succBothProof

plusZero : (y : Natural) -> plus Z y = y
plusZero = ?plusZeroProof

plusSwitch : (x : Natural) -> (y : Natural) -> plus (S x) y = S (plus x y)
plusSwitch = ?plusSwitchProof

plusComm : (x : Natural) -> (y : Natural) -> plus x y = plus y x
plusComm = ?plusCommProof

我已经写好了前三个的证明。现在,当我想证明最后一个定理时,我遇到了应用早期证明的必要性。

Idris> :l Peano.idr
Holes: Peano.plusCommProof
*Peano> :elab plusCommProof
-Peano.plusCommProof> intro `{{x}}
...
-Peano.plusCommProof> intro `{{y}}
...
-Peano.plusCommProof> induction (Var `{{y}})
...
-Peano.plusCommProof> compute
...
-Peano.plusCommProof> attack
----------              Other goals:              ----------
{Z_103},{S_104}
----------              Assumptions:              ----------
 x : Natural
 y : Natural
----------                 Goal:                  ----------
{hole_7} : x = plus Z x

在这个阶段应用 plusZero 是很自然的事情,但我在尝试这样做时遇到了问题。我尝试通过 rewriteWith 应用它,请记住 plusZero 采用 Natural 类型参数。我尝试为其提供 x 变量,认为它能够从假设中推断出其 Natural 类型,但运气不佳:

-Peano.plusCommProof> rewriteWith `(plusZero (Var `{{x}}))
(input):1:15-35:When checking argument y to function Peano.plusZero:
        Type mismatch between
                Raw (Type of Var _)
        and
                Natural (Expected type)

How does one "cast" the < code>Raw 变量转换为上下文中的类型?

I am trying to get some familiarity with theorem proving in Idris1 by exercise and am running into trouble.

Suppose I have the following definition for naturals and the following theorems that I want to prove:

data Natural = Z | S Natural

plus : Natural -> Natural -> Natural
plus x Z = x
plus x (S y) = S (plus x y)

succBoth : {a : Natural} -> {b : Natural} -> (a = b) -> (S a = S b)
succBoth = ?succBothProof

plusZero : (y : Natural) -> plus Z y = y
plusZero = ?plusZeroProof

plusSwitch : (x : Natural) -> (y : Natural) -> plus (S x) y = S (plus x y)
plusSwitch = ?plusSwitchProof

plusComm : (x : Natural) -> (y : Natural) -> plus x y = plus y x
plusComm = ?plusCommProof

I already have written proofs for the first three. Now, when I want to prove the last theorem, I run into necessity of applying an earlier proof.

Idris> :l Peano.idr
Holes: Peano.plusCommProof
*Peano> :elab plusCommProof
-Peano.plusCommProof> intro `{{x}}
...
-Peano.plusCommProof> intro `{{y}}
...
-Peano.plusCommProof> induction (Var `{{y}})
...
-Peano.plusCommProof> compute
...
-Peano.plusCommProof> attack
----------              Other goals:              ----------
{Z_103},{S_104}
----------              Assumptions:              ----------
 x : Natural
 y : Natural
----------                 Goal:                  ----------
{hole_7} : x = plus Z x

It would be natural to apply plusZero at this stage, but I run into issues trying to do that. I try to apply it via rewriteWith, keeping in mind that plusZero takes a Natural type argument. I try to supply it with the x variable, thinking that it will be able to infer its Natural type from assumptions, but no luck:

-Peano.plusCommProof> rewriteWith `(plusZero (Var `{{x}}))
(input):1:15-35:When checking argument y to function Peano.plusZero:
        Type mismatch between
                Raw (Type of Var _)
        and
                Natural (Expected type)

How does one "cast" the Raw variable into its type in context?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨笨の傻瓜2025-01-25 04:34:37

我无法让IDRIS 1版本使用,但是我确实安装了IDRIS 2,并以其样式编写了证明。

module Peano

data Natural = Zero | Succ Natural

plus : Natural -> Natural -> Natural
plus x Zero = x
plus x (Succ y) = Succ (plus x y)

succBoth : {a : Natural} -> {b : Natural} -> (a = b) -> (Succ a = Succ b)
succBoth rfl = cong (\ a => Succ a) rfl

plusZero : (y : Natural) -> plus Zero y = y
plusZero Zero = Refl
plusZero (Succ y)
  = let assumption = plusZero y in
        rewrite assumption in Refl

plusSwitch : (x : Natural) -> (y : Natural) ->
             plus (Succ x) y = Succ (plus x y)
plusSwitch x Zero = Refl
plusSwitch x (Succ y)
  = let assumption = plusSwitch x y in
        rewrite assumption in Refl

plusComm : (x : Natural) -> (y : Natural) -> plus x y = plus y x
plusComm x Zero = rewrite plusZero x in Refl
plusComm x (Succ y)
  = let assumption = plusComm x y in
        rewrite plusSwitch y x in
        rewrite assumption in Refl

诚然,更紧凑,但我更喜欢IDRIS 1:ELAB风格而不是可读性

I couldn't get the Idris 1 version to work but I did install Idris 2 and wrote the proofs in its style instead.

module Peano

data Natural = Zero | Succ Natural

plus : Natural -> Natural -> Natural
plus x Zero = x
plus x (Succ y) = Succ (plus x y)

succBoth : {a : Natural} -> {b : Natural} -> (a = b) -> (Succ a = Succ b)
succBoth rfl = cong (\ a => Succ a) rfl

plusZero : (y : Natural) -> plus Zero y = y
plusZero Zero = Refl
plusZero (Succ y)
  = let assumption = plusZero y in
        rewrite assumption in Refl

plusSwitch : (x : Natural) -> (y : Natural) ->
             plus (Succ x) y = Succ (plus x y)
plusSwitch x Zero = Refl
plusSwitch x (Succ y)
  = let assumption = plusSwitch x y in
        rewrite assumption in Refl

plusComm : (x : Natural) -> (y : Natural) -> plus x y = plus y x
plusComm x Zero = rewrite plusZero x in Refl
plusComm x (Succ y)
  = let assumption = plusComm x y in
        rewrite plusSwitch y x in
        rewrite assumption in Refl

Admittedly much more compact but I prefer the Idris 1 :elab style for readability

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文