关于证明:Coq中有限图的相等性(使用map2定义)

Equality of finite maps in coq (defined using map2)

假设我想在Coq中定义一种Monomials。 这些将是从一些有序的变量集到nat的有限映射,其中x2y3由将x发送到2,y发送到3,其他所有值都为默认值零的映射表示。

基本定义似乎并不难:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Require Import
  Coq.FSets.FMapFacts
  Coq.FSets.FMapList
  Coq.Structures.OrderedType.

Module Monomial (K : OrderedType).
  Module M := FMapList.Make(K).

  Module P := WProperties_fun K M.
  Module F := P.F.

  Definition Var : Type := M.key.
  Definition Monomial : Type := M.t nat.

  Definition mon_one : Monomial := M.empty _.

  Definition add_at (a : option nat) (b : option nat) : option nat :=
    match a, b with
      | Some aa, Some bb => Some (aa + bb)
      | Some aa, None => Some aa
      | None, Some bb => Some bb
      | None, None => None
    end.

  Definition mon_times (M : Monomial) (M' : Monomial) : Monomial :=
    M.map2 add_at M M'.

End Monomial.

在这一点上,我想证明一下:

1
Lemma mon_times_comm : forall M M', mon_times M M' = mon_times M' M.

我可以看到如何使用引理Equal_mapsto_iff证明这两个映射是Equal,但是我真的想说我的类型确实表示单项式,并且乘法是真正可交换的(映射是eq)。 。

我对Coq相当陌生:这是要证明的合理选择吗?

另外,我意识到这可能取决于有限映射的实现:如果FMapList是错误的选择,而另一种实现使此操作更容易,请指出这一点!


I can see how to prove that the two maps are Equal using the lemma Equal_mapsto_iff, but I'd really like to say that my type really represents monomials and that multiplication is genuinely commutative (and the maps are eq).

I'm pretty new to Coq: is this a reasonable thing to try to prove?

Also, I realise that this might depend on the finite map implementation: if FMapList was the wrong choice and another implementation makes this easier, please point me at that!

确实,您在正确的道路上。您使用的集合类型不具有以下属性:在Coq中,两个具有相同元素的集合在定义上相等。由于这些集合被实现为二进制树,因此您可能具有Node(A, Node(B,C)) <> Node(Node(A,B),C)

特别是,由于几个问题,在Coq中拥有良好的"集合类型"是一项极富挑战性的任务,有关更多讨论,请参见anwser如何在coq中定义集合而不将集合定义为元素列表。

进行适当的代数确实确实需要很多复杂的基础架构,@ErikMD的指针是正确的指针,您应该查看math-comp和相关论文以了解最新技术。当然,继续尝试!


关于Coq中单项式和多元多项式的形式化,可以考虑使用多项式库。在OPAM上可用:

1
$ opam install coq-mathcomp-multinomials

它自然证明与mon_times_comm引理类似的结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
From mathcomp Require Import ssreflect ssrfun ssrbool eqtype ssrnat seq.
From mathcomp Require Import choice finfun tuple fintype ssralg bigop.
From SsrMultinomials Require Import freeg mpoly.

Lemma test1 (n : nat) (m1 m2 : 'X_{1..n}) : (m1 + m2 = m2 + m1)%MM.
Proof.
move=> *.
by rewrite addmC.
Qed.

Lemma test2 (n : nat) (R : comRingType) (p q : {mpoly R[n]}) :
  (p * q = q * p)%R.
Proof.
move=> *.
by rewrite mpoly_mulC.
Qed.

请注意,多项式库是基于MathComp库构建的,该库与Coq证明语言的SSReflect扩展密切相关。

最后,请注意,该库对于开发涉及多项式多项式的Coq证明非常方便,但不允许直接使用这些Coq数据类型(Eval vm_compute in ...)进行计算。如果您对此方面也有兴趣,则可能还需要看一下CoqEAL库(尤其是依赖于FMaps的multipoly.v理论)。