Skip to content

refactor: migrate rand and arbitrary #2327

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions array/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,6 @@ pub fn[A, B] zip_to_iter2(self : Array[A], other : Array[B]) -> Iter2[A, B] {
})
}

///|
pub impl[X : @quickcheck.Arbitrary] @quickcheck.Arbitrary for Array[X] with arbitrary(
size,
rs
) {
let len = if size == 0 { 0 } else { rs.next_positive_int() % size }
Array::makei(len, fn(x) { X::arbitrary(x, rs) })
}

///|
/// Join an array of strings using the provided `separator`.
///
Expand Down
9 changes: 0 additions & 9 deletions array/fixedarray.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1240,12 +1240,3 @@ test "FixedArray::join" {
let fixed_array_empty : FixedArray[String] = []
inspect(fixed_array_empty.join(","), content="")
}

///|
pub impl[X : @quickcheck.Arbitrary] @quickcheck.Arbitrary for FixedArray[X] with arbitrary(
size,
rs
) {
let len = if size == 0 { 0 } else { rs.next_positive_int() % size }
FixedArray::makei(len, fn(x) { X::arbitrary(x, rs) })
}
7 changes: 3 additions & 4 deletions array/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/bytes",
"moonbitlang/core/string",
"moonbitlang/core/quickcheck",
"moonbitlang/core/quickcheck/splitmix"
"moonbitlang/core/string"
],
"test-import": [
"moonbitlang/core/char",
"moonbitlang/core/test",
"moonbitlang/core/random",
"moonbitlang/core/json"
"moonbitlang/core/json",
"moonbitlang/core/quickcheck"
],
"targets": {
"array_js.mbt": ["js"],
Expand Down
8 changes: 0 additions & 8 deletions array/view.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,3 @@ pub impl[A : Hash] Hash for View[A] with hash_combine(self, hasher) {
hasher.combine(e)
}
}

///|
pub impl[A : @quickcheck.Arbitrary] @quickcheck.Arbitrary for View[A] with arbitrary(
size,
rs
) {
Array::arbitrary(size, rs)[:]
}
9 changes: 0 additions & 9 deletions bigint/bigint.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ pub impl @json.FromJson for BigInt with from_json(json, path) {
BigInt::from_string(s)
}

///|
pub impl @quickcheck.Arbitrary for BigInt with arbitrary(size, rs) {
if size == 0 {
0
} else {
rs.next_int64() |> BigInt::from_int64
}
}

