我在Coq中定义了几个模块,以从三位对中递归地从位类型构建一个字节类型。但是我遇到了一个问题,为字节类型定义了Numeral Notation
。
代码如下:
Require Import ZArith.
(* bit sequence abstracted interface *)
Module Type Numeric.
Parameter T: Set.
Parameter MAX: T. (* sequence of 1...1 = 2^n - 1 *)
Parameter to: T -> Z. (* conversion to Z *)
Parameter of: Z -> option T. (* conversion from Z *)
End Numeric.
(* a single bit *)
Module Bit.
Inductive bit: Set := bit0 | bit1.
Definition T: Set := bit.
Definition MAX: T := bit1.
Definition to (i: T): Z :=
match i with
| bit0 => 0%Z
| bit1 => 1%Z
end.
Definition of (n: Z): option T :=
match n with
| Z0 => Some bit0
| Zpos xH => Some bit1
| _ => None
end.
End Bit.
(* concatenation of two bit sequences *)
Module ConcatNumeric (m1 m2: Numeric).
Definition T: Set := m1.T * m2.T.
Definition MAX: T := (m1.MAX, m2.MAX).
Definition to (x: T): Z :=
let (x1, x2) := x in
let i1 := m1.to x1 in
let i2 := m2.to x2 in
let base := (m2.to m2.MAX + 1)%Z in
(i1 * base + i2)%Z.
Definition of (i: Z): option T :=
let base := (m2.to m2.MAX + 1)%Z in
let i2 := (i mod base)%Z in
let i1 := (i / base)%Z in
match m1.of i1, m2.of i2 with
| Some z1, Some z2 => Some (z1, z2)
| _, _ => None
end.
End ConcatNumeric.
(* hierarchy defining a byte from bits *)
Module Crumb: Numeric := ConcatNumeric Bit Bit.
Module Nibble: Numeric := ConcatNumeric Crumb Crumb.
Module Byte: Numeric := ConcatNumeric Nibble Nibble.
(* boxing Byte.T in an inductive type to make Numeral Notation happy *)
Inductive u8: Set := u8_box (x: Byte.T).
Definition u8_unbox := fun x => match x with u8_box x => x end.
Definition u8_of := fun i => option_map u8_box (Byte.of i).
Definition u8_to := fun x => Byte.to (u8_unbox x).
(* defines the scope and the Numeral Notation *)
Declare Scope u8_scope.
Delimit Scope u8_scope with u8.
Numeral Notation u8 u8_of u8_to: u8_scope.
(* testing the code *)
Open Scope u8_scope.
Definition x: u8 := 1. (* error here! *)
我收到此错误:
Error: Unexpected non-option term
match Byte.of 1 with
| Some a => Some (u8_box a)
| None => None
end while parsing a numeral notation.
似乎不特定于Numeral Notation
,而是一个更普遍的问题,与Byte.of
无法扩展这一事实有关。有人可以阐明正在发生的事情吗?是否有解决办法,这似乎是一个限制?
Coq版本8.11.2
答案 0 :(得分:3)
在编写Module Byte: Numeric := Foo
时,您告诉Coq删除Foo
中的所有定义,并仅保留Numeric
的签名。这会导致Byte.of
失去身体。
对于您而言,您不想将Byte
的内容限制为Numeric
,而只是为了证明它与Numeric
兼容。您可以使用Module Byte <: Numeric := Foo
。
顺便说一句,您可以将文档移到Byte
上,而不是将本文档放在ConcatNumeric
上:
Module ConcatNumeric (m1 m2: Numeric) <: Numeric.
...
End ConcatNumeric.
Module Byte := ConcatNumeric Nibble Nibble.