Proving if n = m and m = o, then n + m = m + o in Idris?

吃可爱长大的小学妹 提交于 2019-12-11 02:23:32

问题


I am trying to improve my Idris skill by looking at some of the exercises Software Foundations (originally for Coq, but I am hoping the translation to Idris not too bad). I am having trouble with the "Exercise: 1 star (plus_id_exercise)" which reads:

Remove "Admitted." and fill in the proof.

Theorem plus_id_exercise : ∀ n m o : nat,
  n = m → m = o → n + m = m + o.
Proof.
  (* FILL IN HERE *) Admitted.

I have translated to the following problem in Idris:

plusIdExercise : (n : Nat) ->
                 (m : Nat) ->
                 (o : Nat) ->
                 (n == m) = True ->
                 (m == o) = True ->
                 (n + m == m + o) = True

I am trying to perform a case by case analysis and I am having a lot of issues. The first case:

plusIdExercise Z Z Z n_eq_m n_eq_o = Refl

seems to work, but then I want to say for instance:

plusIdExercise (S n) Z Z n_eq_m n_eq_o = absurd

But this doesn't work and gives:

When checking right hand side of plusIdExercise with expected type
        S n + 0 == 0 + 0 = True

Type mismatch between
        t -> a (Type of absurd)
and
        False = True (Expected type)

Specifically:
        Type mismatch between
                \uv => t -> uv
        and
                (=) FalseUnification failure

I am trying to say this case can never happen because n == m, but Z (= m) is never the successor of any number (n). Is there anything I can do to fix this? Am I approaching this correctly? I am somewhat confused.


回答1:


I would argue that the translation is not entirely correct. The lemma stated in Coq does not use boolean equality on natural numbers, it uses the so-called propositional equality. In Coq you can ask the system to give you more information about things:

Coq < About "=".
eq : forall A : Type, A -> A -> Prop

The above means = (it is syntactic sugar for eq type) takes two arguments of some type A and produces a proposition, not a boolean value.

That means that a direct translation would be the following snippet

plusIdExercise : (n = m) -> (m = o) -> (n + m = m + o)
plusIdExercise Refl Refl = Refl

And when you pattern-match on values of the equality type, Idris essentially rewrites terms according to the corresponding equation (it's roughly equivalent to Coq's rewrite tactic).

By the way, you might find the Software Foundations in Idris project useful.



来源:https://stackoverflow.com/questions/50539825/proving-if-n-m-and-m-o-then-n-m-m-o-in-idris

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!