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

时间:2017-10-20 16:58:46

标签: idris theorem-proving

我想写一个带有两个自然参数的函数,并返回一个可能是它们相等的证明。

我正在尝试

equal : (a: Nat) -> (b: Nat) -> Maybe ((a == b) = True)
equal a b = case (a == b) of
    True => Just Refl
    False => Nothing

但是我收到以下错误

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

这是正确的方法吗?

此外,作为奖金问题,如果我做

equal : (a: Nat) -> (b: Nat) -> Maybe ((a == b) = True)
equal a b = case (a == b) of
    True => proof search
    False => Nothing

我得到了

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

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

2 个答案:

答案 0 :(得分:5)

让我们来看看Eq的{​​{1}}界面的实现:

Nat

您可以通过遵循Eq Nat where Z == Z = True (S l) == (S r) = l == r _ == _ = False 函数的结构来解决问题,如下所示:

(==)

答案 1 :(得分:4)

您可以使用with代替case(依赖模式匹配)来执行此操作:

equal : (a: Nat) -> (b: Nat) -> Maybe ((a == b) = True)
equal a b with (a == b)
  | True = Just Refl
  | False = Nothing

请注意,正如安东指出的那样,这仅仅是布尔测试结果的见证,是一个比适当平等更弱的主张。它可能有助于推进有关if a==b then ...的证明,但它不允许您将a替换为b