通过List.filter搜索列表

时间:2019-03-02 19:39:29

标签: coq

在我的程序中,我使用List.filter搜索列表以查找特定元素。我正在证明List.filter是否在列表中找到了一些元素,然后通过追加另一个列表,我们仍然可以在追加之前获得第一个列表中的那些元素。我有点无法证明filterKeepSameElementsAfterAppending。为了简化程序,我将程序的数据更改为customTypemydata

 Require Import  List Nat.
 Inductive customType : Type :=
  |Const1:  nat -> customType
  |Const2: list nat -> customType.

Inductive mydata : Set :=
   |Set1: customType * customType ->mydata
   |Set2: customType ->mydata.

   Fixpoint custome_Equal (c1 c2:customType) :bool:=
        match c1 with
            |Const1 nt => match c2 with 
                       |Const1 mt =>  eqb nt  mt
                       |Const2 (hm::lmt) => eqb nt hm
                      | _ => false
                                     end
           |Const2 (hn::lnt) => match c2 with                                                                            
                           |Const1 mt => eqb  hn  mt
                           |Const2 (hm:: lmt) => eqb hn  hm
                           | _ => false
                                     end
           | _ => false
          end.

 Fixpoint Search (l: mydata) (t:customType):  bool :=
    match l with
       |Set1 (a1, a2) =>  if (custome_Equal a2 t)  then  true else false
       | _=>false
    end.

Lemma filterKeepSameElementsAfterAppending(l1 l2: list mydata)(x:mydata)(ta:customType):
In x (filter (fun n => Search n ta) (l1)) -> In x (filter (fun n  => Search n ta) (l2++ l1)).
Proof.
intro.

1 个答案:

答案 0 :(得分:1)

filter_cat引理应该给您一些启发:

filter_cat
   forall (T : Type) (a : pred T) (s1 s2 : seq T),
   [seq x <- s1 ++ s2 | a x] = [seq x <- s1 | a x] ++ [seq x <- s2 | a x]

mem_cat一起应该做您想要的事情:

mem_cat
   forall (T : eqType) (x : T) (s1 s2 : seq T),
   (x \in s1 ++ s2) = (x \in s1) || (x \in s2)

完整代码:

From mathcomp Require Import all_ssreflect.

Lemma mem_filter_cat (T : eqType) p (l1 l2 : seq T) x :
  x \in filter p l1 -> x \in filter p (l1 ++ l2).
Proof. by rewrite filter_cat mem_cat => ->. Qed.