Benchmark for random lambda terms.

This commit is contained in:
Maciej Bendkowski 2022-04-02 15:17:00 +02:00
parent 3ba303ad86
commit dc21fa524c
5 changed files with 134 additions and 2 deletions

52
bench/Lambda.hs Normal file
View File

@ -0,0 +1,52 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TemplateHaskell #-}
module Lambda (Lambda (..), DeBruijn (..), lambdaSampler) where
import Control.DeepSeq
import Control.Monad (replicateM)
import Data.Boltzmann (
BoltzmannSampler (..),
Constructable ((<:>)),
LowerBound (MkLowerBound),
System (System, frequencies, meanSize, targetType, weights),
UpperBound (MkUpperBound),
evalIO,
mkBoltzmannSampler,
mkDefWeights,
rejectionSampler,
)
import Data.Default (def)
import GHC.Generics (Generic)
import System.Random.SplitMix (SMGen)
data DeBruijn
= Z
| S DeBruijn
deriving (Show, Generic)
instance NFData DeBruijn
data Lambda
= Index DeBruijn
| App Lambda Lambda
| Abs Lambda
deriving (Show, Generic)
instance NFData Lambda
mkBoltzmannSampler
System
{ targetType = ''Lambda
, meanSize = 1000
, frequencies = def
, weights =
('Index, 0)
<:> $(mkDefWeights ''Lambda)
}
lambdaSampler :: Int -> IO [Lambda]
lambdaSampler n =
evalIO $
replicateM n $
rejectionSampler @SMGen (MkLowerBound 800) (MkUpperBound 1200)

12
bench/Main.hs Normal file
View File

@ -0,0 +1,12 @@
import Criterion.Main (bench, bgroup, defaultMain, nfIO)
import Lambda (lambdaSampler)
main :: IO ()
main =
defaultMain
[ bgroup
"Boltzmann sampler for lambda terms of sizes [800..1200]"
[ bench "100 random terms" $ nfIO (lambdaSampler 100)
, bench "1000 random terms" $ nfIO (lambdaSampler 1000)
]
]

View File

@ -120,3 +120,34 @@ test-suite generic-boltzmann-brain-test
, transformers >=0.5.6
, vector >=0.12.3.1
default-language: Haskell2010
benchmark generic-boltzmann-brain
type: exitcode-stdio-1.0
main-is: Main.hs
other-modules:
Lambda
Paths_generic_boltzmann_brain
hs-source-dirs:
bench
default-extensions:
NumericUnderscores LambdaCase BangPatterns DerivingVia FlexibleInstances UndecidableInstances TypeApplications ScopedTypeVariables Rank2Types
ghc-options: -O2 -Wall -Wcompat -Wmissing-export-lists -Wincomplete-record-updates -Wincomplete-uni-patterns -Wredundant-constraints -Wno-name-shadowing -fwarn-missing-signatures -ddump-splices
build-depends:
QuickCheck >=2.14.2
, base >=4.7 && <5
, containers >=0.6.4
, criterion >=1.5.12.0
, data-default >=0.7.1.1
, deepseq >=1.4.5.0
, generic-boltzmann-brain
, mtl >=2.2.2
, paganini-hs >=0.3.0.0
, random >=1.2.0
, splitmix >=0.1.0.4
, template-haskell >=2.17.0.0
, th-abstraction >=0.4.3.0
, th-lift >=0.8.2
, th-lift-instances >=0.1.18
, transformers >=0.5.6
, vector >=0.12.3.1
default-language: Haskell2010

View File

@ -1,6 +1,6 @@
cradle:
stack:
- path: "./src"
- path: "./api"
component: "generic-boltzmann-brain:lib"
- path: "./internal"
@ -8,3 +8,9 @@ cradle:
- path: "./test"
component: "generic-boltzmann-brain:test:generic-boltzmann-brain-test"
- path: "./bench/Main.hs"
component: "generic-boltzmann-brain:bench:generic-boltzmann-brain"
- path: "./bench/Paths_generic_boltzmann_brain.hs"
component: "generic-boltzmann-brain:bench:generic-boltzmann-brain"

View File

@ -83,6 +83,37 @@ internal-libraries:
ScopedTypeVariables
Rank2Types
benchmarks:
generic-boltzmann-brain:
source-dirs: bench
main: Main.hs
ghc-options:
- -O2
- -Wall
- -Wcompat
- -Wmissing-export-lists
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wredundant-constraints
- -Wno-name-shadowing
- -fwarn-missing-signatures
- -ddump-splices
default-extensions:
NumericUnderscores
LambdaCase
BangPatterns
DerivingVia
FlexibleInstances
UndecidableInstances
TypeApplications
ScopedTypeVariables
Rank2Types
dependencies:
- generic-boltzmann-brain
- criterion >= 1.5.12.0
- deepseq >= 1.4.5.0
- QuickCheck >= 2.14.2
tests:
generic-boltzmann-brain-test:
main: Spec.hs