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

zify: anomaly when adding a parameterized InjTyp instance #14054

Closed
samuelgruetter opened this issue Apr 1, 2021 · 2 comments · Fixed by #14061
Closed

zify: anomaly when adding a parameterized InjTyp instance #14054

samuelgruetter opened this issue Apr 1, 2021 · 2 comments · Fixed by #14061
Labels
kind: anomaly An uncaught exception has been raised. part: micromega The lia, nia, lra, nra and psatz tactics. Also the legacy omega tactic.
Milestone

Comments

@samuelgruetter
Copy link
Contributor

By adapting the code from this Coq-club email by @fajb, I was able to instrument zify so that it adds bounds for injections of my custom data type tuple byte 4 into Z:

Require Import Coq.ZArith.ZArith. Open Scope Z_scope.
Require Coq.Init.Byte Coq.Strings.Byte.
Require Import Coq.micromega.ZifyClasses Coq.micromega.Lia.

Notation byte := Coq.Init.Byte.byte.

Module byte.
  Definition unsigned(b: byte): Z := Z.of_N (Byte.to_N b).
End byte.

Section WithA.
  Context (A: Type).
  Fixpoint tuple(n: nat): Type :=
    match n with
    | O => unit
    | S m => A * tuple m
    end.
End WithA.

Module LittleEndian.
  Fixpoint combine (n : nat) : forall (bs : tuple byte n), Z :=
    match n with
    | O => fun _ => 0
    | S n => fun bs => Z.lor (byte.unsigned (fst bs))
                             (Z.shiftl (combine n (snd bs)) 8)
    end.
  Lemma combine_bound: forall {n: nat} (t: tuple byte n),
      0 <= LittleEndian.combine n t < 2 ^ (8 * Z.of_nat n).
  Admitted.
End LittleEndian.

Instance InjByteTuple4: InjTyp (tuple byte 4) Z := {|
  inj := LittleEndian.combine 4;
  pred x := 0 <= x < 2 ^ 32;
  cstr := @LittleEndian.combine_bound 4;
|}.
Add Zify InjTyp InjByteTuple4.

Program Instance Op_combine4 : UnOp (LittleEndian.combine 4) :=
  {| TUOp := fun x  => x ;  |}.
Add Zify UnOp Op_combine4.

Goal forall w: tuple byte 4,
    LittleEndian.combine 4 w mod 2 ^ 32 = LittleEndian.combine 4 w.
Proof.
  intros.
  zify.
  Z.to_euclidean_division_equations.
  lia.
Qed.

However, if I generalize my solution to tuples of any size n,

Instance InjByteTuple{n: nat}: InjTyp (tuple byte n) Z := {|
  inj := LittleEndian.combine n;
  pred x := 0 <= x < 2 ^ (8 * Z.of_nat n);
  cstr := @LittleEndian.combine_bound n;
|}.
Add Zify InjTyp InjByteTuple.

I get

Error: Anomaly "in econstr: grounding a non evar-free term" Please report at http://coq.inria.fr/bugs/.

Coq version: 8.13.0 and master are affected.

Might be a duplicate of #13242, but with a less artificial (though less minimal) example.

@pi8027 pi8027 added kind: anomaly An uncaught exception has been raised. part: micromega The lia, nia, lra, nra and psatz tactics. Also the legacy omega tactic. labels Apr 2, 2021
@fajb
Copy link
Contributor

fajb commented Apr 2, 2021

Hi @samuelgruetter ,

Don't expect zify to work with parameterized instances. Some error checking is missing but the error message should be something like "Dependent types are not allowed".
This is a strong restriction of the implementation that is hardwired very deep. Sorry.

If possible the solution would be to instantiate with the ns of interest.

PS: It may be possible to implement a more expressive zify like tactic in pure Ltac using typeclasse inference.

@samuelgruetter
Copy link
Contributor Author

Thanks for the information, and yes in my case I can just instantiate n with 1,2,4 and 8, that's all I need.

fajb added a commit to fajb/coq that referenced this issue Apr 2, 2021
The vernacular command take a reference instead of a constr.
Moreover, the head symbol is checked i.e
Add Zify InjTyp ref checks that the referenced term has type
Intyp X1 ... Xn

Closes coq#14054, coq#13242
fajb added a commit to fajb/coq that referenced this issue Apr 3, 2021
The vernacular command takes a reference instead of a constr.
Moreover, the head symbol is checked i.e
Add Zify InjTyp ref checks that the referenced term has type
Intyp X1 ... Xn

Closes coq#14054, coq#13242

Improved recovery
fajb added a commit to fajb/coq that referenced this issue Apr 8, 2021
The vernacular command takes a reference instead of a constr.
Moreover, the head symbol is checked i.e
Add Zify InjTyp ref checks that the referenced term has type
Intyp X1 ... Xn

Closes coq#14054, coq#13242
@vbgl vbgl closed this as completed in 2e75d66 Apr 12, 2021
@coqbot-app coqbot-app bot added this to the 8.14+rc1 milestone Apr 12, 2021
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. part: micromega The lia, nia, lra, nra and psatz tactics. Also the legacy omega tactic.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants