Skip to content

Commit adfbb29

Browse files
refactor: introduce Source trait for random generator
1 parent 80cb0d9 commit adfbb29

File tree

5 files changed

+95
-67
lines changed

5 files changed

+95
-67
lines changed

builtin/builtin.mbti

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -780,9 +780,8 @@ impl[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq, T6 : Eq, T7 : Eq, T8
780780

781781
pub(open) trait Hash {
782782
hash_combine(Self, Hasher) -> Unit
783-
hash(Self) -> Int
783+
hash(Self) -> Int = _
784784
}
785-
impl Hash::hash
786785
impl Hash for Byte
787786
impl Hash for Int
788787
impl Hash for UInt
@@ -800,9 +799,8 @@ impl[A : Hash, B : Hash, C : Hash, D : Hash, E : Hash, F : Hash, G : Hash] Hash
800799
pub(open) trait Logger {
801800
write_string(Self, String) -> Unit
802801
write_substring(Self, String, Int, Int) -> Unit
803-
write_char(Self, Char) -> Unit
802+
write_char(Self, Char) -> Unit = _
804803
}
805-
impl Logger::write_char
806804

807805
pub(open) trait Mod {
808806
op_mod(Self, Self) -> Self
@@ -842,9 +840,8 @@ impl Shl for UInt64
842840

843841
pub(open) trait Show {
844842
output(Self, &Logger) -> Unit
845-
to_string(Self) -> String
843+
to_string(Self) -> String = _
846844
}
847-
impl Show::to_string
848845
impl Show for Unit
849846
impl Show for Bool
850847
impl Show for Byte

prelude/prelude.mbti

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,111 @@
11
package "moonbitlang/core/prelude"
22

3+
import(
4+
"moonbitlang/core/builtin"
5+
)
6+
37
// Values
48
fn abort[T](String) -> T
59

6-
fn assert_eq[T : Eq + Show](T, T, loc~ : SourceLoc = _) -> Unit!
10+
fn assert_eq[T : @builtin.Eq + @builtin.Show](T, T, loc~ : @builtin.SourceLoc = _) -> Unit!
711

8-
fn assert_false(Bool, loc~ : SourceLoc = _) -> Unit!
12+
fn assert_false(Bool, loc~ : @builtin.SourceLoc = _) -> Unit!
913

10-
fn assert_not_eq[T : Eq + Show](T, T, loc~ : SourceLoc = _) -> Unit!
14+
fn assert_not_eq[T : @builtin.Eq + @builtin.Show](T, T, loc~ : @builtin.SourceLoc = _) -> Unit!
1115

12-
fn assert_true(Bool, loc~ : SourceLoc = _) -> Unit!
16+
fn assert_true(Bool, loc~ : @builtin.SourceLoc = _) -> Unit!
1317

1418
#deprecated
15-
fn dump[T](T, name? : String, loc~ : SourceLoc = _) -> T
19+
fn dump[T](T, name? : String, loc~ : @builtin.SourceLoc = _) -> T
1620

17-
fn fail[T](String, loc~ : SourceLoc = _) -> T!Failure
21+
fn fail[T](String, loc~ : @builtin.SourceLoc = _) -> T!@builtin.Failure
1822

1923
fn ignore[T](T) -> Unit
2024

21-
fn inspect(&Show, content~ : String = .., loc~ : SourceLoc = _, args_loc~ : ArgsLoc = _) -> Unit!InspectError
25+
fn inspect(&@builtin.Show, content~ : String = .., loc~ : @builtin.SourceLoc = _, args_loc~ : @builtin.ArgsLoc = _) -> Unit!@builtin.InspectError
2226

2327
fn not(Bool) -> Bool
2428

2529
fn panic[T]() -> T
2630

2731
fn physical_equal[T](T, T) -> Bool
2832

29-
fn println[T : Show](T) -> Unit
33+
fn println[T : @builtin.Show](T) -> Unit
3034

31-
fn repr[T : Show](T) -> String
35+
fn repr[T : @builtin.Show](T) -> String
3236

3337
// Types and methods
3438

3539
// Type aliases
36-
pub typealias ArgsLoc = ArgsLoc
40+
pub typealias ArgsLoc = @builtin.ArgsLoc
3741

38-
pub typealias Array[X] = Array[X]
42+
pub typealias Array[X] = @builtin.Array[X]
3943

40-
pub typealias ArrayView[X] = ArrayView[X]
44+
pub typealias ArrayView[X] = @builtin.ArrayView[X]
4145

42-
pub typealias BigInt = BigInt
46+
pub typealias BigInt = @builtin.BigInt
4347

44-
pub typealias Failure = Failure
48+
pub typealias Failure = @builtin.Failure
4549

46-
pub typealias Hasher = Hasher
50+
pub typealias Hasher = @builtin.Hasher
4751

48-
pub typealias InspectError = InspectError
52+
pub typealias InspectError = @builtin.InspectError
4953

50-
pub typealias Iter[X] = Iter[X]
54+
pub typealias Iter[X] = @builtin.Iter[X]
5155

52-
pub typealias Iter2[X, Y] = Iter2[X, Y]
56+
pub typealias Iter2[X, Y] = @builtin.Iter2[X, Y]
5357

54-
pub typealias IterResult = IterResult
58+
pub typealias IterResult = @builtin.IterResult
5559

56-
pub typealias Json = Json
60+
pub typealias Json = @builtin.Json
5761

58-
pub typealias Map[K, V] = Map[K, V]
62+
pub typealias Map[K, V] = @builtin.Map[K, V]
5963

60-
pub typealias Set[X] = Set[X]
64+
pub typealias Set[X] = @builtin.Set[X]
6165

62-
pub typealias SnapshotError = SnapshotError
66+
pub typealias SnapshotError = @builtin.SnapshotError
6367

64-
pub typealias SourceLoc = SourceLoc
68+
pub typealias SourceLoc = @builtin.SourceLoc
6569

66-
pub typealias StringBuilder = StringBuilder
70+
pub typealias StringBuilder = @builtin.StringBuilder
6771

68-
pub typealias UninitializedArray[X] = UninitializedArray[X]
72+
pub typealias UninitializedArray[X] = @builtin.UninitializedArray[X]
6973

70-
pub traitalias Add = Add
74+
pub traitalias Add = @builtin.Add
7175

72-
pub traitalias BitAnd = BitAnd
76+
pub traitalias BitAnd = @builtin.BitAnd
7377

74-
pub traitalias BitOr = BitOr
78+
pub traitalias BitOr = @builtin.BitOr
7579

76-
pub traitalias BitXOr = BitXOr
80+
pub traitalias BitXOr = @builtin.BitXOr
7781

78-
pub traitalias Compare = Compare
82+
pub traitalias Compare = @builtin.Compare
7983

80-
pub traitalias Default = Default
84+
pub traitalias Default = @builtin.Default
8185

82-
pub traitalias Div = Div
86+
pub traitalias Div = @builtin.Div
8387

84-
pub traitalias Eq = Eq
88+
pub traitalias Eq = @builtin.Eq
8589

86-
pub traitalias Hash = Hash
90+
pub traitalias Hash = @builtin.Hash
8791

88-
pub traitalias Logger = Logger
92+
pub traitalias Logger = @builtin.Logger
8993

90-
pub traitalias Mod = Mod
94+
pub traitalias Mod = @builtin.Mod
9195

92-
pub traitalias Mul = Mul
96+
pub traitalias Mul = @builtin.Mul
9397

94-
pub traitalias Neg = Neg
98+
pub traitalias Neg = @builtin.Neg
9599

96-
pub traitalias Shl = Shl
100+
pub traitalias Shl = @builtin.Shl
97101

98-
pub traitalias Show = Show
102+
pub traitalias Show = @builtin.Show
99103

100-
pub traitalias Shr = Shr
104+
pub traitalias Shr = @builtin.Shr
101105

102-
pub traitalias Sub = Sub
106+
pub traitalias Sub = @builtin.Sub
103107

104-
pub traitalias ToJson = ToJson
108+
pub traitalias ToJson = @builtin.ToJson
105109

106110
// Traits
107111

random/README.mbt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Internally, it uses the `Chacha8` cipher to generate random numbers. It is a cry
77
# Usage
88

99
```moonbit
10-
let r = @random.new()
10+
let r = @random.Rand::new(@random.chacha8())
1111
assert_eq!(r.uint(limit=10), 7)
1212
assert_eq!(r.uint(limit=10), 0)
1313
assert_eq!(r.uint(limit=10), 5)

random/random.mbt

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,57 @@
1313
// limitations under the License.
1414

1515
///|
16-
/// Currently we only support [chacha8] as the source of randomness.
17-
struct Rand {
18-
src : @random_source.ChaCha8
16+
/// `Rand` is a pseudo-random number generator (PRNG) that provides various
17+
/// methods to generate random numbers of different types.
18+
type Rand &Source
19+
20+
///|
21+
/// The [Source] trait defines a method to generate random numbers.
22+
pub(open) trait Source {
23+
next(Self) -> UInt64
24+
}
25+
26+
///|
27+
impl Source for @random_source.ChaCha8 with next(self : @random_source.ChaCha8) -> UInt64 {
28+
for {
29+
let x = self.state.next()
30+
if x.1 {
31+
return x.0
32+
}
33+
self.state.refill()
34+
}
1935
}
2036

2137
///|
2238
/// Create a new random number generator with [seed].
2339
/// @alert unsafe "Panic if seed is not 32 bytes long"
40+
#deprecated("Use `Rand::new(chacha8())` instead")
2441
pub fn new(seed~ : Bytes = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ123456") -> Rand {
2542
if seed.length() != 32 {
2643
abort("seed must be 32 bytes long")
2744
}
28-
let src = @random_source.ChaCha8::new(seed)
29-
{ src, }
45+
@random_source.ChaCha8::new(seed) as &Source
46+
}
47+
48+
///|
49+
/// Create a new random number generator with [seed].
50+
/// @alert unsafe "Panic if seed is not 32 bytes long"
51+
pub fn chacha8(seed~ : Bytes = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ123456") -> &Source {
52+
if seed.length() != 32 {
53+
abort("seed must be 32 bytes long")
54+
}
55+
@random_source.ChaCha8::new(seed) as &Source
3056
}
3157

32-
///| @alert deprecated "use `@random.new` instead"
33-
pub fn Rand::new(seed~ : Bytes = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ123456") -> Rand {
34-
new(seed~)
58+
///|
59+
/// Create a new random number generator with a given [Gen] source.
60+
pub fn Rand::new(generator : &Source) -> Rand {
61+
generator
3562
}
3663

3764
///|
3865
fn next(self : Rand) -> UInt64 {
39-
for {
40-
let x = self.src.state.next()
41-
if x.1 {
42-
return x.0
43-
}
44-
self.src.state.refill()
45-
}
66+
self._.next()
4667
}
4768

4869
///|

random/random.mbti

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package "moonbitlang/core/random"
22

33
// Values
4+
fn chacha8(seed~ : Bytes = ..) -> &Source
5+
46
fn double(Rand) -> Double
57

68
fn float(Rand) -> Float
@@ -9,6 +11,7 @@ fn int(Rand, limit~ : Int = ..) -> Int
911

1012
fn int64(Rand, limit~ : Int64 = ..) -> Int64
1113

14+
#deprecated
1215
fn new(seed~ : Bytes = ..) -> Rand
1316

1417
fn shuffle(Rand, Int, (Int, Int) -> Unit) -> Unit
@@ -24,7 +27,7 @@ impl Rand {
2427
float(Self) -> Float
2528
int(Self, limit~ : Int = ..) -> Int
2629
int64(Self, limit~ : Int64 = ..) -> Int64
27-
new(seed~ : Bytes = ..) -> Self //deprecated
30+
new(&Source) -> Self
2831
shuffle(Self, Int, (Int, Int) -> Unit) -> Unit
2932
uint(Self, limit~ : UInt = ..) -> UInt
3033
uint64(Self, limit~ : UInt64 = ..) -> UInt64
@@ -33,4 +36,7 @@ impl Rand {
3336
// Type aliases
3437

3538
// Traits
39+
pub(open) trait Source {
40+
next(Self) -> UInt64
41+
}
3642

0 commit comments

Comments
 (0)