Coq模块系统定义扩展问题
我在Coq中定义了几个模块,以递归方式从位类型构建字节类型,作为三对。但是我遇到了一个为字节类型定义数字符号的问题 代码如下:Coq模块系统定义扩展问题,coq,notation,Coq,Notation,我在Coq中定义了几个模块,以递归方式从位类型构建字节类型,作为三对。但是我遇到了一个为字节类型定义数字符号的问题 代码如下: Require Import ZArith. (* bit sequence abstracted interface *) Module Type Numeric. Parameter T: Set. Parameter MAX: T. (* sequence of 1...1 = 2^n - 1 *
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.
这似乎不是特定于数字符号,而是一个更普遍的问题,与Byte.of无法扩展这一事实有关。有人能解释一下正在发生的事情吗?如果有办法解决这个问题,这似乎是一个限制
Coq版本8.11.2当您写入模块Byte:Numeric:=Foo时,您告诉Coq从Foo中删除所有定义,并仅保留Numeric的签名。这会导致Byte.of失去其主体
在您的情况下,您不希望将Byte的内容限制为Numeric,而只希望记录它与Numeric兼容。您可以使用模块字节执行此操作,当您写入模块字节:Numeric:=Foo时,您告诉Coq从Foo中删除所有定义,并仅保留Numeric的签名。这会导致Byte.of失去其主体
在您的情况下,您不希望将Byte的内容限制为Numeric,而只希望记录它与Numeric兼容。您可以使用模块字节完成此操作。谢谢,它工作正常!我不仅要修改字节定义,还要将其级联到crump定义,这很有意义。但后一种修改ConcatNumeric的建议不起作用,它对我有效。您是否确保先删除所有出现的模块Foo:Numeric?谢谢,它可以工作!我不仅要修改字节定义,还要将其级联到crump定义,这很有意义。但后一种修改ConcatNumeric的建议不起作用,它对我有效。是否确保先删除所有出现的模块Foo:Numeric?
Module ConcatNumeric (m1 m2: Numeric) <: Numeric.
...
End ConcatNumeric.
Module Byte := ConcatNumeric Nibble Nibble.