Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean equality of lists in Coq?

Tags:

rocq-prover

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?

like image 537
Alyssa Byrnes Avatar asked Nov 25 '25 10:11

Alyssa Byrnes


2 Answers

I use the Mathematical Components Library boolean equality operators:

From mathcomp Require Import all_ssreflect.

...

Eval vm_compute in list 1 == list 2
like image 193
ejgallego Avatar answered Nov 26 '25 22:11

ejgallego


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.

like image 45
Heiko Becker Avatar answered Nov 26 '25 23:11

Heiko Becker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!