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. |
我可以看到如何使用引理
我对Coq相当陌生:这是要证明的合理选择吗?
另外,我意识到这可能取决于有限映射的实现:如果
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中,两个具有相同元素的集合在定义上相等。由于这些集合被实现为二进制树,因此您可能具有
特别是,由于几个问题,在Coq中拥有良好的"集合类型"是一项极富挑战性的任务,有关更多讨论,请参见anwser如何在coq中定义集合而不将集合定义为元素列表。
进行适当的代数确实确实需要很多复杂的基础架构,@ErikMD的指针是正确的指针,您应该查看math-comp和相关论文以了解最新技术。当然,继续尝试!
关于Coq中单项式和多元多项式的形式化,可以考虑使用多项式库。在OPAM上可用:
1 | $ opam install coq-mathcomp-multinomials |
它自然证明与
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数据类型(