Skip to content

Commit

Permalink
Report equiv errors modulo commutativity
Browse files Browse the repository at this point in the history
Should give a better error message for mit-plv#1123 (comment)
  • Loading branch information
JasonGross committed Feb 10, 2022
1 parent 77731c4 commit 4a818f1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions _CoqProject
Expand Up @@ -302,6 +302,7 @@ src/Util/Decidable/Bool2Prop.v
src/Util/Decidable/Decidable2Bool.v
src/Util/ErrorT/Show.v
src/Util/FMapPositive/Equality.v
src/Util/ListUtil/FilterN.v
src/Util/ListUtil/FoldBool.v
src/Util/ListUtil/FoldMap.v
src/Util/ListUtil/Forall.v
Expand Down
19 changes: 19 additions & 0 deletions src/Assembly/Equivalence.v
Expand Up @@ -16,6 +16,7 @@ Require Import Crypto.Util.Strings.Show.
Require Import Crypto.Util.Option.
Require Import Crypto.Util.ListUtil.
Require Import Crypto.Util.ListUtil.FoldMap.
Require Import Crypto.Util.ListUtil.FilterN.
Require Import Crypto.Util.Sum.
Require Import Crypto.Util.OptionList.
Require Import Crypto.Util.Notations.
Expand Down Expand Up @@ -189,6 +190,20 @@ Definition explain_mismatch_from_state
((make_old_descr left_descr asm_idx)
++ (make_old_descr right_descr PHOAS_idx))%list.

(* If the operation is commutative, we want to exclude any values that appear on both sides *)
Fixpoint minimize_array_idx_modulo_commutativity
(asm_array PHOAS_array : list idx)
{struct asm_array}
: list idx * list idx
:= match asm_array with
| [] => ([], PHOAS_array)
| val :: vals
=> if List.existsb (N.eqb val) PHOAS_array
then minimize_array_idx_modulo_commutativity vals (List.filtern (N.eqb val) PHOAS_array 1)
else let '(vals, PHOAS_array) := minimize_array_idx_modulo_commutativity vals PHOAS_array in
(val :: vals, PHOAS_array)
end.

Fixpoint explain_idx_unification_error
(left_descr right_descr : string)
(st : symbolic_state) (fuel : nat) (asm_idx PHOAS_idx : idx) {struct fuel} : list string
Expand All @@ -215,6 +230,10 @@ Fixpoint explain_idx_unification_error
=> (([show asm ++ " != " ++ show PHOAS]%string)
++ (if Decidable.dec (asm_o = PHOAS_o)
then
let '(asm_e, PHOAS_e) :=
if commutative asm_o
then minimize_array_idx_modulo_commutativity asm_e PHOAS_e
else (asm_e, PHOAS_e) in
explain_array_unification_error "argument" "arguments" left_descr right_descr recr asm_e PHOAS_e 0
else (([reveal_show_node asm ++ " != " ++ reveal_show_node PHOAS
; "Operation mismatch: " ++ show asm_o ++ " != " ++ show PHOAS_o]%string)
Expand Down
15 changes: 15 additions & 0 deletions src/Util/ListUtil/FilterN.v
@@ -0,0 +1,15 @@
Require Import Coq.Lists.List.
Import ListNotations.
Local Open Scope list_scope.

Module List.
Section __.
Context {A} (f : A -> bool).
Fixpoint filtern (l : list A) (n : nat) : list A
:= match n, l with
| O, _ => l
| _, [] => []
| S n', x :: xs => if f x then filtern xs n' else x :: filtern xs n
end.
End __.
End List.

0 comments on commit 4a818f1

Please sign in to comment.