Skip to content

Commit 2cfcd99

Browse files
committed
feat: add Random instance
Closes #18.
1 parent 3144150 commit 2cfcd99

3 files changed

Lines changed: 9 additions & 0 deletions

File tree

closed.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ library
3939
, deepseq
4040
, hashable
4141
, persistent
42+
, random
4243
, text
4344
default-language: Haskell2010
4445

library/Closed/Internal.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import Database.Persist.Sql
3232
import GHC.Generics
3333
import GHC.Stack
3434
import GHC.TypeLits
35+
import System.Random (Random (..))
3536
import Test.QuickCheck
3637
import Text.ParserCombinators.ReadP (pfail, readP_to_S, readS_to_P)
3738

@@ -164,6 +165,12 @@ instance (KnownNat a, KnownNat b, a <= b) => Integral (Closed a b) where
164165

165166
instance NFData (Closed a b)
166167

168+
instance (KnownNat a, KnownNat b, a <= b) => Random (Closed a b) where
169+
randomR (a, b) g =
170+
let (x, g') = randomR (getClosed a, getClosed b) g
171+
in (unsafeClosed x, g')
172+
random = randomR (minBound, maxBound)
173+
167174
instance Hashable (Closed a b)
168175

169176
instance ToJSON (Closed a b) where

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ library:
2121
dependencies:
2222
- deepseq
2323
- hashable
24+
- random
2425
- QuickCheck
2526

2627
tests:

0 commit comments

Comments
 (0)