伊德里斯 - 证明两个数字相等

mar*_*osh 3 theorem-proving idris

我想编写一个函数,它接受两个自然参数,并返回一个可能的相等证明.

我正在尝试

equal : (a: Nat) -> (b: Nat) -> Maybe ((a == b) = True)
equal a b = case (a == b) of
    True => Just Refl
    False => Nothing
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误

When checking argument x to constructor Prelude.Maybe.Just:
        Type mismatch between
                True = True (Type of Refl)
        and
                Prelude.Nat.Nat implementation of Prelude.Interfaces.Eq, method == a
                                                                                   b =
                True (Expected type)

        Specifically:
                Type mismatch between
                        True
                and
                        Prelude.Nat.Nat implementation of Prelude.Interfaces.Eq, method == a
                                                                                           b
Run Code Online (Sandbox Code Playgroud)

这是正确的方法吗?

而且,作为奖金问题,如果我这样做

equal : (a: Nat) -> (b: Nat) -> Maybe ((a == b) = True)
equal a b = case (a == b) of
    True => proof search
    False => Nothing
Run Code Online (Sandbox Code Playgroud)

我明白了

INTERNAL ERROR: Proof done, nothing to run tactic on: Solve
pat {a_504} : Prelude.Nat.Nat. pat {b_505} : Prelude.Nat.Nat. Prelude.Maybe.Nothing (= Prelude.Bool.Bool Prelude.Bool.Bool (Prelude.Interfaces.Prelude.Nat.Nat implementation of Prelude.Interfaces.Eq, method == {a_504} {b_505}) Prelude.Bool.True)
This is probably a bug, or a missing error message.
Please consider reporting at https://github.com/idris-lang/Idris-dev/issues
Run Code Online (Sandbox Code Playgroud)

这是一个已知问题还是应该报告?

Ant*_*nov 5

我们来看看Eq接口的实现Nat:

Eq Nat where
  Z == Z         = True
  (S l) == (S r) = l == r
  _ == _         = False
Run Code Online (Sandbox Code Playgroud)

您可以通过遵循(==)函数的结构来解决问题,如下所示:

total
equal : (a: Nat) -> (b: Nat) -> Maybe ((a == b) = True)
equal Z Z = Just Refl
equal (S l) (S r) = equal l r
equal _ _ = Nothing
Run Code Online (Sandbox Code Playgroud)

  • `(==)`返回一个布尔值,因此它(原则上)可以一直返回`False`.我们怎么知道`a == b`需要`a = b`而不看`(==)`的实现?使用证明,要么您的函数必须具有足够强大的输出类型(如`decEq`)来传达其含义,或者您需要访问函数体. (2认同)