问题
Yo!
I need a random stream of nats with guaranteed subset types, like this stream will only give 0 < nat < 10
. Anyone up for helping me with this?
I found this function for generating random numbers:
CoFixpoint rand (seed n1 n2 : Z) : Stream Z :=
let seed' := Zmod seed n2 in Cons seed' (rand (seed' * n1) n1 n2).
I want to replace Z
with any subset type, e.g.
Definition Z_gt0 := { Z | Z > 0}.
So we have:
CoFixpoint rand (seed n1 n2 : Z_gt0) : Stream Z_gt0 :=
let seed' := Zmod seed n2 in Cons seed' (rand (seed' * n1) n1 n2).
Now the problem is that Zmod
does accept Z
but not Z_gt0
.
Do I have to redefine all functions? Or is there already a library function ready to use?
TO MOD: Please add a tag for subset-types or refinement-types.
回答1:
The issue with your type is that Zmod seed n2
is a positive integer that can be 0, so seed'
can be 0, which means that seed' * n1
can be 0 too.
In the end your CoFixpoint
is not typable, the seed should be in some Z_ge0
type, not in Z_gt0
.
EDIT: to answer the part about the library, you might be interested by the positive
type, which is the type of binary integer strictly greater than 0. In fact, Z
is defined as:
Inductive Z : Set :=
Z0 : Z (* 0 *)
| Zpos : positive -> Z (* z > 0 *)
| Zneg : positive -> Z (* z < 0 *)
However the problem is still the same: taking the modulo of positive integer can escape positive
since you can end up with 0.
来源:https://stackoverflow.com/questions/26475258/random-nat-stream-and-subset-types-in-coq