Skip to content

Commit 1d59304

Browse files
Improve documentation in a bunch of places
1 parent 4d330c5 commit 1d59304

File tree

44 files changed

+1548
-1211
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1548
-1211
lines changed

libs/cardano-ledger-conformance/src/Test/Cardano/Ledger/Conformance/ExecSpecRule/Core.hs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,15 @@ module Test.Cardano.Ledger.Conformance.ExecSpecRule.Core (
3030
import Cardano.Ledger.BaseTypes (Globals, Inject (..), ShelleyBase)
3131
import Cardano.Ledger.Binary (EncCBOR)
3232
import Cardano.Ledger.Core (Era, EraRule, eraProtVerLow)
33-
import qualified Constrained.API as CV2 (HasSpec, Specification, genFromSpec, genFromSpecT)
34-
import Constrained.GenT (GE (..), GenMode (..))
35-
import qualified Constrained.GenT as CV1 (runGenT)
36-
import Constrained.Generation (shrinkWithSpec, simplifySpec)
33+
import Constrained.API (GE (..))
34+
import qualified Constrained.API as CV2 (
35+
HasSpec,
36+
Specification,
37+
genFromSpec,
38+
genFromSpecT,
39+
looseGen,
40+
shrinkWithSpec,
41+
)
3742
import Control.Monad.Cont (ContT (..))
3843
import Control.Monad.Trans (MonadTrans (..))
3944
import Control.State.Transition.Extended (STS (..))
@@ -428,9 +433,8 @@ conformsToImpl = property @(ImpTestM era Property) . (`runContT` pure) $ do
428433
let
429434
forAllSpec spec = do
430435
let
431-
simplifiedSpec = simplifySpec spec
432-
generator = CV1.runGenT (CV2.genFromSpecT simplifiedSpec) Loose []
433-
shrinker (Result x) = pure <$> shrinkWithSpec simplifiedSpec x
436+
generator = CV2.looseGen (CV2.genFromSpecT spec)
437+
shrinker (Result x) = pure <$> CV2.shrinkWithSpec spec x
434438
shrinker _ = []
435439
res :: GE a <- ContT $ \c ->
436440
pure $ forAllShrinkBlind generator shrinker c

libs/cardano-ledger-test/src/Test/Cardano/Ledger/Constrained/Conway/Instances/Basic.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import Cardano.Ledger.Plutus.CostModels (CostModels)
4646
import Cardano.Ledger.Plutus.ExUnits
4747
import Cardano.Ledger.Shelley.PParams (ProposedPPUpdates (..))
4848
import Constrained.API
49-
import Constrained.GenT
5049
import Constrained.NumOrd
5150
import Control.Monad.Identity (Identity (..))
5251
import Control.Monad.Trans.Fail.String

libs/cardano-ledger-test/src/Test/Cardano/Ledger/Constrained/Conway/Instances/Ledger.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ import Cardano.Ledger.UMap
118118
import Cardano.Ledger.Val (Val)
119119
import Constrained.API.Extend hiding (Sized)
120120
import Constrained.API.Extend qualified as C
121-
import Constrained.GenT (pureGen, vectorOfT)
122-
import Constrained.Generic
123121
import Control.DeepSeq (NFData)
124122
import Crypto.Hash (Blake2b_224)
125123
import Data.ByteString qualified as BS

libs/cardano-ledger-test/src/Test/Cardano/Ledger/Constrained/Conway/WitnessUniverse.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ import Cardano.Ledger.Shelley (ShelleyEra)
6969
import Cardano.Ledger.Shelley.Scripts
7070
import Cardano.Ledger.Shelley.TxCert
7171
import Constrained.API
72-
import Constrained.GenT (pureGen)
7372
import Control.DeepSeq (NFData (..), deepseq)
7473
import Control.Monad (replicateM)
7574
import Data.ByteString (ByteString)

libs/constrained-generators/constrained-generators.cabal

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ library
2626
Constrained.Core
2727
Constrained.DependencyInjection
2828
Constrained.Env
29-
Constrained.Examples
30-
Constrained.Examples.Basic
31-
Constrained.Examples.CheatSheet
32-
Constrained.Examples.Either
33-
Constrained.Examples.Fold
34-
Constrained.Examples.List
35-
Constrained.Examples.ManualExamples
36-
Constrained.Examples.Map
37-
Constrained.Examples.Set
38-
Constrained.Examples.Tree
3929
Constrained.FunctionSymbol
4030
Constrained.GenT
4131
Constrained.Generation
@@ -47,6 +37,7 @@ library
4737
Constrained.Properties
4838
Constrained.Spec.Map
4939
Constrained.Spec.Set
40+
Constrained.Spec.List
5041
Constrained.Spec.SumProd
5142
Constrained.Spec.Tree
5243
Constrained.SumList
@@ -76,6 +67,39 @@ library
7667
random,
7768
template-haskell,
7869

70+
library examples
71+
exposed-modules:
72+
Constrained.Examples
73+
Constrained.Examples.Basic
74+
Constrained.Examples.BinTree
75+
Constrained.Examples.CheatSheet
76+
Constrained.Examples.Either
77+
Constrained.Examples.Fold
78+
Constrained.Examples.List
79+
Constrained.Examples.ManualExamples
80+
Constrained.Examples.Map
81+
Constrained.Examples.Set
82+
Constrained.Examples.Tree
83+
84+
hs-source-dirs: examples
85+
default-language: Haskell2010
86+
ghc-options:
87+
-Wall
88+
-Wcompat
89+
-Wincomplete-record-updates
90+
-Wincomplete-uni-patterns
91+
-Wpartial-fields
92+
-Wredundant-constraints
93+
-Wunused-packages
94+
95+
build-depends:
96+
QuickCheck >=2.14,
97+
base >=4.18 && <5,
98+
constrained-generators,
99+
containers,
100+
prettyprinter,
101+
random,
102+
79103
library testlib
80104
exposed-modules:
81105
Test.Minimal.Base
@@ -122,6 +146,7 @@ test-suite constrained
122146
QuickCheck,
123147
base,
124148
constrained-generators,
149+
constrained-generators:examples,
125150
containers,
126151
hspec,
127152

libs/constrained-generators/src/Constrained/Examples.hs renamed to libs/constrained-generators/examples/Constrained/Examples.hs

File renamed without changes.

libs/constrained-generators/src/Constrained/Examples/Basic.hs renamed to libs/constrained-generators/examples/Constrained/Examples/Basic.hs

File renamed without changes.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{-# LANGUAGE TypeApplications #-}
2+
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE TypeFamilies #-}
4+
{-# LANGUAGE FunctionalDependencies #-}
5+
{-# LANGUAGE FlexibleInstances #-}
6+
{-# LANGUAGE ScopedTypeVariables #-}
7+
module Constrained.Examples.BinTree where
8+
9+
import Constrained.API
10+
import GHC.Generics
11+
12+
------------------------------------------------------------------------
13+
-- The types
14+
------------------------------------------------------------------------
15+
16+
data BinTree a
17+
= BinTip
18+
| BinNode (BinTree a) a (BinTree a)
19+
deriving (Ord, Eq, Show, Generic)
20+
21+
------------------------------------------------------------------------
22+
-- HasSpec for BinTree
23+
------------------------------------------------------------------------
24+
25+
data BinTreeSpec a = BinTreeSpec (Maybe Integer) (Specification (BinTree a, a, BinTree a))
26+
deriving (Show)
27+
28+
instance Forallable (BinTree a) (BinTree a, a, BinTree a) where
29+
fromForAllSpec = typeSpec . BinTreeSpec Nothing
30+
forAllToList BinTip = []
31+
forAllToList (BinNode left a right) = (left, a, right) : forAllToList left ++ forAllToList right
32+
33+
instance HasSpec a => HasSpec (BinTree a) where
34+
type TypeSpec (BinTree a) = BinTreeSpec a
35+
36+
emptySpec = BinTreeSpec Nothing mempty
37+
38+
combineSpec (BinTreeSpec sz s) (BinTreeSpec sz' s') =
39+
typeSpec $ BinTreeSpec (unionWithMaybe min sz sz') (s <> s')
40+
41+
conformsTo BinTip _ = True
42+
conformsTo (BinNode left a right) s@(BinTreeSpec _ es) =
43+
and
44+
[ (left, a, right) `conformsToSpec` es
45+
, left `conformsTo` s
46+
, right `conformsTo` s
47+
]
48+
49+
genFromTypeSpec (BinTreeSpec msz s)
50+
| Just sz <- msz, sz <= 0 = pure BinTip
51+
| otherwise = do
52+
let sz = maybe 20 id msz
53+
sz' = sz `div` 2
54+
oneofT
55+
[ do
56+
(left, a, right) <- genFromSpecT @(BinTree a, a, BinTree a) $
57+
constrained $ \ctx ->
58+
[ match ctx $ \left _ right ->
59+
[ forAll left (`satisfies` s)
60+
, genHint sz' left
61+
, forAll right (`satisfies` s)
62+
, genHint sz' right
63+
]
64+
, ctx `satisfies` s
65+
]
66+
pure $ BinNode left a right
67+
, pure BinTip
68+
]
69+
70+
shrinkWithTypeSpec _ BinTip = []
71+
shrinkWithTypeSpec s (BinNode left a right) =
72+
BinTip
73+
: left
74+
: right
75+
: (BinNode left a <$> shrinkWithTypeSpec s right)
76+
++ ((\l -> BinNode l a right) <$> shrinkWithTypeSpec s left)
77+
78+
cardinalTypeSpec _ = mempty
79+
80+
toPreds t (BinTreeSpec msz s) =
81+
(forAll t $ \n -> n `satisfies` s)
82+
<> maybe mempty (flip genHint t) msz
83+
84+
instance HasSpec a => HasGenHint (BinTree a) where
85+
type Hint (BinTree a) = Integer
86+
giveHint h = typeSpec $ BinTreeSpec (Just h) mempty

libs/constrained-generators/src/Constrained/Examples/CheatSheet.hs renamed to libs/constrained-generators/examples/Constrained/Examples/CheatSheet.hs

File renamed without changes.

libs/constrained-generators/src/Constrained/Examples/Either.hs renamed to libs/constrained-generators/examples/Constrained/Examples/Either.hs

File renamed without changes.

0 commit comments

Comments
 (0)