///|
/// Tests equality between a `BigInt` and an `Int` value.
///
Expand Down
2 changes: 0 additions & 2 deletions bigint/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"moonbitlang/core/char",
"moonbitlang/core/uint",
"moonbitlang/core/json",
"moonbitlang/core/quickcheck",
"moonbitlang/core/quickcheck/splitmix",
"moonbitlang/core/string"
],
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion option/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/quickcheck",
"moonbitlang/core/quickcheck/splitmix"
"moonbitlang/core/random"
],
"targets": {
"panic_test.mbt": ["not", "native", "llvm"]
Expand Down
2 changes: 1 addition & 1 deletion priority_queue/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"moonbitlang/core/builtin",
"moonbitlang/core/array",
"moonbitlang/core/quickcheck",
"moonbitlang/core/quickcheck/splitmix"
"moonbitlang/core/random"
],
"test-import": ["moonbitlang/core/random"],
"targets": {
Expand Down
31 changes: 28 additions & 3 deletions quickcheck/arbitrary.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(open) trait Arbitrary {
// `arbitrary` function takes a random number generator and a number that
// determines the size of the generated value as arguments, and returns a
// randomly generated value of certain type.
arbitrary(Int, @splitmix.RandomState) -> Self
arbitrary(Int, &@random.Source) -> Self
}

///|
Expand Down Expand Up @@ -124,13 +124,38 @@ pub impl[X : Arbitrary] Arbitrary for Iter[X] with arbitrary(size, rs) {
}

///|
pub fn[T : Arbitrary] gen(size? : Int, state? : @splitmix.RandomState) -> T {
pub impl[X : Arbitrary] Arbitrary for Array[X] with arbitrary(size, rs) {
let len = if size == 0 { 0 } else { rs.next_positive_int() % size }
Array::makei(len, fn(x) { X::arbitrary(x, rs) })
}

///|
pub impl[X : Arbitrary] Arbitrary for FixedArray[X] with arbitrary(size, rs) {
let len = if size == 0 { 0 } else { rs.next_positive_int() % size }
FixedArray::makei(len, fn(x) { X::arbitrary(x, rs) })
}

///|
pub impl[A : Arbitrary] Arbitrary for @array.View[A] with arbitrary(size, rs) {
(Arbitrary::arbitrary(size, rs) : Array[A])[:]
}

///|
pub impl Arbitrary for @bigint.BigInt with arbitrary(size, rs) {
let len = if size == 0 { 0 } else { rs.next_positive_int() % size }
rs.next_bigint(len)
}

///|
pub fn[T : Arbitrary] gen(size? : Int, state? : &@random.Source) -> T {
let size = match size {
None => 0
Some(x) => x
}
let state = match state {
None => @splitmix.RandomState::default()
None => {
@splitmix.RandomState::default() as &@random.Source
}
Some(x) => x
}
Arbitrary::arbitrary(size, state)
Expand Down
9 changes: 5 additions & 4 deletions quickcheck/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"import": [
"moonbitlang/core/quickcheck/splitmix",
"moonbitlang/core/array",
"moonbitlang/core/builtin",
"moonbitlang/core/char"
"moonbitlang/core/char",
"moonbitlang/core/random",
"moonbitlang/core/bigint"
],
"test-import": [
"moonbitlang/core/array",
"moonbitlang/core/bigint",
"moonbitlang/core/float",
"moonbitlang/core/double",
"moonbitlang/core/tuple"
]
}
}
5 changes: 4 additions & 1 deletion quickcheck/splitmix/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"import": ["moonbitlang/core/builtin"]
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/random"
]
}
7 changes: 7 additions & 0 deletions quickcheck/splitmix/random.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ struct RandomState {
gamma : UInt64
} derive(Show, Default)

///|
pub impl @random.Source for RandomState with next(self) {
let { seed, gamma } = self
self.seed = seed + gamma
mix64(self.seed)
}

///|
let golden_gamma : UInt64 = 0x9e3779b97f4a7c15

Expand Down
51 changes: 51 additions & 0 deletions random/source.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
///|
pub fn next_uint64(self : &Source) -> UInt64 {
Rand(self).uint64()
}

///|
pub fn next_uint(self : &Source) -> UInt {
Rand(self).uint()
}

///|
pub fn next_int64(self : &Source) -> Int64 {
Rand(self).uint64().reinterpret_as_int64()
}

///|
pub fn next_two_uint(self : &Source) -> (UInt, UInt) {
let u1 = Rand(self).uint()
let u2 = Rand(self).uint()
(u1, u2)
}

///|
pub fn next_int(self : &Source) -> Int {
Rand(self).uint().reinterpret_as_int()
}

///|
pub fn next_positive_int(self : &Source) -> Int {
Rand(self).int()
}

///|
pub fn next_float(self : &Source) -> Float {
Rand(self).float()
}

///|
pub fn next_double(self : &Source) -> Double {
Rand(self).double()
}

///|
pub fn next_bigint(self : &Source, bits : Int) -> @bigint.BigInt {
Rand(self).bigint(bits)
}

///|
pub fn split(self : &Source) -> &Source {
self
}
2 changes: 1 addition & 1 deletion tuple/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/quickcheck",
"moonbitlang/core/quickcheck/splitmix"
"moonbitlang/core/random"
],
"test-import": [
"moonbitlang/core/char"
Expand Down
Loading