Skip to content

Commit 1ffb0af

Browse files
jamesmckinnaplt-amy
authored andcommitted
[ refactor ] make step a manifest field of Data.Nat.LCG.Generator (#2937)
1 parent 62aa61b commit 1ffb0af

3 files changed

Lines changed: 14 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ Non-backwards compatible changes
2424
Minor improvements
2525
------------------
2626

27+
* The function `Data.Nat.LCG.step` is now a manifest field of the record type
28+
`Generator`, as per the discussion on #2936 and upstream issues/PRs. This is
29+
consistent with a minimal API for such LCGs, and should be backwards compatible.
30+
2731
* The types of `Data.Vec.Base.{truncate|padRight}` have been weakened so
2832
that the argument of type `m ≤ n` is marked as irrelevant. This should be
2933
backwards compatible, but does change the intensional behaviour of these

src/Data/Nat/PseudoRandom/LCG.agda

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
module Data.Nat.PseudoRandom.LCG where
1111

12-
open import Data.Nat.Base
12+
open import Data.Nat.Base using (ℕ; _+_; _*_; _^_; NonZero)
1313
open import Data.Nat.DivMod using (_%_)
14-
open import Data.List.Base using (List; []; _∷_)
14+
open import Data.List.Base using (List; iterate)
1515

1616
------------------------------------------------------------------------
1717
-- Type and generator
@@ -22,14 +22,13 @@ record Generator : Set where
2222
modulus :
2323
.{{modulus≢0}} : NonZero modulus
2424

25-
step : Generator
26-
step gen@record{} x =
27-
let open Generator gen in
28-
((multiplier * x + increment) % modulus)
25+
step :
26+
step x = (multiplier * x + increment) % modulus
27+
28+
open Generator public using (step)
2929

3030
list : Generator List ℕ
31-
list zero gen x = []
32-
list (suc k) gen x = x ∷ list k gen (step gen x)
31+
list k gen x = iterate (step gen) x k
3332

3433
------------------------------------------------------------------------
3534
-- Examples of parameters

src/Data/Nat/PseudoRandom/LCG/Unsafe.agda

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88

99
{-# OPTIONS --cubical-compatible --sized-types #-}
1010

11-
open import Codata.Sized.Stream using (Stream; unfold)
11+
open import Codata.Sized.Stream using (Stream; iterate)
1212
open import Data.Nat.PseudoRandom.LCG using (Generator; step)
1313
open import Data.Nat.Base using (ℕ)
14-
open import Data.Product.Base using (<_,_>)
15-
open import Function.Base using (id)
14+
open import Function.Base using (_∘_)
1615

1716
module Data.Nat.PseudoRandom.LCG.Unsafe where
1817

1918
------------------------------------------------------------------------
2019
-- An infinite stream of random numbers
2120

2221
stream : Generator Stream ℕ _
23-
stream gen = unfold < step gen , id >
22+
stream = iterate ∘ step

0 commit comments

Comments
 (0)