diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c69237a..a4f213f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,30 +6,26 @@ on: pull_request: branches: [master] +permissions: + contents: read + jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - - uses: purescript-contrib/setup-purescript@main + - uses: actions/checkout@v6 + - uses: actions/setup-node@v6 with: - purescript: "unstable" - - - uses: actions/setup-node@v2 - with: - node-version: "14.x" + node-version: "24" - name: Install dependencies - run: | - npm install -g bower - npm install - bower install --production + run: npm install - name: Build source - run: npm run-script build + run: npm run build - name: Run tests - run: | - bower install - npm run-script test --if-present + run: npm test + + - name: Check formatting + run: npm run format:check diff --git a/.gitignore b/.gitignore index 7224331..630673a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ /.* !/.gitignore -!/.eslintrc.json +!/.tidyrc.json !/.github/ -package-lock.json -/bower_components/ +/package-lock.json +/spago.lock /node_modules/ /output/ diff --git a/.tidyrc.json b/.tidyrc.json new file mode 100644 index 0000000..9e39cf1 --- /dev/null +++ b/.tidyrc.json @@ -0,0 +1,10 @@ +{ + "importSort": "ide", + "importWrap": "source", + "indent": 2, + "operatorsFile": null, + "ribbon": 1, + "typeArrowPlacement": "first", + "unicode": "never", + "width": null +} diff --git a/CHANGELOG.md b/CHANGELOG.md index 39ace0b..ad28b99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased] +## [Unreleased](https://github.com/purescript/purescript-gen/compare/v4.0.1...HEAD) Breaking changes: @@ -12,9 +12,16 @@ Bugfixes: Other improvements: +## [v4.0.1](https://github.com/purescript/purescript-gen/releases/tag/v4.0.1) - 2026-10-17 + +Other improvements: + +- Migrate from Bower/Pulp to Spago and update CI (#37 by @thomashoneyman) + ## [v4.0.0](https://github.com/purescript/purescript-gen/releases/tag/v4.0.0) - 2022-04-27 Breaking changes: + - Update project and deps to PureScript v0.15.0 (#35 by @JordanMartinez) New features: @@ -26,15 +33,18 @@ Other improvements: ## [v3.0.0](https://github.com/purescript/purescript-gen/releases/tag/v3.0.0) - 2021-02-26 Breaking changes: + - Added support for PureScript 0.14 and dropped support for all previous versions (#26) New features: Bugfixes: + - Make `elements` test no longer hang at cost of possible false negative (#29) - Fixed `fromIndex` internal helper in tests (#23) Other improvements: + - Refer people to the `quickcheck` package's `Gen` type as a default implementation for `MonadGen` (#25) - Restored frequency tests (#28) - Migrated CI to GitHub Actions and updated installation instructions to use Spago (#30) diff --git a/bower.json b/bower.json deleted file mode 100644 index 8392bc5..0000000 --- a/bower.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "name": "purescript-gen", - "homepage": "https://github.com/purescript/purescript-gen", - "license": "BSD-3-Clause", - "repository": { - "type": "git", - "url": "https://github.com/purescript/purescript-gen.git" - }, - "ignore": [ - "**/.*", - "bower_components", - "node_modules", - "output", - "test", - "bower.json", - "package.json" - ], - "dependencies": { - "purescript-either": "^6.0.0", - "purescript-foldable-traversable": "^6.0.0", - "purescript-identity": "^6.0.0", - "purescript-maybe": "^6.0.0", - "purescript-newtype": "^5.0.0", - "purescript-nonempty": "^7.0.0", - "purescript-prelude": "^6.0.0", - "purescript-tailrec": "^6.0.0", - "purescript-tuples": "^7.0.0", - "purescript-unfoldable": "^6.0.0" - }, - "devDependencies": { - "purescript-arrays": "^7.0.0", - "purescript-assert": "^6.0.0", - "purescript-console": "^6.0.0", - "purescript-lcg": "^4.0.0", - "purescript-transformers": "^6.0.0" - } -} diff --git a/package.json b/package.json index 30a6ad0..e5eb282 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,15 @@ { "private": true, "scripts": { - "clean": "rimraf output && rimraf .pulp-cache", - "build": "pulp build -- --censor-lib --strict", - "test": "pulp test" + "build": "spago build", + "test": "spago test", + "format": "purs-tidy format-in-place src test && oxfmt --write .", + "format:check": "purs-tidy check src test && oxfmt --check ." }, "devDependencies": { - "eslint": "^7.15.0", - "pulp": "16.0.0-0", - "purescript-psa": "^0.8.2", - "rimraf": "^3.0.2" + "oxfmt": "^0.73.0", + "purescript": "^0.15.16", + "purs-tidy": "^0.11.1", + "spago": "^1.0.5" } } diff --git a/spago.yaml b/spago.yaml new file mode 100644 index 0000000..768c6a3 --- /dev/null +++ b/spago.yaml @@ -0,0 +1,39 @@ +package: + name: gen + description: A type class for random generator implementations. + build: + strict: true + pedanticPackages: true + publish: + version: 4.0.1 + license: BSD-3-Clause + location: + githubOwner: purescript + githubRepo: purescript-gen + dependencies: + - either: ">=6.0.0 <7.0.0" + - foldable-traversable: ">=6.0.0 <7.0.0" + - identity: ">=6.0.0 <7.0.0" + - maybe: ">=6.0.0 <7.0.0" + - newtype: ">=5.0.0 <6.0.0" + - nonempty: ">=7.0.0 <8.0.0" + - prelude: ">=6.0.0 <7.0.0" + - tailrec: ">=6.0.0 <7.0.0" + - tuples: ">=7.0.0 <8.0.0" + - unfoldable: ">=6.0.0 <7.0.0" + test: + main: Test.Main + strict: true + pedanticPackages: true + dependencies: + - arrays: ">=7.0.0 <8.0.0" + - assert: ">=6.0.0 <7.0.0" + - bifunctors: ">=6.0.0 <7.0.0" + - console: ">=6.0.0 <7.0.0" + - effect: ">=4.0.0 <5.0.0" + - integers: ">=6.0.0 <7.0.0" + - lcg: ">=4.0.0 <5.0.0" + - numbers: ">=9.0.0 <10.0.0" + - partial: ">=4.0.0 <5.0.0" + - transformers: ">=6.0.0 <7.0.0" +workspace: {} diff --git a/src/Control/Monad/Gen.purs b/src/Control/Monad/Gen.purs index 3ceee57..5065745 100644 --- a/src/Control/Monad/Gen.purs +++ b/src/Control/Monad/Gen.purs @@ -41,9 +41,10 @@ newtype FreqSemigroup a = FreqSemigroup (Number -> Tuple (Maybe Number) a) freqSemigroup :: forall a. Tuple Number a -> FreqSemigroup a freqSemigroup (Tuple weight x) = FreqSemigroup \pos -> - if pos >= weight - then Tuple (Just (pos - weight)) x - else Tuple Nothing x + if pos >= weight then + Tuple (Just (pos - weight)) x + else + Tuple Nothing x getFreqVal :: forall a. FreqSemigroup a -> Number -> a getFreqVal (FreqSemigroup f) = snd <<< f @@ -65,9 +66,9 @@ frequency => Foldable1 f => f (Tuple Number (m a)) -> m a -frequency xs = +frequency xs = do let total = alaF Additive foldMap fst xs - in chooseFloat 0.0 total >>= getFreqVal (foldMap1 freqSemigroup xs) + chooseFloat 0.0 total >>= getFreqVal (foldMap1 freqSemigroup xs) -- | Creates a generator that outputs a value chosen from a selection with -- | uniform probability. @@ -92,12 +93,13 @@ unfoldable unfoldable gen = unfoldr unfold <$> sized (tailRecM loopGen <<< Tuple Nil) where loopGen :: Tuple (LL a) Int -> m (Step (Tuple (LL a) Int) (LL a)) - loopGen (Tuple acc n) - | n <= 0 = - pure $ Done acc - | otherwise = do - x <- gen - pure $ Loop (Tuple (Cons x acc) (n - 1)) + loopGen (Tuple acc n) = + if n <= 0 then + pure $ Done acc + else do + x <- gen + pure $ Loop (Tuple (Cons x acc) (n - 1)) + unfold :: LL a -> Maybe (Tuple a (LL a)) unfold = case _ of Nil -> Nothing @@ -125,8 +127,9 @@ filtered gen = tailRecM go unit fromIndex :: forall f a. Foldable1 f => Int -> f a -> a fromIndex i xs = go i (foldr Cons Nil xs) where - go _ (Cons a Nil) = a - go j (Cons a _) | j <= 0 = a - go j (Cons _ as) = go (j - 1) as + go j = case _ of + Cons a Nil -> a + Cons a _ | j <= 0 -> a + Cons _ as -> go (j - 1) as -- next case is "impossible", but serves as proof of non-emptyness - go _ Nil = un Last (foldMap1 Last xs) + Nil -> un Last (foldMap1 Last xs) diff --git a/test/Frequency.purs b/test/Frequency.purs index b52c144..0538fa4 100644 --- a/test/Frequency.purs +++ b/test/Frequency.purs @@ -3,11 +3,11 @@ module Test.Frequency where import Prelude import Control.Monad.Gen (class MonadGen, frequency) -import Control.Monad.State (State, class MonadState, get, put, evalStateT) -import Data.Array (replicate, groupAll, length) +import Control.Monad.State (class MonadState, State, evalStateT, get, put) +import Data.Array (groupAll, length, replicate) import Data.Array.NonEmpty (toNonEmpty) import Data.Newtype (unwrap) -import Data.NonEmpty ((:|), NonEmpty(..)) +import Data.NonEmpty (NonEmpty(..), (:|)) import Data.Number (remainder) import Data.Traversable (sequence) import Data.Tuple (Tuple(..)) @@ -16,6 +16,7 @@ import Partial.Unsafe (unsafeCrashWith) import Test.Assert (assert) newtype TestGenFrequency a = TestGenFrequency (State Number a) + derive newtype instance testGenFunctor :: Functor TestGenFrequency derive newtype instance testGenApply :: Apply TestGenFrequency derive newtype instance testGenBind :: Bind TestGenFrequency @@ -37,19 +38,19 @@ runTestGenFrequency :: TestGenFrequency ~> State Number runTestGenFrequency (TestGenFrequency x) = x check :: Effect Unit -check = +check = do let abcGen :: TestGenFrequency String abcGen = frequency $ - ( Tuple 10.0 $ pure "A" ) :| - [ Tuple 20.0 $ pure "B" - , Tuple 0.0 $ pure "Z" - , Tuple 30.0 $ pure "C" - , Tuple 40.0 $ pure "D" - , Tuple 50.0 $ pure "E" - , Tuple 50.0 $ pure "F" - ] + (Tuple 10.0 $ pure "A") :| + [ Tuple 20.0 $ pure "B" + , Tuple 0.0 $ pure "Z" + , Tuple 30.0 $ pure "C" + , Tuple 40.0 $ pure "D" + , Tuple 50.0 $ pure "E" + , Tuple 50.0 $ pure "F" + ] abcArrGen = sequence $ replicate 200 abcGen abcArr = runTestGenFrequency abcArrGen `evalStateT` 0.0 # unwrap actual = groupAll abcArr <#> \nea -> case toNonEmpty nea of @@ -62,5 +63,4 @@ check = , (Tuple 50 "E") , (Tuple 50 "F") ] - in - assert (expected == actual) + assert (expected == actual) diff --git a/test/Main.purs b/test/Main.purs index 66f21cf..a7dd9ef 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -7,8 +7,8 @@ import Control.Monad.Gen.Common as GenC import Control.Monad.Rec.Class (class MonadRec, tailRecM) import Data.Array as Array import Data.Bifunctor (bimap) -import Data.Int as Int import Data.Foldable (foldl) +import Data.Int as Int import Data.NonEmpty (NonEmpty, (:|)) import Data.Tuple (Tuple(..), snd) import Effect (Effect) @@ -21,22 +21,22 @@ import Test.Frequency as Frequency main :: Effect Unit main = do - seed ← LCG.randomSeed + seed <- LCG.randomSeed runTestGen seed do log $ show seed log "`unfoldable` should not loop on negative size" - nil :: Array Unit ← Gen.resize (const (-1)) $ Gen.unfoldable (pure unit) + nil :: Array Unit <- Gen.resize (const (-1)) $ Gen.unfoldable (pure unit) liftEffect $ assertEqual { actual: nil, expected: [] } log "`genNonEmpty` should not reduce the remainder size below zero" - one :: NonEmpty Array Int ← Gen.resize (const 0) $ GenC.genNonEmpty (Gen.sized pure) + one :: NonEmpty Array Int <- Gen.resize (const 0) $ GenC.genNonEmpty (Gen.sized pure) liftEffect $ assertEqual { actual: one, expected: 0 :| [] } log "Ensure that `elements` will produce all possible values (low chance of false negative)" - let testElems = "A" :| ["B", "C", "D"] + let testElems = "A" :| [ "B", "C", "D" ] -- use `resize` to generate an Array of 1,000 elements -- to reduce the chance of a false positive. @@ -79,9 +79,9 @@ instance monadTestGen :: Monad TestGen instance monadRecTestGen :: MonadRec TestGen where tailRecM f a = TestGen (tailRecM go <<< flip Tuple a) where - go (Tuple s a') = - unTestGen (f a') s >>= \(Tuple s' m) -> - pure $ bimap (Tuple s') (Tuple s') m + go (Tuple s a') = + unTestGen (f a') s >>= \(Tuple s' m) -> + pure $ bimap (Tuple s') (Tuple s') m instance monadGenTestGen :: Gen.MonadGen TestGen where chooseInt a b = Int.round <$> Gen.chooseFloat (Int.toNumber a) (Int.toNumber b) @@ -99,7 +99,7 @@ lcgStep = pure $ Tuple (Tuple (LCG.lcgNext seed) size) (LCG.unSeed seed) uniform :: TestGen Number -uniform = lcgStep <#> (\n -> Int.toNumber n / Int.toNumber LCG.lcgM) +uniform = lcgStep <#> \n -> Int.toNumber n / Int.toNumber LCG.lcgM unTestGen :: forall a. TestGen a -> GenState -> Effect (Tuple GenState a) unTestGen (TestGen k) = k