Check for negative entries in system specification

This commit is contained in:
Maciej Bendkowski 2022-03-31 19:35:11 +02:00
parent e7fa5dccb8
commit 6c908da3f9
2 changed files with 30 additions and 0 deletions

View File

@ -29,6 +29,7 @@ import Data.Boltzmann.System (
Types (Types, regTypes), Types (Types, regTypes),
collectTypes, collectTypes,
collectTypes', collectTypes',
hasNonNegativeEntries,
hasProperConstructors, hasProperConstructors,
hasProperFrequencies, hasProperFrequencies,
paganiniSpecIO, paganiniSpecIO,
@ -184,6 +185,7 @@ mkSystemCtx sys = do
hasProperConstructors sys' hasProperConstructors sys'
hasProperFrequencies sys' hasProperFrequencies sys'
hasNonNegativeEntries sys'
types <- collectTypes sys' types <- collectTypes sys'
distributions <- runIO $ do distributions <- runIO $ do

View File

@ -19,6 +19,7 @@ module Data.Boltzmann.System (
paganiniSpecIO, paganiniSpecIO,
hasProperConstructors, hasProperConstructors,
hasProperFrequencies, hasProperFrequencies,
hasNonNegativeEntries,
Constructable (..), Constructable (..),
) where ) where
@ -230,6 +231,33 @@ hasProperFrequencies sys = do
"Frequencies definied for non-system constructors: " "Frequencies definied for non-system constructors: "
++ format additionalConstrs ++ format additionalConstrs
hasNonNegativeEntries :: System -> Q ()
hasNonNegativeEntries sys = do
unless (meanSize sys >= 0) $ do
fail "Negative mean target size"
let negativeWeights =
filter
(\(_, w) -> w < 0)
(unConstructorWeights $ weights sys)
unless (null negativeWeights) $ do
fail $
"Negative weight for constructors: "
++ format (Set.fromList $ map fst negativeWeights)
let negativeFrequencies =
filter
(\(_, w) -> w < 0)
(unConstructorFrequencies $ frequencies sys)
unless (null negativeFrequencies) $ do
fail $
"Negative frequencies for constructors: "
++ format (Set.fromList $ map fst negativeFrequencies)
pure ()
mkVariables :: Set Name -> Spec (Map Name Let) mkVariables :: Set Name -> Spec (Map Name Let)
mkVariables sys = do mkVariables sys = do
let n = Set.size sys let n = Set.size sys