Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/.*
!/.gitignore
!/.eslintrc.json
!/.tidyrc.json
!/.github/
package-lock.json
/bower_components/
/package-lock.json
/spago.lock
/node_modules/
/output/
10 changes: 10 additions & 0 deletions .tidyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"importSort": "ide",
"importWrap": "source",
"indent": 2,
"operatorsFile": null,
"ribbon": 1,
"typeArrowPlacement": "first",
"unicode": "never",
"width": null
}
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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:
Expand All @@ -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)
Expand Down
37 changes: 0 additions & 37 deletions bower.json

This file was deleted.

15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
39 changes: 39 additions & 0 deletions spago.yaml
Original file line number Diff line number Diff line change
@@ -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: {}
33 changes: 18 additions & 15 deletions src/Control/Monad/Gen.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
28 changes: 14 additions & 14 deletions test/Frequency.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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(..))
Expand All @@ -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
Expand All @@ -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
Expand All @@ -62,5 +63,4 @@ check =
, (Tuple 50 "E")
, (Tuple 50 "F")
]
in
assert (expected == actual)
assert (expected == actual)
18 changes: 9 additions & 9 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
Loading