3
votes

I want to be able to compare two items of type "list" in Coq and get a boolean "true" or "false" for their equivalence.

Right now, I'm comparing the two lists this way:

Eval vm_compute in (list 1 = list 2). 

I get a Prop of the form:

= nil
   :: (2 :: 3 :: nil)
      :: (2 :: nil)
         :: (3 :: nil) :: nil =
   nil
   :: (2 :: 3 :: nil)
      :: (2 :: nil)
         :: (3 :: nil) :: nil
 : Prop

Obviously list1 = list2, so how do I get it to just return true or false?

2
Have you made your own definition of list? Usually it is a type constructor, such that list nat is the type of lists of numbers. But you seem to be using it as a (value) constructor, creating a concrete list, so you must have cooked up something of your own, right? - larsr
Note that = returns a Prop, which is not the same thing as a boolean. This answer explains the issue in more detail. - Arthur Azevedo De Amorim

2 Answers

3
votes

I use the Mathematical Components Library boolean equality operators:

From mathcomp Require Import all_ssreflect.

...

Eval vm_compute in list 1 == list 2
2
votes

You can generate a boolean list equality function that takes as input a boolean equality over the elements automatically using Coq's commands:

Require Import Coq.Lists.List Coq.Bool.Bool.

Import Coq.Lists.List.ListNotations.

Scheme Equality for list.

This prints:

list_beq is defined
list_eq_dec is defined

where list_beq is a boolean equality function on lists that takes as first parameter a comparison function for the lists elements and then two lists:

Print list_beq.

Gives

list_beq = 
fun (A : Type) (eq_A : A -> A -> bool) =>
fix list_eqrec (X Y : list A) {struct X} : bool :=
  match X with
  | [] => match Y with
          | [] => true
          | _ :: _ => false
          end
  | x :: x0 => match Y with
               | [] => false
               | x1 :: x2 => eq_A x x1 && list_eqrec x0 x2
               end
  end
     : forall A : Type, (A -> A -> bool) -> list A -> list A -> bool

and

Check list_eq_dec

gives

list_eq_dec
     : forall (A : Type) (eq_A : A -> A -> bool),
       (forall x y : A, eq_A x y = true -> x = y) ->
       (forall x y : A, x = y -> eq_A x y = true) -> forall x y : list A, {x =  y} + {x <> y}

showing that list equality is decidable if the underlying types equality is agrees with leibniz equality.