@@ -21,6 +21,7 @@ import Codec.CBOR.Cuddle.CDDL (
21
21
Name (.. ),
22
22
OccurrenceIndicator (.. ),
23
23
Value (.. ),
24
+ ValueVariant (.. ),
24
25
)
25
26
import Codec.CBOR.Cuddle.CDDL.CTree (CTree , CTreeRoot' (.. ))
26
27
import Codec.CBOR.Cuddle.CDDL.CTree qualified as CTree
@@ -38,7 +39,6 @@ import Data.ByteString (ByteString)
38
39
import Data.ByteString.Base16 qualified as Base16
39
40
import Data.Functor ((<&>) )
40
41
import Data.Functor.Identity (Identity (runIdentity ))
41
- import Data.List (foldl' )
42
42
import Data.List.NonEmpty qualified as NE
43
43
import Data.Map.Strict qualified as Map
44
44
import Data.Maybe (fromMaybe )
@@ -155,7 +155,7 @@ genDepthBiasedRM bounds = do
155
155
genDepthBiasedBool :: forall g . RandomGen g => M g Bool
156
156
genDepthBiasedBool = do
157
157
d <- get @ " depth"
158
- foldl' (&&) True <$> replicateM d genRandomM
158
+ and <$> replicateM d genRandomM
159
159
160
160
genRandomM :: forall g a . (Random a , RandomGen g ) => M g a
161
161
genRandomM = asksM @ " fakeSeed" randomM
@@ -304,15 +304,15 @@ genForCTree (CTree.Control op target controller) = do
304
304
tt <- resolveIfRef target
305
305
ct <- resolveIfRef controller
306
306
case (op, ct) of
307
- (CtlOp. Le , CTree. Literal (VUInt n)) -> case tt of
307
+ (CtlOp. Le , CTree. Literal (Value ( VUInt n) _ )) -> case tt of
308
308
CTree. Postlude PTUInt -> S . TInteger <$> genUniformRM (0 , fromIntegral n)
309
309
_ -> error " Cannot apply le operator to target"
310
310
(CtlOp. Le , _) -> error $ " Invalid controller for .le operator: " <> show controller
311
- (CtlOp. Lt , CTree. Literal (VUInt n)) -> case tt of
311
+ (CtlOp. Lt , CTree. Literal (Value ( VUInt n) _ )) -> case tt of
312
312
CTree. Postlude PTUInt -> S . TInteger <$> genUniformRM (0 , fromIntegral n - 1 )
313
313
_ -> error " Cannot apply lt operator to target"
314
314
(CtlOp. Lt , _) -> error $ " Invalid controller for .lt operator: " <> show controller
315
- (CtlOp. Size , CTree. Literal (VUInt n)) -> case tt of
315
+ (CtlOp. Size , CTree. Literal (Value ( VUInt n) _ )) -> case tt of
316
316
CTree. Postlude PTText -> S . TString <$> genText (fromIntegral n)
317
317
CTree. Postlude PTBytes -> S . TBytes <$> genBytes (fromIntegral n)
318
318
CTree. Postlude PTUInt -> S . TInteger <$> genUniformRM (0 , 2 ^ n - 1 )
@@ -321,7 +321,7 @@ genForCTree (CTree.Control op target controller) = do
321
321
f <- resolveIfRef from
322
322
t <- resolveIfRef to
323
323
case (f, t) of
324
- (CTree. Literal (VUInt f1), CTree. Literal (VUInt t1)) -> case tt of
324
+ (CTree. Literal (Value ( VUInt f1) _) , CTree. Literal (Value ( VUInt t1) _ )) -> case tt of
325
325
CTree. Postlude PTText ->
326
326
genUniformRM (fromIntegral f1, fromIntegral t1)
327
327
>>= (fmap (S . TString ) . genText)
@@ -418,17 +418,20 @@ applyOccurenceIndicator (OIBounded mlb mub) oldGen =
418
418
>>= \ i -> G <$> replicateM (fromIntegral i) oldGen
419
419
420
420
genValue :: RandomGen g => Value -> M g Term
421
- genValue (VUInt i) = pure . TInt $ fromIntegral i
422
- genValue (VNInt i) = pure . TInt $ fromIntegral (- i)
423
- genValue (VBignum i) = pure $ TInteger i
424
- genValue (VFloat16 i) = pure . THalf $ i
425
- genValue (VFloat32 i) = pure . TFloat $ i
426
- genValue (VFloat64 i) = pure . TDouble $ i
427
- genValue (VText t) = pure $ TString t
428
- genValue (VBytes b) = case Base16. decode b of
421
+ genValue (Value x _) = genValueVariant x
422
+
423
+ genValueVariant :: RandomGen g => ValueVariant -> M g Term
424
+ genValueVariant (VUInt i) = pure . TInt $ fromIntegral i
425
+ genValueVariant (VNInt i) = pure . TInt $ fromIntegral (- i)
426
+ genValueVariant (VBignum i) = pure $ TInteger i
427
+ genValueVariant (VFloat16 i) = pure . THalf $ i
428
+ genValueVariant (VFloat32 i) = pure . TFloat $ i
429
+ genValueVariant (VFloat64 i) = pure . TDouble $ i
430
+ genValueVariant (VText t) = pure $ TString t
431
+ genValueVariant (VBytes b) = case Base16. decode b of
429
432
Right bHex -> pure $ TBytes bHex
430
433
Left err -> error $ " Unable to parse hex encoded bytestring: " <> err
431
- genValue (VBool b) = pure $ TBool b
434
+ genValueVariant (VBool b) = pure $ TBool b
432
435
433
436
--------------------------------------------------------------------------------
434
437
-- Generator functions
0 commit comments