Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anomaly non functional construction in zify #16940

Open
SkySkimmer opened this issue Dec 5, 2022 · 0 comments
Open

anomaly non functional construction in zify #16940

SkySkimmer opened this issue Dec 5, 2022 · 0 comments
Labels
kind: anomaly An uncaught exception has been raised.

Comments

@SkySkimmer
Copy link
Contributor

From Coq Require Import Program.Basics.
From Coq Require Import Vectors.Vector.

Import VectorNotations.

Require Import Lia.
Require Import ZArith.
Import ZifyClasses.

Definition Vec (n : nat) (a : Type) : Type := VectorDef.t a n.
Notation bitvector n := (Vec n bool).

(* Workaround for https://github.com/coq/coq/issues/16803 *)
Constraint Vec.u1 <= mkapp2.u0.
Constraint Vec.u1 <= mkapp2.u1.
Constraint Vec.u1 <= mkapp2.u2.
Constraint Vec.u1 <= mkrel.u0.

Definition bvToInt : forall w, bitvector w -> Z. Admitted.
Definition bvAdd : forall w, bitvector w -> bitvector w -> bitvector w. Admitted.

Axiom w : nat.

Definition bitvector_w := bitvector w.

(* now we have to use of_nat since w : nat *)
Notation modulus := (Z.pow 2 (Z.of_nat w)).

Global Program Instance Inj_bv_Z : InjTyp bitvector_w Z :=
  { inj := bvToInt w
  ; pred := fun x => Z.le 0 x /\ Z.lt x modulus
  }.
Next Obligation.
Admitted.

Global Program Instance Rel_eq_bv : BinRel (@eq bitvector_w) :=
  { TR := @eq Z
  }.
Next Obligation.
Admitted.

Global Program Instance Op_bvAdd : BinOp (bvAdd w : bitvector_w -> bitvector_w -> bitvector_w) :=
  { TBOp := fun x y => Z.modulo (Z.add x y) modulus
  }.
Next Obligation.
Admitted.

Add Zify InjTyp Inj_bv_Z.
Add Zify BinRel Rel_eq_bv.
Add Zify BinOp Op_bvAdd.

Module Good.

Axiom intToBv : Z -> bitvector w.
Axiom intToBv_ok : forall x, (bvToInt w (intToBv x) = x mod (Z.pow 2 (Z.of_nat w)))%Z.

#[export] Program Instance i2bv : UnOpSpec (intToBv : Z -> bitvector_w) :=
  { UPred := fun x b => bvToInt w b = (x mod modulus)%Z }.
Next Obligation. (* bvToInt w (intToBv x) = (x mod modulus)%Z *)
  apply intToBv_ok.
Qed.
Add Zify UnOpSpec i2bv.

End Good.

Module Bad.

Axiom intToBv : forall {w}, Z -> bitvector w.
Axiom intToBv_ok : forall w x, (bvToInt w (intToBv x) = x mod (Z.pow 2 (Z.of_nat w)))%Z.

#[export] Program Instance i2bv : UnOpSpec (@intToBv w : Z -> bitvector_w) :=
  { UPred := fun x b => bvToInt w b = (x mod modulus)%Z }.
Next Obligation. (* bvToInt w (intToBv x) = (x mod modulus)%Z *)
  apply intToBv_ok.
Qed.
Add Zify UnOpSpec i2bv.

End Bad.

Ltac hook :=
  repeat match goal with
      H : bvToInt _ _ = _ |- _ => rewrite H;clear H
    end.

Ltac Zify.zify_post_hook ::= hook.

(* if Bad imported instead, zify will anomaly non functional construction
   it tries to do
   "Pose (H2, (i2bv_obligation_1 w 0%Z))"
   where the "w" argument was gotten from (intToBv w 0) in spec_of_term but should not be there
 *)
Import Good.

Lemma test_bvAdd_max_1 : bvAdd w (intToBv (modulus - 1)) (intToBv 1) = intToBv 0.
Proof.
  zify;simpl in *. (* anomaly here if Bad was imported *)
  rewrite Zmod_0_l.
  rewrite <-Zplus_mod.
  rewrite <-(Z_mod_same_full modulus).
  f_equal;lia.
Qed.

Not sure if this is supposed to be supported, but at least it shouldn't anomaly.

@Alizter Alizter added the kind: anomaly An uncaught exception has been raised. label Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: anomaly An uncaught exception has been raised.
Projects
None yet
Development

No branches or pull requests

2 participants